76

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?

Eli Rose
  • 6,788
  • 8
  • 35
  • 55

7 Answers7

65

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.

Community
  • 1
  • 1
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • 44
    That entire issue thread is frustrating to read. They're rejecting the idea based on a false premise. `I` (not `i`) is used for current in engineering and physics, and Python is case-sensitive. Also, `I` is never used as a suffix to numbers - `A` is used instead (amperes). `I` is the variable, and `A` is the unit. `i` is only ever used for imaginary numbers. `j` as a suffix is actually more confusing, because `J` as a suffix is actually used in physics and engineering (Joules). – DaveS May 11 '15 at 14:27
  • 9
    `i` is sometimes used to represent AC current in order to distinguish from DC values ([for example](https://en.wikipedia.org/wiki/Alternating_current#Mathematics_of_AC_voltages)). Not that it's still not odd reasoning, just not necessarily a false premise. – sappjw Oct 20 '16 at 13:04
  • 11
    That whole reasoning is ridiculous, because every symbol (including `j`) is used for something in physics, there's no reason to make current the exception. Even worse... while `I` instead of `i` is used for current, `j` is used for current density (and fluxes in general). So whoever makes this argument, actually shoots himself in both knees with a single arrow. Also, lowercase `i` looks neat in typesetting (and doesn't resemble any digits, because it's half-width), while `j` sticks below the baseline. This gets uglier when it's typeset in the exponent (not a python issue, but still...). – orion Dec 10 '16 at 23:34
  • 6
    @DaveS yes, I think guido `i` looks like `1` is the only "barely" acceptable reason... But technically it's also false if you use a font that make a `i` and `1` to look alike, you're already asking for problems... The whole "electrical engineering" could explain the origin, but it doesn't mean that it makes sense. Because `i` and `I` do not actually have a particular meaning in python. For all I know, almost every possible symbols are being used in pretty much any science field and they even use more than we can actually type with a symbol keyboard... think about APL... – Loïc Faure-Lacroix Feb 03 '17 at 14:18
  • 2
    `If you want to parse complex numbers using 'i' instead of 'j', you have plenty of solutions available already` - what is he talking about? – Kolay.Ne Oct 25 '20 at 08:31
  • 1
    @Kolay.Ne My guess is, he's talking about other programming languages. – Bill the Lizard Oct 25 '20 at 22:56
  • I agree it's weird that Python uses `j`, but it really does make sense in electrical engineering. We often use `i` for AC currents and `I` for DC ones, and in addition AC currents are often represented using complex numbers, which means you would need both `i`s in the same equation. Sure `j` is used for current density, but that occurs much less often and it's also typically not complex as far as I know. Of course we could've solved this by picking a different symbol for current instead, since `i` seems pretty arbitrary for that. But we do need both symbols to be different. – PieterNuyts Dec 07 '21 at 08:05
17

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.

Andy
  • 49,085
  • 60
  • 166
  • 233
  • 1
    I remember that Guido had already answered this question (but I'm sure it was a different source). However I cannot find it in [The History of Python](http://python-history.blogspot.it/). And, yes I *do* remember that Guido stated exactly your argument: `j` is used in Engineering and we adopted that convention. – Bakuriu Jul 17 '14 at 20:15
  • `i` isn't used to represent current though. `I` is. But not as a suffix to numbers. `A` is used for that. And as for Guido's reasoning, Python is case-sensitive, so why argue that `i` looks too much like a `1`? `I` can, but `i` doesn't. – DaveS May 11 '15 at 14:33
  • 3
    And I know they have `1J` aliased to `1j`, but they shouldn't. `1J` looks like `1 Joule` to any physicist or engineer. – DaveS May 11 '15 at 14:39
15

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;
Nick Matteo
  • 4,453
  • 1
  • 24
  • 35
  • 3
    Almost any other language that I know of that support imaginary numbers uses `i`. – Loïc Faure-Lacroix Feb 03 '17 at 14:07
  • 11
    Even MATLAB, which is mostly used by engineers, supports `i` for imaginary numbers. It supports both `i` and `j`. And python does not use `i` as a suffix for any purpose. If the compatibility of old codes is the problem, then why not support both like MATLAB as new versions' feature? *C++, Ruby, Go, R, and Perl* all use `i`. – Naetmul Feb 12 '18 at 09:35
  • I wonder how many PEPs were made to change that awful decision @Naetmul – testing_22 Apr 01 '22 at 00:14
1

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.

Oleksii
  • 11
  • 2
  • 4
    And `j` is used for current density, and even worse, it stands directly next to the imaginary unit in Drude's model. How would you rewrite (1+iωτ)j=σE in this notation? This reasoning is bogus. – orion Dec 10 '16 at 23:44
1

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.

1

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)
Scott Centoni
  • 1,019
  • 11
  • 13
-3
    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

Patman1O1
  • 5
  • 4
  • This is how you convert the j to i – Patman1O1 Jun 04 '22 at 19:14
  • 1
    You should include your comment as part of the answer instead – altermetax Jun 04 '22 at 21:57
  • This is worthless you are just making a number into a string now it is no longer a number, the question means why not use i instead of j (they mean to change the syntax) – user16714199 Jun 05 '22 at 06:01
  • @user16714199, I understand the question was to change the syntax for Python. However, given that this post is 7 years old and the syntax still hasn't changed, I don't think Python will ever intend on changing it from j to i. Therefore, I have provided a solution for people who wish to print imaginary numbers as i instead of j. To address the first part of your statement. No, the number is still a number. I converted the complex number to a string, removed the j with a null string, and converted the string back into a number. I tested this by adding res_num by itself which equaled 2 and got 4. – Patman1O1 Jun 07 '22 at 20:55
  • @Patman1O1 yes I mean 2.00000000i is not a number but 2.00000000 itself is a number – user16714199 Jun 10 '22 at 05:15