Loading [MathJax]/jax/output/HTML-CSS/jax.js

First time here? Check out the FAQ!

Ask Your Question
1

Why is sage giving me wrong answers?

asked 10 years ago

Artus gravatar image

Let's say I want to multiply (1i32)(1+i32). 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??

Preview: (hide)

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 ( 10 years ago )

2 Answers

Sort by » oldest newest most voted
1

answered 10 years ago

Pierre gravatar image

updated 10 years ago

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
Preview: (hide)
link

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 ( 10 years ago )

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

Pierre gravatar imagePierre ( 10 years ago )

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 ( 10 years ago )

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 ( 10 years ago )

There is a similar question in this thread

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

Pierre gravatar imagePierre ( 10 years ago )
1

answered 10 years ago

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
Preview: (hide)
link

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: 10 years ago

Seen: 814 times

Last updated: Apr 11 '15