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 {
3838
3939 /// Imaginary part.
4040 im: T,
41
42 /// Deprecated, use init()
43 pub const new = init;
4144
4245 /// 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 {
4447 return Self{
4548 .re = re,
4649 .im = im,
......@@ -110,32 +113,32 @@ pub fn Complex(comptime T: type) type {
110113const epsilon = 0.0001;
111114
112115test "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);
115118 const c = a.add(b);
116119
117120 try testing.expect(c.re == 7 and c.im == 10);
118121}
119122
120123test "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);
123126 const c = a.sub(b);
124127
125128 try testing.expect(c.re == 3 and c.im == -4);
126129}
127130
128131test "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);
131134 const c = a.mul(b);
132135
133136 try testing.expect(c.re == -11 and c.im == 41);
134137}
135138
136139test "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);
139142 const c = a.div(b);
140143
141144 try testing.expect(math.approxEqAbs(f32, c.re, @as(f32, 31) / 53, epsilon) and
......@@ -143,14 +146,14 @@ test "complex.div" {
143146}
144147
145148test "complex.conjugate" {
146 const a = Complex(f32).new(5, 3);
149 const a = Complex(f32).init(5, 3);
147150 const c = a.conjugate();
148151
149152 try testing.expect(c.re == 5 and c.im == -3);
150153}
151154
152155test "complex.reciprocal" {
153 const a = Complex(f32).new(5, 3);
156 const a = Complex(f32).init(5, 3);
154157 const c = a.reciprocal();
155158
156159 try testing.expect(math.approxEqAbs(f32, c.re, @as(f32, 5) / 34, epsilon) and
......@@ -158,7 +161,7 @@ test "complex.reciprocal" {
158161}
159162
160163test "complex.magnitude" {
161 const a = Complex(f32).new(5, 3);
164 const a = Complex(f32).init(5, 3);
162165 const c = a.magnitude();
163166
164167 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) {
1818const epsilon = 0.0001;
1919
2020test "complex.cabs" {
21 const a = Complex(f32).new(5, 3);
21 const a = Complex(f32).init(5, 3);
2222 const c = abs(a);
2323 try testing.expect(math.approxEqAbs(f32, c, 5.83095, epsilon));
2424}
lib/std/math/complex/acos.zig+2-2
......@@ -13,13 +13,13 @@ const Complex = cmath.Complex;
1313pub fn acos(z: anytype) Complex(@TypeOf(z.re)) {
1414 const T = @TypeOf(z.re);
1515 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);
1717}
1818
1919const epsilon = 0.0001;
2020
2121test "complex.cacos" {
22 const a = Complex(f32).new(5, 3);
22 const a = Complex(f32).init(5, 3);
2323 const c = acos(a);
2424
2525 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;
1313pub fn acosh(z: anytype) Complex(@TypeOf(z.re)) {
1414 const T = @TypeOf(z.re);
1515 const q = cmath.acos(z);
16 return Complex(T).new(-q.im, q.re);
16 return Complex(T).init(-q.im, q.re);
1717}
1818
1919const epsilon = 0.0001;
2020
2121test "complex.cacosh" {
22 const a = Complex(f32).new(5, 3);
22 const a = Complex(f32).init(5, 3);
2323 const c = acosh(a);
2424
2525 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) {
1818const epsilon = 0.0001;
1919
2020test "complex.carg" {
21 const a = Complex(f32).new(5, 3);
21 const a = Complex(f32).init(5, 3);
2222 const c = arg(a);
2323 try testing.expect(math.approxEqAbs(f32, c, 0.540420, epsilon));
2424}
lib/std/math/complex/asin.zig+4-4
......@@ -15,17 +15,17 @@ pub fn asin(z: anytype) Complex(@TypeOf(z.re)) {
1515 const x = z.re;
1616 const y = z.im;
1717
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);
2020 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);
2323}
2424
2525const epsilon = 0.0001;
2626
2727test "complex.casin" {
28 const a = Complex(f32).new(5, 3);
28 const a = Complex(f32).init(5, 3);
2929 const c = asin(a);
3030
3131 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;
1212/// Returns the hyperbolic arc-sine of z.
1313pub fn asinh(z: anytype) Complex(@TypeOf(z.re)) {
1414 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);
1616 const r = cmath.asin(q);
17 return Complex(T).new(r.im, -r.re);
17 return Complex(T).init(r.im, -r.re);
1818}
1919
2020const epsilon = 0.0001;
2121
2222test "complex.casinh" {
23 const a = Complex(f32).new(5, 3);
23 const a = Complex(f32).init(5, 3);
2424 const c = asinh(a);
2525
2626 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) {
5050
5151 if ((x == 0.0) and (y > 1.0)) {
5252 // overflow
53 return Complex(f32).new(maxnum, maxnum);
53 return Complex(f32).init(maxnum, maxnum);
5454 }
5555
5656 const x2 = x * x;
5757 var a = 1.0 - x2 - (y * y);
5858 if (a == 0.0) {
5959 // overflow
60 return Complex(f32).new(maxnum, maxnum);
60 return Complex(f32).init(maxnum, maxnum);
6161 }
6262
6363 var t = 0.5 * math.atan2(f32, 2.0 * x, a);
......@@ -67,12 +67,12 @@ fn atan32(z: Complex(f32)) Complex(f32) {
6767 a = x2 + t * t;
6868 if (a == 0.0) {
6969 // overflow
70 return Complex(f32).new(maxnum, maxnum);
70 return Complex(f32).init(maxnum, maxnum);
7171 }
7272
7373 t = y + 1.0;
7474 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));
7676}
7777
7878fn redupif64(x: f64) f64 {
......@@ -99,14 +99,14 @@ fn atan64(z: Complex(f64)) Complex(f64) {
9999
100100 if ((x == 0.0) and (y > 1.0)) {
101101 // overflow
102 return Complex(f64).new(maxnum, maxnum);
102 return Complex(f64).init(maxnum, maxnum);
103103 }
104104
105105 const x2 = x * x;
106106 var a = 1.0 - x2 - (y * y);
107107 if (a == 0.0) {
108108 // overflow
109 return Complex(f64).new(maxnum, maxnum);
109 return Complex(f64).init(maxnum, maxnum);
110110 }
111111
112112 var t = 0.5 * math.atan2(f64, 2.0 * x, a);
......@@ -116,18 +116,18 @@ fn atan64(z: Complex(f64)) Complex(f64) {
116116 a = x2 + t * t;
117117 if (a == 0.0) {
118118 // overflow
119 return Complex(f64).new(maxnum, maxnum);
119 return Complex(f64).init(maxnum, maxnum);
120120 }
121121
122122 t = y + 1.0;
123123 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));
125125}
126126
127127const epsilon = 0.0001;
128128
129129test "complex.catan32" {
130 const a = Complex(f32).new(5, 3);
130 const a = Complex(f32).init(5, 3);
131131 const c = atan(a);
132132
133133 try testing.expect(math.approxEqAbs(f32, c.re, 1.423679, epsilon));
......@@ -135,7 +135,7 @@ test "complex.catan32" {
135135}
136136
137137test "complex.catan64" {
138 const a = Complex(f64).new(5, 3);
138 const a = Complex(f64).init(5, 3);
139139 const c = atan(a);
140140
141141 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;
1212/// Returns the hyperbolic arc-tangent of z.
1313pub fn atanh(z: anytype) Complex(@TypeOf(z.re)) {
1414 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);
1616 const r = cmath.atan(q);
17 return Complex(T).new(r.im, -r.re);
17 return Complex(T).init(r.im, -r.re);
1818}
1919
2020const epsilon = 0.0001;
2121
2222test "complex.catanh" {
23 const a = Complex(f32).new(5, 3);
23 const a = Complex(f32).init(5, 3);
2424 const c = atanh(a);
2525
2626 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;
1212/// Returns the complex conjugate of z.
1313pub fn conj(z: anytype) Complex(@TypeOf(z.re)) {
1414 const T = @TypeOf(z.re);
15 return Complex(T).new(z.re, -z.im);
15 return Complex(T).init(z.re, -z.im);
1616}
1717
1818test "complex.conj" {
19 const a = Complex(f32).new(5, 3);
19 const a = Complex(f32).init(5, 3);
2020 const c = a.conjugate();
2121
2222 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;
1212/// Returns the cosine of z.
1313pub fn cos(z: anytype) Complex(@TypeOf(z.re)) {
1414 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);
1616 return cmath.cosh(p);
1717}
1818
1919const epsilon = 0.0001;
2020
2121test "complex.ccos" {
22 const a = Complex(f32).new(5, 3);
22 const a = Complex(f32).init(5, 3);
2323 const c = cos(a);
2424
2525 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) {
4040
4141 if (ix < 0x7f800000 and iy < 0x7f800000) {
4242 if (iy == 0) {
43 return Complex(f32).new(math.cosh(x), y);
43 return Complex(f32).init(math.cosh(x), y);
4444 }
4545 // small x: normal case
4646 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));
4848 }
4949
5050 // |x|>= 9, so cosh(x) ~= exp(|x|)
5151 if (ix < 0x42b17218) {
5252 // x < 88.7: exp(|x|) won't overflow
5353 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));
5555 }
5656 // x < 192.7: scale to avoid overflow
5757 else if (ix < 0x4340b1e7) {
58 const v = Complex(f32).new(math.fabs(x), y);
58 const v = Complex(f32).init(math.fabs(x), y);
5959 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));
6161 }
6262 // x >= 192.7: result always overflows
6363 else {
6464 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));
6666 }
6767 }
6868
6969 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)));
7171 }
7272
7373 if (iy == 0 and ix >= 0x7f800000) {
7474 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);
7676 }
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));
7878 }
7979
8080 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));
8282 }
8383
8484 if (ix >= 0x7f800000 and (hx & 0x7fffff) == 0) {
8585 if (iy >= 0x7f800000) {
86 return Complex(f32).new(x * x, x * (y - y));
86 return Complex(f32).init(x * x, x * (y - y));
8787 }
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));
8989 }
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));
9292}
9393
9494fn cosh64(z: Complex(f64)) Complex(f64) {
......@@ -108,61 +108,61 @@ fn cosh64(z: Complex(f64)) Complex(f64) {
108108 // nearly non-exceptional case where x, y are finite
109109 if (ix < 0x7ff00000 and iy < 0x7ff00000) {
110110 if (iy | ly == 0) {
111 return Complex(f64).new(math.cosh(x), x * y);
111 return Complex(f64).init(math.cosh(x), x * y);
112112 }
113113 // small x: normal case
114114 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));
116116 }
117117
118118 // |x|>= 22, so cosh(x) ~= exp(|x|)
119119 if (ix < 0x40862e42) {
120120 // x < 710: exp(|x|) won't overflow
121121 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));
123123 }
124124 // x < 1455: scale to avoid overflow
125125 else if (ix < 0x4096bbaa) {
126 const v = Complex(f64).new(math.fabs(x), y);
126 const v = Complex(f64).init(math.fabs(x), y);
127127 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));
129129 }
130130 // x >= 1455: result always overflows
131131 else {
132132 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));
134134 }
135135 }
136136
137137 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)));
139139 }
140140
141141 if (iy | ly == 0 and ix >= 0x7ff00000) {
142142 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);
144144 }
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));
146146 }
147147
148148 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));
150150 }
151151
152152 if (ix >= 0x7ff00000 and (hx & 0xfffff) | lx == 0) {
153153 if (iy >= 0x7ff00000) {
154 return Complex(f64).new(x * x, x * (y - y));
154 return Complex(f64).init(x * x, x * (y - y));
155155 }
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));
157157 }
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));
160160}
161161
162162const epsilon = 0.0001;
163163
164164test "complex.ccosh32" {
165 const a = Complex(f32).new(5, 3);
165 const a = Complex(f32).init(5, 3);
166166 const c = cosh(a);
167167
168168 try testing.expect(math.approxEqAbs(f32, c.re, -73.467300, epsilon));
......@@ -170,7 +170,7 @@ test "complex.ccosh32" {
170170}
171171
172172test "complex.ccosh64" {
173 const a = Complex(f64).new(5, 3);
173 const a = Complex(f64).init(5, 3);
174174 const c = cosh(a);
175175
176176 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) {
3939 const hy = @bitCast(u32, y) & 0x7fffffff;
4040 // cexp(x + i0) = exp(x) + i0
4141 if (hy == 0) {
42 return Complex(f32).new(math.exp(x), y);
42 return Complex(f32).init(math.exp(x), y);
4343 }
4444
4545 const hx = @bitCast(u32, x);
4646 // cexp(0 + iy) = cos(y) + isin(y)
4747 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));
4949 }
5050
5151 if (hy >= 0x7f800000) {
5252 // cexp(finite|nan +- i inf|nan) = nan + i nan
5353 if ((hx & 0x7fffffff) != 0x7f800000) {
54 return Complex(f32).new(y - y, y - y);
54 return Complex(f32).init(y - y, y - y);
5555 } // cexp(-inf +- i inf|nan) = 0 + i0
5656 else if (hx & 0x80000000 != 0) {
57 return Complex(f32).new(0, 0);
57 return Complex(f32).init(0, 0);
5858 } // cexp(+inf +- i inf|nan) = inf + i nan
5959 else {
60 return Complex(f32).new(x, y - y);
60 return Complex(f32).init(x, y - y);
6161 }
6262 }
6363
......@@ -70,7 +70,7 @@ fn exp32(z: Complex(f32)) Complex(f32) {
7070 // - x = nan
7171 else {
7272 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));
7474 }
7575}
7676
......@@ -87,7 +87,7 @@ fn exp64(z: Complex(f64)) Complex(f64) {
8787
8888 // cexp(x + i0) = exp(x) + i0
8989 if (hy | ly == 0) {
90 return Complex(f64).new(math.exp(x), y);
90 return Complex(f64).init(math.exp(x), y);
9191 }
9292
9393 const fx = @bitCast(u64, x);
......@@ -96,19 +96,19 @@ fn exp64(z: Complex(f64)) Complex(f64) {
9696
9797 // cexp(0 + iy) = cos(y) + isin(y)
9898 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));
100100 }
101101
102102 if (hy >= 0x7ff00000) {
103103 // cexp(finite|nan +- i inf|nan) = nan + i nan
104104 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);
106106 } // cexp(-inf +- i inf|nan) = 0 + i0
107107 else if (hx & 0x80000000 != 0) {
108 return Complex(f64).new(0, 0);
108 return Complex(f64).init(0, 0);
109109 } // cexp(+inf +- i inf|nan) = inf + i nan
110110 else {
111 return Complex(f64).new(x, y - y);
111 return Complex(f64).init(x, y - y);
112112 }
113113 }
114114
......@@ -121,14 +121,14 @@ fn exp64(z: Complex(f64)) Complex(f64) {
121121 // - x = nan
122122 else {
123123 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));
125125 }
126126}
127127
128128const epsilon = 0.0001;
129129
130130test "complex.cexp32" {
131 const a = Complex(f32).new(5, 3);
131 const a = Complex(f32).init(5, 3);
132132 const c = exp(a);
133133
134134 try testing.expect(math.approxEqAbs(f32, c.re, -146.927917, epsilon));
......@@ -136,7 +136,7 @@ test "complex.cexp32" {
136136}
137137
138138test "complex.cexp64" {
139 const a = Complex(f64).new(5, 3);
139 const a = Complex(f64).init(5, 3);
140140 const c = exp(a);
141141
142142 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) {
4848 const half_expt2 = exptf - half_expt1;
4949 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);
5252}
5353
5454fn frexp_exp64(x: f64, expt: *i32) f64 {
......@@ -78,7 +78,7 @@ fn ldexp_cexp64(z: Complex(f64), expt: i32) Complex(f64) {
7878 const half_expt2 = exptf - half_expt1;
7979 const scale2 = @bitCast(f64, (0x3ff + half_expt2) << 20);
8080
81 return Complex(f64).new(
81 return Complex(f64).init(
8282 math.cos(z.im) * exp_x * scale1 * scale2,
8383 math.sin(z.im) * exp_x * scale1 * scale2,
8484 );
lib/std/math/complex/log.zig+2-2
......@@ -15,13 +15,13 @@ pub fn log(z: anytype) Complex(@TypeOf(z.re)) {
1515 const r = cmath.abs(z);
1616 const phi = cmath.arg(z);
1717
18 return Complex(T).new(math.ln(r), phi);
18 return Complex(T).init(math.ln(r), phi);
1919}
2020
2121const epsilon = 0.0001;
2222
2323test "complex.clog" {
24 const a = Complex(f32).new(5, 3);
24 const a = Complex(f32).init(5, 3);
2525 const c = log(a);
2626
2727 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 {
1919const epsilon = 0.0001;
2020
2121test "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);
2424 const c = pow(Complex(f32), a, b);
2525
2626 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)) {
1414 const T = @TypeOf(z.re);
1515
1616 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));
1818 }
1919
20 return Complex(T).new(z.re, z.im);
20 return Complex(T).init(z.re, z.im);
2121}
2222
2323const epsilon = 0.0001;
2424
2525test "complex.cproj" {
26 const a = Complex(f32).new(5, 3);
26 const a = Complex(f32).init(5, 3);
2727 const c = proj(a);
2828
2929 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;
1212/// Returns the sine of z.
1313pub fn sin(z: anytype) Complex(@TypeOf(z.re)) {
1414 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);
1616 const q = cmath.sinh(p);
17 return Complex(T).new(q.im, -q.re);
17 return Complex(T).init(q.im, -q.re);
1818}
1919
2020const epsilon = 0.0001;
2121
2222test "complex.csin" {
23 const a = Complex(f32).new(5, 3);
23 const a = Complex(f32).init(5, 3);
2424 const c = sin(a);
2525
2626 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) {
4040
4141 if (ix < 0x7f800000 and iy < 0x7f800000) {
4242 if (iy == 0) {
43 return Complex(f32).new(math.sinh(x), y);
43 return Complex(f32).init(math.sinh(x), y);
4444 }
4545 // small x: normal case
4646 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));
4848 }
4949
5050 // |x|>= 9, so cosh(x) ~= exp(|x|)
5151 if (ix < 0x42b17218) {
5252 // x < 88.7: exp(|x|) won't overflow
5353 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));
5555 }
5656 // x < 192.7: scale to avoid overflow
5757 else if (ix < 0x4340b1e7) {
58 const v = Complex(f32).new(math.fabs(x), y);
58 const v = Complex(f32).init(math.fabs(x), y);
5959 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);
6161 }
6262 // x >= 192.7: result always overflows
6363 else {
6464 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));
6666 }
6767 }
6868
6969 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);
7171 }
7272
7373 if (iy == 0 and ix >= 0x7f800000) {
7474 if (hx & 0x7fffff == 0) {
75 return Complex(f32).new(x, y);
75 return Complex(f32).init(x, y);
7676 }
77 return Complex(f32).new(x, math.copysign(f32, 0, y));
77 return Complex(f32).init(x, math.copysign(f32, 0, y));
7878 }
7979
8080 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));
8282 }
8383
8484 if (ix >= 0x7f800000 and (hx & 0x7fffff) == 0) {
8585 if (iy >= 0x7f800000) {
86 return Complex(f32).new(x * x, x * (y - y));
86 return Complex(f32).init(x * x, x * (y - y));
8787 }
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));
8989 }
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));
9292}
9393
9494fn sinh64(z: Complex(f64)) Complex(f64) {
......@@ -107,61 +107,61 @@ fn sinh64(z: Complex(f64)) Complex(f64) {
107107
108108 if (ix < 0x7ff00000 and iy < 0x7ff00000) {
109109 if (iy | ly == 0) {
110 return Complex(f64).new(math.sinh(x), y);
110 return Complex(f64).init(math.sinh(x), y);
111111 }
112112 // small x: normal case
113113 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));
115115 }
116116
117117 // |x|>= 22, so cosh(x) ~= exp(|x|)
118118 if (ix < 0x40862e42) {
119119 // x < 710: exp(|x|) won't overflow
120120 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));
122122 }
123123 // x < 1455: scale to avoid overflow
124124 else if (ix < 0x4096bbaa) {
125 const v = Complex(f64).new(math.fabs(x), y);
125 const v = Complex(f64).init(math.fabs(x), y);
126126 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);
128128 }
129129 // x >= 1455: result always overflows
130130 else {
131131 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));
133133 }
134134 }
135135
136136 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);
138138 }
139139
140140 if (iy | ly == 0 and ix >= 0x7ff00000) {
141141 if ((hx & 0xfffff) | lx == 0) {
142 return Complex(f64).new(x, y);
142 return Complex(f64).init(x, y);
143143 }
144 return Complex(f64).new(x, math.copysign(f64, 0, y));
144 return Complex(f64).init(x, math.copysign(f64, 0, y));
145145 }
146146
147147 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));
149149 }
150150
151151 if (ix >= 0x7ff00000 and (hx & 0xfffff) | lx == 0) {
152152 if (iy >= 0x7ff00000) {
153 return Complex(f64).new(x * x, x * (y - y));
153 return Complex(f64).init(x * x, x * (y - y));
154154 }
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));
156156 }
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));
159159}
160160
161161const epsilon = 0.0001;
162162
163163test "complex.csinh32" {
164 const a = Complex(f32).new(5, 3);
164 const a = Complex(f32).init(5, 3);
165165 const c = sinh(a);
166166
167167 try testing.expect(math.approxEqAbs(f32, c.re, -73.460617, epsilon));
......@@ -169,7 +169,7 @@ test "complex.csinh32" {
169169}
170170
171171test "complex.csinh64" {
172 const a = Complex(f64).new(5, 3);
172 const a = Complex(f64).init(5, 3);
173173 const c = sinh(a);
174174
175175 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) {
3232 const y = z.im;
3333
3434 if (x == 0 and y == 0) {
35 return Complex(f32).new(0, y);
35 return Complex(f32).init(0, y);
3636 }
3737 if (math.isInf(y)) {
38 return Complex(f32).new(math.inf(f32), y);
38 return Complex(f32).init(math.inf(f32), y);
3939 }
4040 if (math.isNan(x)) {
4141 // raise invalid if y is not nan
4242 const t = (y - y) / (y - y);
43 return Complex(f32).new(x, t);
43 return Complex(f32).init(x, t);
4444 }
4545 if (math.isInf(x)) {
4646 // sqrt(inf + i nan) = inf + nan i
......@@ -48,9 +48,9 @@ fn sqrt32(z: Complex(f32)) Complex(f32) {
4848 // sqrt(-inf + i nan) = nan +- inf i
4949 // sqrt(-inf + iy) = 0 + inf i
5050 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));
5252 } 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));
5454 }
5555 }
5656
......@@ -62,13 +62,13 @@ fn sqrt32(z: Complex(f32)) Complex(f32) {
6262
6363 if (dx >= 0) {
6464 const t = math.sqrt((dx + math.hypot(f64, dx, dy)) * 0.5);
65 return Complex(f32).new(
65 return Complex(f32).init(
6666 @floatCast(f32, t),
6767 @floatCast(f32, dy / (2.0 * t)),
6868 );
6969 } else {
7070 const t = math.sqrt((-dx + math.hypot(f64, dx, dy)) * 0.5);
71 return Complex(f32).new(
71 return Complex(f32).init(
7272 @floatCast(f32, math.fabs(y) / (2.0 * t)),
7373 @floatCast(f32, math.copysign(f64, t, y)),
7474 );
......@@ -83,15 +83,15 @@ fn sqrt64(z: Complex(f64)) Complex(f64) {
8383 var y = z.im;
8484
8585 if (x == 0 and y == 0) {
86 return Complex(f64).new(0, y);
86 return Complex(f64).init(0, y);
8787 }
8888 if (math.isInf(y)) {
89 return Complex(f64).new(math.inf(f64), y);
89 return Complex(f64).init(math.inf(f64), y);
9090 }
9191 if (math.isNan(x)) {
9292 // raise invalid if y is not nan
9393 const t = (y - y) / (y - y);
94 return Complex(f64).new(x, t);
94 return Complex(f64).init(x, t);
9595 }
9696 if (math.isInf(x)) {
9797 // sqrt(inf + i nan) = inf + nan i
......@@ -99,9 +99,9 @@ fn sqrt64(z: Complex(f64)) Complex(f64) {
9999 // sqrt(-inf + i nan) = nan +- inf i
100100 // sqrt(-inf + iy) = 0 + inf i
101101 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));
103103 } 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));
105105 }
106106 }
107107
......@@ -118,10 +118,10 @@ fn sqrt64(z: Complex(f64)) Complex(f64) {
118118 var result: Complex(f64) = undefined;
119119 if (x >= 0) {
120120 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));
122122 } else {
123123 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));
125125 }
126126
127127 if (scale) {
......@@ -135,7 +135,7 @@ fn sqrt64(z: Complex(f64)) Complex(f64) {
135135const epsilon = 0.0001;
136136
137137test "complex.csqrt32" {
138 const a = Complex(f32).new(5, 3);
138 const a = Complex(f32).init(5, 3);
139139 const c = sqrt(a);
140140
141141 try testing.expect(math.approxEqAbs(f32, c.re, 2.327117, epsilon));
......@@ -143,7 +143,7 @@ test "complex.csqrt32" {
143143}
144144
145145test "complex.csqrt64" {
146 const a = Complex(f64).new(5, 3);
146 const a = Complex(f64).init(5, 3);
147147 const c = sqrt(a);
148148
149149 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;
1212/// Returns the tanget of z.
1313pub fn tan(z: anytype) Complex(@TypeOf(z.re)) {
1414 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);
1616 const r = cmath.tanh(q);
17 return Complex(T).new(r.im, -r.re);
17 return Complex(T).init(r.im, -r.re);
1818}
1919
2020const epsilon = 0.0001;
2121
2222test "complex.ctan" {
23 const a = Complex(f32).new(5, 3);
23 const a = Complex(f32).init(5, 3);
2424 const c = tan(a);
2525
2626 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) {
3636 if (ix >= 0x7f800000) {
3737 if (ix & 0x7fffff != 0) {
3838 const r = if (y == 0) y else x * y;
39 return Complex(f32).new(x, r);
39 return Complex(f32).init(x, r);
4040 }
4141 const xx = @bitCast(f32, hx - 0x40000000);
4242 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));
4444 }
4545
4646 if (!math.isFinite(y)) {
4747 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);
4949 }
5050
5151 // x >= 11
5252 if (ix >= 0x41300000) {
5353 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);
5555 }
5656
5757 // Kahan's algorithm
......@@ -61,7 +61,7 @@ fn tanh32(z: Complex(f32)) Complex(f32) {
6161 const rho = math.sqrt(1 + s * s);
6262 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);
6565}
6666
6767fn tanh64(z: Complex(f64)) Complex(f64) {
......@@ -78,23 +78,23 @@ fn tanh64(z: Complex(f64)) Complex(f64) {
7878 if (ix >= 0x7ff00000) {
7979 if ((ix & 0x7fffff) | lx != 0) {
8080 const r = if (y == 0) y else x * y;
81 return Complex(f64).new(x, r);
81 return Complex(f64).init(x, r);
8282 }
8383
8484 const xx = @bitCast(f64, (@as(u64, hx - 0x40000000) << 32) | lx);
8585 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));
8787 }
8888
8989 if (!math.isFinite(y)) {
9090 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);
9292 }
9393
9494 // x >= 22
9595 if (ix >= 0x40360000) {
9696 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);
9898 }
9999
100100 // Kahan's algorithm
......@@ -104,13 +104,13 @@ fn tanh64(z: Complex(f64)) Complex(f64) {
104104 const rho = math.sqrt(1 + s * s);
105105 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);
108108}
109109
110110const epsilon = 0.0001;
111111
112112test "complex.ctanh32" {
113 const a = Complex(f32).new(5, 3);
113 const a = Complex(f32).init(5, 3);
114114 const c = tanh(a);
115115
116116 try testing.expect(math.approxEqAbs(f32, c.re, 0.999913, epsilon));
......@@ -118,7 +118,7 @@ test "complex.ctanh32" {
118118}
119119
120120test "complex.ctanh64" {
121 const a = Complex(f64).new(5, 3);
121 const a = Complex(f64).init(5, 3);
122122 const c = tanh(a);
123123
124124 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" {
16081608 \\comptime {
16091609 \\ // cexp(finite|nan +- i inf|nan) = nan + i nan
16101610 \\ if ((hx & 0x7fffffff) != 0x7f800000) {
1611 \\ return Complex(f32).new(y - y, y - y);
1611 \\ return Complex(f32).init(y - y, y - y);
16121612 \\ } // cexp(-inf +- i inf|nan) = 0 + i0
16131613 \\ else if (hx & 0x80000000 != 0) {
1614 \\ return Complex(f32).new(0, 0);
1614 \\ return Complex(f32).init(0, 0);
16151615 \\ } // cexp(+inf +- i inf|nan) = inf + i nan
16161616 \\ else {
1617 \\ return Complex(f32).new(x, y - y);
1617 \\ return Complex(f32).init(x, y - y);
16181618 \\ }
16191619 \\}
16201620 \\
......@@ -2267,16 +2267,16 @@ test "zig fmt: line comment between if block and else keyword" {
22672267 \\test "aoeu" {
22682268 \\ // cexp(finite|nan +- i inf|nan) = nan + i nan
22692269 \\ if ((hx & 0x7fffffff) != 0x7f800000) {
2270 \\ return Complex(f32).new(y - y, y - y);
2270 \\ return Complex(f32).init(y - y, y - y);
22712271 \\ }
22722272 \\ // cexp(-inf +- i inf|nan) = 0 + i0
22732273 \\ else if (hx & 0x80000000 != 0) {
2274 \\ return Complex(f32).new(0, 0);
2274 \\ return Complex(f32).init(0, 0);
22752275 \\ }
22762276 \\ // cexp(+inf +- i inf|nan) = inf + i nan
22772277 \\ // another comment
22782278 \\ else {
2279 \\ return Complex(f32).new(x, y - y);
2279 \\ return Complex(f32).init(x, y - y);
22802280 \\ }
22812281 \\}
22822282 \\