ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 26 Dec 2020 01:24:35 +0100How to arrange real and imaginary parts of a complex numberhttps://ask.sagemath.org/question/54821/how-to-arrange-real-and-imaginary-parts-of-a-complex-number/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+3*I, but 3*I+2.
How can I arrange terms of a complex number so that the real part is followed by the imaginary part?Tue, 22 Dec 2020 06:53:10 +0100https://ask.sagemath.org/question/54821/how-to-arrange-real-and-imaginary-parts-of-a-complex-number/Comment by slelievre for <p>Hi,
My question is about the complex numbers in Sage.</p>
<p>Actually, I don't like the order of real and imaginary parts arranged by Sage.
For example, not 2+3<em>I, but 3</em>I+2.</p>
<p>How can I arrange terms of a complex number so that the real part is followed by the imaginary part?</p>
https://ask.sagemath.org/question/54821/how-to-arrange-real-and-imaginary-parts-of-a-complex-number/?comment=54822#post-id-54822Welcome to Ask Sage!
Thank you for your question!Tue, 22 Dec 2020 09:01:20 +0100https://ask.sagemath.org/question/54821/how-to-arrange-real-and-imaginary-parts-of-a-complex-number/?comment=54822#post-id-54822Comment by Emmanuel Charpentier for <p>Hi,
My question is about the complex numbers in Sage.</p>
<p>Actually, I don't like the order of real and imaginary parts arranged by Sage.
For example, not 2+3<em>I, but 3</em>I+2.</p>
<p>How can I arrange terms of a complex number so that the real part is followed by the imaginary part?</p>
https://ask.sagemath.org/question/54821/how-to-arrange-real-and-imaginary-parts-of-a-complex-number/?comment=54838#post-id-54838@`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
Also:
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]
False
This might deserve some documentation, possibly in the tutorials...Tue, 22 Dec 2020 13:50:25 +0100https://ask.sagemath.org/question/54821/how-to-arrange-real-and-imaginary-parts-of-a-complex-number/?comment=54838#post-id-54838Comment by slelievre for <p>Hi,
My question is about the complex numbers in Sage.</p>
<p>Actually, I don't like the order of real and imaginary parts arranged by Sage.
For example, not 2+3<em>I, but 3</em>I+2.</p>
<p>How can I arrange terms of a complex number so that the real part is followed by the imaginary part?</p>
https://ask.sagemath.org/question/54821/how-to-arrange-real-and-imaginary-parts-of-a-complex-number/?comment=54825#post-id-54825Typing [ imaginary part ] in Ask Sage's "search or ask your question" box reveals a similar question:
- [Ask Sage question 44894: How to display a complex number with its real part before its imaginary part?](https://ask.sagemath.org/question/44894)
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.Tue, 22 Dec 2020 09:12:39 +0100https://ask.sagemath.org/question/54821/how-to-arrange-real-and-imaginary-parts-of-a-complex-number/?comment=54825#post-id-54825Comment by slelievre for <p>Hi,
My question is about the complex numbers in Sage.</p>
<p>Actually, I don't like the order of real and imaginary parts arranged by Sage.
For example, not 2+3<em>I, but 3</em>I+2.</p>
<p>How can I arrange terms of a complex number so that the real part is followed by the imaginary part?</p>
https://ask.sagemath.org/question/54821/how-to-arrange-real-and-imaginary-parts-of-a-complex-number/?comment=54873#post-id-54873@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
1
sage: a.parent()
Integer Ring
sage: b = a * x
sage: b
x
sage: b.parent()
Symbolic Ring
sage: c = b.coefficient(x)
sage: c
1
sage: c.parent()
Symbolic Ring
sage: d = c.pyobject()
sage: d
1
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)
FalseWed, 23 Dec 2020 15:37:44 +0100https://ask.sagemath.org/question/54821/how-to-arrange-real-and-imaginary-parts-of-a-complex-number/?comment=54873#post-id-54873Answer by slelievre for <p>Hi,
My question is about the complex numbers in Sage.</p>
<p>Actually, I don't like the order of real and imaginary parts arranged by Sage.
For example, not 2+3<em>I, but 3</em>I+2.</p>
<p>How can I arrange terms of a complex number so that the real part is followed by the imaginary part?</p>
https://ask.sagemath.org/question/54821/how-to-arrange-real-and-imaginary-parts-of-a-complex-number/?answer=54905#post-id-54905Complex 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):
r"""
Print this complex number as `a + b*i` instead of `b*I + a`.
EXAMPLES:
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()
print(R[['i']](v.list()))
(It would be easy to tweak to return a string instead of printing it,
and to optionally provide a LaTeX form.)
Examples:
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)*iSat, 26 Dec 2020 01:24:35 +0100https://ask.sagemath.org/question/54821/how-to-arrange-real-and-imaginary-parts-of-a-complex-number/?answer=54905#post-id-54905