跳转到内容

std.math.complex

标准库Zig 0.17.1官方原文
源码std/math/complex.zig

第 1 行

std.math.complex.abs

别名

第 5 行

别名目标 std.math.complex.abs.abs

std.math.complex.acos

别名

第 7 行

别名目标 std.math.complex.acos.acos

std.math.complex.acosh

别名

第 6 行

别名目标 std.math.complex.acosh.acosh

std.math.complex.arg

别名

第 8 行

别名目标 std.math.complex.arg.arg

std.math.complex.asin

别名

第 10 行

别名目标 std.math.complex.asin.asin

std.math.complex.asinh

别名

第 9 行

别名目标 std.math.complex.asinh.asinh

std.math.complex.atan

别名

第 12 行

别名目标 std.math.complex.atan.atan

std.math.complex.atanh

别名

第 11 行

别名目标 std.math.complex.atanh.atanh

std.math.complex.Complex

类型函数

第 27 行

由实部和虚部组成的复数。T 必须是浮点类型。

pub fn Complex(comptime T: type) type

参数

T: type

字段

re: T

实部。

im: T

虚部。

std.math.complex.Complex.add

函数

第 46 行

返回两个复数的和。

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

参数

self: Self
other: Self

std.math.complex.Complex.conjugate

函数

第 82 行

返回数的复共轭。

pub fn conjugate(self: Self) Self

参数

self: Self

std.math.complex.Complex.div

函数

第 70 行

返回两个复数的商。

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

参数

self: Self
other: Self

std.math.complex.Complex.init

函数

第 38 行

根据给定实部和虚部创建新的 Complex 数。

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

参数

re: T
im: T

std.math.complex.Complex.magnitude

函数

第 115 行

返回复数的模。

pub fn magnitude(self: Self) T

参数

self: Self

std.math.complex.Complex.mul

函数

第 62 行

返回两个复数的乘积。

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

参数

self: Self
other: Self

std.math.complex.Complex.mulbyi

函数

第 98 行

返回复数与 i=sqrt(-1) 的乘积。

pub fn mulbyi(self: Self) Self

参数

self: Self

std.math.complex.Complex.neg

函数

第 90 行

返回复数的相反数。

pub fn neg(self: Self) Self

参数

self: Self

std.math.complex.Complex.reciprocal

函数

第 106 行

返回复数的倒数。

pub fn reciprocal(self: Self) Self

参数

self: Self

std.math.complex.Complex.Self

别名

第 29 行

别名目标 std.math.complex.Complex

std.math.complex.Complex.squaredMagnitude

函数

第 119 行

pub fn squaredMagnitude(self: Self) T

参数

self: Self

std.math.complex.Complex.sub

函数

第 54 行

返回两个复数的差。

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

参数

self: Self
other: Self

std.math.complex.conj

别名

第 13 行

别名目标 std.math.complex.conj.conj

std.math.complex.cos

别名

第 15 行

别名目标 std.math.complex.cos.cos

std.math.complex.cosh

别名

第 14 行

别名目标 std.math.complex.cosh.cosh

std.math.complex.epsilon

常量

第 125 行

std.math.complex.exp

别名

第 16 行

别名目标 std.math.complex.exp.exp

std.math.complex.log

别名

第 17 行

别名目标 std.math.complex.log.log

std.math.complex.math

别名

第 3 行

别名目标 std.math

std.math.complex.pow

别名

第 18 行

别名目标 std.math.complex.pow.pow

std.math.complex.proj

别名

第 19 行

别名目标 std.math.complex.proj.proj

std.math.complex.sin

别名

第 21 行

别名目标 std.math.complex.sin.sin

std.math.complex.sinh

别名

第 20 行

别名目标 std.math.complex.sinh.sinh

std.math.complex.sqrt

别名

第 22 行

别名目标 std.math.complex.sqrt.sqrt

std.math.complex.std

别名

第 1 行

别名目标 std

std.math.complex.tan

别名

第 24 行

别名目标 std.math.complex.tan.tan

std.math.complex.tanh

别名

第 23 行

别名目标 std.math.complex.tanh.tanh

std.math.complex.testing

别名

第 2 行

别名目标 std.testing

源码

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;

/// 由实部和虚部组成的复数。T 必须是浮点类型。
pub fn Complex(comptime T: type) type {
    return struct {
        const Self = @This();

        /// 实部。
        re: T,

        /// 虚部。
        im: T,

        /// 根据给定实部和虚部创建新的 Complex 数。
        pub fn init(re: T, im: T) Self {
            return Self{
                .re = re,
                .im = im,
            };
        }

        /// 返回两个复数的和。
        pub fn add(self: Self, other: Self) Self {
            return Self{
                .re = self.re + other.re,
                .im = self.im + other.im,
            };
        }

        /// 返回两个复数的差。
        pub fn sub(self: Self, other: Self) Self {
            return Self{
                .re = self.re - other.re,
                .im = self.im - other.im,
            };
        }

        /// 返回两个复数的乘积。
        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,
            };
        }

        /// 返回两个复数的商。
        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,
            };
        }

        /// 返回数的复共轭。
        pub fn conjugate(self: Self) Self {
            return Self{
                .re = self.re,
                .im = -self.im,
            };
        }

        /// 返回复数的相反数。
        pub fn neg(self: Self) Self {
            return Self{
                .re = -self.re,
                .im = -self.im,
            };
        }

        /// 返回复数与 i=sqrt(-1) 的乘积。
        pub fn mulbyi(self: Self) Self {
            return Self{
                .re = -self.im,
                .im = self.re,
            };
        }

        /// 返回复数的倒数。
        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,
            };
        }

        /// 返回复数的模。
        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");
}