Data and Math
vcomplex
Complex numbers as a shape of their own, with the arithmetic to match.
Released v1.0.0 · 2026-09-28use "stdlib/complex.ven"
Complex numbers as their own shape, with a real and an imaginary field, built entirely from ordinary number arithmetic and the existing square root, sine, cosine and arctangent builtins. It covers addition, multiplication, division, magnitude, conjugate, polar form, the square root, and the exponential, logarithm, sine and cosine of a complex value. There is no complex power operator and no ordering between complex numbers, since they have none.
z = Complex(3.0, 4.0)
print z.real
:: prints: 3.0
print z.imag
:: prints: 4.0
Reference (23 functions)
API reference
cplx_coerce(value)
Coerces a plain Number or an existing Complex into a Complex.
cplx_display(cplx_coerce(5.0))
:: prints: 5.0 + 0.0icplx_add(a, b)
Adds two complex numbers (or a complex number and a plain Number).
cplx_display(cplx_add(Complex(3.0, 4.0), Complex(1.0, 2.0)))
:: prints: 4.0 + 6.0icplx_subtract(a, b)
Subtracts b from a (or a plain Number from a complex number).
cplx_display(cplx_subtract(Complex(3.0, 4.0), Complex(1.0, 2.0)))
:: prints: 2.0 + 2.0icplx_multiply(a, b)
Multiplies two complex numbers using the standard FOIL identity.
cplx_display(cplx_multiply(Complex(1.0, 2.0), Complex(3.0, 4.0)))
:: prints: -5.0 + 10.0icplx_divide(a, b)
Divides a by b, by multiplying through by bs conjugate.
cplx_display(cplx_divide(Complex(1.0, 2.0), Complex(3.0, -4.0)))
:: prints: -0.2 + 0.4icplx_negate(z)
Negates a complex number, flipping the sign of both parts.
cplx_display(cplx_negate(Complex(3.0, 4.0)))
:: prints: -3.0 - 4.0icplx_equals(a, b)
Tests two complex numbers (or a complex number and a plain Number) for equality.
cplx_equals(Complex(3.0, 0.0), 3.0)
:: gives yescplx_is_zero(z)
cplx_is_zero(Complex(0.0, 0.0))
:: gives yescplx_magnitude_squared(z)
Gives the squared distance from the origin, without taking a square root.
cplx_magnitude_squared(Complex(3.0, 4.0))
:: gives 25.0cplx_magnitude(z)
Gives the distance from the origin (the complex modulus).
cplx_magnitude(Complex(3.0, 4.0))
:: gives 5.0cplx_abs(z)
Gives the distance from the origin, identical to cplx_magnitude.
cplx_abs(Complex(3.0, 4.0))
:: gives 5.0cplx_conjugate(z)
Gives the complex conjugate, the same real part with the imaginary part negated.
cplx_display(cplx_conjugate(Complex(3.0, 4.0)))
:: prints: 3.0 - 4.0icplx_to_polar(z)
Converts a complex number to its polar form, a (magnitude, angle) pair.
p = cplx_to_polar(Complex(0.0, 5.0))
print(p[0])
:: prints: 5.0
print(round_to(p[1], 6))
:: prints: 1.570796cplx_from_polar(r, theta)
Builds a complex number from a magnitude and an angle in radians.
cplx_display(cplx_from_polar(5.0, 0.0))
:: prints: 5.0 + 0.0icplx_phase(z)
Gives the angle, in radians, of a complex number from the positive real axis.
round_to(cplx_phase(Complex(0.0, 5.0)), 6)
:: gives 1.570796cplx_polar(z)
p = cplx_polar(Complex(0.0, 5.0))
print(p[0])
:: prints: 5.0cplx_rect(r, phi)
cplx_display(cplx_rect(5.0, 0.0))
:: prints: 5.0 + 0.0icplx_exp(z)
Gives e raised to a complex power, using the standard closed-form identity.
cplx_display(cplx_exp(Complex(0.0, 0.0)))
:: prints: 1.0 + 0.0icplx_log(z)
Gives the principal natural logarithm of a complex number.
cplx_display(cplx_log(Complex(2.718281828459045, 0.0)))
:: prints: 1.0 + 0.0icplx_sin(z)
Gives the complex sine of a complex number.
cplx_display(cplx_sin(Complex(0.0, 0.0)))
:: prints: 0.0 + 0.0icplx_cos(z)
Gives the complex cosine of a complex number.
cplx_display(cplx_cos(Complex(0.0, 0.0)))
:: prints: 1.0 + 0.0icplx_sqrt(z)
Gives the principal square root of a complex number.
cplx_display(cplx_sqrt(Complex(3.0, 4.0)))
:: prints: 2.0 + 1.0icplx_display(z)
cplx_display(Complex(3.0, -4.0))
:: gives "3.0 - 4.0i"