# solve() is not giving the right solutions

Hey there,

i need your help on this one. The following sage code is giving me a headache:

• f(x) = x^3 + x^2 - 0.1
• solve(f==0, x)
• -->
• [x == -1/2(1/180Isqrt(13)sqrt(3) + 7/540)^(1/3)(Isqrt(3) + 1) - 1/18(-Isqrt(3) + 1)/(1/180Isqrt(13)sqrt(3) + 7/540)^(1/3) - 1/3, x == -1/2(1/180Isqrt(13)sqrt(3) + 7/540)^(1/3)(-Isqrt(3) + 1) - 1/18(Isqrt(3) + 1)/(1/180Isqrt(13)sqrt(3) + 7/540)^(1/3) - 1/3, x == (1/180Isqrt(13)sqrt(3) + 7/540)^(1/3) + 1/9/(1/180Isqrt(13)sqrt(3) + 7/540)^(1/3) - 1/3]

So i'm getting these 3 solutions, but they are not in R (real numbers), which they should be. Im figuring there is some problem with really long terms or something? Is there an easy solution for this? Is the solve()-method here working as intended?

Regards, Ben

edit retag close merge delete

See casus irreducibilis...

You can get "ostensibly real" solutions by expressing the quantities in trigonometric form. For example :

sage: map(lambda s:s.rhs().imag_part().trig_expand(), (x^3+x^2-1/10).solve(x))
[0, 0, 0]

( 2018-11-15 18:39:11 +0200 )edit

Sort by » oldest newest most voted

Note that even if I appears in the solutions, the solutons are actually real, we can simplify the right-hand side of each solution, and see that I cancel eachother:

sage: s = solve(f==0, x)
sage: [i.rhs().full_simplify() for i in s]
[-1/9*sqrt(3)*(sqrt(3)*cos(1/3*arctan(3/7*sqrt(13)*sqrt(3))) + sqrt(3) - 3*sin(1/3*arctan(3/7*sqrt(13)*sqrt(3)))),
-1/9*sqrt(3)*(sqrt(3)*cos(1/3*arctan(3/7*sqrt(13)*sqrt(3))) + sqrt(3) + 3*sin(1/3*arctan(3/7*sqrt(13)*sqrt(3)))),
2/3*cos(1/3*arctan(3/7*sqrt(13)*sqrt(3))) - 1/3]


Note that 0.1 is interpreted as 1/10 in the symbolic ring, so that the solutions are symbolic and not numerical.

more

I figured that it would cancel each other, but i wasnt aware of the simplify-method. Thank you, im going to try this! :)

e: im still wondering if it should be this way. If i test this with "in RR" i get false which is not true as u said yourself... thats kinda odd

( 2018-11-15 18:15:10 +0200 )edit

sage: RR Real Field with 53 bits of precision

Working in RR entails rounding errors, i. e. imperfect representation of quantities with no finite binary representation, hence inequalities where equalities are expected...

( 2018-11-15 18:48:04 +0200 )edit

Instead of defining f as a sympolic expression and using solve, one can define f as a polynomial over the rationals, and get its roots over the algebraic numbers.

Define ring of polynomials in x over the rationals, then f:

sage: R.<x> = QQ[]
sage: f = x^3 + x^2 - 1/10


Roots of f in QQbar (they are real: no imaginary part is shown):

sage: rr = f.roots(QQbar, multiplicities=False)
sage: print(rr)
[-0.8669513175959773?, -0.4126055722546906?, 0.2795568898506678?]


Further check that all roots are real:

sage: [r.imag() == 0 for r in rr]
[True, True, True]


Get radical expressions (involving I even though all roots are real).

sage: [r.radical_expression() for r in rr]
[-1/2*(1/180*I*sqrt(13)*sqrt(3) + 7/540)^(1/3)*(-I*sqrt(3) + 1)
- 1/18*(I*sqrt(3) + 1)/(1/180*I*sqrt(13)*sqrt(3) + 7/540)^(1/3) - 1/3,
-1/2*(1/180*I*sqrt(13)*sqrt(3) + 7/540)^(1/3)*(I*sqrt(3) + 1)
- 1/18*(-I*sqrt(3) + 1)/(1/180*I*sqrt(13)*sqrt(3) + 7/540)^(1/3) - 1/3,
(1/180*I*sqrt(13)*sqrt(3) + 7/540)^(1/3)
+ 1/9/(1/180*I*sqrt(13)*sqrt(3) + 7/540)^(1/3) - 1/3]


See link provided by @Emmanuel_Charpentier for the mathematics of that.

more