Complex coefficient avoiding real number representation

25 views
Skip to first unread message

Roberto De Renzi

unread,
Feb 23, 2017, 1:50:57 PM2/23/17
to sympy
Hello, I am manipulating symbolic Matrices with complex numeric coefficients (angular momentum operators).
Their real and imaginary parts are algebraic.  I have troubles diagonalizing them because the real representation of the coefficients produce numerical rounding errors. As long as I use only real coefficients I can define them as s=Fraction(int(n),int(m)), and use e.g.  sympy.sqrt(s*(s+1)),  thus avoiding numerical rounding errors. However

              Fraction(1,2)*1j is 0.5*I

and rounding errors are unavoidable. Is there a way out? I am thinking of a complex number with algebraic immutable real and imaginary part.

i) Is the concept sound? and ii) is it implemented (implementable) in sympy?

Thanks for your help

Roberto

Aaron Meurer

unread,
Feb 23, 2017, 5:51:17 PM2/23/17
to sy...@googlegroups.com
If you want an exact symbolic imaginary number, use sympy.I. For
instance, Rational(1, 2)*I (or just I/2) will give an exact number.

The reason you get 0.5*I above is that SymPy automatically evaluates
rational numbers to floats when you multiply them by a float. For
instance, Rational(1, 2)*1.0 gives 0.5. 1j is a Python floating point
complex number. They are always represented as floats, that is, 1j is
the same as 1.0j, so if you use it in a SymPy, it will act like float
would and cause things to evaluate to floats.

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/758f1cbe-3a5a-4518-9155-2702dfa74838%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages