Ask Your Question

Should I use numpy and cython for complex numbers?

asked 2016-08-20 15:24:19 +0200

anonymous user


Here is the problem:

if I try this:

z = 1+2*I
<type 'sage.symbolic.expression.Expression'>

So, it isn't a complex number. BUT, if I do this:

I = CC.0
z = 1+2*I

then I get a complex number

<type 'sage.rings.complex_number.ComplexNumber'>

Should I use numpy and cython for complex numbers?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2016-08-20 16:26:46 +0200

tmonteil gravatar image

updated 2016-08-20 16:44:38 +0200

It depends on what kind of complex number you want. Sage provide tons, you do not need numpy, cython or whatever. Here are some examples (these are not the only ones):


sage: z = 1+2*I
sage: z.parent()
Symbolic Ring

You indeed get symbolic representation of a complex number, so you can do symboloc things such as:

sage: exp(pi*z).simplify()


sage: z = 1+2*CDF.0
sage: z.parent()
Complex Double Field

You get a fast numerical representation of your complex number (you should prefer CDF over CC since both have the same precision, but CDF takes the advantage of the CPU floating-point arithmetics, and the functions you will call from it will use optimized libraries).


sage: z = 1+2*ComplexIntervalField(1000).0
sage: z.parent()
Complex Interval Field with 1000 bits of precision

You get a certified numerical representation of your complex number with high precision (interval arithmetics).


sage: z = 1+2*QQbar.0
sage: z.parent()
Algebraic Field

You get an algebraic (hence exact) representation of your complex number (but pi does not exist here).


sage: z = 1+2*ZZ[i].1
sage: z.parent()
Gaussian Integers in Number Field in I with defining polynomial x^2 + 1

You get a Gaussian integer.

And so on...

Now, you just have to decide what do you want to do with z to select an appropriate representation.

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


Asked: 2016-08-20 15:24:19 +0200

Seen: 651 times

Last updated: Aug 20 '16