authorgravatar for 48591413+chrboesch@users.noreply.github.comChris Boesch <48591413+chrboesch@users.noreply.github.com> 2024-11-26 14:03:48+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-11-26 13:03:48+00:00
log87863a834b074311d70ec4dd3b10c74705fff54d
tree850e2a8d256ef3dedea40ae3847870ba77477fe8
parentb0dcce93f7fca9ee4f8e4f2c0a34523b91b50c46
signaturebadge-check Signed by PGP key B5690EEEBB952194

std.math.complex: Add squared magnitude function (#21998)


1 files changed, 11 insertions(+), 0 deletions(-)

lib/std/math/complex.zig+11
...@@ -115,6 +115,10 @@ pub fn Complex(comptime T: type) type {...@@ -115,6 +115,10 @@ pub fn Complex(comptime T: type) type {
115 pub fn magnitude(self: Self) T {115 pub fn magnitude(self: Self) T {
116 return @sqrt(self.re * self.re + self.im * self.im);116 return @sqrt(self.re * self.re + self.im * self.im);
117 }117 }
118
119 pub fn squaredMagnitude(self: Self) T {
120 return self.re * self.re + self.im * self.im;
121 }
118 };122 };
119}123}
120124
...@@ -189,6 +193,13 @@ test "magnitude" {...@@ -189,6 +193,13 @@ test "magnitude" {
189 try testing.expect(math.approxEqAbs(f32, c, 5.83095, epsilon));193 try testing.expect(math.approxEqAbs(f32, c, 5.83095, epsilon));
190}194}
191195
196test "squaredMagnitude" {
197 const a = Complex(f32).init(5, 3);
198 const c = a.squaredMagnitude();
199
200 try testing.expect(math.approxEqAbs(f32, c, math.pow(f32, a.magnitude(), 2), epsilon));
201}
202
192test {203test {
193 _ = @import("complex/abs.zig");204 _ = @import("complex/abs.zig");
194 _ = @import("complex/acosh.zig");205 _ = @import("complex/acosh.zig");