# How to display a complex number with its real part comes before its imaginary part?

Also I would like to have latex(I) output \mathbf{i} instead of just i. Is there a way to do so?

edit retag close merge delete

Sort by » oldest newest most voted

One way to obtain \mathbf{i} instead of just i is to use a string replacement.

To also get the real part before the imaginary part, why not use a little helper function.

The following might do what you want.

def latex_of_complex(z):
r"""
Return a LaTeX string for this complex number.

EXAMPLE::

sage: latex_of_complex(0)
0
sage: latex_of_complex(2)
2
sage: latex_of_complex(4*i)
4 \mathbf{i}
sage: latex_of_complex(7 + 8*i)
7 + 8 \mathbf{i}
"""
if z == 0:
return LatexExpr('0')
a = z.real()
b = z.imag()
if b == 0:
return latex(a)
s = (latex(a) + ' + ') if a else ''
return LatexExpr(s) + latex(b * i).replace('i', r'\mathbf{i}')


Now use latex_of_complex instead of latex to latexify complex numbers with the requested customization.

more

Thanks. It does what I want! Perhaps, sage should have a built-in function for that task. The codes above typeset a-bi as a+ -bi. but just a minor modification fixes that:


def cpx(z):
if z == 0:
return LatexExpr('0')
a = z.real()
b = z.imag()
if (a == 0) or (b==0) :
return LatexExpr(latex(z).replace('i',r'\mathbf{i}'))
elif b > 0:
s = '+'
else:
s = '-'
return latex(a) + LatexExpr(s) + latex(abs(b) * i).replace('i', r'\mathbf{i}')

( 2019-01-05 01:09:12 -0600 )edit

I don't know about redefining, but you can do something like this:

bI = var('bI', latex_name='\mathbf{i}')
var('a,b')
assume(a, 'real')
assume(b, 'real')
expr = (a + b*I)^3
show(expr.real_part() + bI*expr.imag_part())


Note that getting real and imaginary parts expands the expression, so this may be not what you want.

more

Thanks, Well, what I want is to make some homework and use sage (sagetex) to do the computations. Now I realize a bigger problem is that sage displays imaginary part in front of the real part. E.g. latex(7+8i) returns 8i + 7. I understand this is a different question but can I get some help here?

( 2019-01-04 11:17:42 -0600 )edit

Well after using the code a couple days, I modified it again and here is a better version. E.g. now 2+(1-sqrt(3))i is typeset as shown v.s. as 2+i+sqrt(3)i in the previous version.


def cpx(z):
r"""
Return a LaTeX string for this complex number.

EXAMPLE::

sage: latex_of_complex(0)
0
sage: latex_of_complex(2)
2
sage: latex_of_complex(4*i)
4 \mathbf{i}
sage: latex_of_complex(7 + 8*i)
7 + 8 \mathbf{i}
"""
if z == 0:
return LatexExpr('0')
re = z.real().simplify()
im = z.imag().simplify()
if (re == 0) or (im==0) :
return LatexExpr(latex(z).replace('i',r'\mathbf{i}'))
elif im > 0:
s = '+'
else:
s = '-'
rel = latex(re)
iml = latex(abs(im))
if rel.find('+') >= 1 or rel.find('-') >=1:
repl = r'\left('+rel+r'\right)'
else:
repl = rel
if iml.find('+') >=1 or iml.find('-') >=1:
impl = r'\left('+iml+r'\right)'+r'\mathbf{i}'
else:
impl = iml+r'\mathbf{i}'
if abs(im) ==1:
return LatexExpr(repl + s + r'\mathbf{i}')
return LatexExpr(repl + s + impl)

more

Good exploration of this pretty-printing exercise.

In my experience, pretty-printing always takes a while to get right.

Hints:

• add the new examples to the function's documentation
• update the function's documentation to use the new name
• try more examples and make more changes if needed

For instance, the following examples might suggest some changes:

sage: aa = [0, 1]
sage: bb = [0, 1, -1, 2, -2, 1 - sqrt(3), sqrt(3) - 1, 1 + sqrt(3), -1 - sqrt(3)]
sage: for a in aa:
....:     for b in bb:
....:         print('{:20} {:50}'.format(str(a + b * I), cpx(a + b * I)))

( 2019-01-07 22:43:32 -0600 )edit