Skip to content
Standard libraryZig 0.17.1Official documentation
Sourcestd/math/complex.zig

Source line 1

std.math.complex.abs

alias

Source line 5

Alias of std.math.complex.abs.abs

std.math.complex.acos

alias

Source line 7

Alias of std.math.complex.acos.acos

std.math.complex.acosh

alias

Source line 6

Alias of std.math.complex.acosh.acosh

std.math.complex.arg

alias

Source line 8

Alias of std.math.complex.arg.arg

std.math.complex.asin

alias

Source line 10

Alias of std.math.complex.asin.asin

std.math.complex.asinh

alias

Source line 9

Alias of std.math.complex.asinh.asinh

std.math.complex.atan

alias

Source line 12

Alias of std.math.complex.atan.atan

std.math.complex.atanh

alias

Source line 11

Alias of std.math.complex.atanh.atanh

std.math.complex.Complex

type-function

Source line 27

A complex number consisting of a real an imaginary part. T must be a floating-point value.

pub fn Complex(comptime T: type) type

Parameters

T: type

Fields

re: T

Real part.

im: T

Imaginary part.

std.math.complex.Complex.add

function

Source line 46

Returns the sum of two complex numbers.

pub fn add(self: Self, other: Self) Self

Parameters

self: Self
other: Self

std.math.complex.Complex.conjugate

function

Source line 82

Returns the complex conjugate of a number.

pub fn conjugate(self: Self) Self

Parameters

self: Self

std.math.complex.Complex.div

function

Source line 70

Returns the quotient of two complex numbers.

pub fn div(self: Self, other: Self) Self

Parameters

self: Self
other: Self

std.math.complex.Complex.init

function

Source line 38

Create a new Complex number from the given real and imaginary parts.

pub fn init(re: T, im: T) Self

Parameters

re: T
im: T

std.math.complex.Complex.magnitude

function

Source line 115

Returns the magnitude of a complex number.

pub fn magnitude(self: Self) T

Parameters

self: Self

std.math.complex.Complex.mul

function

Source line 62

Returns the product of two complex numbers.

pub fn mul(self: Self, other: Self) Self

Parameters

self: Self
other: Self

std.math.complex.Complex.mulbyi

function

Source line 98

Returns the product of complex number and i=sqrt(-1)

pub fn mulbyi(self: Self) Self

Parameters

self: Self

std.math.complex.Complex.neg

function

Source line 90

Returns the negation of a complex number.

pub fn neg(self: Self) Self

Parameters

self: Self

std.math.complex.Complex.reciprocal

function

Source line 106

Returns the reciprocal of a complex number.

pub fn reciprocal(self: Self) Self

Parameters

self: Self

std.math.complex.Complex.Self

alias

Source line 29

Alias of std.math.complex.Complex

std.math.complex.Complex.squaredMagnitude

function

Source line 119

pub fn squaredMagnitude(self: Self) T

Parameters

self: Self

std.math.complex.Complex.sub

function

Source line 54

Returns the subtraction of two complex numbers.

pub fn sub(self: Self, other: Self) Self

Parameters

self: Self
other: Self

std.math.complex.conj

alias

Source line 13

Alias of std.math.complex.conj.conj

std.math.complex.cos

alias

Source line 15

Alias of std.math.complex.cos.cos

std.math.complex.cosh

alias

Source line 14

Alias of std.math.complex.cosh.cosh

std.math.complex.epsilon

constant

Source line 125

std.math.complex.exp

alias

Source line 16

Alias of std.math.complex.exp.exp

std.math.complex.log

alias

Source line 17

Alias of std.math.complex.log.log

std.math.complex.math

alias

Source line 3

Alias of std.math

std.math.complex.pow

alias

Source line 18

Alias of std.math.complex.pow.pow

std.math.complex.proj

alias

Source line 19

Alias of std.math.complex.proj.proj

std.math.complex.sin

alias

Source line 21

Alias of std.math.complex.sin.sin

std.math.complex.sinh

alias

Source line 20

Alias of std.math.complex.sinh.sinh

std.math.complex.sqrt

alias

Source line 22

Alias of std.math.complex.sqrt.sqrt

std.math.complex.std

alias

Source line 1

Alias of std

std.math.complex.tan

alias

Source line 24

Alias of std.math.complex.tan.tan

std.math.complex.tanh

alias

Source line 23

Alias of std.math.complex.tanh.tanh

std.math.complex.testing

alias

Source line 2

Alias of std.testing

Source code

std/math/complex.zig

const std = @import("../std.zig");
const testing = std.testing;
const math = std.math;

pub const abs = @import("complex/abs.zig").abs;
pub const acosh = @import("complex/acosh.zig").acosh;
pub const acos = @import("complex/acos.zig").acos;
pub const arg = @import("complex/arg.zig").arg;
pub const asinh = @import("complex/asinh.zig").asinh;
pub const asin = @import("complex/asin.zig").asin;
pub const atanh = @import("complex/atanh.zig").atanh;
pub const atan = @import("complex/atan.zig").atan;
pub const conj = @import("complex/conj.zig").conj;
pub const cosh = @import("complex/cosh.zig").cosh;
pub const cos = @import("complex/cos.zig").cos;
pub const exp = @import("complex/exp.zig").exp;
pub const log = @import("complex/log.zig").log;
pub const pow = @import("complex/pow.zig").pow;
pub const proj = @import("complex/proj.zig").proj;
pub const sinh = @import("complex/sinh.zig").sinh;
pub const sin = @import("complex/sin.zig").sin;
pub const sqrt = @import("complex/sqrt.zig").sqrt;
pub const tanh = @import("complex/tanh.zig").tanh;
pub const tan = @import("complex/tan.zig").tan;

/// A complex number consisting of a real an imaginary part. T must be a floating-point value.
pub fn Complex(comptime T: type) type {
    return struct {
        const Self = @This();

        /// Real part.
        re: T,

        /// Imaginary part.
        im: T,

        /// Create a new Complex number from the given real and imaginary parts.
        pub fn init(re: T, im: T) Self {
            return Self{
                .re = re,
                .im = im,
            };
        }

        /// Returns the sum of two complex numbers.
        pub fn add(self: Self, other: Self) Self {
            return Self{
                .re = self.re + other.re,
                .im = self.im + other.im,
            };
        }

        /// Returns the subtraction of two complex numbers.
        pub fn sub(self: Self, other: Self) Self {
            return Self{
                .re = self.re - other.re,
                .im = self.im - other.im,
            };
        }

        /// Returns the product of two complex numbers.
        pub fn mul(self: Self, other: Self) Self {
            return Self{
                .re = self.re * other.re - self.im * other.im,
                .im = self.im * other.re + self.re * other.im,
            };
        }

        /// Returns the quotient of two complex numbers.
        pub fn div(self: Self, other: Self) Self {
            const re_num = self.re * other.re + self.im * other.im;
            const im_num = self.im * other.re - self.re * other.im;
            const den = other.re * other.re + other.im * other.im;

            return Self{
                .re = re_num / den,
                .im = im_num / den,
            };
        }

        /// Returns the complex conjugate of a number.
        pub fn conjugate(self: Self) Self {
            return Self{
                .re = self.re,
                .im = -self.im,
            };
        }

        /// Returns the negation of a complex number.
        pub fn neg(self: Self) Self {
            return Self{
                .re = -self.re,
                .im = -self.im,
            };
        }

        /// Returns the product of complex number and i=sqrt(-1)
        pub fn mulbyi(self: Self) Self {
            return Self{
                .re = -self.im,
                .im = self.re,
            };
        }

        /// Returns the reciprocal of a complex number.
        pub fn reciprocal(self: Self) Self {
            const m = self.re * self.re + self.im * self.im;
            return Self{
                .re = self.re / m,
                .im = -self.im / m,
            };
        }

        /// Returns the magnitude of a complex number.
        pub fn magnitude(self: Self) T {
            return @sqrt(self.re * self.re + self.im * self.im);
        }

        pub fn squaredMagnitude(self: Self) T {
            return self.re * self.re + self.im * self.im;
        }
    };
}

const epsilon = 0.0001;

test "add" {
    const a = Complex(f32).init(5, 3);
    const b = Complex(f32).init(2, 7);
    const c = a.add(b);

    try testing.expect(c.re == 7 and c.im == 10);
}

test "sub" {
    const a = Complex(f32).init(5, 3);
    const b = Complex(f32).init(2, 7);
    const c = a.sub(b);

    try testing.expect(c.re == 3 and c.im == -4);
}

test "mul" {
    const a = Complex(f32).init(5, 3);
    const b = Complex(f32).init(2, 7);
    const c = a.mul(b);

    try testing.expect(c.re == -11 and c.im == 41);
}

test "div" {
    const a = Complex(f32).init(5, 3);
    const b = Complex(f32).init(2, 7);
    const c = a.div(b);

    try testing.expect(math.approxEqAbs(f32, c.re, @as(f32, 31) / 53, epsilon) and
        math.approxEqAbs(f32, c.im, @as(f32, -29) / 53, epsilon));
}

test "conjugate" {
    const a = Complex(f32).init(5, 3);
    const c = a.conjugate();

    try testing.expect(c.re == 5 and c.im == -3);
}

test "neg" {
    const a = Complex(f32).init(5, 3);
    const c = a.neg();

    try testing.expect(c.re == -5 and c.im == -3);
}

test "mulbyi" {
    const a = Complex(f32).init(5, 3);
    const c = a.mulbyi();

    try testing.expect(c.re == -3 and c.im == 5);
}

test "reciprocal" {
    const a = Complex(f32).init(5, 3);
    const c = a.reciprocal();

    try testing.expect(math.approxEqAbs(f32, c.re, @as(f32, 5) / 34, epsilon) and
        math.approxEqAbs(f32, c.im, @as(f32, -3) / 34, epsilon));
}

test "magnitude" {
    const a = Complex(f32).init(5, 3);
    const c = a.magnitude();

    try testing.expect(math.approxEqAbs(f32, c, 5.83095, epsilon));
}

test "squaredMagnitude" {
    const a = Complex(f32).init(5, 3);
    const c = a.squaredMagnitude();

    try testing.expect(math.approxEqAbs(f32, c, math.pow(f32, a.magnitude(), 2), epsilon));
}

test {
    _ = @import("complex/abs.zig");
    _ = @import("complex/acosh.zig");
    _ = @import("complex/acos.zig");
    _ = @import("complex/arg.zig");
    _ = @import("complex/asinh.zig");
    _ = @import("complex/asin.zig");
    _ = @import("complex/atanh.zig");
    _ = @import("complex/atan.zig");
    _ = @import("complex/conj.zig");
    _ = @import("complex/cosh.zig");
    _ = @import("complex/cos.zig");
    _ = @import("complex/exp.zig");
    _ = @import("complex/log.zig");
    _ = @import("complex/pow.zig");
    _ = @import("complex/proj.zig");
    _ = @import("complex/sinh.zig");
    _ = @import("complex/sin.zig");
    _ = @import("complex/sqrt.zig");
    _ = @import("complex/tanh.zig");
    _ = @import("complex/tan.zig");
}