complex numbers printing real part + imaginary part

44 views
Skip to first unread message

Pedro Cruz

unread,
Apr 11, 2017, 12:25:59 PM4/11/17
to sage-support
Hello,

sage is printing b*I + a and we want to see a + b * I

What could we do ?
We can create a function  to print it a + b*I but is any other standard way?

Thank you,
Pedro

Vincent Delecroix

unread,
Apr 11, 2017, 12:30:49 PM4/11/17
to sage-s...@googlegroups.com
Hello,

Indeed there are some unconsistancies

sage: CC(1,2)
1.00000000000000 + 2.00000000000000*I
sage: CDF(1,2)
1.0 + 2.0*I

versus

sage: QQbar.one() + 2*QQbar.gen()
2*I + 1
sage: K = QuadraticField(-1)
sage: K.one() + 2*K.gen()
2*a + 1

Vincent

Jeroen Demeyer

unread,
Apr 11, 2017, 3:25:46 PM4/11/17
to sage-s...@googlegroups.com
On 2017-04-11 18:25, Pedro Cruz wrote:
> Hello,
>
> sage is printing b*I + a and we want to see a + b * I

Do you have a complete example?

Pedro Cruz

unread,
Apr 12, 2017, 9:59:00 AM4/12/17
to sage-support
We are producing study materials where it's usual to use  "a + b I".

The following occurs in Sage 7.6:

sage: 1+2*I
2*I + 1

and the question is: how to rewrite the complex numbers way of printing.
I've seen it's <Expression> type and maybe changing _repr_ but I have never
tried.

We have done the function below but I was wonder if a more standard way of
printing them exists.

Thank you!
Pedro

def cnice(x):
    if x.imag_part()<0:
        return "{0} - {1}I".format(x.real_part(), abs(x.imag_part()))
    elif x.imag_part()==0:
        return "{0}".format(x.real_part())
    else:
        return "{0} + {1}I".format(x.real_part(), abs(x.imag_part()))
Reply all
Reply to author
Forward
0 new messages