Ask Your Question
1

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

asked 2020-08-05 01:09:56 +0100

Gregory Bard gravatar image

updated 2020-08-05 13:54:06 +0100

slelievre gravatar image

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 flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
3

answered 2020-08-05 03:10:27 +0100

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.

edit flag offensive delete link more
3

answered 2020-08-05 19:41:51 +0100

dan_fulea gravatar image

updated 2020-08-05 19:43:32 +0100

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.

   See also:

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

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
sage: a.canonicalize_radical()                                                                                                              
0

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

edit flag offensive delete link more
2

answered 2020-08-05 10:43:49 +0100

slelievre gravatar image

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
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2020-08-05 01:09:56 +0100

Seen: 675 times

Last updated: Aug 05 '20