# Sage fails to recognize 1 + (1/2 + i*sqrt(3)/2)^3 is zero

The following code:

print( 1 + ( 1/2 + i*sqrt(3)/2 )^3 )
print( simplify( 1 + ( 1/2 + i*sqrt(3)/2 )^3) )
print( N(1 + ( 1/2 + i*sqrt(3)/2 )^3) )


Results in the output:

(1/2*I*sqrt(3) + 1/2)^3 + 1
(1/2*I*sqrt(3) + 1/2)^3 + 1
2.22044604925031e-16 + 1.11022302462516e-16*I


but I'd expect the middle one, and perhaps even the first one, to be just "0." I think this means that there is something which I don't understand about how these expressions are handled.

edit retag close merge delete

Sort by ยป oldest newest most voted

The other two answers are already good and compact, they gave examples, and showed already how to proceed, here is only the explanation for the "poor work" done by the function simplify. Let us define the expression to be simplified to have a handy nickname.

sage: a = 1 + ( 1/2 + i*sqrt(3)/2 )^3
sage: a.simplify()
(1/2*I*sqrt(3) + 1/2)^3 + 1
sage: a.simplify?


So asking for a simplification did not simplify (in the sense of a human). So the last line shows what is doing this method:

sage: a.simplify?
Docstring:
Return a simplified version of this symbolic expression.

Note:

Currently, this just sends the expression to Maxima and converts
it back to Sage.

"simplify_full()", "simplify_trig()", "simplify_rational()",
"simplify_rectform()" "simplify_factorial()", "simplify_log()",
"simplify_real()", "simplify_hypergeometric()",


and so on. So we obtain a "maxima simplification"". And maxima in known for not trying to touch too much expressions. So the prints just submit this string version of the poorly simplified a. The "See also" part (of the doc string of the simplify function/method) above may be an answer to the question. In such cases, the human part is responsible for the "way to handle expressions". For instance:

If we explicitly expand, we get the result.

sage: a.expand()
0


If we know it should be zero, and are irritated for a second, we can ask for this information explicitly:

sage: a == 0    # this is just an "equation", we have to explicitly bool-evaluate it...
(1/2*I*sqrt(3) + 1/2)^3 + 1 == 0
sage: bool(_)
True


We may try some more simplify methods from the above list...

sage: a.simplify()
(1/2*I*sqrt(3) + 1/2)^3 + 1
sage: a.simplify_factorial()
(1/2*I*sqrt(3) + 1/2)^3 + 1
sage: a.simplify_full()
0
sage: a.simplify_rational()
0
sage: a.simplify_trig()
0
0


Some of the applied methods make no sense (the factorial and the trig lines), but...

more

I did:

a = 1 + ( 1/2 + i*sqrt(3)/2 )^3


Then

bool(a == 0)


returns True, and

a.simpify_full()


returns 0. Try typing a.simplify<TAB> to see the list of possible simplifications.

more

Note that in addition to the symbolic ring, Sage has an implementation of algebraic numbers.

Start in the symbolic ring and convert to algebraic numbers:

sage: a = 1 + (1/2 + i*sqrt(3)/2)^3
sage: aa = QQbar(a)
sage: aa
0.?e-18 + 0.?e-19*I
sage: aa.exactify()
sage: aa
0


Directly work with algebraic numbers:

sage: i = QQbar.0
sage: a = 1 + (1/2 + i*QQbar(3).sqrt()/2)^3
sage: a
0.?e-18 + 0.?e-19*I
sage: a.exactify()
sage: a
0

more