Skip to content

math.complex

complex numbers like 2 + 3i, for math with square roots of negatives

Complex numbers, which have a real part and an imaginary part, a multiple of i, the square root of -1.

Write the imaginary part as a number with i right after it and no space: 3i, 0.5i, and 1i for i itself. abs, exp, ln and sqrt accept complex numbers too.

Tags: algebra

  • re the real part
  • im the imaginary part, as a plain number
  • conj the complex conjugate: the same number with its imaginary part negated
  • arg the angle from the positive real axis to the number, as an angle unit
  • csqrt square root that gives a complex number for negatives, so csqrt(-4) is 2i
  • croots all n complex nth roots of a number, as a list
  • polar make a complex number from its polar form, or with one argument show z in that form
1
# an imaginary number: i touching a number
2
3i
3
# real part + imaginary part
4
2 + 3i
5
# + - * / ** work as usual
6
(1 + 2i) * (3 - 1i)5 + 5i
7
# abs, exp, ln and sqrt accept complex numbers
8
sqrt(-4 + 0i)2i
Example outputCtrl+Enter to run
re(z: num|complex) -> float

the real part

1
re(2 + 3i)2
Example outputCtrl+Enter to run

See also im

im(z: num|complex) -> float

the imaginary part, as a plain number

1
im(2 + 3i)3
Example outputCtrl+Enter to run

See also re

conj(z: num|complex) -> complex

the complex conjugate: the same number with its imaginary part negated

1
conj(2 + 3i)2 - 3i
Example outputCtrl+Enter to run

See also re, im

arg(z: num|complex) -> quantity

the angle from the positive real axis to the number, as an angle unit

1
arg(1i) to deg90 deg
Example outputCtrl+Enter to run

See also polar, abs

csqrt(z: num|complex) -> complex

square root that gives a complex number for negatives, so csqrt(-4) is 2i

sqrt(-4) is an error instead, so that a calculation going negative by mistake doesn’t quietly turn complex. Of the two square roots, gives the one with a positive real part

1
csqrt(-4)2i
Example outputCtrl+Enter to run

See also sqrt

croots(z: num|complex, n: int) -> list

all n complex nth roots of a number, as a list

They sit evenly around a circle, so the first is the principal root and the rest are it turned by 360°/n

1
croots(1, 3)[1 + 0i, -0.5 + 0.866025i, -0.5 - 0.866025i]
2
croots(-4, 2)[2i, -2i]
Example outputCtrl+Enter to run

See also csqrt

polar(r: num, theta: num|quantity) -> complex / polar(z: num|complex) -> str

make a complex number from its polar form, or with one argument show z in that form

A complex number seen as a point on a plane, real part across and imaginary part up, has a polar form: its distance from 0 and its angle. With one argument, the same as z to polar

1
polar(2, 90 deg)2i
2
polar(1i)"polar(1, 90 deg)"
Example outputCtrl+Enter to run

See also arg, abs

1
# Euler's identity: e to the i pi is -1
2
e ** (1i * pi)-1 + 0i
3
# roots of x² + 2x + 5
4
[-1 + csqrt(-4) / 2, -1 - csqrt(-4) / 2][-1 + 1i, -1 - 1i]
5
# as a distance and an angle
6
1 + 1i to polar"polar(1.41421, 45 deg)"
Example outputCtrl+Enter to run

Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.