| author | |
| committer | |
| log | 5414bd48edd460ae8667c811e13aa9b5d9fab919 |
| tree | e87302fc0d1c53518474ef704ec9e64e9e49424c |
| parent | 04d95ea4192a7f70e7c11b8ee67d237cf38da9b7 |
| signature |
23 files changed, 164 insertions(+), 161 deletions(-)
lib/std/math/complex.zig+15-12| ... | ... | @@ -38,9 +38,12 @@ pub fn Complex(comptime T: type) type { |
| 38 | 38 | |
| 39 | 39 | /// Imaginary part. |
| 40 | 40 | im: T, |
| 41 | ||
| 42 | /// Deprecated, use init() | |
| 43 | pub const new = init; | |
| 41 | 44 | |
| 42 | 45 | /// Create a new Complex number from the given real and imaginary parts. |
| 43 | pub fn new(re: T, im: T) Self { | |
| 46 | pub fn init(re: T, im: T) Self { | |
| 44 | 47 | return Self{ |
| 45 | 48 | .re = re, |
| 46 | 49 | .im = im, |
| ... | ... | @@ -110,32 +113,32 @@ pub fn Complex(comptime T: type) type { |
| 110 | 113 | const epsilon = 0.0001; |
| 111 | 114 | |
| 112 | 115 | test "complex.add" { |
| 113 | const a = Complex(f32).new(5, 3); | |
| 114 | const b = Complex(f32).new(2, 7); | |
| 116 | const a = Complex(f32).init(5, 3); | |
| 117 | const b = Complex(f32).init(2, 7); | |
| 115 | 118 | const c = a.add(b); |
| 116 | 119 | |
| 117 | 120 | try testing.expect(c.re == 7 and c.im == 10); |
| 118 | 121 | } |
| 119 | 122 | |
| 120 | 123 | test "complex.sub" { |
| 121 | const a = Complex(f32).new(5, 3); | |
| 122 | const b = Complex(f32).new(2, 7); | |
| 124 | const a = Complex(f32).init(5, 3); | |
| 125 | const b = Complex(f32).init(2, 7); | |
| 123 | 126 | const c = a.sub(b); |
| 124 | 127 | |
| 125 | 128 | try testing.expect(c.re == 3 and c.im == -4); |
| 126 | 129 | } |
| 127 | 130 | |
| 128 | 131 | test "complex.mul" { |
| 129 | const a = Complex(f32).new(5, 3); | |
| 130 | const b = Complex(f32).new(2, 7); | |
| 132 | const a = Complex(f32).init(5, 3); | |
| 133 | const b = Complex(f32).init(2, 7); | |
| 131 | 134 | const c = a.mul(b); |
| 132 | 135 | |
| 133 | 136 | try testing.expect(c.re == -11 and c.im == 41); |
| 134 | 137 | } |
| 135 | 138 | |
| 136 | 139 | test "complex.div" { |
| 137 | const a = Complex(f32).new(5, 3); | |
| 138 | const b = Complex(f32).new(2, 7); | |
| 140 | const a = Complex(f32).init(5, 3); | |
| 141 | const b = Complex(f32).init(2, 7); | |
| 139 | 142 | const c = a.div(b); |
| 140 | 143 | |
| 141 | 144 | try testing.expect(math.approxEqAbs(f32, c.re, @as(f32, 31) / 53, epsilon) and |
| ... | ... | @@ -143,14 +146,14 @@ test "complex.div" { |
| 143 | 146 | } |
| 144 | 147 | |
| 145 | 148 | test "complex.conjugate" { |
| 146 | const a = Complex(f32).new(5, 3); | |
| 149 | const a = Complex(f32).init(5, 3); | |
| 147 | 150 | const c = a.conjugate(); |
| 148 | 151 | |
| 149 | 152 | try testing.expect(c.re == 5 and c.im == -3); |
| 150 | 153 | } |
| 151 | 154 | |
| 152 | 155 | test "complex.reciprocal" { |
| 153 | const a = Complex(f32).new(5, 3); | |
| 156 | const a = Complex(f32).init(5, 3); | |
| 154 | 157 | const c = a.reciprocal(); |
| 155 | 158 | |
| 156 | 159 | try testing.expect(math.approxEqAbs(f32, c.re, @as(f32, 5) / 34, epsilon) and |
| ... | ... | @@ -158,7 +161,7 @@ test "complex.reciprocal" { |
| 158 | 161 | } |
| 159 | 162 | |
| 160 | 163 | test "complex.magnitude" { |
| 161 | const a = Complex(f32).new(5, 3); | |
| 164 | const a = Complex(f32).init(5, 3); | |
| 162 | 165 | const c = a.magnitude(); |
| 163 | 166 | |
| 164 | 167 | try testing.expect(math.approxEqAbs(f32, c, 5.83095, epsilon)); |
lib/std/math/complex/abs.zig+1-1| ... | ... | @@ -18,7 +18,7 @@ pub fn abs(z: anytype) @TypeOf(z.re) { |
| 18 | 18 | const epsilon = 0.0001; |
| 19 | 19 | |
| 20 | 20 | test "complex.cabs" { |
| 21 | const a = Complex(f32).new(5, 3); | |
| 21 | const a = Complex(f32).init(5, 3); | |
| 22 | 22 | const c = abs(a); |
| 23 | 23 | try testing.expect(math.approxEqAbs(f32, c, 5.83095, epsilon)); |
| 24 | 24 | } |
lib/std/math/complex/acos.zig+2-2| ... | ... | @@ -13,13 +13,13 @@ const Complex = cmath.Complex; |
| 13 | 13 | pub fn acos(z: anytype) Complex(@TypeOf(z.re)) { |
| 14 | 14 | const T = @TypeOf(z.re); |
| 15 | 15 | const q = cmath.asin(z); |
| 16 | return Complex(T).new(@as(T, math.pi) / 2 - q.re, -q.im); | |
| 16 | return Complex(T).init(@as(T, math.pi) / 2 - q.re, -q.im); | |
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | const epsilon = 0.0001; |
| 20 | 20 | |
| 21 | 21 | test "complex.cacos" { |
| 22 | const a = Complex(f32).new(5, 3); | |
| 22 | const a = Complex(f32).init(5, 3); | |
| 23 | 23 | const c = acos(a); |
| 24 | 24 | |
| 25 | 25 | try testing.expect(math.approxEqAbs(f32, c.re, 0.546975, epsilon)); |
lib/std/math/complex/acosh.zig+2-2| ... | ... | @@ -13,13 +13,13 @@ const Complex = cmath.Complex; |
| 13 | 13 | pub fn acosh(z: anytype) Complex(@TypeOf(z.re)) { |
| 14 | 14 | const T = @TypeOf(z.re); |
| 15 | 15 | const q = cmath.acos(z); |
| 16 | return Complex(T).new(-q.im, q.re); | |
| 16 | return Complex(T).init(-q.im, q.re); | |
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | const epsilon = 0.0001; |
| 20 | 20 | |
| 21 | 21 | test "complex.cacosh" { |
| 22 | const a = Complex(f32).new(5, 3); | |
| 22 | const a = Complex(f32).init(5, 3); | |
| 23 | 23 | const c = acosh(a); |
| 24 | 24 | |
| 25 | 25 | try testing.expect(math.approxEqAbs(f32, c.re, 2.452914, epsilon)); |
lib/std/math/complex/arg.zig+1-1| ... | ... | @@ -18,7 +18,7 @@ pub fn arg(z: anytype) @TypeOf(z.re) { |
| 18 | 18 | const epsilon = 0.0001; |
| 19 | 19 | |
| 20 | 20 | test "complex.carg" { |
| 21 | const a = Complex(f32).new(5, 3); | |
| 21 | const a = Complex(f32).init(5, 3); | |
| 22 | 22 | const c = arg(a); |
| 23 | 23 | try testing.expect(math.approxEqAbs(f32, c, 0.540420, epsilon)); |
| 24 | 24 | } |
lib/std/math/complex/asin.zig+4-4| ... | ... | @@ -15,17 +15,17 @@ pub fn asin(z: anytype) Complex(@TypeOf(z.re)) { |
| 15 | 15 | const x = z.re; |
| 16 | 16 | const y = z.im; |
| 17 | 17 | |
| 18 | const p = Complex(T).new(1.0 - (x - y) * (x + y), -2.0 * x * y); | |
| 19 | const q = Complex(T).new(-y, x); | |
| 18 | const p = Complex(T).init(1.0 - (x - y) * (x + y), -2.0 * x * y); | |
| 19 | const q = Complex(T).init(-y, x); | |
| 20 | 20 | const r = cmath.log(q.add(cmath.sqrt(p))); |
| 21 | 21 | |
| 22 | return Complex(T).new(r.im, -r.re); | |
| 22 | return Complex(T).init(r.im, -r.re); | |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | const epsilon = 0.0001; |
| 26 | 26 | |
| 27 | 27 | test "complex.casin" { |
| 28 | const a = Complex(f32).new(5, 3); | |
| 28 | const a = Complex(f32).init(5, 3); | |
| 29 | 29 | const c = asin(a); |
| 30 | 30 | |
| 31 | 31 | try testing.expect(math.approxEqAbs(f32, c.re, 1.023822, epsilon)); |
lib/std/math/complex/asinh.zig+3-3| ... | ... | @@ -12,15 +12,15 @@ const Complex = cmath.Complex; |
| 12 | 12 | /// Returns the hyperbolic arc-sine of z. |
| 13 | 13 | pub fn asinh(z: anytype) Complex(@TypeOf(z.re)) { |
| 14 | 14 | const T = @TypeOf(z.re); |
| 15 | const q = Complex(T).new(-z.im, z.re); | |
| 15 | const q = Complex(T).init(-z.im, z.re); | |
| 16 | 16 | const r = cmath.asin(q); |
| 17 | return Complex(T).new(r.im, -r.re); | |
| 17 | return Complex(T).init(r.im, -r.re); | |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | const epsilon = 0.0001; |
| 21 | 21 | |
| 22 | 22 | test "complex.casinh" { |
| 23 | const a = Complex(f32).new(5, 3); | |
| 23 | const a = Complex(f32).init(5, 3); | |
| 24 | 24 | const c = asinh(a); |
| 25 | 25 | |
| 26 | 26 | try testing.expect(math.approxEqAbs(f32, c.re, 2.459831, epsilon)); |
lib/std/math/complex/atan.zig+10-10| ... | ... | @@ -50,14 +50,14 @@ fn atan32(z: Complex(f32)) Complex(f32) { |
| 50 | 50 | |
| 51 | 51 | if ((x == 0.0) and (y > 1.0)) { |
| 52 | 52 | // overflow |
| 53 | return Complex(f32).new(maxnum, maxnum); | |
| 53 | return Complex(f32).init(maxnum, maxnum); | |
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | const x2 = x * x; |
| 57 | 57 | var a = 1.0 - x2 - (y * y); |
| 58 | 58 | if (a == 0.0) { |
| 59 | 59 | // overflow |
| 60 | return Complex(f32).new(maxnum, maxnum); | |
| 60 | return Complex(f32).init(maxnum, maxnum); | |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | var t = 0.5 * math.atan2(f32, 2.0 * x, a); |
| ... | ... | @@ -67,12 +67,12 @@ fn atan32(z: Complex(f32)) Complex(f32) { |
| 67 | 67 | a = x2 + t * t; |
| 68 | 68 | if (a == 0.0) { |
| 69 | 69 | // overflow |
| 70 | return Complex(f32).new(maxnum, maxnum); | |
| 70 | return Complex(f32).init(maxnum, maxnum); | |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | t = y + 1.0; |
| 74 | 74 | a = (x2 + (t * t)) / a; |
| 75 | return Complex(f32).new(w, 0.25 * math.ln(a)); | |
| 75 | return Complex(f32).init(w, 0.25 * math.ln(a)); | |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | fn redupif64(x: f64) f64 { |
| ... | ... | @@ -99,14 +99,14 @@ fn atan64(z: Complex(f64)) Complex(f64) { |
| 99 | 99 | |
| 100 | 100 | if ((x == 0.0) and (y > 1.0)) { |
| 101 | 101 | // overflow |
| 102 | return Complex(f64).new(maxnum, maxnum); | |
| 102 | return Complex(f64).init(maxnum, maxnum); | |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | const x2 = x * x; |
| 106 | 106 | var a = 1.0 - x2 - (y * y); |
| 107 | 107 | if (a == 0.0) { |
| 108 | 108 | // overflow |
| 109 | return Complex(f64).new(maxnum, maxnum); | |
| 109 | return Complex(f64).init(maxnum, maxnum); | |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | var t = 0.5 * math.atan2(f64, 2.0 * x, a); |
| ... | ... | @@ -116,18 +116,18 @@ fn atan64(z: Complex(f64)) Complex(f64) { |
| 116 | 116 | a = x2 + t * t; |
| 117 | 117 | if (a == 0.0) { |
| 118 | 118 | // overflow |
| 119 | return Complex(f64).new(maxnum, maxnum); | |
| 119 | return Complex(f64).init(maxnum, maxnum); | |
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | t = y + 1.0; |
| 123 | 123 | a = (x2 + (t * t)) / a; |
| 124 | return Complex(f64).new(w, 0.25 * math.ln(a)); | |
| 124 | return Complex(f64).init(w, 0.25 * math.ln(a)); | |
| 125 | 125 | } |
| 126 | 126 | |
| 127 | 127 | const epsilon = 0.0001; |
| 128 | 128 | |
| 129 | 129 | test "complex.catan32" { |
| 130 | const a = Complex(f32).new(5, 3); | |
| 130 | const a = Complex(f32).init(5, 3); | |
| 131 | 131 | const c = atan(a); |
| 132 | 132 | |
| 133 | 133 | try testing.expect(math.approxEqAbs(f32, c.re, 1.423679, epsilon)); |
| ... | ... | @@ -135,7 +135,7 @@ test "complex.catan32" { |
| 135 | 135 | } |
| 136 | 136 | |
| 137 | 137 | test "complex.catan64" { |
| 138 | const a = Complex(f64).new(5, 3); | |
| 138 | const a = Complex(f64).init(5, 3); | |
| 139 | 139 | const c = atan(a); |
| 140 | 140 | |
| 141 | 141 | try testing.expect(math.approxEqAbs(f64, c.re, 1.423679, epsilon)); |
lib/std/math/complex/atanh.zig+3-3| ... | ... | @@ -12,15 +12,15 @@ const Complex = cmath.Complex; |
| 12 | 12 | /// Returns the hyperbolic arc-tangent of z. |
| 13 | 13 | pub fn atanh(z: anytype) Complex(@TypeOf(z.re)) { |
| 14 | 14 | const T = @TypeOf(z.re); |
| 15 | const q = Complex(T).new(-z.im, z.re); | |
| 15 | const q = Complex(T).init(-z.im, z.re); | |
| 16 | 16 | const r = cmath.atan(q); |
| 17 | return Complex(T).new(r.im, -r.re); | |
| 17 | return Complex(T).init(r.im, -r.re); | |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | const epsilon = 0.0001; |
| 21 | 21 | |
| 22 | 22 | test "complex.catanh" { |
| 23 | const a = Complex(f32).new(5, 3); | |
| 23 | const a = Complex(f32).init(5, 3); | |
| 24 | 24 | const c = atanh(a); |
| 25 | 25 | |
| 26 | 26 | try testing.expect(math.approxEqAbs(f32, c.re, 0.146947, epsilon)); |
lib/std/math/complex/conj.zig+2-2| ... | ... | @@ -12,11 +12,11 @@ const Complex = cmath.Complex; |
| 12 | 12 | /// Returns the complex conjugate of z. |
| 13 | 13 | pub fn conj(z: anytype) Complex(@TypeOf(z.re)) { |
| 14 | 14 | const T = @TypeOf(z.re); |
| 15 | return Complex(T).new(z.re, -z.im); | |
| 15 | return Complex(T).init(z.re, -z.im); | |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | test "complex.conj" { |
| 19 | const a = Complex(f32).new(5, 3); | |
| 19 | const a = Complex(f32).init(5, 3); | |
| 20 | 20 | const c = a.conjugate(); |
| 21 | 21 | |
| 22 | 22 | try testing.expect(c.re == 5 and c.im == -3); |
lib/std/math/complex/cos.zig+2-2| ... | ... | @@ -12,14 +12,14 @@ const Complex = cmath.Complex; |
| 12 | 12 | /// Returns the cosine of z. |
| 13 | 13 | pub fn cos(z: anytype) Complex(@TypeOf(z.re)) { |
| 14 | 14 | const T = @TypeOf(z.re); |
| 15 | const p = Complex(T).new(-z.im, z.re); | |
| 15 | const p = Complex(T).init(-z.im, z.re); | |
| 16 | 16 | return cmath.cosh(p); |
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | const epsilon = 0.0001; |
| 20 | 20 | |
| 21 | 21 | test "complex.ccos" { |
| 22 | const a = Complex(f32).new(5, 3); | |
| 22 | const a = Complex(f32).init(5, 3); | |
| 23 | 23 | const c = cos(a); |
| 24 | 24 | |
| 25 | 25 | try testing.expect(math.approxEqAbs(f32, c.re, 2.855815, epsilon)); |
lib/std/math/complex/cosh.zig+28-28| ... | ... | @@ -40,55 +40,55 @@ fn cosh32(z: Complex(f32)) Complex(f32) { |
| 40 | 40 | |
| 41 | 41 | if (ix < 0x7f800000 and iy < 0x7f800000) { |
| 42 | 42 | if (iy == 0) { |
| 43 | return Complex(f32).new(math.cosh(x), y); | |
| 43 | return Complex(f32).init(math.cosh(x), y); | |
| 44 | 44 | } |
| 45 | 45 | // small x: normal case |
| 46 | 46 | if (ix < 0x41100000) { |
| 47 | return Complex(f32).new(math.cosh(x) * math.cos(y), math.sinh(x) * math.sin(y)); | |
| 47 | return Complex(f32).init(math.cosh(x) * math.cos(y), math.sinh(x) * math.sin(y)); | |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | // |x|>= 9, so cosh(x) ~= exp(|x|) |
| 51 | 51 | if (ix < 0x42b17218) { |
| 52 | 52 | // x < 88.7: exp(|x|) won't overflow |
| 53 | 53 | const h = math.exp(math.fabs(x)) * 0.5; |
| 54 | return Complex(f32).new(math.copysign(f32, h, x) * math.cos(y), h * math.sin(y)); | |
| 54 | return Complex(f32).init(math.copysign(f32, h, x) * math.cos(y), h * math.sin(y)); | |
| 55 | 55 | } |
| 56 | 56 | // x < 192.7: scale to avoid overflow |
| 57 | 57 | else if (ix < 0x4340b1e7) { |
| 58 | const v = Complex(f32).new(math.fabs(x), y); | |
| 58 | const v = Complex(f32).init(math.fabs(x), y); | |
| 59 | 59 | const r = ldexp_cexp(v, -1); |
| 60 | return Complex(f32).new(r.re, r.im * math.copysign(f32, 1, x)); | |
| 60 | return Complex(f32).init(r.re, r.im * math.copysign(f32, 1, x)); | |
| 61 | 61 | } |
| 62 | 62 | // x >= 192.7: result always overflows |
| 63 | 63 | else { |
| 64 | 64 | const h = 0x1p127 * x; |
| 65 | return Complex(f32).new(h * h * math.cos(y), h * math.sin(y)); | |
| 65 | return Complex(f32).init(h * h * math.cos(y), h * math.sin(y)); | |
| 66 | 66 | } |
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | if (ix == 0 and iy >= 0x7f800000) { |
| 70 | return Complex(f32).new(y - y, math.copysign(f32, 0, x * (y - y))); | |
| 70 | return Complex(f32).init(y - y, math.copysign(f32, 0, x * (y - y))); | |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | if (iy == 0 and ix >= 0x7f800000) { |
| 74 | 74 | if (hx & 0x7fffff == 0) { |
| 75 | return Complex(f32).new(x * x, math.copysign(f32, 0, x) * y); | |
| 75 | return Complex(f32).init(x * x, math.copysign(f32, 0, x) * y); | |
| 76 | 76 | } |
| 77 | return Complex(f32).new(x, math.copysign(f32, 0, (x + x) * y)); | |
| 77 | return Complex(f32).init(x, math.copysign(f32, 0, (x + x) * y)); | |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | 80 | if (ix < 0x7f800000 and iy >= 0x7f800000) { |
| 81 | return Complex(f32).new(y - y, x * (y - y)); | |
| 81 | return Complex(f32).init(y - y, x * (y - y)); | |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | if (ix >= 0x7f800000 and (hx & 0x7fffff) == 0) { |
| 85 | 85 | if (iy >= 0x7f800000) { |
| 86 | return Complex(f32).new(x * x, x * (y - y)); | |
| 86 | return Complex(f32).init(x * x, x * (y - y)); | |
| 87 | 87 | } |
| 88 | return Complex(f32).new((x * x) * math.cos(y), x * math.sin(y)); | |
| 88 | return Complex(f32).init((x * x) * math.cos(y), x * math.sin(y)); | |
| 89 | 89 | } |
| 90 | 90 | |
| 91 | return Complex(f32).new((x * x) * (y - y), (x + x) * (y - y)); | |
| 91 | return Complex(f32).init((x * x) * (y - y), (x + x) * (y - y)); | |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | 94 | fn cosh64(z: Complex(f64)) Complex(f64) { |
| ... | ... | @@ -108,61 +108,61 @@ fn cosh64(z: Complex(f64)) Complex(f64) { |
| 108 | 108 | // nearly non-exceptional case where x, y are finite |
| 109 | 109 | if (ix < 0x7ff00000 and iy < 0x7ff00000) { |
| 110 | 110 | if (iy | ly == 0) { |
| 111 | return Complex(f64).new(math.cosh(x), x * y); | |
| 111 | return Complex(f64).init(math.cosh(x), x * y); | |
| 112 | 112 | } |
| 113 | 113 | // small x: normal case |
| 114 | 114 | if (ix < 0x40360000) { |
| 115 | return Complex(f64).new(math.cosh(x) * math.cos(y), math.sinh(x) * math.sin(y)); | |
| 115 | return Complex(f64).init(math.cosh(x) * math.cos(y), math.sinh(x) * math.sin(y)); | |
| 116 | 116 | } |
| 117 | 117 | |
| 118 | 118 | // |x|>= 22, so cosh(x) ~= exp(|x|) |
| 119 | 119 | if (ix < 0x40862e42) { |
| 120 | 120 | // x < 710: exp(|x|) won't overflow |
| 121 | 121 | const h = math.exp(math.fabs(x)) * 0.5; |
| 122 | return Complex(f64).new(h * math.cos(y), math.copysign(f64, h, x) * math.sin(y)); | |
| 122 | return Complex(f64).init(h * math.cos(y), math.copysign(f64, h, x) * math.sin(y)); | |
| 123 | 123 | } |
| 124 | 124 | // x < 1455: scale to avoid overflow |
| 125 | 125 | else if (ix < 0x4096bbaa) { |
| 126 | const v = Complex(f64).new(math.fabs(x), y); | |
| 126 | const v = Complex(f64).init(math.fabs(x), y); | |
| 127 | 127 | const r = ldexp_cexp(v, -1); |
| 128 | return Complex(f64).new(r.re, r.im * math.copysign(f64, 1, x)); | |
| 128 | return Complex(f64).init(r.re, r.im * math.copysign(f64, 1, x)); | |
| 129 | 129 | } |
| 130 | 130 | // x >= 1455: result always overflows |
| 131 | 131 | else { |
| 132 | 132 | const h = 0x1p1023; |
| 133 | return Complex(f64).new(h * h * math.cos(y), h * math.sin(y)); | |
| 133 | return Complex(f64).init(h * h * math.cos(y), h * math.sin(y)); | |
| 134 | 134 | } |
| 135 | 135 | } |
| 136 | 136 | |
| 137 | 137 | if (ix | lx == 0 and iy >= 0x7ff00000) { |
| 138 | return Complex(f64).new(y - y, math.copysign(f64, 0, x * (y - y))); | |
| 138 | return Complex(f64).init(y - y, math.copysign(f64, 0, x * (y - y))); | |
| 139 | 139 | } |
| 140 | 140 | |
| 141 | 141 | if (iy | ly == 0 and ix >= 0x7ff00000) { |
| 142 | 142 | if ((hx & 0xfffff) | lx == 0) { |
| 143 | return Complex(f64).new(x * x, math.copysign(f64, 0, x) * y); | |
| 143 | return Complex(f64).init(x * x, math.copysign(f64, 0, x) * y); | |
| 144 | 144 | } |
| 145 | return Complex(f64).new(x * x, math.copysign(f64, 0, (x + x) * y)); | |
| 145 | return Complex(f64).init(x * x, math.copysign(f64, 0, (x + x) * y)); | |
| 146 | 146 | } |
| 147 | 147 | |
| 148 | 148 | if (ix < 0x7ff00000 and iy >= 0x7ff00000) { |
| 149 | return Complex(f64).new(y - y, x * (y - y)); | |
| 149 | return Complex(f64).init(y - y, x * (y - y)); | |
| 150 | 150 | } |
| 151 | 151 | |
| 152 | 152 | if (ix >= 0x7ff00000 and (hx & 0xfffff) | lx == 0) { |
| 153 | 153 | if (iy >= 0x7ff00000) { |
| 154 | return Complex(f64).new(x * x, x * (y - y)); | |
| 154 | return Complex(f64).init(x * x, x * (y - y)); | |
| 155 | 155 | } |
| 156 | return Complex(f64).new(x * x * math.cos(y), x * math.sin(y)); | |
| 156 | return Complex(f64).init(x * x * math.cos(y), x * math.sin(y)); | |
| 157 | 157 | } |
| 158 | 158 | |
| 159 | return Complex(f64).new((x * x) * (y - y), (x + x) * (y - y)); | |
| 159 | return Complex(f64).init((x * x) * (y - y), (x + x) * (y - y)); | |
| 160 | 160 | } |
| 161 | 161 | |
| 162 | 162 | const epsilon = 0.0001; |
| 163 | 163 | |
| 164 | 164 | test "complex.ccosh32" { |
| 165 | const a = Complex(f32).new(5, 3); | |
| 165 | const a = Complex(f32).init(5, 3); | |
| 166 | 166 | const c = cosh(a); |
| 167 | 167 | |
| 168 | 168 | try testing.expect(math.approxEqAbs(f32, c.re, -73.467300, epsilon)); |
| ... | ... | @@ -170,7 +170,7 @@ test "complex.ccosh32" { |
| 170 | 170 | } |
| 171 | 171 | |
| 172 | 172 | test "complex.ccosh64" { |
| 173 | const a = Complex(f64).new(5, 3); | |
| 173 | const a = Complex(f64).init(5, 3); | |
| 174 | 174 | const c = cosh(a); |
| 175 | 175 | |
| 176 | 176 | try testing.expect(math.approxEqAbs(f64, c.re, -73.467300, epsilon)); |
lib/std/math/complex/exp.zig+14-14| ... | ... | @@ -39,25 +39,25 @@ fn exp32(z: Complex(f32)) Complex(f32) { |
| 39 | 39 | const hy = @bitCast(u32, y) & 0x7fffffff; |
| 40 | 40 | // cexp(x + i0) = exp(x) + i0 |
| 41 | 41 | if (hy == 0) { |
| 42 | return Complex(f32).new(math.exp(x), y); | |
| 42 | return Complex(f32).init(math.exp(x), y); | |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | 45 | const hx = @bitCast(u32, x); |
| 46 | 46 | // cexp(0 + iy) = cos(y) + isin(y) |
| 47 | 47 | if ((hx & 0x7fffffff) == 0) { |
| 48 | return Complex(f32).new(math.cos(y), math.sin(y)); | |
| 48 | return Complex(f32).init(math.cos(y), math.sin(y)); | |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | if (hy >= 0x7f800000) { |
| 52 | 52 | // cexp(finite|nan +- i inf|nan) = nan + i nan |
| 53 | 53 | if ((hx & 0x7fffffff) != 0x7f800000) { |
| 54 | return Complex(f32).new(y - y, y - y); | |
| 54 | return Complex(f32).init(y - y, y - y); | |
| 55 | 55 | } // cexp(-inf +- i inf|nan) = 0 + i0 |
| 56 | 56 | else if (hx & 0x80000000 != 0) { |
| 57 | return Complex(f32).new(0, 0); | |
| 57 | return Complex(f32).init(0, 0); | |
| 58 | 58 | } // cexp(+inf +- i inf|nan) = inf + i nan |
| 59 | 59 | else { |
| 60 | return Complex(f32).new(x, y - y); | |
| 60 | return Complex(f32).init(x, y - y); | |
| 61 | 61 | } |
| 62 | 62 | } |
| 63 | 63 | |
| ... | ... | @@ -70,7 +70,7 @@ fn exp32(z: Complex(f32)) Complex(f32) { |
| 70 | 70 | // - x = nan |
| 71 | 71 | else { |
| 72 | 72 | const exp_x = math.exp(x); |
| 73 | return Complex(f32).new(exp_x * math.cos(y), exp_x * math.sin(y)); | |
| 73 | return Complex(f32).init(exp_x * math.cos(y), exp_x * math.sin(y)); | |
| 74 | 74 | } |
| 75 | 75 | } |
| 76 | 76 | |
| ... | ... | @@ -87,7 +87,7 @@ fn exp64(z: Complex(f64)) Complex(f64) { |
| 87 | 87 | |
| 88 | 88 | // cexp(x + i0) = exp(x) + i0 |
| 89 | 89 | if (hy | ly == 0) { |
| 90 | return Complex(f64).new(math.exp(x), y); | |
| 90 | return Complex(f64).init(math.exp(x), y); | |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 93 | const fx = @bitCast(u64, x); |
| ... | ... | @@ -96,19 +96,19 @@ fn exp64(z: Complex(f64)) Complex(f64) { |
| 96 | 96 | |
| 97 | 97 | // cexp(0 + iy) = cos(y) + isin(y) |
| 98 | 98 | if ((hx & 0x7fffffff) | lx == 0) { |
| 99 | return Complex(f64).new(math.cos(y), math.sin(y)); | |
| 99 | return Complex(f64).init(math.cos(y), math.sin(y)); | |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | if (hy >= 0x7ff00000) { |
| 103 | 103 | // cexp(finite|nan +- i inf|nan) = nan + i nan |
| 104 | 104 | if (lx != 0 or (hx & 0x7fffffff) != 0x7ff00000) { |
| 105 | return Complex(f64).new(y - y, y - y); | |
| 105 | return Complex(f64).init(y - y, y - y); | |
| 106 | 106 | } // cexp(-inf +- i inf|nan) = 0 + i0 |
| 107 | 107 | else if (hx & 0x80000000 != 0) { |
| 108 | return Complex(f64).new(0, 0); | |
| 108 | return Complex(f64).init(0, 0); | |
| 109 | 109 | } // cexp(+inf +- i inf|nan) = inf + i nan |
| 110 | 110 | else { |
| 111 | return Complex(f64).new(x, y - y); | |
| 111 | return Complex(f64).init(x, y - y); | |
| 112 | 112 | } |
| 113 | 113 | } |
| 114 | 114 | |
| ... | ... | @@ -121,14 +121,14 @@ fn exp64(z: Complex(f64)) Complex(f64) { |
| 121 | 121 | // - x = nan |
| 122 | 122 | else { |
| 123 | 123 | const exp_x = math.exp(x); |
| 124 | return Complex(f64).new(exp_x * math.cos(y), exp_x * math.sin(y)); | |
| 124 | return Complex(f64).init(exp_x * math.cos(y), exp_x * math.sin(y)); | |
| 125 | 125 | } |
| 126 | 126 | } |
| 127 | 127 | |
| 128 | 128 | const epsilon = 0.0001; |
| 129 | 129 | |
| 130 | 130 | test "complex.cexp32" { |
| 131 | const a = Complex(f32).new(5, 3); | |
| 131 | const a = Complex(f32).init(5, 3); | |
| 132 | 132 | const c = exp(a); |
| 133 | 133 | |
| 134 | 134 | try testing.expect(math.approxEqAbs(f32, c.re, -146.927917, epsilon)); |
| ... | ... | @@ -136,7 +136,7 @@ test "complex.cexp32" { |
| 136 | 136 | } |
| 137 | 137 | |
| 138 | 138 | test "complex.cexp64" { |
| 139 | const a = Complex(f64).new(5, 3); | |
| 139 | const a = Complex(f64).init(5, 3); | |
| 140 | 140 | const c = exp(a); |
| 141 | 141 | |
| 142 | 142 | try testing.expect(math.approxEqAbs(f64, c.re, -146.927917, epsilon)); |
lib/std/math/complex/ldexp.zig+2-2| ... | ... | @@ -48,7 +48,7 @@ fn ldexp_cexp32(z: Complex(f32), expt: i32) Complex(f32) { |
| 48 | 48 | const half_expt2 = exptf - half_expt1; |
| 49 | 49 | const scale2 = @bitCast(f32, (0x7f + half_expt2) << 23); |
| 50 | 50 | |
| 51 | return Complex(f32).new(math.cos(z.im) * exp_x * scale1 * scale2, math.sin(z.im) * exp_x * scale1 * scale2); | |
| 51 | return Complex(f32).init(math.cos(z.im) * exp_x * scale1 * scale2, math.sin(z.im) * exp_x * scale1 * scale2); | |
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | fn frexp_exp64(x: f64, expt: *i32) f64 { |
| ... | ... | @@ -78,7 +78,7 @@ fn ldexp_cexp64(z: Complex(f64), expt: i32) Complex(f64) { |
| 78 | 78 | const half_expt2 = exptf - half_expt1; |
| 79 | 79 | const scale2 = @bitCast(f64, (0x3ff + half_expt2) << 20); |
| 80 | 80 | |
| 81 | return Complex(f64).new( | |
| 81 | return Complex(f64).init( | |
| 82 | 82 | math.cos(z.im) * exp_x * scale1 * scale2, |
| 83 | 83 | math.sin(z.im) * exp_x * scale1 * scale2, |
| 84 | 84 | ); |
lib/std/math/complex/log.zig+2-2| ... | ... | @@ -15,13 +15,13 @@ pub fn log(z: anytype) Complex(@TypeOf(z.re)) { |
| 15 | 15 | const r = cmath.abs(z); |
| 16 | 16 | const phi = cmath.arg(z); |
| 17 | 17 | |
| 18 | return Complex(T).new(math.ln(r), phi); | |
| 18 | return Complex(T).init(math.ln(r), phi); | |
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 21 | const epsilon = 0.0001; |
| 22 | 22 | |
| 23 | 23 | test "complex.clog" { |
| 24 | const a = Complex(f32).new(5, 3); | |
| 24 | const a = Complex(f32).init(5, 3); | |
| 25 | 25 | const c = log(a); |
| 26 | 26 | |
| 27 | 27 | try testing.expect(math.approxEqAbs(f32, c.re, 1.763180, epsilon)); |
lib/std/math/complex/pow.zig+2-2| ... | ... | @@ -19,8 +19,8 @@ pub fn pow(comptime T: type, z: T, c: T) T { |
| 19 | 19 | const epsilon = 0.0001; |
| 20 | 20 | |
| 21 | 21 | test "complex.cpow" { |
| 22 | const a = Complex(f32).new(5, 3); | |
| 23 | const b = Complex(f32).new(2.3, -1.3); | |
| 22 | const a = Complex(f32).init(5, 3); | |
| 23 | const b = Complex(f32).init(2.3, -1.3); | |
| 24 | 24 | const c = pow(Complex(f32), a, b); |
| 25 | 25 | |
| 26 | 26 | try testing.expect(math.approxEqAbs(f32, c.re, 58.049110, epsilon)); |
lib/std/math/complex/proj.zig+3-3| ... | ... | @@ -14,16 +14,16 @@ pub fn proj(z: anytype) Complex(@TypeOf(z.re)) { |
| 14 | 14 | const T = @TypeOf(z.re); |
| 15 | 15 | |
| 16 | 16 | if (math.isInf(z.re) or math.isInf(z.im)) { |
| 17 | return Complex(T).new(math.inf(T), math.copysign(T, 0, z.re)); | |
| 17 | return Complex(T).init(math.inf(T), math.copysign(T, 0, z.re)); | |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | return Complex(T).new(z.re, z.im); | |
| 20 | return Complex(T).init(z.re, z.im); | |
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | const epsilon = 0.0001; |
| 24 | 24 | |
| 25 | 25 | test "complex.cproj" { |
| 26 | const a = Complex(f32).new(5, 3); | |
| 26 | const a = Complex(f32).init(5, 3); | |
| 27 | 27 | const c = proj(a); |
| 28 | 28 | |
| 29 | 29 | try testing.expect(c.re == 5 and c.im == 3); |
lib/std/math/complex/sin.zig+3-3| ... | ... | @@ -12,15 +12,15 @@ const Complex = cmath.Complex; |
| 12 | 12 | /// Returns the sine of z. |
| 13 | 13 | pub fn sin(z: anytype) Complex(@TypeOf(z.re)) { |
| 14 | 14 | const T = @TypeOf(z.re); |
| 15 | const p = Complex(T).new(-z.im, z.re); | |
| 15 | const p = Complex(T).init(-z.im, z.re); | |
| 16 | 16 | const q = cmath.sinh(p); |
| 17 | return Complex(T).new(q.im, -q.re); | |
| 17 | return Complex(T).init(q.im, -q.re); | |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | const epsilon = 0.0001; |
| 21 | 21 | |
| 22 | 22 | test "complex.csin" { |
| 23 | const a = Complex(f32).new(5, 3); | |
| 23 | const a = Complex(f32).init(5, 3); | |
| 24 | 24 | const c = sin(a); |
| 25 | 25 | |
| 26 | 26 | try testing.expect(math.approxEqAbs(f32, c.re, -9.654126, epsilon)); |
lib/std/math/complex/sinh.zig+28-28| ... | ... | @@ -40,55 +40,55 @@ fn sinh32(z: Complex(f32)) Complex(f32) { |
| 40 | 40 | |
| 41 | 41 | if (ix < 0x7f800000 and iy < 0x7f800000) { |
| 42 | 42 | if (iy == 0) { |
| 43 | return Complex(f32).new(math.sinh(x), y); | |
| 43 | return Complex(f32).init(math.sinh(x), y); | |
| 44 | 44 | } |
| 45 | 45 | // small x: normal case |
| 46 | 46 | if (ix < 0x41100000) { |
| 47 | return Complex(f32).new(math.sinh(x) * math.cos(y), math.cosh(x) * math.sin(y)); | |
| 47 | return Complex(f32).init(math.sinh(x) * math.cos(y), math.cosh(x) * math.sin(y)); | |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | // |x|>= 9, so cosh(x) ~= exp(|x|) |
| 51 | 51 | if (ix < 0x42b17218) { |
| 52 | 52 | // x < 88.7: exp(|x|) won't overflow |
| 53 | 53 | const h = math.exp(math.fabs(x)) * 0.5; |
| 54 | return Complex(f32).new(math.copysign(f32, h, x) * math.cos(y), h * math.sin(y)); | |
| 54 | return Complex(f32).init(math.copysign(f32, h, x) * math.cos(y), h * math.sin(y)); | |
| 55 | 55 | } |
| 56 | 56 | // x < 192.7: scale to avoid overflow |
| 57 | 57 | else if (ix < 0x4340b1e7) { |
| 58 | const v = Complex(f32).new(math.fabs(x), y); | |
| 58 | const v = Complex(f32).init(math.fabs(x), y); | |
| 59 | 59 | const r = ldexp_cexp(v, -1); |
| 60 | return Complex(f32).new(r.re * math.copysign(f32, 1, x), r.im); | |
| 60 | return Complex(f32).init(r.re * math.copysign(f32, 1, x), r.im); | |
| 61 | 61 | } |
| 62 | 62 | // x >= 192.7: result always overflows |
| 63 | 63 | else { |
| 64 | 64 | const h = 0x1p127 * x; |
| 65 | return Complex(f32).new(h * math.cos(y), h * h * math.sin(y)); | |
| 65 | return Complex(f32).init(h * math.cos(y), h * h * math.sin(y)); | |
| 66 | 66 | } |
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | if (ix == 0 and iy >= 0x7f800000) { |
| 70 | return Complex(f32).new(math.copysign(f32, 0, x * (y - y)), y - y); | |
| 70 | return Complex(f32).init(math.copysign(f32, 0, x * (y - y)), y - y); | |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | if (iy == 0 and ix >= 0x7f800000) { |
| 74 | 74 | if (hx & 0x7fffff == 0) { |
| 75 | return Complex(f32).new(x, y); | |
| 75 | return Complex(f32).init(x, y); | |
| 76 | 76 | } |
| 77 | return Complex(f32).new(x, math.copysign(f32, 0, y)); | |
| 77 | return Complex(f32).init(x, math.copysign(f32, 0, y)); | |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | 80 | if (ix < 0x7f800000 and iy >= 0x7f800000) { |
| 81 | return Complex(f32).new(y - y, x * (y - y)); | |
| 81 | return Complex(f32).init(y - y, x * (y - y)); | |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | if (ix >= 0x7f800000 and (hx & 0x7fffff) == 0) { |
| 85 | 85 | if (iy >= 0x7f800000) { |
| 86 | return Complex(f32).new(x * x, x * (y - y)); | |
| 86 | return Complex(f32).init(x * x, x * (y - y)); | |
| 87 | 87 | } |
| 88 | return Complex(f32).new(x * math.cos(y), math.inf_f32 * math.sin(y)); | |
| 88 | return Complex(f32).init(x * math.cos(y), math.inf_f32 * math.sin(y)); | |
| 89 | 89 | } |
| 90 | 90 | |
| 91 | return Complex(f32).new((x * x) * (y - y), (x + x) * (y - y)); | |
| 91 | return Complex(f32).init((x * x) * (y - y), (x + x) * (y - y)); | |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | 94 | fn sinh64(z: Complex(f64)) Complex(f64) { |
| ... | ... | @@ -107,61 +107,61 @@ fn sinh64(z: Complex(f64)) Complex(f64) { |
| 107 | 107 | |
| 108 | 108 | if (ix < 0x7ff00000 and iy < 0x7ff00000) { |
| 109 | 109 | if (iy | ly == 0) { |
| 110 | return Complex(f64).new(math.sinh(x), y); | |
| 110 | return Complex(f64).init(math.sinh(x), y); | |
| 111 | 111 | } |
| 112 | 112 | // small x: normal case |
| 113 | 113 | if (ix < 0x40360000) { |
| 114 | return Complex(f64).new(math.sinh(x) * math.cos(y), math.cosh(x) * math.sin(y)); | |
| 114 | return Complex(f64).init(math.sinh(x) * math.cos(y), math.cosh(x) * math.sin(y)); | |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | 117 | // |x|>= 22, so cosh(x) ~= exp(|x|) |
| 118 | 118 | if (ix < 0x40862e42) { |
| 119 | 119 | // x < 710: exp(|x|) won't overflow |
| 120 | 120 | const h = math.exp(math.fabs(x)) * 0.5; |
| 121 | return Complex(f64).new(math.copysign(f64, h, x) * math.cos(y), h * math.sin(y)); | |
| 121 | return Complex(f64).init(math.copysign(f64, h, x) * math.cos(y), h * math.sin(y)); | |
| 122 | 122 | } |
| 123 | 123 | // x < 1455: scale to avoid overflow |
| 124 | 124 | else if (ix < 0x4096bbaa) { |
| 125 | const v = Complex(f64).new(math.fabs(x), y); | |
| 125 | const v = Complex(f64).init(math.fabs(x), y); | |
| 126 | 126 | const r = ldexp_cexp(v, -1); |
| 127 | return Complex(f64).new(r.re * math.copysign(f64, 1, x), r.im); | |
| 127 | return Complex(f64).init(r.re * math.copysign(f64, 1, x), r.im); | |
| 128 | 128 | } |
| 129 | 129 | // x >= 1455: result always overflows |
| 130 | 130 | else { |
| 131 | 131 | const h = 0x1p1023 * x; |
| 132 | return Complex(f64).new(h * math.cos(y), h * h * math.sin(y)); | |
| 132 | return Complex(f64).init(h * math.cos(y), h * h * math.sin(y)); | |
| 133 | 133 | } |
| 134 | 134 | } |
| 135 | 135 | |
| 136 | 136 | if (ix | lx == 0 and iy >= 0x7ff00000) { |
| 137 | return Complex(f64).new(math.copysign(f64, 0, x * (y - y)), y - y); | |
| 137 | return Complex(f64).init(math.copysign(f64, 0, x * (y - y)), y - y); | |
| 138 | 138 | } |
| 139 | 139 | |
| 140 | 140 | if (iy | ly == 0 and ix >= 0x7ff00000) { |
| 141 | 141 | if ((hx & 0xfffff) | lx == 0) { |
| 142 | return Complex(f64).new(x, y); | |
| 142 | return Complex(f64).init(x, y); | |
| 143 | 143 | } |
| 144 | return Complex(f64).new(x, math.copysign(f64, 0, y)); | |
| 144 | return Complex(f64).init(x, math.copysign(f64, 0, y)); | |
| 145 | 145 | } |
| 146 | 146 | |
| 147 | 147 | if (ix < 0x7ff00000 and iy >= 0x7ff00000) { |
| 148 | return Complex(f64).new(y - y, x * (y - y)); | |
| 148 | return Complex(f64).init(y - y, x * (y - y)); | |
| 149 | 149 | } |
| 150 | 150 | |
| 151 | 151 | if (ix >= 0x7ff00000 and (hx & 0xfffff) | lx == 0) { |
| 152 | 152 | if (iy >= 0x7ff00000) { |
| 153 | return Complex(f64).new(x * x, x * (y - y)); | |
| 153 | return Complex(f64).init(x * x, x * (y - y)); | |
| 154 | 154 | } |
| 155 | return Complex(f64).new(x * math.cos(y), math.inf_f64 * math.sin(y)); | |
| 155 | return Complex(f64).init(x * math.cos(y), math.inf_f64 * math.sin(y)); | |
| 156 | 156 | } |
| 157 | 157 | |
| 158 | return Complex(f64).new((x * x) * (y - y), (x + x) * (y - y)); | |
| 158 | return Complex(f64).init((x * x) * (y - y), (x + x) * (y - y)); | |
| 159 | 159 | } |
| 160 | 160 | |
| 161 | 161 | const epsilon = 0.0001; |
| 162 | 162 | |
| 163 | 163 | test "complex.csinh32" { |
| 164 | const a = Complex(f32).new(5, 3); | |
| 164 | const a = Complex(f32).init(5, 3); | |
| 165 | 165 | const c = sinh(a); |
| 166 | 166 | |
| 167 | 167 | try testing.expect(math.approxEqAbs(f32, c.re, -73.460617, epsilon)); |
| ... | ... | @@ -169,7 +169,7 @@ test "complex.csinh32" { |
| 169 | 169 | } |
| 170 | 170 | |
| 171 | 171 | test "complex.csinh64" { |
| 172 | const a = Complex(f64).new(5, 3); | |
| 172 | const a = Complex(f64).init(5, 3); | |
| 173 | 173 | const c = sinh(a); |
| 174 | 174 | |
| 175 | 175 | try testing.expect(math.approxEqAbs(f64, c.re, -73.460617, epsilon)); |
lib/std/math/complex/sqrt.zig+16-16| ... | ... | @@ -32,15 +32,15 @@ fn sqrt32(z: Complex(f32)) Complex(f32) { |
| 32 | 32 | const y = z.im; |
| 33 | 33 | |
| 34 | 34 | if (x == 0 and y == 0) { |
| 35 | return Complex(f32).new(0, y); | |
| 35 | return Complex(f32).init(0, y); | |
| 36 | 36 | } |
| 37 | 37 | if (math.isInf(y)) { |
| 38 | return Complex(f32).new(math.inf(f32), y); | |
| 38 | return Complex(f32).init(math.inf(f32), y); | |
| 39 | 39 | } |
| 40 | 40 | if (math.isNan(x)) { |
| 41 | 41 | // raise invalid if y is not nan |
| 42 | 42 | const t = (y - y) / (y - y); |
| 43 | return Complex(f32).new(x, t); | |
| 43 | return Complex(f32).init(x, t); | |
| 44 | 44 | } |
| 45 | 45 | if (math.isInf(x)) { |
| 46 | 46 | // sqrt(inf + i nan) = inf + nan i |
| ... | ... | @@ -48,9 +48,9 @@ fn sqrt32(z: Complex(f32)) Complex(f32) { |
| 48 | 48 | // sqrt(-inf + i nan) = nan +- inf i |
| 49 | 49 | // sqrt(-inf + iy) = 0 + inf i |
| 50 | 50 | if (math.signbit(x)) { |
| 51 | return Complex(f32).new(math.fabs(x - y), math.copysign(f32, x, y)); | |
| 51 | return Complex(f32).init(math.fabs(x - y), math.copysign(f32, x, y)); | |
| 52 | 52 | } else { |
| 53 | return Complex(f32).new(x, math.copysign(f32, y - y, y)); | |
| 53 | return Complex(f32).init(x, math.copysign(f32, y - y, y)); | |
| 54 | 54 | } |
| 55 | 55 | } |
| 56 | 56 | |
| ... | ... | @@ -62,13 +62,13 @@ fn sqrt32(z: Complex(f32)) Complex(f32) { |
| 62 | 62 | |
| 63 | 63 | if (dx >= 0) { |
| 64 | 64 | const t = math.sqrt((dx + math.hypot(f64, dx, dy)) * 0.5); |
| 65 | return Complex(f32).new( | |
| 65 | return Complex(f32).init( | |
| 66 | 66 | @floatCast(f32, t), |
| 67 | 67 | @floatCast(f32, dy / (2.0 * t)), |
| 68 | 68 | ); |
| 69 | 69 | } else { |
| 70 | 70 | const t = math.sqrt((-dx + math.hypot(f64, dx, dy)) * 0.5); |
| 71 | return Complex(f32).new( | |
| 71 | return Complex(f32).init( | |
| 72 | 72 | @floatCast(f32, math.fabs(y) / (2.0 * t)), |
| 73 | 73 | @floatCast(f32, math.copysign(f64, t, y)), |
| 74 | 74 | ); |
| ... | ... | @@ -83,15 +83,15 @@ fn sqrt64(z: Complex(f64)) Complex(f64) { |
| 83 | 83 | var y = z.im; |
| 84 | 84 | |
| 85 | 85 | if (x == 0 and y == 0) { |
| 86 | return Complex(f64).new(0, y); | |
| 86 | return Complex(f64).init(0, y); | |
| 87 | 87 | } |
| 88 | 88 | if (math.isInf(y)) { |
| 89 | return Complex(f64).new(math.inf(f64), y); | |
| 89 | return Complex(f64).init(math.inf(f64), y); | |
| 90 | 90 | } |
| 91 | 91 | if (math.isNan(x)) { |
| 92 | 92 | // raise invalid if y is not nan |
| 93 | 93 | const t = (y - y) / (y - y); |
| 94 | return Complex(f64).new(x, t); | |
| 94 | return Complex(f64).init(x, t); | |
| 95 | 95 | } |
| 96 | 96 | if (math.isInf(x)) { |
| 97 | 97 | // sqrt(inf + i nan) = inf + nan i |
| ... | ... | @@ -99,9 +99,9 @@ fn sqrt64(z: Complex(f64)) Complex(f64) { |
| 99 | 99 | // sqrt(-inf + i nan) = nan +- inf i |
| 100 | 100 | // sqrt(-inf + iy) = 0 + inf i |
| 101 | 101 | if (math.signbit(x)) { |
| 102 | return Complex(f64).new(math.fabs(x - y), math.copysign(f64, x, y)); | |
| 102 | return Complex(f64).init(math.fabs(x - y), math.copysign(f64, x, y)); | |
| 103 | 103 | } else { |
| 104 | return Complex(f64).new(x, math.copysign(f64, y - y, y)); | |
| 104 | return Complex(f64).init(x, math.copysign(f64, y - y, y)); | |
| 105 | 105 | } |
| 106 | 106 | } |
| 107 | 107 | |
| ... | ... | @@ -118,10 +118,10 @@ fn sqrt64(z: Complex(f64)) Complex(f64) { |
| 118 | 118 | var result: Complex(f64) = undefined; |
| 119 | 119 | if (x >= 0) { |
| 120 | 120 | const t = math.sqrt((x + math.hypot(f64, x, y)) * 0.5); |
| 121 | result = Complex(f64).new(t, y / (2.0 * t)); | |
| 121 | result = Complex(f64).init(t, y / (2.0 * t)); | |
| 122 | 122 | } else { |
| 123 | 123 | const t = math.sqrt((-x + math.hypot(f64, x, y)) * 0.5); |
| 124 | result = Complex(f64).new(math.fabs(y) / (2.0 * t), math.copysign(f64, t, y)); | |
| 124 | result = Complex(f64).init(math.fabs(y) / (2.0 * t), math.copysign(f64, t, y)); | |
| 125 | 125 | } |
| 126 | 126 | |
| 127 | 127 | if (scale) { |
| ... | ... | @@ -135,7 +135,7 @@ fn sqrt64(z: Complex(f64)) Complex(f64) { |
| 135 | 135 | const epsilon = 0.0001; |
| 136 | 136 | |
| 137 | 137 | test "complex.csqrt32" { |
| 138 | const a = Complex(f32).new(5, 3); | |
| 138 | const a = Complex(f32).init(5, 3); | |
| 139 | 139 | const c = sqrt(a); |
| 140 | 140 | |
| 141 | 141 | try testing.expect(math.approxEqAbs(f32, c.re, 2.327117, epsilon)); |
| ... | ... | @@ -143,7 +143,7 @@ test "complex.csqrt32" { |
| 143 | 143 | } |
| 144 | 144 | |
| 145 | 145 | test "complex.csqrt64" { |
| 146 | const a = Complex(f64).new(5, 3); | |
| 146 | const a = Complex(f64).init(5, 3); | |
| 147 | 147 | const c = sqrt(a); |
| 148 | 148 | |
| 149 | 149 | try testing.expect(math.approxEqAbs(f64, c.re, 2.3271175190399496, epsilon)); |
lib/std/math/complex/tan.zig+3-3| ... | ... | @@ -12,15 +12,15 @@ const Complex = cmath.Complex; |
| 12 | 12 | /// Returns the tanget of z. |
| 13 | 13 | pub fn tan(z: anytype) Complex(@TypeOf(z.re)) { |
| 14 | 14 | const T = @TypeOf(z.re); |
| 15 | const q = Complex(T).new(-z.im, z.re); | |
| 15 | const q = Complex(T).init(-z.im, z.re); | |
| 16 | 16 | const r = cmath.tanh(q); |
| 17 | return Complex(T).new(r.im, -r.re); | |
| 17 | return Complex(T).init(r.im, -r.re); | |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | const epsilon = 0.0001; |
| 21 | 21 | |
| 22 | 22 | test "complex.ctan" { |
| 23 | const a = Complex(f32).new(5, 3); | |
| 23 | const a = Complex(f32).init(5, 3); | |
| 24 | 24 | const c = tan(a); |
| 25 | 25 | |
| 26 | 26 | try testing.expect(math.approxEqAbs(f32, c.re, -0.002708233, epsilon)); |
lib/std/math/complex/tanh.zig+12-12| ... | ... | @@ -36,22 +36,22 @@ fn tanh32(z: Complex(f32)) Complex(f32) { |
| 36 | 36 | if (ix >= 0x7f800000) { |
| 37 | 37 | if (ix & 0x7fffff != 0) { |
| 38 | 38 | const r = if (y == 0) y else x * y; |
| 39 | return Complex(f32).new(x, r); | |
| 39 | return Complex(f32).init(x, r); | |
| 40 | 40 | } |
| 41 | 41 | const xx = @bitCast(f32, hx - 0x40000000); |
| 42 | 42 | const r = if (math.isInf(y)) y else math.sin(y) * math.cos(y); |
| 43 | return Complex(f32).new(xx, math.copysign(f32, 0, r)); | |
| 43 | return Complex(f32).init(xx, math.copysign(f32, 0, r)); | |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | if (!math.isFinite(y)) { |
| 47 | 47 | const r = if (ix != 0) y - y else x; |
| 48 | return Complex(f32).new(r, y - y); | |
| 48 | return Complex(f32).init(r, y - y); | |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | // x >= 11 |
| 52 | 52 | if (ix >= 0x41300000) { |
| 53 | 53 | const exp_mx = math.exp(-math.fabs(x)); |
| 54 | return Complex(f32).new(math.copysign(f32, 1, x), 4 * math.sin(y) * math.cos(y) * exp_mx * exp_mx); | |
| 54 | return Complex(f32).init(math.copysign(f32, 1, x), 4 * math.sin(y) * math.cos(y) * exp_mx * exp_mx); | |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | // Kahan's algorithm |
| ... | ... | @@ -61,7 +61,7 @@ fn tanh32(z: Complex(f32)) Complex(f32) { |
| 61 | 61 | const rho = math.sqrt(1 + s * s); |
| 62 | 62 | const den = 1 + beta * s * s; |
| 63 | 63 | |
| 64 | return Complex(f32).new((beta * rho * s) / den, t / den); | |
| 64 | return Complex(f32).init((beta * rho * s) / den, t / den); | |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | 67 | fn tanh64(z: Complex(f64)) Complex(f64) { |
| ... | ... | @@ -78,23 +78,23 @@ fn tanh64(z: Complex(f64)) Complex(f64) { |
| 78 | 78 | if (ix >= 0x7ff00000) { |
| 79 | 79 | if ((ix & 0x7fffff) | lx != 0) { |
| 80 | 80 | const r = if (y == 0) y else x * y; |
| 81 | return Complex(f64).new(x, r); | |
| 81 | return Complex(f64).init(x, r); | |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | const xx = @bitCast(f64, (@as(u64, hx - 0x40000000) << 32) | lx); |
| 85 | 85 | const r = if (math.isInf(y)) y else math.sin(y) * math.cos(y); |
| 86 | return Complex(f64).new(xx, math.copysign(f64, 0, r)); | |
| 86 | return Complex(f64).init(xx, math.copysign(f64, 0, r)); | |
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | if (!math.isFinite(y)) { |
| 90 | 90 | const r = if (ix != 0) y - y else x; |
| 91 | return Complex(f64).new(r, y - y); | |
| 91 | return Complex(f64).init(r, y - y); | |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | 94 | // x >= 22 |
| 95 | 95 | if (ix >= 0x40360000) { |
| 96 | 96 | const exp_mx = math.exp(-math.fabs(x)); |
| 97 | return Complex(f64).new(math.copysign(f64, 1, x), 4 * math.sin(y) * math.cos(y) * exp_mx * exp_mx); | |
| 97 | return Complex(f64).init(math.copysign(f64, 1, x), 4 * math.sin(y) * math.cos(y) * exp_mx * exp_mx); | |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | 100 | // Kahan's algorithm |
| ... | ... | @@ -104,13 +104,13 @@ fn tanh64(z: Complex(f64)) Complex(f64) { |
| 104 | 104 | const rho = math.sqrt(1 + s * s); |
| 105 | 105 | const den = 1 + beta * s * s; |
| 106 | 106 | |
| 107 | return Complex(f64).new((beta * rho * s) / den, t / den); | |
| 107 | return Complex(f64).init((beta * rho * s) / den, t / den); | |
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | const epsilon = 0.0001; |
| 111 | 111 | |
| 112 | 112 | test "complex.ctanh32" { |
| 113 | const a = Complex(f32).new(5, 3); | |
| 113 | const a = Complex(f32).init(5, 3); | |
| 114 | 114 | const c = tanh(a); |
| 115 | 115 | |
| 116 | 116 | try testing.expect(math.approxEqAbs(f32, c.re, 0.999913, epsilon)); |
| ... | ... | @@ -118,7 +118,7 @@ test "complex.ctanh32" { |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | test "complex.ctanh64" { |
| 121 | const a = Complex(f64).new(5, 3); | |
| 121 | const a = Complex(f64).init(5, 3); | |
| 122 | 122 | const c = tanh(a); |
| 123 | 123 | |
| 124 | 124 | try testing.expect(math.approxEqAbs(f64, c.re, 0.999913, epsilon)); |
lib/std/zig/parser_test.zig+6-6| ... | ... | @@ -1608,13 +1608,13 @@ test "zig fmt: if-else with comment before else" { |
| 1608 | 1608 | \\comptime { |
| 1609 | 1609 | \\ // cexp(finite|nan +- i inf|nan) = nan + i nan |
| 1610 | 1610 | \\ if ((hx & 0x7fffffff) != 0x7f800000) { |
| 1611 | \\ return Complex(f32).new(y - y, y - y); | |
| 1611 | \\ return Complex(f32).init(y - y, y - y); | |
| 1612 | 1612 | \\ } // cexp(-inf +- i inf|nan) = 0 + i0 |
| 1613 | 1613 | \\ else if (hx & 0x80000000 != 0) { |
| 1614 | \\ return Complex(f32).new(0, 0); | |
| 1614 | \\ return Complex(f32).init(0, 0); | |
| 1615 | 1615 | \\ } // cexp(+inf +- i inf|nan) = inf + i nan |
| 1616 | 1616 | \\ else { |
| 1617 | \\ return Complex(f32).new(x, y - y); | |
| 1617 | \\ return Complex(f32).init(x, y - y); | |
| 1618 | 1618 | \\ } |
| 1619 | 1619 | \\} |
| 1620 | 1620 | \\ |
| ... | ... | @@ -2267,16 +2267,16 @@ test "zig fmt: line comment between if block and else keyword" { |
| 2267 | 2267 | \\test "aoeu" { |
| 2268 | 2268 | \\ // cexp(finite|nan +- i inf|nan) = nan + i nan |
| 2269 | 2269 | \\ if ((hx & 0x7fffffff) != 0x7f800000) { |
| 2270 | \\ return Complex(f32).new(y - y, y - y); | |
| 2270 | \\ return Complex(f32).init(y - y, y - y); | |
| 2271 | 2271 | \\ } |
| 2272 | 2272 | \\ // cexp(-inf +- i inf|nan) = 0 + i0 |
| 2273 | 2273 | \\ else if (hx & 0x80000000 != 0) { |
| 2274 | \\ return Complex(f32).new(0, 0); | |
| 2274 | \\ return Complex(f32).init(0, 0); | |
| 2275 | 2275 | \\ } |
| 2276 | 2276 | \\ // cexp(+inf +- i inf|nan) = inf + i nan |
| 2277 | 2277 | \\ // another comment |
| 2278 | 2278 | \\ else { |
| 2279 | \\ return Complex(f32).new(x, y - y); | |
| 2279 | \\ return Complex(f32).init(x, y - y); | |
| 2280 | 2280 | \\ } |
| 2281 | 2281 | \\} |
| 2282 | 2282 | \\ |