Complex numbers
A complex number has a real part and an imaginary part, written with i, the square root of −1.
This page covers writing complex numbers, doing arithmetic with them, taking them apart into
modulus and argument, finding square roots of negative numbers, and the maths functions that
accept complex values.
Reach for it when a quadratic equation has no real roots, when you’re working with AC circuits or signals, or when you’re checking complex analysis coursework.
Writing complex numbers
2 + 3i gives 2 + 3icomplex(3, -4) gives 3 - 4ii^2 gives -1Put i straight after a number for the imaginary part, as in 3i or 1.5i, or use i on its
own. complex(real, imaginary) builds the same value from two parts, which is handy when the
parts are names: complex(resistance, reactance). Both parts must be plain numbers without units.
i means the imaginary unit only while nothing else in the note is called i. Once you define a
name or a custom unit called i, it takes over:
i = 52 + 3i gives 17complex(2, 3) gives 2 + 3iHere 3i became 3 × 5, and the line gave 17. Names carry across the calc blocks of a note, so an
i = … anywhere earlier in the note has the same effect. Avoid using i as a name in notes that
use complex numbers. complex(2, 3) doesn’t use the name, so it still works.
Arithmetic
(2 + 3i) + (1 - 5i) gives 3 - 2i(1 + 2i) * (3 - i) gives 5 + 5i(2 + 3i) * (2 - 3i) gives 1310 / (3 + 4i) gives 1.2 - 1.6i(1 + i)^8 gives 16Addition, subtraction, multiplication, division and whole-number powers are exact. When the
imaginary part of a result is zero, as with (2 + 3i) * (2 - 3i), the answer shows as an ordinary
number. Use brackets around each complex number when you multiply or divide, so that
(2 + 3i) * (2 - 3i) isn’t read as 2 + (3i * 2) - 3i.
Parts of a complex number
z = 3 + 4ireal(z) gives 3imag(z) gives 4conj(z) gives 3 - 4iabs(z) gives 5arg(1 + i) gives 0.7853981634arg(1 + i) rad in degrees gives 45°| Function | Gives |
|---|---|
real(z) |
The real part |
imag(z) |
The imaginary part, as a plain number |
conj(z) |
The complex conjugate: same real part, imaginary part with its sign flipped |
abs(z) |
The modulus: the distance from zero |
arg(z) |
The argument: the angle from the positive real axis, in radians |
arg returns a plain number of radians between −π and π. To see it in degrees, mark it as
radians and convert, as in the last line. To go the other way, from modulus and angle back to a
complex number, use exp: 2 * exp(i * pi / 3) is 1 + 1.7320508076i.
Square roots of negative numbers
A square root of an ordinary negative number is a real-number calculation, so sqrt(-16) shows
an error that tells you what to write instead: Error: imaginary number. For a complex answer, write sqrt(-16 + 0i). To ask for the complex root, write the number as a complex one: add 0i,
or use complex(…, 0):
sqrt(-16 + 0i) gives 4isqrt(complex(-16, 0)) gives 4isqrt(3 + 4i) gives 2 + isqrt(2i) gives 1 + isqrt returns the principal root, the one with a positive real part (or, for negative numbers,
a positive imaginary part). The other square root is its negative.
A value you write as complex stays complex, even when its imaginary part is zero. You can store
it in a name or carry on calculating with it first: after x = -16 + 0i, sqrt(x) is 4i,
even though x displays as -16.
This makes the quadratic formula work for any quadratic:
# Roots of x^2 + 2x + 5 = 0a = 1b = 2c = 5disc = b^2 - 4*a*c gives -16(-b + sqrt(disc + 0i)) / (2*a) gives -1 + 2i(-b - sqrt(disc + 0i)) / (2*a) gives -1 - 2iCube roots follow the same principal-root rule, which can surprise you with negative numbers:
cbrt(-8) gives -2cbrt(-8 + 0i) gives 1 + 1.7320508076iBoth are cube roots of −8. The real version gives the real root; the complex version gives the principal complex root.
Functions of a complex number
exp(i * pi) gives -1ln(-1 + 0i) gives 3.1415926536ii^i gives 0.2078795764cos(i) gives 1.5430806348sinh(i) gives 0.8414709848i2^i gives 0.7692389014 + 0.6389612763iThese functions accept complex values:
sin,cos,tanand their inversesasin,acos,atansinh,cosh,tanhand their inversesasinh,acosh,atanhexp,ln,log(orlog10),log2,sqrtandcbrt- powers with fractional or complex exponents, such as
i^0.5or2^i
They work in binary floating point, so answers are close approximations. Round-off leftovers
too small to matter show as 0, so exp(i * pi) reads as −1, the answer you’d expect from the
textbook. The stored value keeps every digit, so when you compare such results, check that the
difference is tiny, as in abs(exp(i * pi) + 1) < 1e-9, rather than using ==.
Each function gives a single answer, from its principal branch. A complex number has infinitely
many logarithms and several roots, and you get the standard one. As with sqrt, real inputs stay
real: ln(-1) and log(-100) show the imaginary-number error with the complex form to try, while
ln(-1 + 0i) gives πi. The
logarithm of zero is an error.
Functions that only make sense for real numbers, such as floor and round, don’t accept complex
values.
Comparing complex numbers
2 + 3i == complex(2, 3) gives true2 + 3i != 2 - 3i gives truecomplex(3, 0) >= 3 gives trueabs(3 + 4i) > abs(1 + 2i) gives true== and != work on any complex values. <, <=, > and >= only work when both sides have
an imaginary part of zero, because complex numbers have no natural order: i < 2 is an error. To
compare sizes, compare the moduli with abs, as in the last line. Complex numbers can’t be
sorted.
The full rules are in the advanced mathematics reference.
Putting it together
Here is a check on an AC circuit problem: a 30 Ω resistor and an inductor with 40 Ω of reactance in series, on a 10 V supply:
# Series circuit: 30 Ω resistor, 40 Ω reactance, 10 V supplyresistance = 30reactance = 40z = complex(resistance, reactance) gives 30 + 40iabs(z) gives 50current = 10 / z gives 0.12 - 0.16iabs(current) gives 0.2arg(z) rad in degrees to 1 dp gives 53.1°The impedance is 30 + 40i, with a magnitude of 50 Ω, so the current is 0.2 A and lags the
voltage by about 53.1°. Engineers often write j for the imaginary unit; in Varlig it is always
i. The ohms, volts and amps live in the comment, because the parts of a complex number can’t
carry units.