Merge pull request #20878 from tiehuis/std-math-complex-fixes

std.math.complex fixes
This commit is contained in:
Andrew Kelley 2024-07-31 19:19:27 -07:00 committed by GitHub
commit 059856acfc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
19 changed files with 102 additions and 120 deletions

View File

@ -9,10 +9,9 @@ pub fn abs(z: anytype) @TypeOf(z.re, z.im) {
return math.hypot(z.re, z.im);
}
const epsilon = 0.0001;
test abs {
const epsilon = math.floatEps(f32);
const a = Complex(f32).init(5, 3);
const c = abs(a);
try testing.expect(math.approxEqAbs(f32, c, 5.83095, epsilon));
try testing.expectApproxEqAbs(5.8309517, c, epsilon);
}

View File

@ -11,12 +11,11 @@ pub fn acos(z: anytype) Complex(@TypeOf(z.re, z.im)) {
return Complex(T).init(@as(T, math.pi) / 2 - q.re, -q.im);
}
const epsilon = 0.0001;
test acos {
const epsilon = math.floatEps(f32);
const a = Complex(f32).init(5, 3);
const c = acos(a);
try testing.expect(math.approxEqAbs(f32, c.re, 0.546975, epsilon));
try testing.expect(math.approxEqAbs(f32, c.im, -2.452914, epsilon));
try testing.expectApproxEqAbs(0.5469737, c.re, epsilon);
try testing.expectApproxEqAbs(-2.4529128, c.im, epsilon);
}

View File

@ -8,15 +8,18 @@ const Complex = cmath.Complex;
pub fn acosh(z: anytype) Complex(@TypeOf(z.re, z.im)) {
const T = @TypeOf(z.re, z.im);
const q = cmath.acos(z);
return Complex(T).init(-q.im, q.re);
return if (math.signbit(z.im))
Complex(T).init(q.im, -q.re)
else
Complex(T).init(-q.im, q.re);
}
const epsilon = 0.0001;
test acosh {
const epsilon = math.floatEps(f32);
const a = Complex(f32).init(5, 3);
const c = acosh(a);
try testing.expect(math.approxEqAbs(f32, c.re, 2.452914, epsilon));
try testing.expect(math.approxEqAbs(f32, c.im, 0.546975, epsilon));
try testing.expectApproxEqAbs(2.4529128, c.re, epsilon);
try testing.expectApproxEqAbs(0.5469737, c.im, epsilon);
}

View File

@ -9,10 +9,9 @@ pub fn arg(z: anytype) @TypeOf(z.re, z.im) {
return math.atan2(z.im, z.re);
}
const epsilon = 0.0001;
test arg {
const epsilon = math.floatEps(f32);
const a = Complex(f32).init(5, 3);
const c = arg(a);
try testing.expect(math.approxEqAbs(f32, c, 0.540420, epsilon));
try testing.expectApproxEqAbs(0.5404195, c, epsilon);
}

View File

@ -17,12 +17,11 @@ pub fn asin(z: anytype) Complex(@TypeOf(z.re, z.im)) {
return Complex(T).init(r.im, -r.re);
}
const epsilon = 0.0001;
test asin {
const epsilon = math.floatEps(f32);
const a = Complex(f32).init(5, 3);
const c = asin(a);
try testing.expect(math.approxEqAbs(f32, c.re, 1.023822, epsilon));
try testing.expect(math.approxEqAbs(f32, c.im, 2.452914, epsilon));
try testing.expectApproxEqAbs(1.0238227, c.re, epsilon);
try testing.expectApproxEqAbs(2.4529128, c.im, epsilon);
}

View File

@ -12,12 +12,11 @@ pub fn asinh(z: anytype) Complex(@TypeOf(z.re, z.im)) {
return Complex(T).init(r.im, -r.re);
}
const epsilon = 0.0001;
test asinh {
const epsilon = math.floatEps(f32);
const a = Complex(f32).init(5, 3);
const c = asinh(a);
try testing.expect(math.approxEqAbs(f32, c.re, 2.459831, epsilon));
try testing.expect(math.approxEqAbs(f32, c.im, 0.533999, epsilon));
try testing.expectApproxEqAbs(2.4598298, c.re, epsilon);
try testing.expectApproxEqAbs(0.5339993, c.im, epsilon);
}

View File

@ -32,37 +32,22 @@ fn redupif32(x: f32) f32 {
t -= 0.5;
}
const u = @as(f32, @floatFromInt(@as(i32, @intFromFloat(t))));
return ((x - u * DP1) - u * DP2) - t * DP3;
const u: f32 = @trunc(t);
return ((x - u * DP1) - u * DP2) - u * DP3;
}
fn atan32(z: Complex(f32)) Complex(f32) {
const maxnum = 1.0e38;
const x = z.re;
const y = z.im;
if ((x == 0.0) and (y > 1.0)) {
// overflow
return Complex(f32).init(maxnum, maxnum);
}
const x2 = x * x;
var a = 1.0 - x2 - (y * y);
if (a == 0.0) {
// overflow
return Complex(f32).init(maxnum, maxnum);
}
var t = 0.5 * math.atan2(2.0 * x, a);
const w = redupif32(t);
t = y - 1.0;
a = x2 + t * t;
if (a == 0.0) {
// overflow
return Complex(f32).init(maxnum, maxnum);
}
t = y + 1.0;
a = (x2 + (t * t)) / a;
@ -81,57 +66,42 @@ fn redupif64(x: f64) f64 {
t -= 0.5;
}
const u = @as(f64, @floatFromInt(@as(i64, @intFromFloat(t))));
return ((x - u * DP1) - u * DP2) - t * DP3;
const u: f64 = @trunc(t);
return ((x - u * DP1) - u * DP2) - u * DP3;
}
fn atan64(z: Complex(f64)) Complex(f64) {
const maxnum = 1.0e308;
const x = z.re;
const y = z.im;
if ((x == 0.0) and (y > 1.0)) {
// overflow
return Complex(f64).init(maxnum, maxnum);
}
const x2 = x * x;
var a = 1.0 - x2 - (y * y);
if (a == 0.0) {
// overflow
return Complex(f64).init(maxnum, maxnum);
}
var t = 0.5 * math.atan2(2.0 * x, a);
const w = redupif64(t);
t = y - 1.0;
a = x2 + t * t;
if (a == 0.0) {
// overflow
return Complex(f64).init(maxnum, maxnum);
}
t = y + 1.0;
a = (x2 + (t * t)) / a;
return Complex(f64).init(w, 0.25 * @log(a));
}
const epsilon = 0.0001;
test atan32 {
const epsilon = math.floatEps(f32);
const a = Complex(f32).init(5, 3);
const c = atan(a);
try testing.expect(math.approxEqAbs(f32, c.re, 1.423679, epsilon));
try testing.expect(math.approxEqAbs(f32, c.im, 0.086569, epsilon));
try testing.expectApproxEqAbs(1.423679, c.re, epsilon);
try testing.expectApproxEqAbs(0.086569, c.im, epsilon);
}
test atan64 {
const epsilon = math.floatEps(f64);
const a = Complex(f64).init(5, 3);
const c = atan(a);
try testing.expect(math.approxEqAbs(f64, c.re, 1.423679, epsilon));
try testing.expect(math.approxEqAbs(f64, c.im, 0.086569, epsilon));
try testing.expectApproxEqAbs(1.4236790442393028, c.re, epsilon);
try testing.expectApproxEqAbs(0.08656905917945844, c.im, epsilon);
}

View File

@ -12,12 +12,11 @@ pub fn atanh(z: anytype) Complex(@TypeOf(z.re, z.im)) {
return Complex(T).init(r.im, -r.re);
}
const epsilon = 0.0001;
test atanh {
const epsilon = math.floatEps(f32);
const a = Complex(f32).init(5, 3);
const c = atanh(a);
try testing.expect(math.approxEqAbs(f32, c.re, 0.146947, epsilon));
try testing.expect(math.approxEqAbs(f32, c.im, 1.480870, epsilon));
try testing.expectApproxEqAbs(0.14694665, c.re, epsilon);
try testing.expectApproxEqAbs(1.4808695, c.im, epsilon);
}

View File

@ -14,5 +14,6 @@ test conj {
const a = Complex(f32).init(5, 3);
const c = a.conjugate();
try testing.expect(c.re == 5 and c.im == -3);
try testing.expectEqual(5, c.re);
try testing.expectEqual(-3, c.im);
}

View File

@ -11,12 +11,11 @@ pub fn cos(z: anytype) Complex(@TypeOf(z.re, z.im)) {
return cmath.cosh(p);
}
const epsilon = 0.0001;
test cos {
const epsilon = math.floatEps(f32);
const a = Complex(f32).init(5, 3);
const c = cos(a);
try testing.expect(math.approxEqAbs(f32, c.re, 2.855815, epsilon));
try testing.expect(math.approxEqAbs(f32, c.im, 9.606383, epsilon));
try testing.expectApproxEqAbs(2.8558152, c.re, epsilon);
try testing.expectApproxEqAbs(9.606383, c.im, epsilon);
}

View File

@ -34,7 +34,7 @@ fn cosh32(z: Complex(f32)) Complex(f32) {
if (ix < 0x7f800000 and iy < 0x7f800000) {
if (iy == 0) {
return Complex(f32).init(math.cosh(x), y);
return Complex(f32).init(math.cosh(x), x * y);
}
// small x: normal case
if (ix < 0x41100000) {
@ -45,7 +45,7 @@ fn cosh32(z: Complex(f32)) Complex(f32) {
if (ix < 0x42b17218) {
// x < 88.7: exp(|x|) won't overflow
const h = @exp(@abs(x)) * 0.5;
return Complex(f32).init(math.copysign(h, x) * @cos(y), h * @sin(y));
return Complex(f32).init(h * @cos(y), math.copysign(h, x) * @sin(y));
}
// x < 192.7: scale to avoid overflow
else if (ix < 0x4340b1e7) {
@ -68,7 +68,7 @@ fn cosh32(z: Complex(f32)) Complex(f32) {
if (hx & 0x7fffff == 0) {
return Complex(f32).init(x * x, math.copysign(@as(f32, 0.0), x) * y);
}
return Complex(f32).init(x, math.copysign(@as(f32, 0.0), (x + x) * y));
return Complex(f32).init(x * x, math.copysign(@as(f32, 0.0), (x + x) * y));
}
if (ix < 0x7f800000 and iy >= 0x7f800000) {
@ -123,7 +123,7 @@ fn cosh64(z: Complex(f64)) Complex(f64) {
}
// x >= 1455: result always overflows
else {
const h = 0x1p1023;
const h = 0x1p1023 * x;
return Complex(f64).init(h * h * @cos(y), h * @sin(y));
}
}
@ -153,20 +153,29 @@ fn cosh64(z: Complex(f64)) Complex(f64) {
return Complex(f64).init((x * x) * (y - y), (x + x) * (y - y));
}
const epsilon = 0.0001;
test cosh32 {
const epsilon = math.floatEps(f32);
const a = Complex(f32).init(5, 3);
const c = cosh(a);
try testing.expect(math.approxEqAbs(f32, c.re, -73.467300, epsilon));
try testing.expect(math.approxEqAbs(f32, c.im, 10.471557, epsilon));
try testing.expectApproxEqAbs(-73.467300, c.re, epsilon);
try testing.expectApproxEqAbs(10.471557, c.im, epsilon);
}
test cosh64 {
const epsilon = math.floatEps(f64);
const a = Complex(f64).init(5, 3);
const c = cosh(a);
try testing.expect(math.approxEqAbs(f64, c.re, -73.467300, epsilon));
try testing.expect(math.approxEqAbs(f64, c.im, 10.471557, epsilon));
try testing.expectApproxEqAbs(-73.46729221264526, c.re, epsilon);
try testing.expectApproxEqAbs(10.471557674805572, c.im, epsilon);
}
test "cosh64 musl" {
const epsilon = math.floatEps(f64);
const a = Complex(f64).init(7.44648873421389e17, 1.6008058402057622e19);
const c = cosh(a);
try testing.expectApproxEqAbs(std.math.inf(f64), c.re, epsilon);
try testing.expectApproxEqAbs(std.math.inf(f64), c.im, epsilon);
}

View File

@ -13,12 +13,11 @@ pub fn log(z: anytype) Complex(@TypeOf(z.re, z.im)) {
return Complex(T).init(@log(r), phi);
}
const epsilon = 0.0001;
test log {
const epsilon = math.floatEps(f32);
const a = Complex(f32).init(5, 3);
const c = log(a);
try testing.expect(math.approxEqAbs(f32, c.re, 1.763180, epsilon));
try testing.expect(math.approxEqAbs(f32, c.im, 0.540419, epsilon));
try testing.expectApproxEqAbs(1.7631803, c.re, epsilon);
try testing.expectApproxEqAbs(0.5404195, c.im, epsilon);
}

View File

@ -9,13 +9,12 @@ pub fn pow(z: anytype, s: anytype) Complex(@TypeOf(z.re, z.im, s.re, s.im)) {
return cmath.exp(cmath.log(z).mul(s));
}
const epsilon = 0.0001;
test pow {
const epsilon = math.floatEps(f32);
const a = Complex(f32).init(5, 3);
const b = Complex(f32).init(2.3, -1.3);
const c = pow(a, b);
try testing.expect(math.approxEqAbs(f32, c.re, 58.049110, epsilon));
try testing.expect(math.approxEqAbs(f32, c.im, -101.003433, epsilon));
try testing.expectApproxEqAbs(58.049110, c.re, epsilon);
try testing.expectApproxEqAbs(-101.003433, c.im, epsilon);
}

View File

@ -19,5 +19,6 @@ test proj {
const a = Complex(f32).init(5, 3);
const c = proj(a);
try testing.expect(c.re == 5 and c.im == 3);
try testing.expectEqual(5, c.re);
try testing.expectEqual(3, c.im);
}

View File

@ -12,12 +12,11 @@ pub fn sin(z: anytype) Complex(@TypeOf(z.re, z.im)) {
return Complex(T).init(q.im, -q.re);
}
const epsilon = 0.0001;
test sin {
const epsilon = math.floatEps(f32);
const a = Complex(f32).init(5, 3);
const c = sin(a);
try testing.expect(math.approxEqAbs(f32, c.re, -9.654126, epsilon));
try testing.expect(math.approxEqAbs(f32, c.im, 2.841692, epsilon));
try testing.expectApproxEqAbs(-9.654126, c.re, epsilon);
try testing.expectApproxEqAbs(2.8416924, c.im, epsilon);
}

View File

@ -152,20 +152,20 @@ fn sinh64(z: Complex(f64)) Complex(f64) {
return Complex(f64).init((x * x) * (y - y), (x + x) * (y - y));
}
const epsilon = 0.0001;
test sinh32 {
const epsilon = math.floatEps(f32);
const a = Complex(f32).init(5, 3);
const c = sinh(a);
try testing.expect(math.approxEqAbs(f32, c.re, -73.460617, epsilon));
try testing.expect(math.approxEqAbs(f32, c.im, 10.472508, epsilon));
try testing.expectApproxEqAbs(-73.460617, c.re, epsilon);
try testing.expectApproxEqAbs(10.472508, c.im, epsilon);
}
test sinh64 {
const epsilon = math.floatEps(f64);
const a = Complex(f64).init(5, 3);
const c = sinh(a);
try testing.expect(math.approxEqAbs(f64, c.re, -73.460617, epsilon));
try testing.expect(math.approxEqAbs(f64, c.im, 10.472508, epsilon));
try testing.expectApproxEqAbs(-73.46062169567367, c.re, epsilon);
try testing.expectApproxEqAbs(10.472508533940392, c.im, epsilon);
}

View File

@ -43,7 +43,7 @@ fn sqrt32(z: Complex(f32)) Complex(f32) {
// sqrt(-inf + i nan) = nan +- inf i
// sqrt(-inf + iy) = 0 + inf i
if (math.signbit(x)) {
return Complex(f32).init(@abs(x - y), math.copysign(x, y));
return Complex(f32).init(@abs(y - y), math.copysign(x, y));
} else {
return Complex(f32).init(x, math.copysign(y - y, y));
}
@ -94,7 +94,7 @@ fn sqrt64(z: Complex(f64)) Complex(f64) {
// sqrt(-inf + i nan) = nan +- inf i
// sqrt(-inf + iy) = 0 + inf i
if (math.signbit(x)) {
return Complex(f64).init(@abs(x - y), math.copysign(x, y));
return Complex(f64).init(@abs(y - y), math.copysign(x, y));
} else {
return Complex(f64).init(x, math.copysign(y - y, y));
}
@ -127,20 +127,20 @@ fn sqrt64(z: Complex(f64)) Complex(f64) {
return result;
}
const epsilon = 0.0001;
test sqrt32 {
const epsilon = math.floatEps(f32);
const a = Complex(f32).init(5, 3);
const c = sqrt(a);
try testing.expect(math.approxEqAbs(f32, c.re, 2.327117, epsilon));
try testing.expect(math.approxEqAbs(f32, c.im, 0.644574, epsilon));
try testing.expectApproxEqAbs(2.3271174, c.re, epsilon);
try testing.expectApproxEqAbs(0.6445742, c.im, epsilon);
}
test sqrt64 {
const epsilon = math.floatEps(f64);
const a = Complex(f64).init(5, 3);
const c = sqrt(a);
try testing.expect(math.approxEqAbs(f64, c.re, 2.3271175190399496, epsilon));
try testing.expect(math.approxEqAbs(f64, c.im, 0.6445742373246469, epsilon));
try testing.expectApproxEqAbs(2.3271175190399496, c.re, epsilon);
try testing.expectApproxEqAbs(0.6445742373246469, c.im, epsilon);
}

View File

@ -12,12 +12,11 @@ pub fn tan(z: anytype) Complex(@TypeOf(z.re, z.im)) {
return Complex(T).init(r.im, -r.re);
}
const epsilon = 0.0001;
test tan {
const epsilon = math.floatEps(f32);
const a = Complex(f32).init(5, 3);
const c = tan(a);
try testing.expect(math.approxEqAbs(f32, c.re, -0.002708233, epsilon));
try testing.expect(math.approxEqAbs(f32, c.im, 1.004165, epsilon));
try testing.expectApproxEqAbs(-0.002708233, c.re, epsilon);
try testing.expectApproxEqAbs(1.0041647, c.im, epsilon);
}

View File

@ -70,7 +70,7 @@ fn tanh64(z: Complex(f64)) Complex(f64) {
const ix = hx & 0x7fffffff;
if (ix >= 0x7ff00000) {
if ((ix & 0x7fffff) | lx != 0) {
if ((ix & 0xfffff) | lx != 0) {
const r = if (y == 0) y else x * y;
return Complex(f64).init(x, r);
}
@ -101,20 +101,29 @@ fn tanh64(z: Complex(f64)) Complex(f64) {
return Complex(f64).init((beta * rho * s) / den, t / den);
}
const epsilon = 0.0001;
test tanh32 {
const epsilon = math.floatEps(f32);
const a = Complex(f32).init(5, 3);
const c = tanh(a);
try testing.expect(math.approxEqAbs(f32, c.re, 0.999913, epsilon));
try testing.expect(math.approxEqAbs(f32, c.im, -0.000025, epsilon));
try testing.expectApproxEqAbs(0.99991274, c.re, epsilon);
try testing.expectApproxEqAbs(-0.00002536878, c.im, epsilon);
}
test tanh64 {
const epsilon = math.floatEps(f64);
const a = Complex(f64).init(5, 3);
const c = tanh(a);
try testing.expect(math.approxEqAbs(f64, c.re, 0.999913, epsilon));
try testing.expect(math.approxEqAbs(f64, c.im, -0.000025, epsilon));
try testing.expectApproxEqAbs(0.9999128201513536, c.re, epsilon);
try testing.expectApproxEqAbs(-0.00002536867620767604, c.im, epsilon);
}
test "tanh64 musl" {
const epsilon = math.floatEps(f64);
const a = Complex(f64).init(std.math.inf(f64), std.math.inf(f64));
const c = tanh(a);
try testing.expectApproxEqAbs(1, c.re, epsilon);
try testing.expectApproxEqAbs(0, c.im, epsilon);
}