Wed, Jul 26, 2023, 12:58pm Complex Number Notation in Swift Code
Programming » Math-Science
(Last updated: Thu, Jul 27, 2023, 3:42pm)
y far the most common way to indicate complex numbers in code is by postfixing an i, j, or im to a number. Ex: 3 + 4i. This is based on the old pen-on-paper notation in math, physics and electrical engineering (who use j). There's also a nice "phasor" notation, which uses angle-n-magnitude instead of real-n-imaginary coordinates: 5 ∠ 53.13°, though font designers usually create awful symbols (often very tiny, for some reason), making them unreliable to use in code.

Even in pure math, the i postfix is but a shorthand (by Euler in 1777), and we're not obliged to use it. In Swift it's not directly an option. One could define a variable i and write 3 + 4 * i but this would lock up a very common variable name.

One creative route is to use the upside-down exclamation mark, ¡. It's a legit postfix operator character, and can't be confused with a variable i in the type system. 3 + 4¡ works, and is available on most all keyboard layouts. But the fact that ¡ looks like i, might be confusing to coders (though IDEs like Xcode will colorize ¡ as an operator), and the fact that coders wince at using any character that is unusual (even if at everyone's fingertips), means its use may be doomed before it takes off. (Coders are often surprisingly conservative when it comes to code characters. Just try suggesting we use a real arrow instead of the regrettable -> hack that we've allowed to fester for far too long.)

A different way to go in Swift, is to create a computed variable called i as an extension to all numbers that returns a complex number, which would look like this: 3 + 4.i which isn't bad. Simple. In this and the previous cases, the function takes 4 and maps it to a complex number, and then any real + complex returns a complex number.

Python, Matlab, and the Swift Numerics package, all allow the alternate form complex(3, 4). Not brief, but fairly clear. Numerics also has a nice variant, using argument labels to allow phasor coordinates: Complex(length: 5, phase: 0.93). (But is that phase in radians or degrees? Answer: radians. It seems destined to let in some bugs.) That said, it is an unfortunate amount of clutter to use the Complex(. . .) notation every single time you want to pop a number in there.

Are there other choices? There is the character , which is used in Math to represent the complex number set, so one could write ℂ(3, 4), which does look decent, but few have that character on their keyboard layout, so it's a non-starter. Still, it could be an option.

I'm tempted to include all of these in an update to my Physical Framework and see which ones fly: 3 + 4.i, 3 + 4¡, 5 ∠ 53°, Complex(3, 4), and ℂ(3, 4). (Since Physical already has unit-aware angles, it should make phasor notation more intuitive as well.) Probably the most practical and the easiest for everyone is just 3 + 4.i.

More ideas or suggestions?

Leave a comment