Ask Your Question

pong's profile - activity

2020-02-22 20:44:59 -0500 commented answer Running VIM inside Sage Shell

When I type %edit then 1+1 in vim (which is the editor of my system) and exit, it emits '1+1/n' which is not executed by SAGE. Any help?

2019-12-06 13:15:29 -0500 received badge  Good Question (source)
2019-10-28 22:08:29 -0500 received badge  Nice Question (source)
2019-06-26 10:42:20 -0500 received badge  Popular Question (source)
2019-01-07 22:04:35 -0500 received badge  Self-Learner (source)
2019-01-07 22:04:35 -0500 received badge  Teacher (source)
2019-01-07 18:20:15 -0500 answered a question How to display a complex number with its real part comes before its imaginary part?

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)
2019-01-05 10:53:33 -0500 received badge  Editor (source)
2019-01-05 01:10:41 -0500 received badge  Supporter (source)
2019-01-05 01:10:14 -0500 received badge  Scholar (source)
2019-01-05 01:09:12 -0500 commented answer How to display a complex number with its real part comes before its imaginary part?

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-04 12:53:29 -0500 received badge  Student (source)
2019-01-04 11:17:42 -0500 commented answer How to display a complex number with its real part comes before its imaginary part?

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 09:39:35 -0500 asked a question 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?

2016-06-26 09:18:25 -0500 commented answer sage automatically expanding polynomial

But looks like sage will still expand e.g. 5(2x -7) into 10*x - 35. How to keep it in the original form?