| ... | ... | @@ -5,10 +5,8 @@ const cmath = math.complex; |
| 5 | 5 | const Complex = cmath.Complex; |
| 6 | 6 | |
| 7 | 7 | /// Returns z raised to the complex power of c. |
| 8 | | pub fn pow(comptime T: type, z: T, c: T) T { |
| 9 | | const p = cmath.log(z); |
| 10 | | const q = c.mul(p); |
| 11 | | return cmath.exp(q); |
| 8 | pub fn pow(z: anytype, s: anytype) Complex(@TypeOf(z.re, z.im, s.re, s.im)) { |
| 9 | return cmath.exp(cmath.log(z).mul(s)); |
| 12 | 10 | } |
| 13 | 11 | |
| 14 | 12 | const epsilon = 0.0001; |
| ... | ... | @@ -16,7 +14,7 @@ const epsilon = 0.0001; |
| 16 | 14 | test "complex.cpow" { |
| 17 | 15 | const a = Complex(f32).init(5, 3); |
| 18 | 16 | const b = Complex(f32).init(2.3, -1.3); |
| 19 | | const c = pow(Complex(f32), a, b); |
| 17 | const c = pow(a, b); |
| 20 | 18 | |
| 21 | 19 | try testing.expect(math.approxEqAbs(f32, c.re, 58.049110, epsilon)); |
| 22 | 20 | try testing.expect(math.approxEqAbs(f32, c.im, -101.003433, epsilon)); |