| ... | @@ -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 | } |
| 120 | | 124 | |
| ... | @@ -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 | } |
| 191 | | 195 | |
| | 196 | test "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 | |
| 192 | test { | 203 | test { |
| 193 | _ = @import("complex/abs.zig"); | 204 | _ = @import("complex/abs.zig"); |
| 194 | _ = @import("complex/acosh.zig"); | 205 | _ = @import("complex/acosh.zig"); |