2019-03-02 13:46:04 -08:00
|
|
|
const std = @import("../std.zig");
|
2019-02-08 15:18:47 -08:00
|
|
|
const testing = std.testing;
|
2018-04-24 00:18:31 -07:00
|
|
|
const math = std.math;
|
|
|
|
|
2019-03-02 13:46:04 -08:00
|
|
|
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;
|
2018-04-24 00:18:31 -07:00
|
|
|
|
2019-04-30 23:15:57 -07:00
|
|
|
/// A complex number consisting of a real an imaginary part. T must be a floating-point value.
|
2018-04-24 00:18:31 -07:00
|
|
|
pub fn Complex(comptime T: type) type {
|
2018-11-13 05:08:37 -08:00
|
|
|
return struct {
|
2018-09-13 13:34:33 -07:00
|
|
|
const Self = @This();
|
2018-04-24 00:18:31 -07:00
|
|
|
|
2019-04-30 23:15:57 -07:00
|
|
|
/// Real part.
|
2018-04-24 00:18:31 -07:00
|
|
|
re: T,
|
2019-04-30 23:15:57 -07:00
|
|
|
|
|
|
|
/// Imaginary part.
|
2018-04-24 00:18:31 -07:00
|
|
|
im: T,
|
|
|
|
|
2019-04-30 23:15:57 -07:00
|
|
|
/// Create a new Complex number from the given real and imaginary parts.
|
2018-04-24 00:18:31 -07:00
|
|
|
pub fn new(re: T, im: T) Self {
|
2018-11-13 05:08:37 -08:00
|
|
|
return Self{
|
2018-04-24 00:18:31 -07:00
|
|
|
.re = re,
|
|
|
|
.im = im,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-04-30 23:15:57 -07:00
|
|
|
/// Returns the sum of two complex numbers.
|
2018-06-16 18:32:53 -07:00
|
|
|
pub fn add(self: Self, other: Self) Self {
|
2018-11-13 05:08:37 -08:00
|
|
|
return Self{
|
2018-04-24 00:18:31 -07:00
|
|
|
.re = self.re + other.re,
|
|
|
|
.im = self.im + other.im,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-04-30 23:15:57 -07:00
|
|
|
/// Returns the subtraction of two complex numbers.
|
2018-06-16 18:32:53 -07:00
|
|
|
pub fn sub(self: Self, other: Self) Self {
|
2018-11-13 05:08:37 -08:00
|
|
|
return Self{
|
2018-04-24 00:18:31 -07:00
|
|
|
.re = self.re - other.re,
|
|
|
|
.im = self.im - other.im,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-04-30 23:15:57 -07:00
|
|
|
/// Returns the product of two complex numbers.
|
2018-06-16 18:32:53 -07:00
|
|
|
pub fn mul(self: Self, other: Self) Self {
|
2018-11-13 05:08:37 -08:00
|
|
|
return Self{
|
2018-04-24 00:18:31 -07:00
|
|
|
.re = self.re * other.re - self.im * other.im,
|
|
|
|
.im = self.im * other.re + self.re * other.im,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-04-30 23:15:57 -07:00
|
|
|
/// Returns the quotient of two complex numbers.
|
2018-06-16 18:32:53 -07:00
|
|
|
pub fn div(self: Self, other: Self) Self {
|
2018-04-24 00:18:31 -07:00
|
|
|
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;
|
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
return Self{
|
2018-04-24 00:18:31 -07:00
|
|
|
.re = re_num / den,
|
|
|
|
.im = im_num / den,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-04-30 23:15:57 -07:00
|
|
|
/// Returns the complex conjugate of a number.
|
2018-06-16 18:32:53 -07:00
|
|
|
pub fn conjugate(self: Self) Self {
|
2018-11-13 05:08:37 -08:00
|
|
|
return Self{
|
2018-04-24 00:18:31 -07:00
|
|
|
.re = self.re,
|
|
|
|
.im = -self.im,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-04-30 23:15:57 -07:00
|
|
|
/// Returns the reciprocal of a complex number.
|
2018-06-16 18:32:53 -07:00
|
|
|
pub fn reciprocal(self: Self) Self {
|
2018-04-24 00:18:31 -07:00
|
|
|
const m = self.re * self.re + self.im * self.im;
|
2018-11-13 05:08:37 -08:00
|
|
|
return Self{
|
2018-04-24 00:18:31 -07:00
|
|
|
.re = self.re / m,
|
|
|
|
.im = -self.im / m,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-04-30 23:15:57 -07:00
|
|
|
/// Returns the magnitude of a complex number.
|
2018-06-16 18:32:53 -07:00
|
|
|
pub fn magnitude(self: Self) T {
|
2018-04-24 00:18:31 -07:00
|
|
|
return math.sqrt(self.re * self.re + self.im * self.im);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const epsilon = 0.0001;
|
|
|
|
|
|
|
|
test "complex.add" {
|
|
|
|
const a = Complex(f32).new(5, 3);
|
|
|
|
const b = Complex(f32).new(2, 7);
|
|
|
|
const c = a.add(b);
|
|
|
|
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(c.re == 7 and c.im == 10);
|
2018-04-24 00:18:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
test "complex.sub" {
|
|
|
|
const a = Complex(f32).new(5, 3);
|
|
|
|
const b = Complex(f32).new(2, 7);
|
|
|
|
const c = a.sub(b);
|
|
|
|
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(c.re == 3 and c.im == -4);
|
2018-04-24 00:18:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
test "complex.mul" {
|
|
|
|
const a = Complex(f32).new(5, 3);
|
|
|
|
const b = Complex(f32).new(2, 7);
|
|
|
|
const c = a.mul(b);
|
|
|
|
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(c.re == -11 and c.im == 41);
|
2018-04-24 00:18:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
test "complex.div" {
|
|
|
|
const a = Complex(f32).new(5, 3);
|
|
|
|
const b = Complex(f32).new(2, 7);
|
|
|
|
const c = a.div(b);
|
|
|
|
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(math.approxEq(f32, c.re, f32(31) / 53, epsilon) and
|
2018-05-28 17:23:55 -07:00
|
|
|
math.approxEq(f32, c.im, f32(-29) / 53, epsilon));
|
2018-04-24 00:18:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
test "complex.conjugate" {
|
|
|
|
const a = Complex(f32).new(5, 3);
|
|
|
|
const c = a.conjugate();
|
|
|
|
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(c.re == 5 and c.im == -3);
|
2018-04-24 00:18:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
test "complex.reciprocal" {
|
|
|
|
const a = Complex(f32).new(5, 3);
|
|
|
|
const c = a.reciprocal();
|
|
|
|
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(math.approxEq(f32, c.re, f32(5) / 34, epsilon) and
|
2018-05-28 17:23:55 -07:00
|
|
|
math.approxEq(f32, c.im, f32(-3) / 34, epsilon));
|
2018-04-24 00:18:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
test "complex.magnitude" {
|
|
|
|
const a = Complex(f32).new(5, 3);
|
|
|
|
const c = a.magnitude();
|
|
|
|
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(math.approxEq(f32, c, 5.83095, epsilon));
|
2018-04-24 00:18:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
test "complex.cmath" {
|
2019-03-02 13:46:04 -08:00
|
|
|
_ = @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");
|
2018-04-24 00:18:31 -07:00
|
|
|
}
|