commit 406507c6dcdfff13633f9047549fe1d1f93b6819 (tree)
parent 7ae22813eedbe3461f555b6f4c6e708b8c952b15
Author: BlueAlmost <100024520+BlueAlmost@users.noreply.github.com>
Date: Sun, 27 Mar 2022 10:54:43 +0200
std.math.Complex: add 'negation' and 'mulitply by i'
Diffstat:
1 file changed, 30 insertions(+), 0 deletions(-)
diff --git 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();