showing only real numbers



On Wednesday 19 November 2008 15:21:30 Billinghurst, David (RTATECH) 
wrote:
> Unfortunately life is not so simple.  Some real roots of cubic
> equations, expressed as radicals, can only be expressed using
> complex valued expressions.
>
> Google for "Casus irreducibilis".

realroots computes rational approximations, so it doesn't matter.

As for exact roots, rectform & ratsimp do the trick:

(%i1) solve (x^3 - 6*x + 2);
(%o1) [x = (-sqrt(3)*%i/2-1/2)*(sqrt(7)*%i-1)^(1/3)
          +2*(sqrt(3)*%i/2-1/2)/(sqrt(7)*%i-1)^(1/3),
        x = (sqrt(3)*%i/2-1/2)*(sqrt(7)*%i-1)^(1/3)
          +2*(-sqrt(3)*%i/2-1/2)/(sqrt(7)*%i-1)^(1/3),
        x = (sqrt(7)*%i-1)^(1/3)+2/(sqrt(7)*%i-1)^(1/3)]

(%i2) solve (x^3 - 6*x + 2), rectform;
(%o2) [x = %i*(2*(sin((%pi-atan(sqrt(7)))/3)/(2*sqrt(2))
                  +sqrt(3)*cos((%pi-atan(sqrt(7)))/3)/(2*sqrt(2)))
               -sqrt(2)*sin((%pi-atan(sqrt(7)))/3)/2
               -sqrt(2)*sqrt(3)*cos((%pi-atan(sqrt(7)))/3)/2)
          +2*(sqrt(3)*sin((%pi-atan(sqrt(7)))/3)/(2*sqrt(2))
             -cos((%pi-atan(sqrt(7)))/3)/(2*sqrt(2)))
          +sqrt(2)*sqrt(3)*sin((%pi-atan(sqrt(7)))/3)/2
          -sqrt(2)*cos((%pi-atan(sqrt(7)))/3)/2,
        x = %i*(2*(sin((%pi-atan(sqrt(7)))/3)/(2*sqrt(2))
                  -sqrt(3)*cos((%pi-atan(sqrt(7)))/3)/(2*sqrt(2)))
               -sqrt(2)*sin((%pi-atan(sqrt(7)))/3)/2
               +sqrt(2)*sqrt(3)*cos((%pi-atan(sqrt(7)))/3)/2)
          +2*(-sqrt(3)*sin((%pi-atan(sqrt(7)))/3)/(2*sqrt(2))
             -cos((%pi-atan(sqrt(7)))/3)/(2*sqrt(2)))
          -sqrt(2)*sqrt(3)*sin((%pi-atan(sqrt(7)))/3)/2
          -sqrt(2)*cos((%pi-atan(sqrt(7)))/3)/2,
        x = 2*2^(1/2)*cos((%pi-atan(sqrt(7)))/3)]

(%i3) solve (x^3 - 6*x + 2), rectform, ratsimp;
(%o3) [x = -sqrt(2)*sqrt(3)*sin((atan(sqrt(7))-%pi)/3)
          -sqrt(2)*cos((atan(sqrt(7))-%pi)/3),
        x = sqrt(2)*sqrt(3)*sin((atan(sqrt(7))-%pi)/3)
          -sqrt(2)*cos((atan(sqrt(7))-%pi)/3),
        x = 2*sqrt(2)*cos((atan(sqrt(7))-%pi)/3)]


So I propose something like this:

real_roots_3 (e) := block ([rsol : []],
    for r in ratsimp(rectform(solve(e))) do (
        if freeof(%i,r) then
            rsol : cons(r,rsol)
    ),
    rsol
)$

(%i1) real_roots_3 (x^3 - 6*x + 2);
(%o1) [x = 2*sqrt(2)*cos((atan(sqrt(7))-%pi)/3),
        x = sqrt(2)*sqrt(3)*sin((atan(sqrt(7))-%pi)/3)
          -sqrt(2)*cos((atan(sqrt(7))-%pi)/3),
        x = -sqrt(2)*sqrt(3)*sin((atan(sqrt(7))-%pi)/3)
          -sqrt(2)*cos((atan(sqrt(7))-%pi)/3)]

(%i2) real_roots_3 (x^3-3*x^2+4*x-2);
(%o2) [x = 1]

-- 
Pungenday, Aftermath 31 YOLD 3174
Alexey Beshenov  http://beshenov.ru/