Ask Your Question
0

Ignoring the very small imaginary part

asked 2013-05-01 16:03:09 +0200

k1 gravatar image

I am generating a matrix that its entries come from roots of a polynomial which are all real. Due to calculation errors sage returns entries such as: -2.8 + 2.2e-16*I

How can I ask sage to ignore the small imaginary part and return -2.8?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2013-05-01 16:47:43 +0200

vdelecroix gravatar image

Hi,

I would prefer to have a method .real() for a complex matrix as there exists one for complex number::

sage: CC(1,0.5).real()
1.00000000000000

But it does not exist yet! Moreover, using .real() ignore all imaginary part. Here is a function that makes the job:

trunc_small_imag = lambda x: x.real() if abs(x.imag()) < .000001 else x
mat_trunc_small_imag = lambda m: m.parent()(map(trunc_small_imag, m.list()))

And here is an example:

sage: m = matrix([[CC(1,.0000001), CC(2,-0.5)],[CC(2,0.4),CC(1.1,-.000000004)]])
sage: mat_trunc_small_imag(m)
[                      1.00000000000000 2.00000000000000 - 0.500000000000000*I]
[2.00000000000000 + 0.400000000000000*I                       1.10000000000000]
edit flag offensive delete link more

Comments

Thank you very much. It worked great.

k1 gravatar imagek1 ( 2013-05-01 17:19:44 +0200 )edit

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: 2013-05-01 16:03:09 +0200

Seen: 447 times

Last updated: May 01 '13