I am using isympy and have the expression:
expr = x + 2 * y
And I want to substitute x with the values in [0, 1, 2, 3]. Currently I am doing:
Eq(Symbol('X_0'), expr.subs(x, 0))
Eq(Symbol('X_1'), expr.subs(x, 1))
Eq(Symbol('X_2'), expr.subs(x, 2))
Eq(Symbol('X_3'), expr.subs(x, 3))
Output:
X₀ = 2⋅y
X₁ = 2⋅y + 1
X₂ = 2⋅y + 2
X₃ = 2⋅y + 3
Is there are a better way to do this? I would like Xₖ to be a function which can take a list of k values.
Use a list comprehension to return a list given a list input:
In [1]: expr=x+2*y
In [2]: [expr.subs(x,i) for i in range(4)]
Out[2]: [2⋅y, 2⋅y + 1, 2⋅y + 2, 2⋅y + 3]
This can of course be cast as a function.
I have an indexed symbol x in Sympy and an expression which is a sum of second degree monomials like x[1]*x[2] + x[3]**2 + x[4]*x[1]. I would like to turn such an expression into x[1,2] + x[3,3] + x[4,1], i.e. replacing x[i]*x[j] -> x[i,j]
There is an upper bound on the indices which may appear, so I could construct a large table hard coding each substitution. Is there a better way?
Responding to the comment - to create x I write
from sympy.tensor import IndexedBase
x = IndexedBase('x')
You can use ordered to put the indices in order:
>>> from sympy import *
>>> i, j = symbols('i j', cls=Wild)
>>> x = IndexedBase('x')
>>> e = x[1]*x[3] + x[2]*x[1] + x[3]**2
>>> def new(o, x):
... if o.is_Mul:
... i,j=list(ordered([i.args[1] for i in o.args]))
... elif o.is_Pow:
... i = j = o.base.args[1]
... else:
... raise NotImplementedError
... return x[i, j]
...
>>> e.xreplace(dict([(o, new(o, x)) for o in e.find(x[i]*x[j])]))
x[1, 2] + x[1, 3] + x[3, 3]
But a simpler way to do the same thing is to use a Piecewise result in the replace call:
>>> e.replace(x[i]*x[j], Piecewise((x[i,j],i<j),(x[j,i],True)))
x[1, 2] + x[1, 3] + x[3, 3]
You can use replace with a Wild.
In [1]: i, j = symbols('i j', cls=Wild)
In [2]: x = IndexedBase('x')
In [3]: e = x[1]*x[3] + x[2]*x[1]
In [4]: e.replace(x[i]*x[j], x[i, j])
Out[4]: x[1, 2] + x[1, 3]
I have GeForce 9600 GT with Cuda Compability 1.1 (I know that it is very low), in my kenrel in debug mode I achieved 67% occupancy(128 Threads per Block and use 16 registers), that's enough for me. But in release mode number of used registers has increased to 54 and occupancy was reduced to 17%. So how can I influence the behavior of the compiler?
My Kernel:
#define FUN(a, b, c, d, e) d^e^(a&c)^(a&e)^(b&c)^(b&e)^(c&d)^(d&e)^(a&d&e)^(a&c&e)^(a&b&d)^(a&b&c)
__global__ void Cal(u32* FirstTexts, u32* First, u32* SecondTexts, u32* Second, u32* Num){
u32 Thread_ID = blockIdx.x * blockDim.x + threadIdx.x;
u32 Texts_Mem_Loc[32];
u32 Mem_Loc[16];
u32 Num_Loc[16];
memcpy(Num_Loc, Num, sizeof(u32)*16);
//First
memcpy(Texts_Mem_Loc, FirstTexts + Thread_ID * 32, sizeof(u32)*32);
Mem_Loc[ 0] = FUN(Texts_Mem_Loc[31], Texts_Mem_Loc[26], Texts_Mem_Loc[20], Texts_Mem_Loc[ 9], Texts_Mem_Loc[ 1] ) ^ Texts_Mem_Loc[ 0] ^ Texts_Mem_Loc[16] ^ Num_Loc[ 0];
Mem_Loc[ 1] = FUN( Mem_Loc[ 0], Texts_Mem_Loc[27], Texts_Mem_Loc[21], Texts_Mem_Loc[10], Texts_Mem_Loc[ 2] ) ^ Texts_Mem_Loc[ 1] ^ Texts_Mem_Loc[17] ^ Num_Loc[ 1];
Mem_Loc[ 2] = FUN( Mem_Loc[ 1], Texts_Mem_Loc[28], Texts_Mem_Loc[22], Texts_Mem_Loc[11], Texts_Mem_Loc[ 3] ) ^ Texts_Mem_Loc[ 2] ^ Texts_Mem_Loc[18] ^ Num_Loc[ 2];
Mem_Loc[ 3] = FUN( Mem_Loc[ 2], Texts_Mem_Loc[29], Texts_Mem_Loc[23], Texts_Mem_Loc[12], Texts_Mem_Loc[ 4] ) ^ Texts_Mem_Loc[ 3] ^ Texts_Mem_Loc[19] ^ Num_Loc[ 3];
Mem_Loc[ 4] = FUN( Mem_Loc[ 3], Texts_Mem_Loc[30], Texts_Mem_Loc[24], Texts_Mem_Loc[13], Texts_Mem_Loc[ 5] ) ^ Texts_Mem_Loc[ 4] ^ Texts_Mem_Loc[20] ^ Num_Loc[ 4];
Mem_Loc[ 5] = FUN( Mem_Loc[ 4], Texts_Mem_Loc[31], Texts_Mem_Loc[25], Texts_Mem_Loc[14], Texts_Mem_Loc[ 6] ) ^ Texts_Mem_Loc[ 5] ^ Texts_Mem_Loc[21] ^ Num_Loc[ 5];
Mem_Loc[ 6] = FUN( Mem_Loc[ 5], Mem_Loc[ 0], Texts_Mem_Loc[26], Texts_Mem_Loc[15], Texts_Mem_Loc[ 7] ) ^ Texts_Mem_Loc[ 6] ^ Texts_Mem_Loc[22] ^ Num_Loc[ 6];
Mem_Loc[ 7] = FUN( Mem_Loc[ 6], Mem_Loc[ 1], Texts_Mem_Loc[27], Texts_Mem_Loc[16], Texts_Mem_Loc[ 8] ) ^ Texts_Mem_Loc[ 7] ^ Texts_Mem_Loc[23] ^ Num_Loc[ 7];
Mem_Loc[ 8] = FUN( Mem_Loc[ 7], Mem_Loc[ 2], Texts_Mem_Loc[28], Texts_Mem_Loc[17], Texts_Mem_Loc[ 9] ) ^ Texts_Mem_Loc[ 8] ^ Texts_Mem_Loc[24] ^ Num_Loc[ 8];
Mem_Loc[ 9] = FUN( Mem_Loc[ 8], Mem_Loc[ 3], Texts_Mem_Loc[29], Texts_Mem_Loc[18], Texts_Mem_Loc[10] ) ^ Texts_Mem_Loc[ 9] ^ Texts_Mem_Loc[25] ^ Num_Loc[ 9];
Mem_Loc[10] = FUN( Mem_Loc[ 9], Mem_Loc[ 4], Texts_Mem_Loc[30], Texts_Mem_Loc[19], Texts_Mem_Loc[11] ) ^ Texts_Mem_Loc[10] ^ Texts_Mem_Loc[26] ^ Num_Loc[10];
Mem_Loc[11] = FUN( Mem_Loc[10], Mem_Loc[ 5], Texts_Mem_Loc[31], Texts_Mem_Loc[20], Texts_Mem_Loc[12] ) ^ Texts_Mem_Loc[11] ^ Texts_Mem_Loc[27] ^ Num_Loc[11];
Mem_Loc[12] = FUN( Mem_Loc[11], Mem_Loc[ 6], Mem_Loc[ 0], Texts_Mem_Loc[21], Texts_Mem_Loc[13] ) ^ Texts_Mem_Loc[12] ^ Texts_Mem_Loc[28] ^ Num_Loc[12];
Mem_Loc[13] = FUN( Mem_Loc[12], Mem_Loc[ 7], Mem_Loc[ 1], Texts_Mem_Loc[22], Texts_Mem_Loc[14] ) ^ Texts_Mem_Loc[13] ^ Texts_Mem_Loc[29] ^ Num_Loc[13];
Mem_Loc[14] = FUN( Mem_Loc[13], Mem_Loc[ 8], Mem_Loc[ 2], Texts_Mem_Loc[23], Texts_Mem_Loc[15] ) ^ Texts_Mem_Loc[14] ^ Texts_Mem_Loc[30] ^ Num_Loc[14];
Mem_Loc[15] = FUN( Mem_Loc[14], Mem_Loc[ 9], Mem_Loc[ 3], Texts_Mem_Loc[24], Texts_Mem_Loc[16] ) ^ Texts_Mem_Loc[15] ^ Texts_Mem_Loc[31] ^ Num_Loc[15];
memcpy(Mem_Loc, First + Thread_ID * 16, sizeof(u32)*16);
//Second
memcpy(Texts_Mem_Loc, SecondTexts + Thread_ID * 32, sizeof(u32)*32);
Mem_Loc[15] = FUN( Texts_Mem_Loc[30], Texts_Mem_Loc[25], Texts_Mem_Loc[19], Texts_Mem_Loc[ 8], Texts_Mem_Loc[ 0] ) ^ Texts_Mem_Loc[31] ^ Texts_Mem_Loc[15] ^ Num_Loc[15];
Mem_Loc[14] = FUN( Texts_Mem_Loc[29], Texts_Mem_Loc[24], Texts_Mem_Loc[18], Texts_Mem_Loc[ 7], Mem_Loc[15] ) ^ Texts_Mem_Loc[30] ^ Texts_Mem_Loc[14] ^ Num_Loc[14];
Mem_Loc[13] = FUN( Texts_Mem_Loc[28], Texts_Mem_Loc[23], Texts_Mem_Loc[17], Texts_Mem_Loc[ 6], Mem_Loc[14] ) ^ Texts_Mem_Loc[29] ^ Texts_Mem_Loc[13] ^ Num_Loc[13];
Mem_Loc[12] = FUN( Texts_Mem_Loc[27], Texts_Mem_Loc[22], Texts_Mem_Loc[16], Texts_Mem_Loc[ 5], Mem_Loc[13] ) ^ Texts_Mem_Loc[28] ^ Texts_Mem_Loc[12] ^ Num_Loc[12];
Mem_Loc[11] = FUN( Texts_Mem_Loc[26], Texts_Mem_Loc[21], Texts_Mem_Loc[15], Texts_Mem_Loc[ 4], Mem_Loc[12] ) ^ Texts_Mem_Loc[27] ^ Texts_Mem_Loc[11] ^ Num_Loc[11];
Mem_Loc[10] = FUN( Texts_Mem_Loc[25], Texts_Mem_Loc[20], Texts_Mem_Loc[14], Texts_Mem_Loc[ 3], Mem_Loc[11] ) ^ Texts_Mem_Loc[26] ^ Texts_Mem_Loc[10] ^ Num_Loc[10];
Mem_Loc[ 9] = FUN( Texts_Mem_Loc[24], Texts_Mem_Loc[19], Texts_Mem_Loc[13], Texts_Mem_Loc[ 2], Mem_Loc[10] ) ^ Texts_Mem_Loc[25] ^ Texts_Mem_Loc[ 9] ^ Num_Loc[ 9];
Mem_Loc[ 8] = FUN( Texts_Mem_Loc[23], Texts_Mem_Loc[18], Texts_Mem_Loc[12], Texts_Mem_Loc[ 1], Mem_Loc[ 9] ) ^ Texts_Mem_Loc[24] ^ Texts_Mem_Loc[ 8] ^ Num_Loc[ 8];
Mem_Loc[ 7] = FUN( Texts_Mem_Loc[22], Texts_Mem_Loc[17], Texts_Mem_Loc[11], Texts_Mem_Loc[ 0], Mem_Loc[ 8] ) ^ Texts_Mem_Loc[23] ^ Texts_Mem_Loc[ 7] ^ Num_Loc[ 7];
Mem_Loc[ 6] = FUN( Texts_Mem_Loc[21], Texts_Mem_Loc[16], Texts_Mem_Loc[10], Mem_Loc[15], Mem_Loc[ 7] ) ^ Texts_Mem_Loc[22] ^ Texts_Mem_Loc[ 6] ^ Num_Loc[ 6];
Mem_Loc[ 5] = FUN( Texts_Mem_Loc[20], Texts_Mem_Loc[15], Texts_Mem_Loc[ 9], Mem_Loc[14], Mem_Loc[ 6] ) ^ Texts_Mem_Loc[21] ^ Texts_Mem_Loc[ 5] ^ Num_Loc[ 5];
Mem_Loc[ 4] = FUN( Texts_Mem_Loc[19], Texts_Mem_Loc[14], Texts_Mem_Loc[ 8], Mem_Loc[13], Mem_Loc[ 5] ) ^ Texts_Mem_Loc[20] ^ Texts_Mem_Loc[ 4] ^ Num_Loc[ 4];
Mem_Loc[ 3] = FUN( Texts_Mem_Loc[18], Texts_Mem_Loc[13], Texts_Mem_Loc[ 7], Mem_Loc[12], Mem_Loc[ 4] ) ^ Texts_Mem_Loc[19] ^ Texts_Mem_Loc[ 3] ^ Num_Loc[ 3];
Mem_Loc[ 2] = FUN( Texts_Mem_Loc[17], Texts_Mem_Loc[12], Texts_Mem_Loc[ 6], Mem_Loc[11], Mem_Loc[ 3] ) ^ Texts_Mem_Loc[18] ^ Texts_Mem_Loc[ 2] ^ Num_Loc[ 2];
Mem_Loc[ 1] = FUN( Texts_Mem_Loc[16], Texts_Mem_Loc[11], Texts_Mem_Loc[ 5], Mem_Loc[10], Mem_Loc[ 2] ) ^ Texts_Mem_Loc[17] ^ Texts_Mem_Loc[ 1] ^ Num_Loc[ 1];
Mem_Loc[ 0] = FUN( Texts_Mem_Loc[15], Texts_Mem_Loc[10], Texts_Mem_Loc[ 4], Mem_Loc[ 9], Mem_Loc[ 1] ) ^ Texts_Mem_Loc[16] ^ Texts_Mem_Loc[ 0] ^ Num_Loc[ 0];
memcpy(Mem_Loc, Second + Thread_ID * 16, sizeof(u32)*16); }
solve(-14.4*(x**2)+71.8*x+5.083, x)
result is None. how come? My calculation by hand gives two roots, 5.0559 and -0.063
Perhaps you are not using the most current version. I get
>>> from sympy import *
>>> var('x')
x
>>> solve(-14.4*(x**2)+71.8*x+5.083, x)
[-0.0698162934055920, 5.05592740451670]
More generally:
import sympy as sp
y = 'a * x ** 2 + b * x + c' # for example a quadratic polynomial
s = sp.var('x a b c') # define four symbols as variables
print(sp.solve(y, s )) # sympy solves y(a,b,c,x) for each of a, b, c, x
print(sp.solve(y, x )) # sympy solves Y(a,b,c,x) for x treating a, b, c as constants
print(sp.solve(y, 'x')) # sympy solves Y(a,b,c,x) for x treating a, b, c as constants
Yields:
[(x, -(b*x + c)/x**2, b, c)]
[(-b + sqrt(-4*a*c + b**2))/(2*a), -(b + sqrt(-4*a*c + b**2))/(2*a)]
[(-b + sqrt(-4*a*c + b**2))/(2*a), -(b + sqrt(-4*a*c + b**2))/(2*a)]
While:
s = sp.var('x') # define one symbol as a varible
print(sp.solve(y, s )) # sympy solves Y(a,b,c,x) for x treating a, b, c as constants
print(sp.solve(y, x )) # sympy solves Y(a,b,c,x) for x treating a, b, c as constants
print(sp.solve(y, 'x')) # sympy solves Y(a,b,c,x) for x treating a, b, c as constants
Returns:
[(-b + sqrt(-4*a*c + b**2))/(2*a), -(b + sqrt(-4*a*c + b**2))/(2*a)]
[(-b + sqrt(-4*a*c + b**2))/(2*a), -(b + sqrt(-4*a*c + b**2))/(2*a)]
[(-b + sqrt(-4*a*c + b**2))/(2*a), -(b + sqrt(-4*a*c + b**2))/(2*a)]
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Postfix to Infix conversation
What would be the prefix notation of this expression? I can not solve this expression
6 a b 7 * + - c d g / + e ^ f * +
Any suggestion will be appreciated.
The in-order expression will be
[6-(a + b*7)] + [(c + d/g) ^ e]*f
from that you can find the pre-order, which is
+ -6 + a * b 7 * ^ + c / d g e f
Postfix To Infix::
6 a b 7 * + - c d g / + e ^ f * +
6 a (b*7) + - c d g / + e ^ f * +
6 {a + (b*7)} - c d g / + e ^ f * +
[6 - {a + (b*7)}] c d g / + e ^ f * +
[6 - {a + (b*7)}] c (d / g) + e ^ f * +
[6 - {a + (b*7)}] {c + (d / g)} e ^ f * +
[6 - {a + (b*7)}] [{c + (d / g)} ^ e] f * +
[6 - {a + (b*7)}] ([{c + (d / g)} ^ e] * f) +
[6 - {a + (b*7)}] + ([{c + (d / g)} ^ e] * f)
Infix To Prefix::
[6 - {a + (b*7)}] + ([{c + (d / g)} ^ e] * f)
[6 - {a + (b*7)}] + *[{c + (d / g)} ^ e] f
[6 - {a + (b*7)}] + *[{c + (/ d g)} ^ e] f
[6 - {a + (b*7)}] + *[{+ c / d g} ^ e] f
[6 - {a + (b*7)}] + (*^+ c / d g e f)
[6 - {a + (*b7)}] + (*^+ c / d g e f)
[6 - {+a *b7}] + (*^+ c / d g e f)
[-6 +a *b7] + (*^+ c / d g e f)
+[-6 +a *b7] (*^+ c / d g e f)
+-6 +a *b7 *^+ c / d g e f