Ask Your Question
1

Why is sage giving me wrong answers?

asked 2015-04-11 00:39:58 +0200

Artus gravatar image

Let's say I want to multiply $(\frac{-1 - i \sqrt{3}}{2})(\frac{-1 + i \sqrt{3}}{2})$. Doing it by hand, we easily see that the answer is 1.

However, when I type

n(((-1 + sqrt(-3))/2)*((-1-sqrt(-3))/2))

in sage, I get

1 - 5.55111512312578 x 10^(-17)i

which is (of course) very close to 1...but its not exactly 1. Why is that? How do I fix this??

edit retag flag offensive close merge delete

Comments

Note that if you use a slightly different expression (with simply remove parentheses), you get the correct answer. In the following example, I just removed the (useless) parentheses around the fractions, and you get the same answer if you replace the two divisions by $2$ by a division by $4$.

sage: n(((-1-i*sqrt(3))/2*(-1+i*sqrt(3))/2))
1.00000000000000
B r u n o gravatar imageB r u n o ( 2015-04-11 13:03:00 +0200 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2015-04-11 01:00:20 +0200

Pierre gravatar image

updated 2015-04-11 02:01:50 +0200

Default complex numbers use double precision floating points and their rounding errors (which can't be avoided).

Use variable from the right field and arithmetic specific to complex numbers

var('t,v,a,b', domain="complex")
v=sqrt(-3);
a=(-1+v)/2;
b=conjugate(a);
t = a*b;

v;
a;
b;
t.real();
t.imag();

displays

sqrt(-3)
1/2*sqrt(-3) - 1/2
1/2*conjugate(sqrt(-3)) - 1/2
1
0
edit flag offensive delete link more

Comments

@Pierre Thanks a lot. It works now...but I don't really understand what you mean when you say "Do not use floating points". I just wrote the numbers as they were...what do you mean by "floating points"?

Artus gravatar imageArtus ( 2015-04-11 01:08:45 +0200 )edit

Sage complex numbers are based on floating points, 1+2i is implemented as 1.00000000 + 2.00000000000 * i.

Pierre gravatar imagePierre ( 2015-04-11 01:34:50 +0200 )edit

OOOPs it doesn't work

i=CC(-3); 

j=CC(-1);

k=CC(2);

t = ((j + sqrt(i))/k)*((j - sqrt(i))/k);

t.real();

t.imag();

The error moved into he imaginary part (approx 0.5 E-16).

I must edit the answer .....

Pierre gravatar imagePierre ( 2015-04-11 01:37:57 +0200 )edit

Can be written this way too

var('t,v,a,b', domain="complex");
v=sqrt(-3);   a=(-1+v)/2;  b=(-1-v)/2;
t=a*b; t.real(); t.imag();

or this way

var('t,v', domain="complex");
v=sqrt(-3);
t=((-1+v)/2)*((-1-v)/2);
t.expand().factor();
Pierre gravatar imagePierre ( 2015-04-11 02:08:41 +0200 )edit

There is a similar question in this thread

http://ask.sagemath.org/question/1006...

Pierre gravatar imagePierre ( 2015-04-11 02:30:20 +0200 )edit
1

answered 2015-04-11 11:24:26 +0200

tmonteil gravatar image

You can directly try to simplify the symbolic expresssion, instead of looking for its numerical approximation (which is the of role the n() function):

sage: a = ((-1 + sqrt(-3))/2)*((-1-sqrt(-3))/2)
sage: a
-1/4*(sqrt(-3) + 1)*(sqrt(-3) - 1)
sage: a.parent()
Symbolic Ring
sage: a.full_simplify()
1

Now, if you follow the link provided by @Pierre last comment, you will see that Sage is not very reliable concerning Symbolic expressions (there are also undecidability results behind this). An alternative is to use the Algebraic Field which is much more reliable:

sage: a = QQbar((-1 + sqrt(-3))/2*(-1-sqrt(-3))/2)
sage: a
1.000000000000000? + 0.?e-19*I
sage: a.parent()
Algebraic Field
sage: a.simplify()
sage: a
1
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

1 follower

Stats

Asked: 2015-04-11 00:39:58 +0200

Seen: 705 times

Last updated: Apr 11 '15