Debian Codegraph @

libphobos/testsuite/libphobos.phobos/std_complex.d

raw
1 @safe pure nothrow unittest
2 {
3 import std.complex;
4
5 auto a = complex(1.0);
6 static assert(is(typeof(a) == Complex!double));
7 assert(a.re == 1.0);
8 assert(a.im == 0.0);
9
10 auto b = complex(2.0L);
11 static assert(is(typeof(b) == Complex!real));
12 assert(b.re == 2.0L);
13 assert(b.im == 0.0L);
14
15 auto c = complex(1.0, 2.0);
16 static assert(is(typeof(c) == Complex!double));
17 assert(c.re == 1.0);
18 assert(c.im == 2.0);
19
20 auto d = complex(3.0, 4.0L);
21 static assert(is(typeof(d) == Complex!real));
22 assert(d.re == 3.0);
23 assert(d.im == 4.0L);
24
25 auto e = complex(1);
26 static assert(is(typeof(e) == Complex!double));
27 assert(e.re == 1);
28 assert(e.im == 0);
29
30 auto f = complex(1L, 2);
31 static assert(is(typeof(f) == Complex!double));
32 assert(f.re == 1L);
33 assert(f.im == 2);
34
35 auto g = complex(3, 4.0L);
36 static assert(is(typeof(g) == Complex!real));
37 assert(g.re == 3);
38 assert(g.im == 4.0L);
39 }
40
41 @safe unittest
42 {
43 import std.complex;
44
45 auto c = complex(1.2, 3.4);
46
47 // Vanilla toString formatting:
48 assert(c.toString() == "1.2+3.4i");
49
50 // Formatting with std.string.format specs: the precision and width
51 // specifiers apply to both the real and imaginary parts of the
52 // complex number.
53 import std.format : format;
54 assert(format("%.2f", c) == "1.20+3.40i");
55 assert(format("%4.1f", c) == " 1.2+ 3.4i");
56
57 }
58
59 @safe pure nothrow unittest
60 {
61 import std.complex;
62
63 static import core.math;
64 assert(abs(complex(1.0)) == 1.0);
65 assert(abs(complex(0.0, 1.0)) == 1.0);
66 assert(abs(complex(1.0L, -2.0L)) == core.math.sqrt(5.0L));
67 }
68
69 @safe pure nothrow unittest
70 {
71 import std.complex;
72
73 import std.math.operations : isClose;
74 assert(sqAbs(complex(0.0)) == 0.0);
75 assert(sqAbs(complex(1.0)) == 1.0);
76 assert(sqAbs(complex(0.0, 1.0)) == 1.0);
77 assert(isClose(sqAbs(complex(1.0L, -2.0L)), 5.0L));
78 assert(isClose(sqAbs(complex(-3.0L, 1.0L)), 10.0L));
79 assert(isClose(sqAbs(complex(1.0f,-1.0f)), 2.0f));
80 }
81
82 @safe pure nothrow unittest
83 {
84 import std.complex;
85
86 import std.math.constants : PI_2, PI_4;
87 assert(arg(complex(1.0)) == 0.0);
88 assert(arg(complex(0.0L, 1.0L)) == PI_2);
89 assert(arg(complex(1.0L, 1.0L)) == PI_4);
90 }
91
92 @safe pure nothrow @nogc unittest
93 {
94 import std.complex;
95
96 import std.math.operations : isClose;
97 import std.math.constants : PI;
98 assert(norm(complex(3.0, 4.0)) == 25.0);
99 assert(norm(fromPolar(5.0, 0.0)) == 25.0);
100 assert(isClose(norm(fromPolar(5.0L, PI / 6)), 25.0L));
101 assert(isClose(norm(fromPolar(5.0L, 13 * PI / 6)), 25.0L));
102 }
103
104 @safe pure nothrow unittest
105 {
106 import std.complex;
107
108 assert(conj(complex(1.0)) == complex(1.0));
109 assert(conj(complex(1.0, 2.0)) == complex(1.0, -2.0));
110 }
111
112 @safe pure nothrow unittest
113 {
114 import std.complex;
115
116 assert(proj(complex(1.0)) == complex(1.0));
117 assert(proj(complex(double.infinity, 5.0)) == complex(double.infinity, 0.0));
118 assert(proj(complex(5.0, -double.infinity)) == complex(double.infinity, -0.0));
119 }
120
121 @safe pure nothrow unittest
122 {
123 import std.complex;
124
125 import core.math;
126 import std.math.operations : isClose;
127 import std.math.algebraic : sqrt;
128 import std.math.constants : PI_4;
129 auto z = fromPolar(core.math.sqrt(2.0L), PI_4);
130 assert(isClose(z.re, 1.0L));
131 assert(isClose(z.im, 1.0L));
132 }
133
134 @safe pure nothrow unittest
135 {
136 import std.complex;
137
138 static import core.math;
139 assert(sin(complex(0.0)) == 0.0);
140 assert(sin(complex(2.0, 0)) == core.math.sin(2.0));
141 }
142
143 @safe pure nothrow unittest
144 {
145 import std.complex;
146
147 static import core.math;
148 static import std.math;
149 assert(cos(complex(0.0)) == 1.0);
150 assert(cos(complex(1.3, 0.0)) == core.math.cos(1.3));
151 assert(cos(complex(0.0, 5.2)) == std.math.cosh(5.2));
152 }
153
154 @safe pure nothrow @nogc unittest
155 {
156 import std.complex;
157
158 static import std.math;
159
160 int ceqrel(T)(const Complex!T x, const Complex!T y) @safe pure nothrow @nogc
161 {
162 import std.math.operations : feqrel;
163 const r = feqrel(x.re, y.re);
164 const i = feqrel(x.im, y.im);
165 return r < i ? r : i;
166 }
167 assert(ceqrel(tan(complex(1.0, 0.0)), complex(std.math.tan(1.0), 0.0)) >= double.mant_dig - 2);
168 assert(ceqrel(tan(complex(0.0, 1.0)), complex(0.0, std.math.tanh(1.0))) >= double.mant_dig - 2);
169 }
170
171 @safe pure nothrow unittest
172 {
173 import std.complex;
174
175 import std.math.operations : isClose;
176 import std.math.constants : PI;
177 assert(asin(complex(0.0)) == 0.0);
178 assert(isClose(asin(complex(0.5L)), PI / 6));
179 }
180
181 @safe pure nothrow unittest
182 {
183 import std.complex;
184
185 import std.math.operations : isClose;
186 import std.math.constants : PI;
187 import std.math.trigonometry : std_math_acos = acos;
188 assert(acos(complex(0.0)) == std_math_acos(0.0));
189 assert(isClose(acos(complex(0.5L)), PI / 3));
190 }
191
192 @safe pure nothrow @nogc unittest
193 {
194 import std.complex;
195
196 import std.math.operations : isClose;
197 import std.math.constants : PI;
198 assert(atan(complex(0.0)) == 0.0);
199 assert(isClose(atan(sqrt(complex(3.0L))), PI / 3));
200 assert(isClose(atan(sqrt(complex(3.0f))), float(PI) / 3));
201 }
202
203 @safe pure nothrow unittest
204 {
205 import std.complex;
206
207 static import std.math;
208 assert(sinh(complex(0.0)) == 0.0);
209 assert(sinh(complex(1.0L)) == std.math.sinh(1.0L));
210 assert(sinh(complex(1.0f)) == std.math.sinh(1.0f));
211 }
212
213 @safe pure nothrow unittest
214 {
215 import std.complex;
216
217 static import std.math;
218 assert(cosh(complex(0.0)) == 1.0);
219 assert(cosh(complex(1.0L)) == std.math.cosh(1.0L));
220 assert(cosh(complex(1.0f)) == std.math.cosh(1.0f));
221 }
222
223 @safe pure nothrow @nogc unittest
224 {
225 import std.complex;
226
227 import std.math.operations : isClose;
228 import std.math.trigonometry : std_math_tanh = tanh;
229 assert(tanh(complex(0.0)) == 0.0);
230 assert(isClose(tanh(complex(1.0L)), std_math_tanh(1.0L)));
231 assert(isClose(tanh(complex(1.0f)), std_math_tanh(1.0f)));
232 }
233
234 @safe pure nothrow unittest
235 {
236 import std.complex;
237
238 import std.math.operations : isClose;
239 import std.math.trigonometry : std_math_asinh = asinh;
240 assert(asinh(complex(0.0)) == 0.0);
241 assert(isClose(asinh(complex(1.0L)), std_math_asinh(1.0L)));
242 assert(isClose(asinh(complex(1.0f)), std_math_asinh(1.0f)));
243 }
244
245 @safe pure nothrow unittest
246 {
247 import std.complex;
248
249 import std.math.operations : isClose;
250 import std.math.trigonometry : std_math_acosh = acosh;
251 assert(acosh(complex(1.0)) == 0.0);
252 assert(isClose(acosh(complex(3.0L)), std_math_acosh(3.0L)));
253 assert(isClose(acosh(complex(3.0f)), std_math_acosh(3.0f)));
254 }
255
256 @safe pure nothrow @nogc unittest
257 {
258 import std.complex;
259
260 import std.math.operations : isClose;
261 import std.math.trigonometry : std_math_atanh = atanh;
262 assert(atanh(complex(0.0)) == 0.0);
263 assert(isClose(atanh(complex(0.5L)), std_math_atanh(0.5L)));
264 assert(isClose(atanh(complex(0.5f)), std_math_atanh(0.5f)));
265 }
266
267 @safe pure nothrow unittest
268 {
269 import std.complex;
270
271 import core.math : cos, sin;
272 assert(expi(0.0L) == 1.0L);
273 assert(expi(1.3e5L) == complex(cos(1.3e5L), sin(1.3e5L)));
274 }
275
276 @safe pure nothrow @nogc unittest
277 {
278 import std.complex;
279
280 import std.math.trigonometry : cosh, sinh;
281 assert(coshisinh(3.0L) == complex(cosh(3.0L), sinh(3.0L)));
282 }
283
284 @safe pure nothrow unittest
285 {
286 import std.complex;
287
288 static import core.math;
289 assert(sqrt(complex(0.0)) == 0.0);
290 assert(sqrt(complex(1.0L, 0)) == core.math.sqrt(1.0L));
291 assert(sqrt(complex(-1.0L, 0)) == complex(0, 1.0L));
292 assert(sqrt(complex(-8.0, -6.0)) == complex(1.0, -3.0));
293 }
294
295 @safe pure nothrow @nogc unittest
296 {
297 import std.complex;
298
299 import std.math.operations : isClose;
300 import std.math.constants : PI;
301
302 assert(exp(complex(0.0, 0.0)) == complex(1.0, 0.0));
303
304 auto a = complex(2.0, 1.0);
305 assert(exp(conj(a)) == conj(exp(a)));
306
307 auto b = exp(complex(0.0L, 1.0L) * PI);
308 assert(isClose(b, -1.0L, 0.0, 1e-15));
309 }
310
311 @safe pure nothrow @nogc unittest
312 {
313 import std.complex;
314
315 import core.math : sqrt;
316 import std.math.constants : PI;
317 import std.math.operations : isClose;
318
319 auto a = complex(2.0, 1.0);
320 assert(log(conj(a)) == conj(log(a)));
321
322 auto b = 2.0 * log10(complex(0.0, 1.0));
323 auto c = 4.0 * log10(complex(sqrt(2.0) / 2, sqrt(2.0) / 2));
324 assert(isClose(b, c, 0.0, 1e-15));
325
326 assert(log(complex(-1.0L, 0.0L)) == complex(0.0L, PI));
327 assert(log(complex(-1.0L, -0.0L)) == complex(0.0L, -PI));
328 }
329
330 @safe pure nothrow @nogc unittest
331 {
332 import std.complex;
333
334 import core.math : sqrt;
335 import std.math.constants : LN10, PI;
336 import std.math.operations : isClose;
337
338 auto a = complex(2.0, 1.0);
339 assert(log10(a) == log(a) / log(complex(10.0)));
340
341 auto b = log10(complex(0.0, 1.0)) * 2.0;
342 auto c = log10(complex(sqrt(2.0) / 2, sqrt(2.0) / 2)) * 4.0;
343 assert(isClose(b, c, 0.0, 1e-15));
344 }
345
346 @safe pure nothrow @nogc unittest
347 {
348 import std.complex;
349
350 import std.math.operations : isClose;
351
352 auto a = complex(1.0, 2.0);
353 assert(pow(a, 2) == a * a);
354 assert(pow(a, 3) == a * a * a);
355 assert(pow(a, -2) == 1.0 / (a * a));
356 assert(isClose(pow(a, -3), 1.0 / (a * a * a)));
357 }
358
359 @safe pure nothrow @nogc unittest
360 {
361 import std.complex;
362
363 import std.math.operations : isClose;
364 assert(pow(complex(0.0), 2.0) == complex(0.0));
365 assert(pow(complex(5.0), 2.0) == complex(25.0));
366
367 auto a = pow(complex(-1.0, 0.0), 0.5);
368 assert(isClose(a, complex(0.0, +1.0), 0.0, 1e-16));
369
370 auto b = pow(complex(-1.0, -0.0), 0.5);
371 assert(isClose(b, complex(0.0, -1.0), 0.0, 1e-16));
372 }
373
374 @safe pure nothrow @nogc unittest
375 {
376 import std.complex;
377
378 import std.math.operations : isClose;
379 import std.math.exponential : exp;
380 import std.math.constants : PI;
381 auto a = complex(0.0);
382 auto b = complex(2.0);
383 assert(pow(a, b) == complex(0.0));
384
385 auto c = complex(0.0L, 1.0L);
386 assert(isClose(pow(c, c), exp((-PI) / 2)));
387 }
388
389 @safe pure nothrow @nogc unittest
390 {
391 import std.complex;
392
393 import std.math.operations : isClose;
394 assert(pow(2.0, complex(0.0)) == complex(1.0));
395 assert(pow(2.0, complex(5.0)) == complex(32.0));
396
397 auto a = pow(-2.0, complex(-1.0));
398 assert(isClose(a, complex(-0.5), 0.0, 1e-16));
399
400 auto b = pow(-0.5, complex(-1.0));
401 assert(isClose(b, complex(-2.0), 0.0, 1e-15));
402 }
403