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?
5 Answers
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.
-
36That entire issue thread is frustrating to read. They're rejecting the idea based on a false premise.
I
(noti
) 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, andA
is the unit.i
is only ever used for imaginary numbers.j
as a suffix is actually more confusing, becauseJ
as a suffix is actually used in physics and engineering (Joules).– DaveSMay 11, 2015 at 14:27 -
9
i
is sometimes used to represent AC current in order to distinguish from DC values (for example). Not that it's still not odd reasoning, just not necessarily a false premise.– sappjwOct 20, 2016 at 13:04 -
10That 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... whileI
instead ofi
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, lowercasei
looks neat in typesetting (and doesn't resemble any digits, because it's half-width), whilej
sticks below the baseline. This gets uglier when it's typeset in the exponent (not a python issue, but still...).– orionDec 10, 2016 at 23:34 -
6@DaveS yes, I think guido
i
looks like1
is the only "barely" acceptable reason... But technically it's also false if you use a font that make ai
and1
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. Becausei
andI
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... Feb 3, 2017 at 14:18 -
1
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.NeOct 25, 2020 at 8:31
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.
-
1I 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. And, yes I do remember that Guido stated exactly your argument:
j
is used in Engineering and we adopted that convention.– BakuriuJul 17, 2014 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 thati
looks too much like a1
?I
can, buti
doesn't.– DaveSMay 11, 2015 at 14:33 -
3And I know they have
1J
aliased to1j
, but they shouldn't.1J
looks like1 Joule
to any physicist or engineer.– DaveSMay 11, 2015 at 14:39
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;
-
1Almost any other language that I know of that support imaginary numbers uses
i
. Feb 3, 2017 at 14:07 -
10Even MATLAB, which is mostly used by engineers, supports
i
for imaginary numbers. It supports bothi
andj
. And python does not usei
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 usei
.– NaetmulFeb 12, 2018 at 9:35 -
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.
-
3And
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.– orionDec 10, 2016 at 23:44
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.