Ask Your Question

How to arrange real and imaginary parts of a complex number

asked 2020-12-22 06:53:10 +0200

Souichi gravatar image

Hi, My question is about the complex numbers in Sage.

Actually, I don't like the order of real and imaginary parts arranged by Sage. For example, not 2+3I, but 3I+2.

How can I arrange terms of a complex number so that the real part is followed by the imaginary part?

edit retag flag offensive close merge delete


Welcome to Ask Sage!

Thank you for your question!

slelievre gravatar imageslelievre ( 2020-12-22 09:01:20 +0200 )edit

Typing [ imaginary part ] in Ask Sage's "search or ask your question" box reveals a similar question:

Things have changed in Sage since two years ago though, and nowadays inputting 2 + 3*I gives an element in QQ[i] rather than in the symbolic ring, so the discussion at Ask Sage question 44894 is somewhat obsolete, and it's good you asked again.

slelievre gravatar imageslelievre ( 2020-12-22 09:12:39 +0200 )edit

@slelievre : Would you care to explain :

sage: (2+3*I).parent()
Number Field in I with defining polynomial x^2 + 1 with I = 1*I
sage: ((2+3*I)*x).coefficient(x).parent()
Symbolic Ring


sage: QQ[i]
Number Field in I with defining polynomial x^2 + 1 with I = 1*I
sage: (2+3*I).parent()
Number Field in I with defining polynomial x^2 + 1 with I = 1*I
sage: (2+3*I).parent() == QQ[i]

This might deserve some documentation, possibly in the tutorials...

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2020-12-22 13:50:25 +0200 )edit

@Emmanuel Charpentier -- slightly off topic here (could be a separate Ask Sage question) but here we go.

The result of multiplying by x lives in the symbolic ring. The symbolic ring wraps its constants. Compare:

sage: a = 1
sage: a
sage: a.parent()
Integer Ring

sage: b = a * x
sage: b
sage: b.parent()
Symbolic Ring

sage: c = b.coefficient(x)
sage: c
sage: c.parent()
Symbolic Ring

sage: d = c.pyobject()
sage: d
sage: d.parent()
Integer Ring

This allows to write equations such as

sage: eq = SR(1) == SR(2)
sage: eq
1 == 2
sage: bool(eq)
slelievre gravatar imageslelievre ( 2020-12-23 15:37:44 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2020-12-26 01:24:35 +0200

slelievre gravatar image

updated 2020-12-27 12:32:07 +0200

Complex numbers can be thought of as polynomials of degree at most one in i.

To print polynomial expressions in some quantity, two common choices are to order by increasing or by decreasing powers of that quantity.

The choice made in Sage is usually by decreasing powers, which is at odds with the usage of writing complex numbers with real component first and imaginary component second.

Power series are another family of structures whose elements involve combinations of powers of a variable, but which, in Sage, are printed by ascending powers of the variables.

Here is a little function for printing complex numbers in our usual order, taking advantage of that.

def cprint(z):
    Print this complex number as `a + b*i` instead of `b*I + a`.


        sage: aa = [2 + 3*i, -1/2 + sqrt(3)/2*i, (1+sqrt(5))/2 + (1+sqrt(5))/2*i]
        sage: for a in aa:
        ....:     cprint(a)
    v = vector([z.real(), z.imag()])
    R = v.base_ring()

(It would be easy to tweak to return a string instead of printing it, and to optionally provide a LaTeX form.)


sage: u = 2 + 3*i
sage: print(u); cprint(u)
3*I + 2
2 + 3*i

sage: v = -1/2 + sqrt(3)/2*i
sage: print(v); cprint(v)
1/2*I*sqrt(3) - 1/2
-1/2 + 1/2*sqrt(3)*i

sage: w = (1+sqrt(5))/2 + (1+sqrt(5))/2*i
sage: print(w); cprint(w)
(1/2*I + 1/2)*sqrt(5) + 1/2*I + 1/2
1/2*sqrt(5) + 1/2 + (1/2*sqrt(5) + 1/2)*i
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: 2020-12-22 06:53:10 +0200

Seen: 801 times

Last updated: Dec 27 '20