diff --git a/lib/std/math/complex.zig b/lib/std/math/complex.zig index 447b5f9fc2..42342faa3e 100644 --- a/lib/std/math/complex.zig +++ b/lib/std/math/complex.zig @@ -88,6 +88,22 @@ pub fn Complex(comptime T: type) type { }; } + /// 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; @@ -146,6 +162,20 @@ test "complex.conjugate" { try testing.expect(c.re == 5 and c.im == -3); } +test "complex.neg" { + const a = Complex(f32).init(5, 3); + const c = a.neg(); + + try testing.expect(c.re == -5 and c.im == -3); +} + +test "complex.mulbyi" { + const a = Complex(f32).init(5, 3); + const c = a.mulbyi(); + + try testing.expect(c.re == -3 and c.im == 5); +} + test "complex.reciprocal" { const a = Complex(f32).init(5, 3); const c = a.reciprocal();