2019-03-02 21:46:04 +00:00
|
|
|
const std = @import("../../std.zig");
|
2019-02-08 23:18:47 +00:00
|
|
|
const testing = std.testing;
|
2018-04-24 08:18:31 +01:00
|
|
|
const math = std.math;
|
|
|
|
const cmath = math.complex;
|
|
|
|
const Complex = cmath.Complex;
|
|
|
|
|
2019-05-01 07:15:57 +01:00
|
|
|
// Returns the arc-sine of z.
|
2023-11-07 20:10:43 +00:00
|
|
|
pub fn asin(z: anytype) Complex(@TypeOf(z.re, z.im)) {
|
|
|
|
const T = @TypeOf(z.re, z.im);
|
2018-04-24 08:18:31 +01:00
|
|
|
const x = z.re;
|
|
|
|
const y = z.im;
|
|
|
|
|
2021-05-17 20:57:51 +01:00
|
|
|
const p = Complex(T).init(1.0 - (x - y) * (x + y), -2.0 * x * y);
|
|
|
|
const q = Complex(T).init(-y, x);
|
2018-04-24 08:18:31 +01:00
|
|
|
const r = cmath.log(q.add(cmath.sqrt(p)));
|
|
|
|
|
2021-05-17 20:57:51 +01:00
|
|
|
return Complex(T).init(r.im, -r.re);
|
2018-04-24 08:18:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const epsilon = 0.0001;
|
|
|
|
|
2024-02-26 07:31:53 +00:00
|
|
|
test asin {
|
2021-05-17 20:57:51 +01:00
|
|
|
const a = Complex(f32).init(5, 3);
|
2018-04-24 08:18:31 +01:00
|
|
|
const c = asin(a);
|
|
|
|
|
2021-05-04 18:47:26 +01:00
|
|
|
try testing.expect(math.approxEqAbs(f32, c.re, 1.023822, epsilon));
|
|
|
|
try testing.expect(math.approxEqAbs(f32, c.im, 2.452914, epsilon));
|
2018-04-24 08:18:31 +01:00
|
|
|
}
|