1

I got a sympy array

sympyarray = Matrix([[2/(dx**2*(exp(I*theta) + 1)), -2*exp(-I*theta)/dx**2, 2*exp(-I*theta)/(dx**2*(exp(I*theta) + 1))]])

and want to transform it into a numpy array

numpyarray=np.array([2/(dx**2*(np.exp(1j*theta) + 1)), -2*np.exp(-1j*theta)/dx**2, 2*np.exp(-1j*theta)/(dx**2*(np.exp(1j*theta) + 1))])

and wonder if there are any good way to do this?

My method has until now been the following:

  1. convert the complex " I " in sympyarray to "1j" using sympy.subs
  2. convert the sympy array into numpy array using

    numpyarray = sympyarray.tolist()

  3. inserting "np." before all the exp(1j*theta) in the printed numpyarray, since the .tolist() dont change the sympy exponential into a numpy exponential.

It surely must be an easier way?

Note: To my knowlage, lambdify is not the answer here. As I read the documentation, lambdify converts a sympy expression into a function that need numerical input? Or am I way off?

2
  • Are you hoping to evaluate theta and dx to have a numerical numpy array, or to keep it as a function that returns an ndarray? Commented Aug 3, 2016 at 17:15
  • I am going to insert values for theta and dx in the array, but i need them to be dynamic to be able to change the dx and theta value. Or was that an answer to your question? Commented Aug 4, 2016 at 8:34

1 Answer 1

1

Use lambdify:

In [10]: expr = Matrix([[2/(dx**2*(exp(I*theta) + 1)), -2*exp(-I*theta)/dx**2, 2*exp(-I*theta)/(dx**2*(exp(I*theta) + 1))]])

In [12]: f = lambdify([dx, theta], expr, 'numpy')

In [15]: f(np.array([1], dtype=complex), np.array([np.pi], dtype=complex))
Out[15]:
array([[[ 0. -1.63312394e+16j],
        [ 2. +2.44929360e-16j],
        [-2. +1.63312394e+16j]]])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.