From 81591e50ab2834b94ea55b22e1c6320b5cfec00d Mon Sep 17 00:00:00 2001 From: expikr <77922942+expikr@users.noreply.github.com> Date: Wed, 6 Mar 2024 17:47:41 +0800 Subject: [PATCH 1/5] Update complex.zig --- lib/std/math/complex.zig | 51 +++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/lib/std/math/complex.zig b/lib/std/math/complex.zig index 8398d0a69a64..1506e4083b6d 100644 --- a/lib/std/math/complex.zig +++ b/lib/std/math/complex.zig @@ -43,34 +43,53 @@ pub fn Complex(comptime T: type) type { } /// 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, + pub fn add(self: Self, other: anytype) Self { + return switch (@TypeOf(other)) { + Self => .{ + .re = self.re + other.re, + .im = self.im + other.im, + }, + else => .{ + .re = self.re + other, + .im = self.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, + pub fn sub(self: Self, other: anytype) Self { + return switch (@TypeOf(other)) { + Self => .{ + .re = self.re - other.re, + .im = self.im - other.im, + }, + else => .{ + .re = self.re - other, + .im = self.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, + pub fn mul(self: Self, other: anytype) Self { + return switch (@TypeOf(other)) { + Self => .{ + .re = self.re * other.re - self.im * other.im, + .im = self.im * other.re + self.re * other.im, + }, + else => .{ + .re = self.re * other, + .im = self.im * other, + }, }; } /// 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; + pub fn div(self: Self, other: anytype) Self { + const same = Self == @TypeOf(other); + const re_num = if (same) self.re * other.re + self.im * other.im else self.re; + const im_num = if (same) self.im * other.re - self.re * other.im else self.im; + const den = if (same) other.re * other.re + other.im * other.im else other; return Self{ .re = re_num / den, From 703e9fec870560745f17f5ebcb9716cba15b9715 Mon Sep 17 00:00:00 2001 From: expikr <77922942+expikr@users.noreply.github.com> Date: Thu, 7 Mar 2024 03:23:58 +0800 Subject: [PATCH 2/5] Update complex.zig --- lib/std/math/complex.zig | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/std/math/complex.zig b/lib/std/math/complex.zig index 1506e4083b6d..cc9ff39dfdb0 100644 --- a/lib/std/math/complex.zig +++ b/lib/std/math/complex.zig @@ -143,33 +143,38 @@ test "add" { const a = Complex(f32).init(5, 3); const b = Complex(f32).init(2, 7); const c = a.add(b); - + const d = c.add(1); try testing.expect(c.re == 7 and c.im == 10); + try testing.expect(d.re == 8 and d.im == 10); } test "sub" { const a = Complex(f32).init(5, 3); const b = Complex(f32).init(2, 7); const c = a.sub(b); - + const d = c.sub(1); try testing.expect(c.re == 3 and c.im == -4); + try testing.expect(d.re == 2 and d.im == -4); } test "mul" { const a = Complex(f32).init(5, 3); const b = Complex(f32).init(2, 7); const c = a.mul(b); - + const d = c.mul(2); try testing.expect(c.re == -11 and c.im == 41); + try testing.expect(d.re == -22 and d.im == 82); } test "div" { const a = Complex(f32).init(5, 3); const b = Complex(f32).init(2, 7); const c = a.div(b); - + const d = c.div(2); try testing.expect(math.approxEqAbs(f32, c.re, @as(f32, 31) / 53, epsilon) and math.approxEqAbs(f32, c.im, @as(f32, -29) / 53, epsilon)); + try testing.expect(math.approxEqAbs(f32, d.re, @as(f32, 31) / 106, epsilon) and + math.approxEqAbs(f32, d.im, @as(f32, -29) / 106, epsilon)); } test "conjugate" { From 914e5202f65a72224277556089c5db2349907273 Mon Sep 17 00:00:00 2001 From: expikr <77922942+expikr@users.noreply.github.com> Date: Wed, 5 Jun 2024 03:17:08 -0700 Subject: [PATCH 3/5] Update complex.zig --- lib/std/math/complex.zig | 60 +++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 35 deletions(-) diff --git a/lib/std/math/complex.zig b/lib/std/math/complex.zig index cc9ff39dfdb0..845ac350261c 100644 --- a/lib/std/math/complex.zig +++ b/lib/std/math/complex.zig @@ -44,56 +44,46 @@ pub fn Complex(comptime T: type) type { /// Returns the sum of two complex numbers. pub fn add(self: Self, other: anytype) Self { - return switch (@TypeOf(other)) { - Self => .{ - .re = self.re + other.re, - .im = self.im + other.im, - }, - else => .{ - .re = self.re + other, - .im = self.im, - }, + return if (Self == @TypeOf(other)) .{ + .re = self.re + other.re, + .im = self.im + other.im, + } else .{ + .re = self.re + other, + .im = self.im, }; } /// Returns the subtraction of two complex numbers. pub fn sub(self: Self, other: anytype) Self { - return switch (@TypeOf(other)) { - Self => .{ - .re = self.re - other.re, - .im = self.im - other.im, - }, - else => .{ - .re = self.re - other, - .im = self.im, - }, + return if (Self == @TypeOf(other)) .{ + .re = self.re - other.re, + .im = self.im - other.im, + } else .{ + .re = self.re - other, + .im = self.im, }; } /// Returns the product of two complex numbers. pub fn mul(self: Self, other: anytype) Self { - return switch (@TypeOf(other)) { - Self => .{ - .re = self.re * other.re - self.im * other.im, - .im = self.im * other.re + self.re * other.im, - }, - else => .{ - .re = self.re * other, - .im = self.im * other, - }, + return if (Self == @TypeOf(other)) .{ + .re = self.re * other.re - self.im * other.im, + .im = self.im * other.re + self.re * other.im, + } else .{ + .re = self.re * other, + .im = self.im * other, }; } /// Returns the quotient of two complex numbers. pub fn div(self: Self, other: anytype) Self { - const same = Self == @TypeOf(other); - const re_num = if (same) self.re * other.re + self.im * other.im else self.re; - const im_num = if (same) self.im * other.re - self.re * other.im else self.im; - const den = if (same) other.re * other.re + other.im * other.im else other; - - return Self{ - .re = re_num / den, - .im = im_num / den, + const abs2 = if (Self == @TypeOf(other)) other.re * other.re + other.im * other.im else other; + return if (Self == @TypeOf(other)) .{ + .re = (self.re * other.re + self.im * other.im) / abs2, + .im = (self.im * other.re - self.re * other.im) / abs2, + } else .{ + .re = self.re / other, + .im = self.im / other, }; } From 333db43e2cedfdf2b1bb7a984cc52abf619fb02d Mon Sep 17 00:00:00 2001 From: expikr <77922942+expikr@users.noreply.github.com> Date: Fri, 7 Jun 2024 23:57:51 -0700 Subject: [PATCH 4/5] Update complex.zig --- lib/std/math/complex.zig | 98 ++++++++++++++++++++++++++++++++++------ 1 file changed, 83 insertions(+), 15 deletions(-) diff --git a/lib/std/math/complex.zig b/lib/std/math/complex.zig index 845ac350261c..f2c9bde0383c 100644 --- a/lib/std/math/complex.zig +++ b/lib/std/math/complex.zig @@ -87,6 +87,51 @@ pub fn Complex(comptime T: type) type { }; } + /// Add self by a number rotated by the imaginary unit. + pub fn iadd(self: Self, other: anytype) Self { + return if (Self == @TypeOf(other)) .{ + .re = self.re - other.im, + .im = self.im + other.re, + } else .{ + .re = self.re, + .im = self.im + other, + }; + } + + /// Subtract self by a number rotated by the imaginary unit. + pub fn isub(self: Self, other: anytype) Self { + return if (Self == @TypeOf(other)) .{ + .re = self.re + other.im, + .im = self.im - other.re, + } else .{ + .re = self.re, + .im = self.im - other, + }; + } + + /// Multiply self by a number rotated by the imaginary unit. + pub fn imul(self: Self, other: anytype) Self { + return if (Self == @TypeOf(other)) .{ + .re = -(self.im * other.re + self.re * other.im), + .im = self.re * other.re - self.im * other.im, + } else .{ + .re = -self.im * other, + .im = self.re * other, + }; + } + + /// Divide self by a number rotated by the imaginary unit. + pub fn idiv(self: Self, other: anytype) Self { + const abs2 = if (Self == @TypeOf(other)) other.re * other.re + other.im * other.im else other; + return if (Self == @TypeOf(other)) .{ + .re = (self.im * other.re - self.re * other.im) / abs2, + .im = -(self.re * other.re + self.im * other.im) / abs2, + } else .{ + .re = self.im / other, + .im = -self.re / other, + }; + } + /// Returns the complex conjugate of a number. pub fn conjugate(self: Self) Self { return Self{ @@ -103,14 +148,6 @@ pub fn Complex(comptime T: type) type { }; } - /// 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; @@ -167,6 +204,44 @@ test "div" { math.approxEqAbs(f32, d.im, @as(f32, -29) / 106, epsilon)); } +test iadd { + const a = Complex(f32).init(5, 3); + const b = Complex(f32).init(2, 7); + const c = a.iadd(b); + const d = c.iadd(1); + try testing.expect(c.re == -2 and c.im == 5); + try testing.expect(d.re == -2 and d.im == 6); +} + +test isub { + const a = Complex(f32).init(5, 3); + const b = Complex(f32).init(2, 7); + const c = a.isub(b); + const d = c.isub(1); + try testing.expect(c.re == 12 and c.im == 1); + try testing.expect(d.re == 12 and d.im == 0); +} + +test imul { + const a = Complex(f32).init(5, 3); + const b = Complex(f32).init(2, 7); + const c = a.imul(b); + const d = c.imul(2); + try testing.expect(c.re == -41 and c.im == -11); + try testing.expect(d.re == 22 and d.im == -82); +} + +test idiv { + const a = Complex(f32).init(5, 3); + const b = Complex(f32).init(2, 7); + const c = a.idiv(b); + const d = c.idiv(2); + try testing.expect(math.approxEqAbs(f32, c.re, @as(f32, -29) / 53, epsilon) and + math.approxEqAbs(f32, c.im, @as(f32, -31) / 53, epsilon)); + try testing.expect(math.approxEqAbs(f32, d.re, @as(f32, -31) / 106, epsilon) and + math.approxEqAbs(f32, d.im, @as(f32, 29) / 106, epsilon)); +} + test "conjugate" { const a = Complex(f32).init(5, 3); const c = a.conjugate(); @@ -181,13 +256,6 @@ test "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(); From 433e48315b701290fa5c9a381a1f9c4b6ab81af4 Mon Sep 17 00:00:00 2001 From: expikr <77922942+expikr@users.noreply.github.com> Date: Sat, 8 Jun 2024 00:04:07 -0700 Subject: [PATCH 5/5] Update complex.zig --- lib/std/math/complex.zig | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/std/math/complex.zig b/lib/std/math/complex.zig index f2c9bde0383c..c3df5d008ea1 100644 --- a/lib/std/math/complex.zig +++ b/lib/std/math/complex.zig @@ -204,7 +204,7 @@ test "div" { math.approxEqAbs(f32, d.im, @as(f32, -29) / 106, epsilon)); } -test iadd { +test "iadd" { const a = Complex(f32).init(5, 3); const b = Complex(f32).init(2, 7); const c = a.iadd(b); @@ -213,7 +213,7 @@ test iadd { try testing.expect(d.re == -2 and d.im == 6); } -test isub { +test "isub" { const a = Complex(f32).init(5, 3); const b = Complex(f32).init(2, 7); const c = a.isub(b); @@ -222,7 +222,7 @@ test isub { try testing.expect(d.re == 12 and d.im == 0); } -test imul { +test "imul" { const a = Complex(f32).init(5, 3); const b = Complex(f32).init(2, 7); const c = a.imul(b); @@ -231,7 +231,7 @@ test imul { try testing.expect(d.re == 22 and d.im == -82); } -test idiv { +test "idiv" { const a = Complex(f32).init(5, 3); const b = Complex(f32).init(2, 7); const c = a.idiv(b);