From d5e99cc05ea05d701adffce55defc512d75e10d1 Mon Sep 17 00:00:00 2001 From: Marc Tiehuis Date: Tue, 24 Apr 2018 19:18:31 +1200 Subject: Add initial complex-number support - Library type instead of builtin - All C complex functions implemented Partial WIP: Needs more tests for edge cases. --- std/math/complex/sin.zig | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 std/math/complex/sin.zig (limited to 'std/math/complex/sin.zig') diff --git a/std/math/complex/sin.zig b/std/math/complex/sin.zig new file mode 100644 index 0000000000..a334d6625c --- /dev/null +++ b/std/math/complex/sin.zig @@ -0,0 +1,25 @@ +const std = @import("../../index.zig"); +const debug = std.debug; +const math = std.math; +const cmath = math.complex; +const Complex = cmath.Complex; + +pub fn sin(z: var) Complex(@typeOf(z.re)) { + const T = @typeOf(z.re); + const p = Complex(T).new(-z.im, z.re); + const q = cmath.sinh(p); + return Complex(T).new(q.im, -q.re); +} + +const epsilon = 0.0001; + +test "complex.csin" { + const a = Complex(f32).new(5, 3); + const c = sin(a); + + const re = c.re; + const im = c.im; + + debug.assert(math.approxEq(f32, re, -9.654126, epsilon)); + debug.assert(math.approxEq(f32, im, 2.841692, epsilon)); +} -- cgit v1.2.3 From 1d998d5dce73d8d500dee5e41a3dca92a7f9b03e Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 24 Apr 2018 21:14:12 -0400 Subject: clean up complex math tests --- std/math/complex/acos.zig | 7 ++----- std/math/complex/acosh.zig | 7 ++----- std/math/complex/asin.zig | 7 ++----- std/math/complex/asinh.zig | 7 ++----- std/math/complex/atan.zig | 18 ++++++------------ std/math/complex/atanh.zig | 7 ++----- std/math/complex/cos.zig | 7 ++----- std/math/complex/cosh.zig | 14 ++++---------- std/math/complex/exp.zig | 14 ++++---------- std/math/complex/log.zig | 7 ++----- std/math/complex/pow.zig | 7 ++----- std/math/complex/sin.zig | 7 ++----- std/math/complex/sinh.zig | 14 ++++---------- std/math/complex/sqrt.zig | 14 ++++---------- std/math/complex/tan.zig | 7 ++----- std/math/complex/tanh.zig | 14 ++++---------- 16 files changed, 46 insertions(+), 112 deletions(-) (limited to 'std/math/complex/sin.zig') diff --git a/std/math/complex/acos.zig b/std/math/complex/acos.zig index cd37ddd92e..a5760b4ace 100644 --- a/std/math/complex/acos.zig +++ b/std/math/complex/acos.zig @@ -16,9 +16,6 @@ test "complex.cacos" { const a = Complex(f32).new(5, 3); const c = acos(a); - const re = c.re; - const im = c.im; - - debug.assert(math.approxEq(f32, re, 0.546975, epsilon)); - debug.assert(math.approxEq(f32, im, -2.452914, epsilon)); + debug.assert(math.approxEq(f32, c.re, 0.546975, epsilon)); + debug.assert(math.approxEq(f32, c.im, -2.452914, epsilon)); } diff --git a/std/math/complex/acosh.zig b/std/math/complex/acosh.zig index 830ff79e5f..8dd91b2836 100644 --- a/std/math/complex/acosh.zig +++ b/std/math/complex/acosh.zig @@ -16,9 +16,6 @@ test "complex.cacosh" { const a = Complex(f32).new(5, 3); const c = acosh(a); - const re = c.re; - const im = c.im; - - debug.assert(math.approxEq(f32, re, 2.452914, epsilon)); - debug.assert(math.approxEq(f32, im, 0.546975, epsilon)); + debug.assert(math.approxEq(f32, c.re, 2.452914, epsilon)); + debug.assert(math.approxEq(f32, c.im, 0.546975, epsilon)); } diff --git a/std/math/complex/asin.zig b/std/math/complex/asin.zig index 84cabc6941..584a3a1a9b 100644 --- a/std/math/complex/asin.zig +++ b/std/math/complex/asin.zig @@ -22,9 +22,6 @@ test "complex.casin" { const a = Complex(f32).new(5, 3); const c = asin(a); - const re = c.re; - const im = c.im; - - debug.assert(math.approxEq(f32, re, 1.023822, epsilon)); - debug.assert(math.approxEq(f32, im, 2.452914, epsilon)); + debug.assert(math.approxEq(f32, c.re, 1.023822, epsilon)); + debug.assert(math.approxEq(f32, c.im, 2.452914, epsilon)); } diff --git a/std/math/complex/asinh.zig b/std/math/complex/asinh.zig index 5cf086d520..0c4dc2b6e4 100644 --- a/std/math/complex/asinh.zig +++ b/std/math/complex/asinh.zig @@ -17,9 +17,6 @@ test "complex.casinh" { const a = Complex(f32).new(5, 3); const c = asinh(a); - const re = c.re; - const im = c.im; - - debug.assert(math.approxEq(f32, re, 2.459831, epsilon)); - debug.assert(math.approxEq(f32, im, 0.533999, epsilon)); + debug.assert(math.approxEq(f32, c.re, 2.459831, epsilon)); + debug.assert(math.approxEq(f32, c.im, 0.533999, epsilon)); } diff --git a/std/math/complex/atan.zig b/std/math/complex/atan.zig index 2aa0211333..b7bbf930eb 100644 --- a/std/math/complex/atan.zig +++ b/std/math/complex/atan.zig @@ -113,24 +113,18 @@ fn atan64(z: &const Complex(f64)) Complex(f64) { const epsilon = 0.0001; -test "complex.ctan32" { +test "complex.catan32" { const a = Complex(f32).new(5, 3); const c = atan(a); - const re = c.re; - const im = c.im; - - debug.assert(math.approxEq(f32, re, 1.423679, epsilon)); - debug.assert(math.approxEq(f32, im, 0.086569, epsilon)); + debug.assert(math.approxEq(f32, c.re, 1.423679, epsilon)); + debug.assert(math.approxEq(f32, c.im, 0.086569, epsilon)); } -test "complex.ctan64" { +test "complex.catan64" { const a = Complex(f64).new(5, 3); const c = atan(a); - const re = c.re; - const im = c.im; - - debug.assert(math.approxEq(f64, re, 1.423679, epsilon)); - debug.assert(math.approxEq(f64, im, 0.086569, epsilon)); + debug.assert(math.approxEq(f64, c.re, 1.423679, epsilon)); + debug.assert(math.approxEq(f64, c.im, 0.086569, epsilon)); } diff --git a/std/math/complex/atanh.zig b/std/math/complex/atanh.zig index 5fbbe2eb4b..f70c741765 100644 --- a/std/math/complex/atanh.zig +++ b/std/math/complex/atanh.zig @@ -17,9 +17,6 @@ test "complex.catanh" { const a = Complex(f32).new(5, 3); const c = atanh(a); - const re = c.re; - const im = c.im; - - debug.assert(math.approxEq(f32, re, 0.146947, epsilon)); - debug.assert(math.approxEq(f32, im, 1.480870, epsilon)); + debug.assert(math.approxEq(f32, c.re, 0.146947, epsilon)); + debug.assert(math.approxEq(f32, c.im, 1.480870, epsilon)); } diff --git a/std/math/complex/cos.zig b/std/math/complex/cos.zig index 35908898a6..96e4ffcdb0 100644 --- a/std/math/complex/cos.zig +++ b/std/math/complex/cos.zig @@ -16,9 +16,6 @@ test "complex.ccos" { const a = Complex(f32).new(5, 3); const c = cos(a); - const re = c.re; - const im = c.im; - - debug.assert(math.approxEq(f32, re, 2.855815, epsilon)); - debug.assert(math.approxEq(f32, im, 9.606383, epsilon)); + debug.assert(math.approxEq(f32, c.re, 2.855815, epsilon)); + debug.assert(math.approxEq(f32, c.im, 9.606383, epsilon)); } diff --git a/std/math/complex/cosh.zig b/std/math/complex/cosh.zig index 1387ecb05f..96eac68556 100644 --- a/std/math/complex/cosh.zig +++ b/std/math/complex/cosh.zig @@ -152,20 +152,14 @@ test "complex.ccosh32" { const a = Complex(f32).new(5, 3); const c = cosh(a); - const re = c.re; - const im = c.im; - - debug.assert(math.approxEq(f32, re, -73.467300, epsilon)); - debug.assert(math.approxEq(f32, im, 10.471557, epsilon)); + debug.assert(math.approxEq(f32, c.re, -73.467300, epsilon)); + debug.assert(math.approxEq(f32, c.im, 10.471557, epsilon)); } test "complex.ccosh64" { const a = Complex(f64).new(5, 3); const c = cosh(a); - const re = c.re; - const im = c.im; - - debug.assert(math.approxEq(f64, re, -73.467300, epsilon)); - debug.assert(math.approxEq(f64, im, 10.471557, epsilon)); + debug.assert(math.approxEq(f64, c.re, -73.467300, epsilon)); + debug.assert(math.approxEq(f64, c.im, 10.471557, epsilon)); } diff --git a/std/math/complex/exp.zig b/std/math/complex/exp.zig index ace596771a..03f7f9e41b 100644 --- a/std/math/complex/exp.zig +++ b/std/math/complex/exp.zig @@ -127,20 +127,14 @@ test "complex.cexp32" { const a = Complex(f32).new(5, 3); const c = exp(a); - const re = c.re; - const im = c.im; - - debug.assert(math.approxEq(f32, re, -146.927917, epsilon)); - debug.assert(math.approxEq(f32, im, 20.944065, epsilon)); + debug.assert(math.approxEq(f32, c.re, -146.927917, epsilon)); + debug.assert(math.approxEq(f32, c.im, 20.944065, epsilon)); } test "complex.cexp64" { const a = Complex(f32).new(5, 3); const c = exp(a); - const re = c.re; - const im = c.im; - - debug.assert(math.approxEq(f64, re, -146.927917, epsilon)); - debug.assert(math.approxEq(f64, im, 20.944065, epsilon)); + debug.assert(math.approxEq(f64, c.re, -146.927917, epsilon)); + debug.assert(math.approxEq(f64, c.im, 20.944065, epsilon)); } diff --git a/std/math/complex/log.zig b/std/math/complex/log.zig index 6fe4d6b50d..a4a1d1664f 100644 --- a/std/math/complex/log.zig +++ b/std/math/complex/log.zig @@ -18,9 +18,6 @@ test "complex.clog" { const a = Complex(f32).new(5, 3); const c = log(a); - const re = c.re; - const im = c.im; - - debug.assert(math.approxEq(f32, re, 1.763180, epsilon)); - debug.assert(math.approxEq(f32, im, 0.540419, epsilon)); + debug.assert(math.approxEq(f32, c.re, 1.763180, epsilon)); + debug.assert(math.approxEq(f32, c.im, 0.540419, epsilon)); } diff --git a/std/math/complex/pow.zig b/std/math/complex/pow.zig index 34414254ce..bef9fde542 100644 --- a/std/math/complex/pow.zig +++ b/std/math/complex/pow.zig @@ -17,9 +17,6 @@ test "complex.cpow" { const b = Complex(f32).new(2.3, -1.3); const c = pow(Complex(f32), a, b); - const re = c.re; - const im = c.im; - - debug.assert(math.approxEq(f32, re, 58.049110, epsilon)); - debug.assert(math.approxEq(f32, im, -101.003433, epsilon)); + debug.assert(math.approxEq(f32, c.re, 58.049110, epsilon)); + debug.assert(math.approxEq(f32, c.im, -101.003433, epsilon)); } diff --git a/std/math/complex/sin.zig b/std/math/complex/sin.zig index a334d6625c..d32b771d3b 100644 --- a/std/math/complex/sin.zig +++ b/std/math/complex/sin.zig @@ -17,9 +17,6 @@ test "complex.csin" { const a = Complex(f32).new(5, 3); const c = sin(a); - const re = c.re; - const im = c.im; - - debug.assert(math.approxEq(f32, re, -9.654126, epsilon)); - debug.assert(math.approxEq(f32, im, 2.841692, epsilon)); + debug.assert(math.approxEq(f32, c.re, -9.654126, epsilon)); + debug.assert(math.approxEq(f32, c.im, 2.841692, epsilon)); } diff --git a/std/math/complex/sinh.zig b/std/math/complex/sinh.zig index 799ee9ad6e..09a62ca058 100644 --- a/std/math/complex/sinh.zig +++ b/std/math/complex/sinh.zig @@ -151,20 +151,14 @@ test "complex.csinh32" { const a = Complex(f32).new(5, 3); const c = sinh(a); - const re = c.re; - const im = c.im; - - debug.assert(math.approxEq(f32, re, -73.460617, epsilon)); - debug.assert(math.approxEq(f32, im, 10.472508, epsilon)); + debug.assert(math.approxEq(f32, c.re, -73.460617, epsilon)); + debug.assert(math.approxEq(f32, c.im, 10.472508, epsilon)); } test "complex.csinh64" { const a = Complex(f64).new(5, 3); const c = sinh(a); - const re = c.re; - const im = c.im; - - debug.assert(math.approxEq(f64, re, -73.460617, epsilon)); - debug.assert(math.approxEq(f64, im, 10.472508, epsilon)); + debug.assert(math.approxEq(f64, c.re, -73.460617, epsilon)); + debug.assert(math.approxEq(f64, c.im, 10.472508, epsilon)); } diff --git a/std/math/complex/sqrt.zig b/std/math/complex/sqrt.zig index 03935bd3b3..02e6390572 100644 --- a/std/math/complex/sqrt.zig +++ b/std/math/complex/sqrt.zig @@ -121,20 +121,14 @@ test "complex.csqrt32" { const a = Complex(f32).new(5, 3); const c = sqrt(a); - const re = c.re; - const im = c.im; - - debug.assert(math.approxEq(f32, re, 2.327117, epsilon)); - debug.assert(math.approxEq(f32, im, 0.644574, epsilon)); + debug.assert(math.approxEq(f32, c.re, 2.327117, epsilon)); + debug.assert(math.approxEq(f32, c.im, 0.644574, epsilon)); } test "complex.csqrt64" { const a = Complex(f64).new(5, 3); const c = sqrt(a); - const re = c.re; - const im = c.im; - - debug.assert(math.approxEq(f64, re, 2.3271175190399496, epsilon)); - debug.assert(math.approxEq(f64, im, 0.6445742373246469, epsilon)); + debug.assert(math.approxEq(f64, c.re, 2.3271175190399496, epsilon)); + debug.assert(math.approxEq(f64, c.im, 0.6445742373246469, epsilon)); } diff --git a/std/math/complex/tan.zig b/std/math/complex/tan.zig index cb43ff36dc..4ea5182fa7 100644 --- a/std/math/complex/tan.zig +++ b/std/math/complex/tan.zig @@ -17,9 +17,6 @@ test "complex.ctan" { const a = Complex(f32).new(5, 3); const c = tan(a); - const re = c.re; - const im = c.im; - - debug.assert(math.approxEq(f32, re, -0.002708233, epsilon)); - debug.assert(math.approxEq(f32, im, 1.004165, epsilon)); + debug.assert(math.approxEq(f32, c.re, -0.002708233, epsilon)); + debug.assert(math.approxEq(f32, c.im, 1.004165, epsilon)); } diff --git a/std/math/complex/tanh.zig b/std/math/complex/tanh.zig index bc68aafb01..6af62f48ae 100644 --- a/std/math/complex/tanh.zig +++ b/std/math/complex/tanh.zig @@ -98,20 +98,14 @@ test "complex.ctanh32" { const a = Complex(f32).new(5, 3); const c = tanh(a); - const re = c.re; - const im = c.im; - - debug.assert(math.approxEq(f32, re, 0.999913, epsilon)); - debug.assert(math.approxEq(f32, im, -0.000025, epsilon)); + debug.assert(math.approxEq(f32, c.re, 0.999913, epsilon)); + debug.assert(math.approxEq(f32, c.im, -0.000025, epsilon)); } test "complex.ctanh64" { const a = Complex(f64).new(5, 3); const c = tanh(a); - const re = c.re; - const im = c.im; - - debug.assert(math.approxEq(f64, re, 0.999913, epsilon)); - debug.assert(math.approxEq(f64, im, -0.000025, epsilon)); + debug.assert(math.approxEq(f64, c.re, 0.999913, epsilon)); + debug.assert(math.approxEq(f64, c.im, -0.000025, epsilon)); } -- cgit v1.2.3