authorgravatar for 78422626+aizawey672@users.noreply.github.comaiz <78422626+aizawey672@users.noreply.github.com> 2021-05-18 02:57:51+07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-05-17 21:57:51+02:00
log5414bd48edd460ae8667c811e13aa9b5d9fab919
treee87302fc0d1c53518474ef704ec9e64e9e49424c
parent04d95ea4192a7f70e7c11b8ee67d237cf38da9b7
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

std.math.Complex: Change `new()` to `init()`


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,9 +38,12 @@ pub fn Complex(comptime T: type) type {
3838
39 /// Imaginary part.39 /// Imaginary part.
40 im: T,40 im: T,
41
42 /// Deprecated, use init()
43 pub const new = init;
4144
42 /// Create a new Complex number from the given real and imaginary parts.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 return Self{47 return Self{
45 .re = re,48 .re = re,
46 .im = im,49 .im = im,
...@@ -110,32 +113,32 @@ pub fn Complex(comptime T: type) type {...@@ -110,32 +113,32 @@ pub fn Complex(comptime T: type) type {
110const epsilon = 0.0001;113const epsilon = 0.0001;
111114
112test "complex.add" {115test "complex.add" {
113 const a = Complex(f32).new(5, 3);116 const a = Complex(f32).init(5, 3);
114 const b = Complex(f32).new(2, 7);117 const b = Complex(f32).init(2, 7);
115 const c = a.add(b);118 const c = a.add(b);
116119
117 try testing.expect(c.re == 7 and c.im == 10);120 try testing.expect(c.re == 7 and c.im == 10);
118}121}
119122
120test "complex.sub" {123test "complex.sub" {
121 const a = Complex(f32).new(5, 3);124 const a = Complex(f32).init(5, 3);
122 const b = Complex(f32).new(2, 7);125 const b = Complex(f32).init(2, 7);
123 const c = a.sub(b);126 const c = a.sub(b);
124127
125 try testing.expect(c.re == 3 and c.im == -4);128 try testing.expect(c.re == 3 and c.im == -4);
126}129}
127130
128test "complex.mul" {131test "complex.mul" {
129 const a = Complex(f32).new(5, 3);132 const a = Complex(f32).init(5, 3);
130 const b = Complex(f32).new(2, 7);133 const b = Complex(f32).init(2, 7);
131 const c = a.mul(b);134 const c = a.mul(b);
132135
133 try testing.expect(c.re == -11 and c.im == 41);136 try testing.expect(c.re == -11 and c.im == 41);
134}137}
135138
136test "complex.div" {139test "complex.div" {
137 const a = Complex(f32).new(5, 3);140 const a = Complex(f32).init(5, 3);
138 const b = Complex(f32).new(2, 7);141 const b = Complex(f32).init(2, 7);
139 const c = a.div(b);142 const c = a.div(b);
140143
141 try testing.expect(math.approxEqAbs(f32, c.re, @as(f32, 31) / 53, epsilon) and144 try testing.expect(math.approxEqAbs(f32, c.re, @as(f32, 31) / 53, epsilon) and
...@@ -143,14 +146,14 @@ test "complex.div" {...@@ -143,14 +146,14 @@ test "complex.div" {
143}146}
144147
145test "complex.conjugate" {148test "complex.conjugate" {
146 const a = Complex(f32).new(5, 3);149 const a = Complex(f32).init(5, 3);
147 const c = a.conjugate();150 const c = a.conjugate();
148151
149 try testing.expect(c.re == 5 and c.im == -3);152 try testing.expect(c.re == 5 and c.im == -3);
150}153}
151154
152test "complex.reciprocal" {155test "complex.reciprocal" {
153 const a = Complex(f32).new(5, 3);156 const a = Complex(f32).init(5, 3);
154 const c = a.reciprocal();157 const c = a.reciprocal();
155158
156 try testing.expect(math.approxEqAbs(f32, c.re, @as(f32, 5) / 34, epsilon) and159 try testing.expect(math.approxEqAbs(f32, c.re, @as(f32, 5) / 34, epsilon) and
...@@ -158,7 +161,7 @@ test "complex.reciprocal" {...@@ -158,7 +161,7 @@ test "complex.reciprocal" {
158}161}
159162
160test "complex.magnitude" {163test "complex.magnitude" {
161 const a = Complex(f32).new(5, 3);164 const a = Complex(f32).init(5, 3);
162 const c = a.magnitude();165 const c = a.magnitude();
163166
164 try testing.expect(math.approxEqAbs(f32, c, 5.83095, epsilon));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,7 +18,7 @@ pub fn abs(z: anytype) @TypeOf(z.re) {
18const epsilon = 0.0001;18const epsilon = 0.0001;
1919
20test "complex.cabs" {20test "complex.cabs" {
21 const a = Complex(f32).new(5, 3);21 const a = Complex(f32).init(5, 3);
22 const c = abs(a);22 const c = abs(a);
23 try testing.expect(math.approxEqAbs(f32, c, 5.83095, epsilon));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 +13,13 @@ const Complex = cmath.Complex;
13pub fn acos(z: anytype) Complex(@TypeOf(z.re)) {13pub fn acos(z: anytype) Complex(@TypeOf(z.re)) {
14 const T = @TypeOf(z.re);14 const T = @TypeOf(z.re);
15 const q = cmath.asin(z);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}
1818
19const epsilon = 0.0001;19const epsilon = 0.0001;
2020
21test "complex.cacos" {21test "complex.cacos" {
22 const a = Complex(f32).new(5, 3);22 const a = Complex(f32).init(5, 3);
23 const c = acos(a);23 const c = acos(a);
2424
25 try testing.expect(math.approxEqAbs(f32, c.re, 0.546975, epsilon));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 +13,13 @@ const Complex = cmath.Complex;
13pub fn acosh(z: anytype) Complex(@TypeOf(z.re)) {13pub fn acosh(z: anytype) Complex(@TypeOf(z.re)) {
14 const T = @TypeOf(z.re);14 const T = @TypeOf(z.re);
15 const q = cmath.acos(z);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}
1818
19const epsilon = 0.0001;19const epsilon = 0.0001;
2020
21test "complex.cacosh" {21test "complex.cacosh" {
22 const a = Complex(f32).new(5, 3);22 const a = Complex(f32).init(5, 3);
23 const c = acosh(a);23 const c = acosh(a);
2424
25 try testing.expect(math.approxEqAbs(f32, c.re, 2.452914, epsilon));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,7 +18,7 @@ pub fn arg(z: anytype) @TypeOf(z.re) {
18const epsilon = 0.0001;18const epsilon = 0.0001;
1919
20test "complex.carg" {20test "complex.carg" {
21 const a = Complex(f32).new(5, 3);21 const a = Complex(f32).init(5, 3);
22 const c = arg(a);22 const c = arg(a);
23 try testing.expect(math.approxEqAbs(f32, c, 0.540420, epsilon));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,17 +15,17 @@ pub fn asin(z: anytype) Complex(@TypeOf(z.re)) {
15 const x = z.re;15 const x = z.re;
16 const y = z.im;16 const y = z.im;
1717
18 const p = Complex(T).new(1.0 - (x - y) * (x + y), -2.0 * x * y);18 const p = Complex(T).init(1.0 - (x - y) * (x + y), -2.0 * x * y);
19 const q = Complex(T).new(-y, x);19 const q = Complex(T).init(-y, x);
20 const r = cmath.log(q.add(cmath.sqrt(p)));20 const r = cmath.log(q.add(cmath.sqrt(p)));
2121
22 return Complex(T).new(r.im, -r.re);22 return Complex(T).init(r.im, -r.re);
23}23}
2424
25const epsilon = 0.0001;25const epsilon = 0.0001;
2626
27test "complex.casin" {27test "complex.casin" {
28 const a = Complex(f32).new(5, 3);28 const a = Complex(f32).init(5, 3);
29 const c = asin(a);29 const c = asin(a);
3030
31 try testing.expect(math.approxEqAbs(f32, c.re, 1.023822, epsilon));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,15 +12,15 @@ const Complex = cmath.Complex;
12/// Returns the hyperbolic arc-sine of z.12/// Returns the hyperbolic arc-sine of z.
13pub fn asinh(z: anytype) Complex(@TypeOf(z.re)) {13pub fn asinh(z: anytype) Complex(@TypeOf(z.re)) {
14 const T = @TypeOf(z.re);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 const r = cmath.asin(q);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}
1919
20const epsilon = 0.0001;20const epsilon = 0.0001;
2121
22test "complex.casinh" {22test "complex.casinh" {
23 const a = Complex(f32).new(5, 3);23 const a = Complex(f32).init(5, 3);
24 const c = asinh(a);24 const c = asinh(a);
2525
26 try testing.expect(math.approxEqAbs(f32, c.re, 2.459831, epsilon));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,14 +50,14 @@ fn atan32(z: Complex(f32)) Complex(f32) {
5050
51 if ((x == 0.0) and (y > 1.0)) {51 if ((x == 0.0) and (y > 1.0)) {
52 // overflow52 // overflow
53 return Complex(f32).new(maxnum, maxnum);53 return Complex(f32).init(maxnum, maxnum);
54 }54 }
5555
56 const x2 = x * x;56 const x2 = x * x;
57 var a = 1.0 - x2 - (y * y);57 var a = 1.0 - x2 - (y * y);
58 if (a == 0.0) {58 if (a == 0.0) {
59 // overflow59 // overflow
60 return Complex(f32).new(maxnum, maxnum);60 return Complex(f32).init(maxnum, maxnum);
61 }61 }
6262
63 var t = 0.5 * math.atan2(f32, 2.0 * x, a);63 var t = 0.5 * math.atan2(f32, 2.0 * x, a);
...@@ -67,12 +67,12 @@ fn atan32(z: Complex(f32)) Complex(f32) {...@@ -67,12 +67,12 @@ fn atan32(z: Complex(f32)) Complex(f32) {
67 a = x2 + t * t;67 a = x2 + t * t;
68 if (a == 0.0) {68 if (a == 0.0) {
69 // overflow69 // overflow
70 return Complex(f32).new(maxnum, maxnum);70 return Complex(f32).init(maxnum, maxnum);
71 }71 }
7272
73 t = y + 1.0;73 t = y + 1.0;
74 a = (x2 + (t * t)) / a;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}
7777
78fn redupif64(x: f64) f64 {78fn redupif64(x: f64) f64 {
...@@ -99,14 +99,14 @@ fn atan64(z: Complex(f64)) Complex(f64) {...@@ -99,14 +99,14 @@ fn atan64(z: Complex(f64)) Complex(f64) {
9999
100 if ((x == 0.0) and (y > 1.0)) {100 if ((x == 0.0) and (y > 1.0)) {
101 // overflow101 // overflow
102 return Complex(f64).new(maxnum, maxnum);102 return Complex(f64).init(maxnum, maxnum);
103 }103 }
104104
105 const x2 = x * x;105 const x2 = x * x;
106 var a = 1.0 - x2 - (y * y);106 var a = 1.0 - x2 - (y * y);
107 if (a == 0.0) {107 if (a == 0.0) {
108 // overflow108 // overflow
109 return Complex(f64).new(maxnum, maxnum);109 return Complex(f64).init(maxnum, maxnum);
110 }110 }
111111
112 var t = 0.5 * math.atan2(f64, 2.0 * x, a);112 var t = 0.5 * math.atan2(f64, 2.0 * x, a);
...@@ -116,18 +116,18 @@ fn atan64(z: Complex(f64)) Complex(f64) {...@@ -116,18 +116,18 @@ fn atan64(z: Complex(f64)) Complex(f64) {
116 a = x2 + t * t;116 a = x2 + t * t;
117 if (a == 0.0) {117 if (a == 0.0) {
118 // overflow118 // overflow
119 return Complex(f64).new(maxnum, maxnum);119 return Complex(f64).init(maxnum, maxnum);
120 }120 }
121121
122 t = y + 1.0;122 t = y + 1.0;
123 a = (x2 + (t * t)) / a;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}
126126
127const epsilon = 0.0001;127const epsilon = 0.0001;
128128
129test "complex.catan32" {129test "complex.catan32" {
130 const a = Complex(f32).new(5, 3);130 const a = Complex(f32).init(5, 3);
131 const c = atan(a);131 const c = atan(a);
132132
133 try testing.expect(math.approxEqAbs(f32, c.re, 1.423679, epsilon));133 try testing.expect(math.approxEqAbs(f32, c.re, 1.423679, epsilon));
...@@ -135,7 +135,7 @@ test "complex.catan32" {...@@ -135,7 +135,7 @@ test "complex.catan32" {
135}135}
136136
137test "complex.catan64" {137test "complex.catan64" {
138 const a = Complex(f64).new(5, 3);138 const a = Complex(f64).init(5, 3);
139 const c = atan(a);139 const c = atan(a);
140140
141 try testing.expect(math.approxEqAbs(f64, c.re, 1.423679, epsilon));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,15 +12,15 @@ const Complex = cmath.Complex;
12/// Returns the hyperbolic arc-tangent of z.12/// Returns the hyperbolic arc-tangent of z.
13pub fn atanh(z: anytype) Complex(@TypeOf(z.re)) {13pub fn atanh(z: anytype) Complex(@TypeOf(z.re)) {
14 const T = @TypeOf(z.re);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 const r = cmath.atan(q);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}
1919
20const epsilon = 0.0001;20const epsilon = 0.0001;
2121
22test "complex.catanh" {22test "complex.catanh" {
23 const a = Complex(f32).new(5, 3);23 const a = Complex(f32).init(5, 3);
24 const c = atanh(a);24 const c = atanh(a);
2525
26 try testing.expect(math.approxEqAbs(f32, c.re, 0.146947, epsilon));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,11 +12,11 @@ const Complex = cmath.Complex;
12/// Returns the complex conjugate of z.12/// Returns the complex conjugate of z.
13pub fn conj(z: anytype) Complex(@TypeOf(z.re)) {13pub fn conj(z: anytype) Complex(@TypeOf(z.re)) {
14 const T = @TypeOf(z.re);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}
1717
18test "complex.conj" {18test "complex.conj" {
19 const a = Complex(f32).new(5, 3);19 const a = Complex(f32).init(5, 3);
20 const c = a.conjugate();20 const c = a.conjugate();
2121
22 try testing.expect(c.re == 5 and c.im == -3);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,14 +12,14 @@ const Complex = cmath.Complex;
12/// Returns the cosine of z.12/// Returns the cosine of z.
13pub fn cos(z: anytype) Complex(@TypeOf(z.re)) {13pub fn cos(z: anytype) Complex(@TypeOf(z.re)) {
14 const T = @TypeOf(z.re);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 return cmath.cosh(p);16 return cmath.cosh(p);
17}17}
1818
19const epsilon = 0.0001;19const epsilon = 0.0001;
2020
21test "complex.ccos" {21test "complex.ccos" {
22 const a = Complex(f32).new(5, 3);22 const a = Complex(f32).init(5, 3);
23 const c = cos(a);23 const c = cos(a);
2424
25 try testing.expect(math.approxEqAbs(f32, c.re, 2.855815, epsilon));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,55 +40,55 @@ fn cosh32(z: Complex(f32)) Complex(f32) {
4040
41 if (ix < 0x7f800000 and iy < 0x7f800000) {41 if (ix < 0x7f800000 and iy < 0x7f800000) {
42 if (iy == 0) {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 // small x: normal case45 // small x: normal case
46 if (ix < 0x41100000) {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 }
4949
50 // |x|>= 9, so cosh(x) ~= exp(|x|)50 // |x|>= 9, so cosh(x) ~= exp(|x|)
51 if (ix < 0x42b17218) {51 if (ix < 0x42b17218) {
52 // x < 88.7: exp(|x|) won't overflow52 // x < 88.7: exp(|x|) won't overflow
53 const h = math.exp(math.fabs(x)) * 0.5;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 // x < 192.7: scale to avoid overflow56 // x < 192.7: scale to avoid overflow
57 else if (ix < 0x4340b1e7) {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 const r = ldexp_cexp(v, -1);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 // x >= 192.7: result always overflows62 // x >= 192.7: result always overflows
63 else {63 else {
64 const h = 0x1p127 * x;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 }
6868
69 if (ix == 0 and iy >= 0x7f800000) {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 }
7272
73 if (iy == 0 and ix >= 0x7f800000) {73 if (iy == 0 and ix >= 0x7f800000) {
74 if (hx & 0x7fffff == 0) {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 }
7979
80 if (ix < 0x7f800000 and iy >= 0x7f800000) {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 }
8383
84 if (ix >= 0x7f800000 and (hx & 0x7fffff) == 0) {84 if (ix >= 0x7f800000 and (hx & 0x7fffff) == 0) {
85 if (iy >= 0x7f800000) {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 }
9090
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}
9393
94fn cosh64(z: Complex(f64)) Complex(f64) {94fn cosh64(z: Complex(f64)) Complex(f64) {
...@@ -108,61 +108,61 @@ fn cosh64(z: Complex(f64)) Complex(f64) {...@@ -108,61 +108,61 @@ fn cosh64(z: Complex(f64)) Complex(f64) {
108 // nearly non-exceptional case where x, y are finite108 // nearly non-exceptional case where x, y are finite
109 if (ix < 0x7ff00000 and iy < 0x7ff00000) {109 if (ix < 0x7ff00000 and iy < 0x7ff00000) {
110 if (iy | ly == 0) {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 // small x: normal case113 // small x: normal case
114 if (ix < 0x40360000) {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 }
117117
118 // |x|>= 22, so cosh(x) ~= exp(|x|)118 // |x|>= 22, so cosh(x) ~= exp(|x|)
119 if (ix < 0x40862e42) {119 if (ix < 0x40862e42) {
120 // x < 710: exp(|x|) won't overflow120 // x < 710: exp(|x|) won't overflow
121 const h = math.exp(math.fabs(x)) * 0.5;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 // x < 1455: scale to avoid overflow124 // x < 1455: scale to avoid overflow
125 else if (ix < 0x4096bbaa) {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 const r = ldexp_cexp(v, -1);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 // x >= 1455: result always overflows130 // x >= 1455: result always overflows
131 else {131 else {
132 const h = 0x1p1023;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 }
136136
137 if (ix | lx == 0 and iy >= 0x7ff00000) {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 }
140140
141 if (iy | ly == 0 and ix >= 0x7ff00000) {141 if (iy | ly == 0 and ix >= 0x7ff00000) {
142 if ((hx & 0xfffff) | lx == 0) {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 }
147147
148 if (ix < 0x7ff00000 and iy >= 0x7ff00000) {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 }
151151
152 if (ix >= 0x7ff00000 and (hx & 0xfffff) | lx == 0) {152 if (ix >= 0x7ff00000 and (hx & 0xfffff) | lx == 0) {
153 if (iy >= 0x7ff00000) {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 }
158158
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}
161161
162const epsilon = 0.0001;162const epsilon = 0.0001;
163163
164test "complex.ccosh32" {164test "complex.ccosh32" {
165 const a = Complex(f32).new(5, 3);165 const a = Complex(f32).init(5, 3);
166 const c = cosh(a);166 const c = cosh(a);
167167
168 try testing.expect(math.approxEqAbs(f32, c.re, -73.467300, epsilon));168 try testing.expect(math.approxEqAbs(f32, c.re, -73.467300, epsilon));
...@@ -170,7 +170,7 @@ test "complex.ccosh32" {...@@ -170,7 +170,7 @@ test "complex.ccosh32" {
170}170}
171171
172test "complex.ccosh64" {172test "complex.ccosh64" {
173 const a = Complex(f64).new(5, 3);173 const a = Complex(f64).init(5, 3);
174 const c = cosh(a);174 const c = cosh(a);
175175
176 try testing.expect(math.approxEqAbs(f64, c.re, -73.467300, epsilon));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,25 +39,25 @@ fn exp32(z: Complex(f32)) Complex(f32) {
39 const hy = @bitCast(u32, y) & 0x7fffffff;39 const hy = @bitCast(u32, y) & 0x7fffffff;
40 // cexp(x + i0) = exp(x) + i040 // cexp(x + i0) = exp(x) + i0
41 if (hy == 0) {41 if (hy == 0) {
42 return Complex(f32).new(math.exp(x), y);42 return Complex(f32).init(math.exp(x), y);
43 }43 }
4444
45 const hx = @bitCast(u32, x);45 const hx = @bitCast(u32, x);
46 // cexp(0 + iy) = cos(y) + isin(y)46 // cexp(0 + iy) = cos(y) + isin(y)
47 if ((hx & 0x7fffffff) == 0) {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 }
5050
51 if (hy >= 0x7f800000) {51 if (hy >= 0x7f800000) {
52 // cexp(finite|nan +- i inf|nan) = nan + i nan52 // cexp(finite|nan +- i inf|nan) = nan + i nan
53 if ((hx & 0x7fffffff) != 0x7f800000) {53 if ((hx & 0x7fffffff) != 0x7f800000) {
54 return Complex(f32).new(y - y, y - y);54 return Complex(f32).init(y - y, y - y);
55 } // cexp(-inf +- i inf|nan) = 0 + i055 } // cexp(-inf +- i inf|nan) = 0 + i0
56 else if (hx & 0x80000000 != 0) {56 else if (hx & 0x80000000 != 0) {
57 return Complex(f32).new(0, 0);57 return Complex(f32).init(0, 0);
58 } // cexp(+inf +- i inf|nan) = inf + i nan58 } // cexp(+inf +- i inf|nan) = inf + i nan
59 else {59 else {
60 return Complex(f32).new(x, y - y);60 return Complex(f32).init(x, y - y);
61 }61 }
62 }62 }
6363
...@@ -70,7 +70,7 @@ fn exp32(z: Complex(f32)) Complex(f32) {...@@ -70,7 +70,7 @@ fn exp32(z: Complex(f32)) Complex(f32) {
70 // - x = nan70 // - x = nan
71 else {71 else {
72 const exp_x = math.exp(x);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}
7676
...@@ -87,7 +87,7 @@ fn exp64(z: Complex(f64)) Complex(f64) {...@@ -87,7 +87,7 @@ fn exp64(z: Complex(f64)) Complex(f64) {
8787
88 // cexp(x + i0) = exp(x) + i088 // cexp(x + i0) = exp(x) + i0
89 if (hy | ly == 0) {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 }
9292
93 const fx = @bitCast(u64, x);93 const fx = @bitCast(u64, x);
...@@ -96,19 +96,19 @@ fn exp64(z: Complex(f64)) Complex(f64) {...@@ -96,19 +96,19 @@ fn exp64(z: Complex(f64)) Complex(f64) {
9696
97 // cexp(0 + iy) = cos(y) + isin(y)97 // cexp(0 + iy) = cos(y) + isin(y)
98 if ((hx & 0x7fffffff) | lx == 0) {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 }
101101
102 if (hy >= 0x7ff00000) {102 if (hy >= 0x7ff00000) {
103 // cexp(finite|nan +- i inf|nan) = nan + i nan103 // cexp(finite|nan +- i inf|nan) = nan + i nan
104 if (lx != 0 or (hx & 0x7fffffff) != 0x7ff00000) {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 } // cexp(-inf +- i inf|nan) = 0 + i0106 } // cexp(-inf +- i inf|nan) = 0 + i0
107 else if (hx & 0x80000000 != 0) {107 else if (hx & 0x80000000 != 0) {
108 return Complex(f64).new(0, 0);108 return Complex(f64).init(0, 0);
109 } // cexp(+inf +- i inf|nan) = inf + i nan109 } // cexp(+inf +- i inf|nan) = inf + i nan
110 else {110 else {
111 return Complex(f64).new(x, y - y);111 return Complex(f64).init(x, y - y);
112 }112 }
113 }113 }
114114
...@@ -121,14 +121,14 @@ fn exp64(z: Complex(f64)) Complex(f64) {...@@ -121,14 +121,14 @@ fn exp64(z: Complex(f64)) Complex(f64) {
121 // - x = nan121 // - x = nan
122 else {122 else {
123 const exp_x = math.exp(x);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}
127127
128const epsilon = 0.0001;128const epsilon = 0.0001;
129129
130test "complex.cexp32" {130test "complex.cexp32" {
131 const a = Complex(f32).new(5, 3);131 const a = Complex(f32).init(5, 3);
132 const c = exp(a);132 const c = exp(a);
133133
134 try testing.expect(math.approxEqAbs(f32, c.re, -146.927917, epsilon));134 try testing.expect(math.approxEqAbs(f32, c.re, -146.927917, epsilon));
...@@ -136,7 +136,7 @@ test "complex.cexp32" {...@@ -136,7 +136,7 @@ test "complex.cexp32" {
136}136}
137137
138test "complex.cexp64" {138test "complex.cexp64" {
139 const a = Complex(f64).new(5, 3);139 const a = Complex(f64).init(5, 3);
140 const c = exp(a);140 const c = exp(a);
141141
142 try testing.expect(math.approxEqAbs(f64, c.re, -146.927917, epsilon));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,7 +48,7 @@ fn ldexp_cexp32(z: Complex(f32), expt: i32) Complex(f32) {
48 const half_expt2 = exptf - half_expt1;48 const half_expt2 = exptf - half_expt1;
49 const scale2 = @bitCast(f32, (0x7f + half_expt2) << 23);49 const scale2 = @bitCast(f32, (0x7f + half_expt2) << 23);
5050
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}
5353
54fn frexp_exp64(x: f64, expt: *i32) f64 {54fn frexp_exp64(x: f64, expt: *i32) f64 {
...@@ -78,7 +78,7 @@ fn ldexp_cexp64(z: Complex(f64), expt: i32) Complex(f64) {...@@ -78,7 +78,7 @@ fn ldexp_cexp64(z: Complex(f64), expt: i32) Complex(f64) {
78 const half_expt2 = exptf - half_expt1;78 const half_expt2 = exptf - half_expt1;
79 const scale2 = @bitCast(f64, (0x3ff + half_expt2) << 20);79 const scale2 = @bitCast(f64, (0x3ff + half_expt2) << 20);
8080
81 return Complex(f64).new(81 return Complex(f64).init(
82 math.cos(z.im) * exp_x * scale1 * scale2,82 math.cos(z.im) * exp_x * scale1 * scale2,
83 math.sin(z.im) * exp_x * scale1 * scale2,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,13 +15,13 @@ pub fn log(z: anytype) Complex(@TypeOf(z.re)) {
15 const r = cmath.abs(z);15 const r = cmath.abs(z);
16 const phi = cmath.arg(z);16 const phi = cmath.arg(z);
1717
18 return Complex(T).new(math.ln(r), phi);18 return Complex(T).init(math.ln(r), phi);
19}19}
2020
21const epsilon = 0.0001;21const epsilon = 0.0001;
2222
23test "complex.clog" {23test "complex.clog" {
24 const a = Complex(f32).new(5, 3);24 const a = Complex(f32).init(5, 3);
25 const c = log(a);25 const c = log(a);
2626
27 try testing.expect(math.approxEqAbs(f32, c.re, 1.763180, epsilon));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,8 +19,8 @@ pub fn pow(comptime T: type, z: T, c: T) T {
19const epsilon = 0.0001;19const epsilon = 0.0001;
2020
21test "complex.cpow" {21test "complex.cpow" {
22 const a = Complex(f32).new(5, 3);22 const a = Complex(f32).init(5, 3);
23 const b = Complex(f32).new(2.3, -1.3);23 const b = Complex(f32).init(2.3, -1.3);
24 const c = pow(Complex(f32), a, b);24 const c = pow(Complex(f32), a, b);
2525
26 try testing.expect(math.approxEqAbs(f32, c.re, 58.049110, epsilon));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,16 +14,16 @@ pub fn proj(z: anytype) Complex(@TypeOf(z.re)) {
14 const T = @TypeOf(z.re);14 const T = @TypeOf(z.re);
1515
16 if (math.isInf(z.re) or math.isInf(z.im)) {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 }
1919
20 return Complex(T).new(z.re, z.im);20 return Complex(T).init(z.re, z.im);
21}21}
2222
23const epsilon = 0.0001;23const epsilon = 0.0001;
2424
25test "complex.cproj" {25test "complex.cproj" {
26 const a = Complex(f32).new(5, 3);26 const a = Complex(f32).init(5, 3);
27 const c = proj(a);27 const c = proj(a);
2828
29 try testing.expect(c.re == 5 and c.im == 3);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,15 +12,15 @@ const Complex = cmath.Complex;
12/// Returns the sine of z.12/// Returns the sine of z.
13pub fn sin(z: anytype) Complex(@TypeOf(z.re)) {13pub fn sin(z: anytype) Complex(@TypeOf(z.re)) {
14 const T = @TypeOf(z.re);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 const q = cmath.sinh(p);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}
1919
20const epsilon = 0.0001;20const epsilon = 0.0001;
2121
22test "complex.csin" {22test "complex.csin" {
23 const a = Complex(f32).new(5, 3);23 const a = Complex(f32).init(5, 3);
24 const c = sin(a);24 const c = sin(a);
2525
26 try testing.expect(math.approxEqAbs(f32, c.re, -9.654126, epsilon));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,55 +40,55 @@ fn sinh32(z: Complex(f32)) Complex(f32) {
4040
41 if (ix < 0x7f800000 and iy < 0x7f800000) {41 if (ix < 0x7f800000 and iy < 0x7f800000) {
42 if (iy == 0) {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 // small x: normal case45 // small x: normal case
46 if (ix < 0x41100000) {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 }
4949
50 // |x|>= 9, so cosh(x) ~= exp(|x|)50 // |x|>= 9, so cosh(x) ~= exp(|x|)
51 if (ix < 0x42b17218) {51 if (ix < 0x42b17218) {
52 // x < 88.7: exp(|x|) won't overflow52 // x < 88.7: exp(|x|) won't overflow
53 const h = math.exp(math.fabs(x)) * 0.5;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 // x < 192.7: scale to avoid overflow56 // x < 192.7: scale to avoid overflow
57 else if (ix < 0x4340b1e7) {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 const r = ldexp_cexp(v, -1);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 // x >= 192.7: result always overflows62 // x >= 192.7: result always overflows
63 else {63 else {
64 const h = 0x1p127 * x;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 }
6868
69 if (ix == 0 and iy >= 0x7f800000) {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 }
7272
73 if (iy == 0 and ix >= 0x7f800000) {73 if (iy == 0 and ix >= 0x7f800000) {
74 if (hx & 0x7fffff == 0) {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 }
7979
80 if (ix < 0x7f800000 and iy >= 0x7f800000) {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 }
8383
84 if (ix >= 0x7f800000 and (hx & 0x7fffff) == 0) {84 if (ix >= 0x7f800000 and (hx & 0x7fffff) == 0) {
85 if (iy >= 0x7f800000) {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 }
9090
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}
9393
94fn sinh64(z: Complex(f64)) Complex(f64) {94fn sinh64(z: Complex(f64)) Complex(f64) {
...@@ -107,61 +107,61 @@ fn sinh64(z: Complex(f64)) Complex(f64) {...@@ -107,61 +107,61 @@ fn sinh64(z: Complex(f64)) Complex(f64) {
107107
108 if (ix < 0x7ff00000 and iy < 0x7ff00000) {108 if (ix < 0x7ff00000 and iy < 0x7ff00000) {
109 if (iy | ly == 0) {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 // small x: normal case112 // small x: normal case
113 if (ix < 0x40360000) {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 }
116116
117 // |x|>= 22, so cosh(x) ~= exp(|x|)117 // |x|>= 22, so cosh(x) ~= exp(|x|)
118 if (ix < 0x40862e42) {118 if (ix < 0x40862e42) {
119 // x < 710: exp(|x|) won't overflow119 // x < 710: exp(|x|) won't overflow
120 const h = math.exp(math.fabs(x)) * 0.5;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 // x < 1455: scale to avoid overflow123 // x < 1455: scale to avoid overflow
124 else if (ix < 0x4096bbaa) {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 const r = ldexp_cexp(v, -1);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 // x >= 1455: result always overflows129 // x >= 1455: result always overflows
130 else {130 else {
131 const h = 0x1p1023 * x;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 }
135135
136 if (ix | lx == 0 and iy >= 0x7ff00000) {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 }
139139
140 if (iy | ly == 0 and ix >= 0x7ff00000) {140 if (iy | ly == 0 and ix >= 0x7ff00000) {
141 if ((hx & 0xfffff) | lx == 0) {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 }
146146
147 if (ix < 0x7ff00000 and iy >= 0x7ff00000) {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 }
150150
151 if (ix >= 0x7ff00000 and (hx & 0xfffff) | lx == 0) {151 if (ix >= 0x7ff00000 and (hx & 0xfffff) | lx == 0) {
152 if (iy >= 0x7ff00000) {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 }
157157
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}
160160
161const epsilon = 0.0001;161const epsilon = 0.0001;
162162
163test "complex.csinh32" {163test "complex.csinh32" {
164 const a = Complex(f32).new(5, 3);164 const a = Complex(f32).init(5, 3);
165 const c = sinh(a);165 const c = sinh(a);
166166
167 try testing.expect(math.approxEqAbs(f32, c.re, -73.460617, epsilon));167 try testing.expect(math.approxEqAbs(f32, c.re, -73.460617, epsilon));
...@@ -169,7 +169,7 @@ test "complex.csinh32" {...@@ -169,7 +169,7 @@ test "complex.csinh32" {
169}169}
170170
171test "complex.csinh64" {171test "complex.csinh64" {
172 const a = Complex(f64).new(5, 3);172 const a = Complex(f64).init(5, 3);
173 const c = sinh(a);173 const c = sinh(a);
174174
175 try testing.expect(math.approxEqAbs(f64, c.re, -73.460617, epsilon));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,15 +32,15 @@ fn sqrt32(z: Complex(f32)) Complex(f32) {
32 const y = z.im;32 const y = z.im;
3333
34 if (x == 0 and y == 0) {34 if (x == 0 and y == 0) {
35 return Complex(f32).new(0, y);35 return Complex(f32).init(0, y);
36 }36 }
37 if (math.isInf(y)) {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 if (math.isNan(x)) {40 if (math.isNan(x)) {
41 // raise invalid if y is not nan41 // raise invalid if y is not nan
42 const t = (y - y) / (y - y);42 const t = (y - y) / (y - y);
43 return Complex(f32).new(x, t);43 return Complex(f32).init(x, t);
44 }44 }
45 if (math.isInf(x)) {45 if (math.isInf(x)) {
46 // sqrt(inf + i nan) = inf + nan i46 // sqrt(inf + i nan) = inf + nan i
...@@ -48,9 +48,9 @@ fn sqrt32(z: Complex(f32)) Complex(f32) {...@@ -48,9 +48,9 @@ fn sqrt32(z: Complex(f32)) Complex(f32) {
48 // sqrt(-inf + i nan) = nan +- inf i48 // sqrt(-inf + i nan) = nan +- inf i
49 // sqrt(-inf + iy) = 0 + inf i49 // sqrt(-inf + iy) = 0 + inf i
50 if (math.signbit(x)) {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 } else {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 }
5656
...@@ -62,13 +62,13 @@ fn sqrt32(z: Complex(f32)) Complex(f32) {...@@ -62,13 +62,13 @@ fn sqrt32(z: Complex(f32)) Complex(f32) {
6262
63 if (dx >= 0) {63 if (dx >= 0) {
64 const t = math.sqrt((dx + math.hypot(f64, dx, dy)) * 0.5);64 const t = math.sqrt((dx + math.hypot(f64, dx, dy)) * 0.5);
65 return Complex(f32).new(65 return Complex(f32).init(
66 @floatCast(f32, t),66 @floatCast(f32, t),
67 @floatCast(f32, dy / (2.0 * t)),67 @floatCast(f32, dy / (2.0 * t)),
68 );68 );
69 } else {69 } else {
70 const t = math.sqrt((-dx + math.hypot(f64, dx, dy)) * 0.5);70 const t = math.sqrt((-dx + math.hypot(f64, dx, dy)) * 0.5);
71 return Complex(f32).new(71 return Complex(f32).init(
72 @floatCast(f32, math.fabs(y) / (2.0 * t)),72 @floatCast(f32, math.fabs(y) / (2.0 * t)),
73 @floatCast(f32, math.copysign(f64, t, y)),73 @floatCast(f32, math.copysign(f64, t, y)),
74 );74 );
...@@ -83,15 +83,15 @@ fn sqrt64(z: Complex(f64)) Complex(f64) {...@@ -83,15 +83,15 @@ fn sqrt64(z: Complex(f64)) Complex(f64) {
83 var y = z.im;83 var y = z.im;
8484
85 if (x == 0 and y == 0) {85 if (x == 0 and y == 0) {
86 return Complex(f64).new(0, y);86 return Complex(f64).init(0, y);
87 }87 }
88 if (math.isInf(y)) {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 if (math.isNan(x)) {91 if (math.isNan(x)) {
92 // raise invalid if y is not nan92 // raise invalid if y is not nan
93 const t = (y - y) / (y - y);93 const t = (y - y) / (y - y);
94 return Complex(f64).new(x, t);94 return Complex(f64).init(x, t);
95 }95 }
96 if (math.isInf(x)) {96 if (math.isInf(x)) {
97 // sqrt(inf + i nan) = inf + nan i97 // sqrt(inf + i nan) = inf + nan i
...@@ -99,9 +99,9 @@ fn sqrt64(z: Complex(f64)) Complex(f64) {...@@ -99,9 +99,9 @@ fn sqrt64(z: Complex(f64)) Complex(f64) {
99 // sqrt(-inf + i nan) = nan +- inf i99 // sqrt(-inf + i nan) = nan +- inf i
100 // sqrt(-inf + iy) = 0 + inf i100 // sqrt(-inf + iy) = 0 + inf i
101 if (math.signbit(x)) {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 } else {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 }
107107
...@@ -118,10 +118,10 @@ fn sqrt64(z: Complex(f64)) Complex(f64) {...@@ -118,10 +118,10 @@ fn sqrt64(z: Complex(f64)) Complex(f64) {
118 var result: Complex(f64) = undefined;118 var result: Complex(f64) = undefined;
119 if (x >= 0) {119 if (x >= 0) {
120 const t = math.sqrt((x + math.hypot(f64, x, y)) * 0.5);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 } else {122 } else {
123 const t = math.sqrt((-x + math.hypot(f64, x, y)) * 0.5);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 }
126126
127 if (scale) {127 if (scale) {
...@@ -135,7 +135,7 @@ fn sqrt64(z: Complex(f64)) Complex(f64) {...@@ -135,7 +135,7 @@ fn sqrt64(z: Complex(f64)) Complex(f64) {
135const epsilon = 0.0001;135const epsilon = 0.0001;
136136
137test "complex.csqrt32" {137test "complex.csqrt32" {
138 const a = Complex(f32).new(5, 3);138 const a = Complex(f32).init(5, 3);
139 const c = sqrt(a);139 const c = sqrt(a);
140140
141 try testing.expect(math.approxEqAbs(f32, c.re, 2.327117, epsilon));141 try testing.expect(math.approxEqAbs(f32, c.re, 2.327117, epsilon));
...@@ -143,7 +143,7 @@ test "complex.csqrt32" {...@@ -143,7 +143,7 @@ test "complex.csqrt32" {
143}143}
144144
145test "complex.csqrt64" {145test "complex.csqrt64" {
146 const a = Complex(f64).new(5, 3);146 const a = Complex(f64).init(5, 3);
147 const c = sqrt(a);147 const c = sqrt(a);
148148
149 try testing.expect(math.approxEqAbs(f64, c.re, 2.3271175190399496, epsilon));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,15 +12,15 @@ const Complex = cmath.Complex;
12/// Returns the tanget of z.12/// Returns the tanget of z.
13pub fn tan(z: anytype) Complex(@TypeOf(z.re)) {13pub fn tan(z: anytype) Complex(@TypeOf(z.re)) {
14 const T = @TypeOf(z.re);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 const r = cmath.tanh(q);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}
1919
20const epsilon = 0.0001;20const epsilon = 0.0001;
2121
22test "complex.ctan" {22test "complex.ctan" {
23 const a = Complex(f32).new(5, 3);23 const a = Complex(f32).init(5, 3);
24 const c = tan(a);24 const c = tan(a);
2525
26 try testing.expect(math.approxEqAbs(f32, c.re, -0.002708233, epsilon));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,22 +36,22 @@ fn tanh32(z: Complex(f32)) Complex(f32) {
36 if (ix >= 0x7f800000) {36 if (ix >= 0x7f800000) {
37 if (ix & 0x7fffff != 0) {37 if (ix & 0x7fffff != 0) {
38 const r = if (y == 0) y else x * y;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 const xx = @bitCast(f32, hx - 0x40000000);41 const xx = @bitCast(f32, hx - 0x40000000);
42 const r = if (math.isInf(y)) y else math.sin(y) * math.cos(y);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 }
4545
46 if (!math.isFinite(y)) {46 if (!math.isFinite(y)) {
47 const r = if (ix != 0) y - y else x;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 }
5050
51 // x >= 1151 // x >= 11
52 if (ix >= 0x41300000) {52 if (ix >= 0x41300000) {
53 const exp_mx = math.exp(-math.fabs(x));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 }
5656
57 // Kahan's algorithm57 // Kahan's algorithm
...@@ -61,7 +61,7 @@ fn tanh32(z: Complex(f32)) Complex(f32) {...@@ -61,7 +61,7 @@ fn tanh32(z: Complex(f32)) Complex(f32) {
61 const rho = math.sqrt(1 + s * s);61 const rho = math.sqrt(1 + s * s);
62 const den = 1 + beta * s * s;62 const den = 1 + beta * s * s;
6363
64 return Complex(f32).new((beta * rho * s) / den, t / den);64 return Complex(f32).init((beta * rho * s) / den, t / den);
65}65}
6666
67fn tanh64(z: Complex(f64)) Complex(f64) {67fn tanh64(z: Complex(f64)) Complex(f64) {
...@@ -78,23 +78,23 @@ fn tanh64(z: Complex(f64)) Complex(f64) {...@@ -78,23 +78,23 @@ fn tanh64(z: Complex(f64)) Complex(f64) {
78 if (ix >= 0x7ff00000) {78 if (ix >= 0x7ff00000) {
79 if ((ix & 0x7fffff) | lx != 0) {79 if ((ix & 0x7fffff) | lx != 0) {
80 const r = if (y == 0) y else x * y;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 }
8383
84 const xx = @bitCast(f64, (@as(u64, hx - 0x40000000) << 32) | lx);84 const xx = @bitCast(f64, (@as(u64, hx - 0x40000000) << 32) | lx);
85 const r = if (math.isInf(y)) y else math.sin(y) * math.cos(y);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 }
8888
89 if (!math.isFinite(y)) {89 if (!math.isFinite(y)) {
90 const r = if (ix != 0) y - y else x;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 }
9393
94 // x >= 2294 // x >= 22
95 if (ix >= 0x40360000) {95 if (ix >= 0x40360000) {
96 const exp_mx = math.exp(-math.fabs(x));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 }
9999
100 // Kahan's algorithm100 // Kahan's algorithm
...@@ -104,13 +104,13 @@ fn tanh64(z: Complex(f64)) Complex(f64) {...@@ -104,13 +104,13 @@ fn tanh64(z: Complex(f64)) Complex(f64) {
104 const rho = math.sqrt(1 + s * s);104 const rho = math.sqrt(1 + s * s);
105 const den = 1 + beta * s * s;105 const den = 1 + beta * s * s;
106106
107 return Complex(f64).new((beta * rho * s) / den, t / den);107 return Complex(f64).init((beta * rho * s) / den, t / den);
108}108}
109109
110const epsilon = 0.0001;110const epsilon = 0.0001;
111111
112test "complex.ctanh32" {112test "complex.ctanh32" {
113 const a = Complex(f32).new(5, 3);113 const a = Complex(f32).init(5, 3);
114 const c = tanh(a);114 const c = tanh(a);
115115
116 try testing.expect(math.approxEqAbs(f32, c.re, 0.999913, epsilon));116 try testing.expect(math.approxEqAbs(f32, c.re, 0.999913, epsilon));
...@@ -118,7 +118,7 @@ test "complex.ctanh32" {...@@ -118,7 +118,7 @@ test "complex.ctanh32" {
118}118}
119119
120test "complex.ctanh64" {120test "complex.ctanh64" {
121 const a = Complex(f64).new(5, 3);121 const a = Complex(f64).init(5, 3);
122 const c = tanh(a);122 const c = tanh(a);
123123
124 try testing.expect(math.approxEqAbs(f64, c.re, 0.999913, epsilon));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,13 +1608,13 @@ test "zig fmt: if-else with comment before else" {
1608 \\comptime {1608 \\comptime {
1609 \\ // cexp(finite|nan +- i inf|nan) = nan + i nan1609 \\ // cexp(finite|nan +- i inf|nan) = nan + i nan
1610 \\ if ((hx & 0x7fffffff) != 0x7f800000) {1610 \\ if ((hx & 0x7fffffff) != 0x7f800000) {
1611 \\ return Complex(f32).new(y - y, y - y);1611 \\ return Complex(f32).init(y - y, y - y);
1612 \\ } // cexp(-inf +- i inf|nan) = 0 + i01612 \\ } // cexp(-inf +- i inf|nan) = 0 + i0
1613 \\ else if (hx & 0x80000000 != 0) {1613 \\ else if (hx & 0x80000000 != 0) {
1614 \\ return Complex(f32).new(0, 0);1614 \\ return Complex(f32).init(0, 0);
1615 \\ } // cexp(+inf +- i inf|nan) = inf + i nan1615 \\ } // cexp(+inf +- i inf|nan) = inf + i nan
1616 \\ else {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,16 +2267,16 @@ test "zig fmt: line comment between if block and else keyword" {
2267 \\test "aoeu" {2267 \\test "aoeu" {
2268 \\ // cexp(finite|nan +- i inf|nan) = nan + i nan2268 \\ // cexp(finite|nan +- i inf|nan) = nan + i nan
2269 \\ if ((hx & 0x7fffffff) != 0x7f800000) {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 \\ // cexp(-inf +- i inf|nan) = 0 + i02272 \\ // cexp(-inf +- i inf|nan) = 0 + i0
2273 \\ else if (hx & 0x80000000 != 0) {2273 \\ else if (hx & 0x80000000 != 0) {
2274 \\ return Complex(f32).new(0, 0);2274 \\ return Complex(f32).init(0, 0);
2275 \\ }2275 \\ }
2276 \\ // cexp(+inf +- i inf|nan) = inf + i nan2276 \\ // cexp(+inf +- i inf|nan) = inf + i nan
2277 \\ // another comment2277 \\ // another comment
2278 \\ else {2278 \\ else {
2279 \\ return Complex(f32).new(x, y - y);2279 \\ return Complex(f32).init(x, y - y);
2280 \\ }2280 \\ }
2281 \\}2281 \\}
2282 \\2282 \\