| 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 the natural logarithm of z. |
| 8 | pub fn log(z: anytype) Complex(@TypeOf(z.re, z.im)) { |
| 9 | const T = @TypeOf(z.re, z.im); |
| 10 | const r = cmath.abs(z); |
| 11 | const phi = cmath.arg(z); |
| 12 | |
| 13 | return Complex(T).init(@log(r), phi); |
| 14 | } |
| 15 | |
| 16 | test log { |
| 17 | const epsilon = math.floatEps(f32); |
| 18 | const a = Complex(f32).init(5, 3); |
| 19 | const c = log(a); |
| 20 | |
| 21 | try testing.expectApproxEqAbs(1.7631803, c.re, epsilon); |
| 22 | try testing.expectApproxEqAbs(0.5404195, c.im, epsilon); |
| 23 | } |