How do I tell sympy that i^2 = -1?

Viewed 102

I'm currently using sympy to check my algebra on some nasty equations involving second order derivatives and complex numbers.

import sympy
from sympy.abc import a, e, i, h, t, x, m, A

# define a wavefunction
Psi = A * sympy.exp(-a * ((m*x**2 /h)+i*t))

# take the first order time derivative
Psi_dt = sympy.diff(Psi, t)
# take the second order space derivative
Psi_d2x = sympy.diff(Psi, x, 2)

# write an expression for energy potential (rearrange Schrodingers Equation)
V = 1/Psi * (i*h*Psi_dt + (((h**2)/2*m) * Psi_d2x))


# symplify potential function
sympy.simplify(V)

Which yields this nice thing:

a*(-h*i**2 + m**2*(2*a*m*x**2 - h))

It would be really nice if sympy simplified i^2 to -1. So how do I tell it that i represents the square root of -1?

On a related note, it would also be really nice to tell sympy that e is eulers number, so if I call sympy.diff(e**x, x) I get e**x as output.

1 Answers

You need to use the SymPy built-ins, rather than treating those symbols as free variables. In particular:

from sympy import I, E

I is sqrt(-1); E is Euler's number.

Then use the complexes methods to manipulate the complex numbers as needed.

Related