| 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 arc-sine of z. |
| 8 | pub fn asin(z: anytype) Complex(@TypeOf(z.re, z.im)) { |
| 9 | const T = @TypeOf(z.re, z.im); |
| 10 | const x = z.re; |
| 11 | const y = z.im; |
| 12 | |
| 13 | const p = Complex(T).init(1.0 - (x - y) * (x + y), -2.0 * x * y); |
| 14 | const q = Complex(T).init(-y, x); |
| 15 | const r = cmath.log(q.add(cmath.sqrt(p))); |
| 16 | |
| 17 | return Complex(T).init(r.im, -r.re); |
| 18 | } |
| 19 | |
| 20 | test asin { |
| 21 | const epsilon = math.floatEps(f32); |
| 22 | const a = Complex(f32).init(5, 3); |
| 23 | const c = asin(a); |
| 24 | |
| 25 | try testing.expectApproxEqAbs(1.0238227, c.re, epsilon); |
| 26 | try testing.expectApproxEqAbs(2.4529128, c.im, epsilon); |
| 27 | } |