Convert sympy to numpy

345 views Asked by At

I have two NumPy array (two variables), which contains complex numbers. Since they have been defined in NumPy, so the complex notation is denoted by "j".

for i in range(0, 8):
cg1 = np.array(eigVal1[i])
det_eval = eval(det)
AA = det_eval[0:2,0:2]
bb = det_eval[0:2,2] * (-1)
roots = np.append(roots, solve(AA, bb))
a = np.append(a, roots[i])
b = np.append(b, roots[i+1])

Output:

a = array([-9.03839731e-04+0.00091541j,  3.02435614e-07-0.00043776j,
   -9.03839731e-04-0.00091541j,  3.02435614e-07+0.00043776j,
    9.03812649e-04+0.00092323j,  4.17553402e-07+0.00043764j,
    9.03812649e-04-0.00092323j,  4.17553402e-07-0.00043764j])
b = array([ 3.02435614e-07-0.00043776j, -9.03839731e-04-0.00091541j,
    3.02435614e-07+0.00043776j,  9.03812649e-04+0.00092323j,
    4.17553402e-07+0.00043764j,  9.03812649e-04-0.00092323j,
    4.17553402e-07-0.00043764j, -5.53769989e-05-0.00243369j])

I also have a long equation which some variables have been defined symbolic (y).

u_n = A0*y**(1322.5696672125 + 1317.38942049453*I) + A1*y**(1322.5696672125 - 1317.38942049453*I) + A2*y**(-1322.5696672125 + 1317.38942049453*I) + A3*y**(-1322.5696672125 - 1317.38942049453*I) + ..

My problem is when I want to substitute the two variables (a and b) into the equation, all complex numbers change to "I" and it makes the equation more complex, because I am not able to simplify the equation further. Is there any solution to convert "I" to "j" in sympy.

for i in range(0, 8):
u_n = u_n.subs(A[i], (a[i] * C[i]))

The result is:

u_n = C0*y**(1322.5696672125 + 1317.38942049453*I)*(-0.000903839731101097 + 0.000915407724097998*I) + C1*y**(1322.5696672125 - 1317.38942049453*I)*(3.02435613673241e-7 - 0.000437760318205723*I) +..

As you see I can not simplify it further, even if I use simplify(u_n). However, in numpy for example,(2+3j)(5+6j) will be reduced to (-8+27j), but when a symbolic notation comes to my equation it won't be simplified further. y**(2+3j)(5+6j) -->> y**(2+3I)(5+6*I). I would like to have y**(-8+27j) which y is symbolic. I would appreciate it if someone help me with that.

1

There are 1 answers

0
hpaulj On

From you last paragraph:

The python expression:

In [38]: (2+3j)*(5+6j)
Out[38]: (-8+27j)

sympy: (y is a sympy symbol):

In [39]: y**(2+3j)*(5+6j)
Out[39]: 

enter image description here

With corrective () to group the multiply before the power:

In [40]: y**((2+3j)*(5+6j))
Out[40]: 



Even in plain python the operator order matters:

In [44]: 1**(2+3j)*(5+6j)
Out[44]: (5+6j)

In [45]: 1**((2+3j)*(5+6j))
Out[45]: (1+0j)