Question

I have the hamiltonian for harmonic oscillator where I would like to see (n+1/2) but sympy see 1/2 is zero or if I type 1/2.0 =0.5 . Is it possible to keep as 1/2 and at the same time it maintains as an fraction not a symbol? Thanks,

Was it helpful?

Solution

You need to type Rational(1, 2), or, more concisely, S(1)/2.

See http://docs.sympy.org/latest/tutorial/gotchas.html#two-final-notes-and. 1/2 is entirely Python: no SymPy types are involved. The only way to get Python from doing what it does with int/int (integer division in Python 2 and float division in Python 3 or Python 2 with from __future__ import division) is to use a SymPy type somewhere, which is what both of the above do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top