Skip to content

Commit

Permalink
Merge pull request #20878 from tiehuis/std-math-complex-fixes
Browse files Browse the repository at this point in the history
std.math.complex fixes
  • Loading branch information
andrewrk authored Aug 1, 2024
2 parents 7c5ee3e + 8438855 commit 059856a
Show file tree
Hide file tree
Showing 19 changed files with 102 additions and 120 deletions.
5 changes: 2 additions & 3 deletions lib/std/math/complex/abs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
7 changes: 3 additions & 4 deletions lib/std/math/complex/acos.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
13 changes: 8 additions & 5 deletions lib/std/math/complex/acosh.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

const epsilon = 0.0001;
return if (math.signbit(z.im))
Complex(T).init(q.im, -q.re)
else
Complex(T).init(-q.im, q.re);
}

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);
}
5 changes: 2 additions & 3 deletions lib/std/math/complex/arg.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
7 changes: 3 additions & 4 deletions lib/std/math/complex/asin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
7 changes: 3 additions & 4 deletions lib/std/math/complex/asinh.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
50 changes: 10 additions & 40 deletions lib/std/math/complex/atan.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
7 changes: 3 additions & 4 deletions lib/std/math/complex/atanh.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
3 changes: 2 additions & 1 deletion lib/std/math/complex/conj.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
7 changes: 3 additions & 4 deletions lib/std/math/complex/cos.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
29 changes: 19 additions & 10 deletions lib/std/math/complex/cosh.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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));
}
}
Expand Down Expand Up @@ -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);
}
7 changes: 3 additions & 4 deletions lib/std/math/complex/log.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
7 changes: 3 additions & 4 deletions lib/std/math/complex/pow.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
3 changes: 2 additions & 1 deletion lib/std/math/complex/proj.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
7 changes: 3 additions & 4 deletions lib/std/math/complex/sin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
12 changes: 6 additions & 6 deletions lib/std/math/complex/sinh.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Loading

0 comments on commit 059856a

Please sign in to comment.