Ask Your Question

Ignoring the very small imaginary part

asked 2013-05-01 09:03:09 -0500

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

answered 2013-05-01 09:47:43 -0500


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()

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


Thank you very much. It worked great.

k1 gravatar imagek1 ( 2013-05-01 10:19:44 -0500 )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


Asked: 2013-05-01 09:03:09 -0500

Seen: 132 times

Last updated: May 01 '13