| 1 | const std = @import("../../std.zig"); |
| 2 | const testing = std.testing; |
| 3 | const math = std.math; |
| 4 | const cmath = math.complex; |
| 5 | const Complex = cmath.Complex; |
| 6 | |
| 7 | /// Returns z raised to the complex power of c. |
| 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)); |
| 10 | } |
| 11 | |
| 12 | test pow { |
| 13 | const epsilon = math.floatEps(f32); |
| 14 | const a = Complex(f32).init(5, 3); |
| 15 | const b = Complex(f32).init(2.3, -1.3); |
| 16 | const c = pow(a, b); |
| 17 | |
| 18 | try testing.expectApproxEqAbs(58.049110, c.re, epsilon); |
| 19 | try testing.expectApproxEqAbs(-101.003433, c.im, epsilon); |
| 20 | } |