0

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.

  • As a general rule, putting numpy arrays into sympy expressions, or sympy into numpy is not a good idea. Here though you are simply trying to put Python complex values into sympy expressions, right? – hpaulj Jun 29 '22 at 22:08
  • applying `exapnd_mul` will simplify the exponent, `y**((2+3*I)*(5+6*I))` gives what you expect. – smichr Jun 29 '22 at 23:18
  • Looking at your example, after the substitution `u_n` is a summation of terms of the form `C * (a + bj) * y**(c + dj)` where `a, b, c, d` are float numbers. This is as simple as it gets, I don't think it can be simplified further. Are you sure you wrote your code correctly? – Davide_sd Jun 30 '22 at 07:23
  • You question would be better if you stated the last paragraph clearly, and omitted all the extra stuff about numpy and long expression. `2j` in a sympy expression will be displayed as `2.0i`. That's no big deal. – hpaulj Jun 30 '22 at 16:04
  • @smichr, thank you so much. It was very helpful. I got my answer. I appreciate – Amin Azimi Jun 30 '22 at 18:47
  • @Davide_sd, Thanks David, yeah, I wrote all my code correctly, but I was looking for a function or a way to convert sympy complex numbers into numpy. By the way, by exapnd_mul that was introduced me, I am able to keep moving. Thanks – Amin Azimi Jun 30 '22 at 18:52

1 Answers1

0

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)
hpaulj
  • 221,503
  • 14
  • 230
  • 353