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