zig/lib/std / math/complex.zig

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

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

abs

complex/abs.zig

Real part.


pub const abs = @import("complex/abs.zig").abs;

acosh

complex/acosh.zig

Imaginary part.

pub const acosh = @import("complex/acosh.zig").acosh;

acos

complex/acos.zig

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

pub const acos = @import("complex/acos.zig").acos;

arg

complex/arg.zig

Returns the sum of two complex numbers.

pub const arg = @import("complex/arg.zig").arg;

asinh

complex/asinh.zig

Returns the subtraction of two complex numbers.

pub const asinh = @import("complex/asinh.zig").asinh;

asin

complex/asin.zig

Returns the product of two complex numbers.

pub const asin = @import("complex/asin.zig").asin;

atanh

complex/atanh.zig

Returns the quotient of two complex numbers.

pub const atanh = @import("complex/atanh.zig").atanh;

atan

complex/atan.zig

Returns the complex conjugate of a number.

pub const atan = @import("complex/atan.zig").atan;

conj

complex/conj.zig

Returns the negation of a complex number.

pub const conj = @import("complex/conj.zig").conj;

cosh

complex/cosh.zig

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

pub const cosh = @import("complex/cosh.zig").cosh;

cos

complex/cos.zig

Returns the reciprocal of a complex number.

pub const cos = @import("complex/cos.zig").cos;

exp

complex/exp.zig

Returns the magnitude of a complex number.

pub const exp = @import("complex/exp.zig").exp;

log

complex/log.zig
pub const log = @import("complex/log.zig").log;

pow

complex/pow.zig
pub const pow = @import("complex/pow.zig").pow;

proj

complex/proj.zig
pub const proj = @import("complex/proj.zig").proj;

sinh

complex/sinh.zig
pub const sinh = @import("complex/sinh.zig").sinh;

sin

complex/sin.zig
pub const sin = @import("complex/sin.zig").sin;

sqrt

complex/sqrt.zig
pub const sqrt = @import("complex/sqrt.zig").sqrt;

tanh

complex/tanh.zig
pub const tanh = @import("complex/tanh.zig").tanh;

tan

complex/tan.zig
pub const tan = @import("complex/tan.zig").tan;

Complex()


/// 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();

init()


        /// Real part.
        re: T,

add()


        /// Imaginary part.
        im: T,

sub()


        /// 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,
            };
        }

mul()


        /// 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,
            };
        }

div()


        /// 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,
            };
        }

conjugate()


        /// 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,
            };
        }

neg()


        /// 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;

mulbyi()


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

reciprocal()


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

magnitude()


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

Test:

add


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

Test:

sub


        /// 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,
            };
        }

Test:

mul


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

Test:

div


const epsilon = 0.0001;

Test:

conjugate


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

Test:

neg


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

Test:

mulbyi


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

Test:

reciprocal


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

Test:

magnitude


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 {
    _ = @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");
}