Why are complex numbers in Python denoted with 'j' instead of 'i'?

Question:

I know this is an electrical engineering convention, but I’m still wondering why it was chosen for Python. I don’t know other programming languages with complex-number literals, so I don’t have anything to compare against, but does anyone know any that do use i?

Asked By: Eli Rose

||

Answers:

Python adopted the convention used by electrical engineers. In that field, i is used to represent current and use j as the square root of -1.

There was a bug logged to change it to i in Python 3.3. It was resolves as a “WONTFIX” with this reasoning by Guido van Rossum:

This will not be fixed. For one thing, the letter ‘i’ or upper case
‘I’ look too much like digits. The way numbers are parsed either by
the language parser (in source code) or by the built-in functions
(int, float, complex) should not be localizable or configurable in any
way; that’s asking for huge disappointments down the road. If you want
to parse complex numbers using ‘i’ instead of ‘j’, you have plenty of
solutions available already.

Answered By: Andy

It appears to be, as you guessed, because Python follows the electrical engineering convention. Here’s an exchange from the Python bug tracker Issue10562:

BoĊĦtjan Mejak: In Python, the letter ‘j’ denotes the imaginary unit. It would be great if we would follow mathematics in this regard and let the imaginary unit be denoted with an ‘i’.

Michael Foord: We follow engineering which uses j.

(I was about to close this as wontfix but Antoine is particularly keen that Mark deals with this issue…)

Mark Dickinson: Just to add my own thoughts: ‘j’ for a (not the ) square root of -1 has, as Michael points out, a history of use in engineering (particularly electrical engineering) and physics. Personally, I would have preferred ‘i’ to ‘j’ here, but changing it now would cause (IMO) gratuitous breakage. It really doesn’t seem a big enough issue to be worth making a fuss about.

Much later:

Guido van Rossum: This will not be fixed. For one thing, the letter ‘i’ or upper case ‘I’ look too much like digits. The way numbers are parsed either by the language parser (in source code) or by the built-in functions (int, float, complex) should not be localizable or configurable in any way; that’s asking for huge disappointments down the road. If you want to parse complex numbers using ‘i’ instead of ‘j’, you have plenty of solutions available already.

Answered By: Bill the Lizard

j (not J) is used in Electrical Engineering as mentioned before.
i for current: yes, both I (dc) and i (ac) are used for current.

Answered By: Oleksii

To answer “does anyone know any [other programming languages with complex-number literals] that do use i?”

Yes, C++ since the C++14 standard. You have to use the right namespace though:

#include <complex>
using namespace std::complex_literals;

std::complex<double> z = 2 + 3i;
Answered By: Nick Matteo

i in electrical engineering is typically used for i(t) or instantaneous current. I is for steady state DC (non-complex) or rms values of AC current. In addition spacial coordinates are generally expressed as i,j,k but for two dimensional items i,j are all that are needed and the “i” is dropped so the perpendicular “j” is used as in 4j3 vs 4+3i or 4i3 -See that this is not 413 at a glance.
J recognizes this notation in handling complex numbers. As a retired EE prof- I do like the use of “j” As for Current density “J” is used.

Answered By: user10973708
    num = 2j
    print(num)
    num_str = str(num)

    res_str = num_str.replace('j', '') 

    res_num = float(res_str)
    print('{}i'.format(res_num))

Output

2.00000000j

2.00000000i

Answered By: Patman1O1

Fortran, which predates almost every other programming language living or dead, introduced complex numbers in the late 1950s. Python is one of the few programming languages other than Fortran that supports complex numbers as a built-in data type rather than an external library. Fortran represents complex numbers as ordered pairs (x,y) rather than a binomial with an imaginary unit x+iy. Example code and output:

$ cat test_cmplx.f90 
program test_cmplx
    integer :: i = 42
    real :: x = 3.14
    complex :: z
    z = cmplx(i, x)
    print *, z, cmplx(x)
end program test_cmplx
$ gfortran test_cmplx.f90 -o test_cmplx && ./test_cmplx
             (42.0000000,3.14000010)             (3.14000010,0.00000000)
Answered By: Scott Centoni
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.