Ask Your Question
3

How to display complex numbers with real part before imaginary part?

asked 2019-01-04 16:39:35 +0100

pong gravatar image

updated 2020-12-22 09:03:47 +0100

slelievre gravatar image

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

edit retag flag offensive close merge delete

Comments

Some things have changed in Sage since that question was asked and answered in 2019-01.

The same question was asked again in 2020-12, and answered taking these changes into account:

Ask Sage question 54821: How to arrange real and imaginary parts of a complex number

slelievre gravatar imageslelievre ( 2020-12-26 01:27:49 +0100 )edit

3 Answers

Sort by ยป oldest newest most voted
2

answered 2019-01-04 20:18:30 +0100

slelievre gravatar image

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.

edit flag offensive delete link more

Comments

1

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}')
pong gravatar imagepong ( 2019-01-05 08:09:12 +0100 )edit
2

answered 2019-01-04 17:29:12 +0100

rburing gravatar image

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.

edit flag offensive delete link more

Comments

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?

pong gravatar imagepong ( 2019-01-04 18:17:42 +0100 )edit
1

answered 2019-01-08 01:20:15 +0100

pong gravatar image

updated 2019-01-08 01:20:57 +0100

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)
edit flag offensive delete link more

Comments

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)))
slelievre gravatar imageslelievre ( 2019-01-08 05:43:32 +0100 )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

1 follower

Stats

Asked: 2019-01-04 16:39:35 +0100

Seen: 1,498 times

Last updated: Dec 22 '20