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 On Thu, Feb 23, 2017 at 1:35 PM, Roberto De Renzi <[email protected]> wrote: > 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 > > -- > 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 [email protected]. > To post to this group, send email to [email protected]. > 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. -- 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 [email protected]. To post to this group, send email to [email protected]. 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/CAKgW%3D6%2B_eaj4_WW%3D7%3DLEvovbV8QpdmH3HwHK34CpAgHGaQidGw%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
