This module contains the Complex
type, which is used to represent complex numbers, along with related mathematical operations and functions.
Complex
will eventually replace the built-in types cfloat
, cdouble
, creal
, ifloat
, idouble
, and ireal
.
Helper function that returns a complex number with the specified real and imaginary parts.
R | (template parameter) type of real part of complex number |
I | (template parameter) type of imaginary part of complex number |
R re
| real part of complex number to be constructed |
I im
| (optional) imaginary part of complex number, 0 if omitted. |
Complex
instance with real and imaginary parts set to the values provided as input. If neither re
nor im
are floating-point numbers, the return type will be Complex!double
. Otherwise, the return type is deduced using std.traits.CommonType!(R, I)
.auto a = complex(1.0); static assert(is(typeof(a) == Complex!double)); writeln(a.re); // 1.0 writeln(a.im); // 0.0 auto b = complex(2.0L); static assert(is(typeof(b) == Complex!real)); writeln(b.re); // 2.0L writeln(b.im); // 0.0L auto c = complex(1.0, 2.0); static assert(is(typeof(c) == Complex!double)); writeln(c.re); // 1.0 writeln(c.im); // 2.0 auto d = complex(3.0, 4.0L); static assert(is(typeof(d) == Complex!real)); writeln(d.re); // 3.0 writeln(d.im); // 4.0L auto e = complex(1); static assert(is(typeof(e) == Complex!double)); writeln(e.re); // 1 writeln(e.im); // 0 auto f = complex(1L, 2); static assert(is(typeof(f) == Complex!double)); writeln(f.re); // 1L writeln(f.im); // 2 auto g = complex(3, 4.0L); static assert(is(typeof(g) == Complex!real)); writeln(g.re); // 3 writeln(g.im); // 4.0L
A complex number parametrised by a type T
, which must be either float
, double
or real
.
The real part of the number.
The imaginary part of the number.
Converts the complex number to a string representation.
The second form of this function is usually not called directly; instead, it is used via std.string.format
, as shown in the examples below. Supported format characters are 'e', 'f', 'g', 'a', and 's'.
See the std.format
and std.string.format
documentation for more information.
auto c = complex(1.2, 3.4); // Vanilla toString formatting: writeln(c.toString()); // "1.2+3.4i" // Formatting with std.string.format specs: the precision and width // specifiers apply to both the real and imaginary parts of the // complex number. import std.format : format; writeln(format("%.2f", c)); // "1.20+3.40i" writeln(format("%4.1f", c)); // " 1.2+ 3.4i"
Construct a complex number with the specified real and imaginary parts. In the case where a single argument is passed that is not complex, the imaginary part of the result will be zero.
Complex!T z
| A complex number. |
z
.static import std.math; writeln(abs(complex(1.0))); // 1.0 writeln(abs(complex(0.0, 1.0))); // 1.0 writeln(abs(complex(1.0L, -2.0L))); // std.math.sqrt(5.0L)
Complex!T z
| A complex number. |
T x
| A real number. |
z
. For genericity, if called on a real number, returns its square.import std.math; writeln(sqAbs(complex(0.0))); // 0.0 writeln(sqAbs(complex(1.0))); // 1.0 writeln(sqAbs(complex(0.0, 1.0))); // 1.0 assert(approxEqual(sqAbs(complex(1.0L, -2.0L)), 5.0L)); assert(approxEqual(sqAbs(complex(-3.0L, 1.0L)), 10.0L)); assert(approxEqual(sqAbs(complex(1.0f,-1.0f)), 2.0f));
Complex!T z
| A complex number. |
z
.import std.math; writeln(arg(complex(1.0))); // 0.0 writeln(arg(complex(0.0L, 1.0L))); // PI_2 writeln(arg(complex(1.0L, 1.0L))); // PI_4
Complex!T z
| A complex number. |
z
.writeln(conj(complex(1.0))); // complex(1.0) writeln(conj(complex(1.0, 2.0))); // complex(1.0, -2.0)
Constructs a complex number given its absolute value and argument
.
T modulus
| The modulus
|
U argument
| The argument
|
modulus
and argument
.import std.math; auto z = fromPolar(std.math.sqrt(2.0), PI_4); assert(approxEqual(z.re, 1.0L, real.epsilon)); assert(approxEqual(z.im, 1.0L, real.epsilon));
Trigonometric functions on complex numbers.
Complex!T z
| A complex number. |
z
, respectively.static import std.math; writeln(sin(complex(0.0))); // 0.0 writeln(sin(complex(2.0L, 0))); // std.math.sin(2.0L)
import std.complex; import std.math; writeln(cos(complex(0.0))); // 1.0 writeln(cos(complex(1.3L))); // std.math.cos(1.3L) writeln(cos(complex(0, 5.2L))); // cosh(5.2L)
real y
| A real number. |
y
) + i sin(y
). expi
is included here for convenience and for easy migration of code that uses std.math.expi
. Unlike std.math.expi
, which uses the x87 fsincos instruction when possible, this function is no faster than calculating cos(y
) and sin(y
) separately.static import std.math; writeln(expi(1.3e5L)); // complex(std.math.cos(1.3e5L), std.math.sin(1.3e5L)) writeln(expi(0.0L)); // 1.0L auto z1 = expi(1.234); auto z2 = std.math.expi(1.234); assert(z1.re == z2.re && z1.im == z2.im);
Complex!T z
| A complex number. |
z
.static import std.math; writeln(sqrt(complex(0.0))); // 0.0 writeln(sqrt(complex(1.0L, 0))); // std.math.sqrt(1.0L) writeln(sqrt(complex(-1.0L, 0))); // complex(0, 1.0L)
© 1999–2017 The D Language Foundation
Licensed under the Boost License 1.0.
https://dlang.org/phobos/std_complex.html