I guess this is just a deficiency in the root finding algorithms. For quadratics, it uses the quadratic formula, but as you point out, if you factor the polynomial, you get a simpler result. SymPy won't simplify sqrt(x**2) to x because it's not true unless x is nonnegative, but what *is* true for any complex x is that sqrt(x**2) is either x or -x, and so by the symmetry of the quadratic formula, it is OK to do this reduction.
I wonder if this issue comes up in the root formulas for higher degrees as well. Aaron Meurer On Tue, Oct 8, 2013 at 11:49 AM, Ondřej Čertík <[email protected]> wrote: > On Tue, Oct 8, 2013 at 11:48 AM, Ondřej Čertík <[email protected]> > wrote: >> On Tue, Oct 8, 2013 at 10:02 AM, Taylan Şengül <[email protected]> >> wrote: >>> Hi all, >>> >>> I am quite new to sympy. >>> >>> When I type the following >>> >>> a = symbols('a') >>> m = Matrix( [ [a, 0], [0, 1] ] ) >>> m.eigenvals() >>> >>> I expect the answer to >>> {a: 1, 1: 1} >>> >>> But I get >>> >>> {a/2 + sqrt((a - 1)**2)/2 + 1/2: 1, a/2 - sqrt((a - 1)**2)/2 + 1/2: 1} >>> >>> I think first the characteristic polynomial is computed and then roots are >>> found. This produces the mess. Wouldn't it be better to try to factor the >>> characteristic polynomial first and then find roots? >> >> >> SymPy assumes that "a" is complex, so no simplifications can be done, isn't >> it? >> But you can tell SymPy that "a" is real, then some simplifications can be >> done: >> >> In [1]: a = symbols('a') >> >> In [2]: m = Matrix( [ [a, 0], [0, 1] ] ) >> >> In [3]: m.eigenvals() >> Out[3]: >> ⎧ __________ __________ ⎫ >> ⎪ ╱ 2 ╱ 2 ⎪ >> ⎨a ╲╱ (a - 1) 1 a ╲╱ (a - 1) 1 ⎬ >> ⎪─ - ───────────── + ─: 1, ─ + ───────────── + ─: 1⎪ >> ⎩2 2 2 2 2 2 ⎭ >> >> In [4]: a = Symbol("a", real=True) >> >> In [5]: m = Matrix( [ [a, 0], [0, 1] ] ) >> >> In [6]: m.eigenvals() >> Out[6]: >> ⎧a │a - 1│ 1 a │a - 1│ 1 ⎫ >> ⎨─ - ─────── + ─: 1, ─ + ─────── + ─: 1⎬ >> ⎩2 2 2 2 2 2 ⎭ > > > Ah, I see your point --- because the eigenvalue are symmetric, you can > actually simplify this to "a" and 1. > > Ondrej > > -- > 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 http://groups.google.com/group/sympy. > For more options, visit https://groups.google.com/groups/opt_out. -- 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 http://groups.google.com/group/sympy. For more options, visit https://groups.google.com/groups/opt_out.
