Report a bug
If you spot a problem with this page, click here to create a GitHub issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

mir.complex

Complex numbers
Authors:
Ilia Ki, Lars Tandle Kyllingstad, Don Clugston
struct Complex(T) if (is(T == float) || is(T == double) || is(T == real));

Complex!T complex(T)(const T re, const T im = 0)
if (is(T == float) || is(T == double) || is(T == real));
Generic complex number type
T re;
Real part. Default value is zero.
T im;
Imaginary part. Default value is zero.
ref Complex opAssign(R)(Complex!R rhs)
if (!is(R == T));
ref Complex opAssign(F)(const F rhs)
if (isNumeric!F);
ref Complex opOpAssign(string op : "+", R)(Complex!R rhs) return;
ref Complex opOpAssign(string op : "-", R)(Complex!R rhs) return;
ref Complex opOpAssign(string op, R)(Complex!R rhs) return
if (op == "*" || op == "/");
ref Complex opOpAssign(string op : "+", R)(const R rhs) return
if (isNumeric!R);
ref Complex opOpAssign(string op : "-", R)(const R rhs) return
if (isNumeric!R);
ref Complex opOpAssign(string op : "*", R)(const R rhs) return
if (isNumeric!R);
ref Complex opOpAssign(string op : "/", R)(const R rhs) return
if (isNumeric!R);
bool opEquals(const Complex rhs);
size_t toHash();
bool opEquals(R)(Complex!R rhs)
if (!is(R == T));
bool opEquals(F)(const F rhs)
if (isNumeric!F);
Complex opUnary(string op : "+")();
Complex opUnary(string op : "-")();
Complex!(CommonType!(T, R)) opBinary(string op : "+", R)(Complex!R rhs);
Complex!(CommonType!(T, R)) opBinary(string op : "-", R)(Complex!R rhs);
Complex!(CommonType!(T, R)) opBinary(string op : "*", R)(Complex!R rhs);
Complex!(CommonType!(T, R)) opBinary(string op : "/", R)(Complex!R rhs);
Complex!(CommonType!(T, R)) opBinary(string op : "+", R)(const R rhs)
if (isNumeric!R);
Complex!(CommonType!(T, R)) opBinary(string op : "-", R)(const R rhs)
if (isNumeric!R);
Complex!(CommonType!(T, R)) opBinary(string op : "*", R)(const R rhs)
if (isNumeric!R);
Complex!(CommonType!(T, R)) opBinary(string op : "/", R)(const R rhs)
if (isNumeric!R);
Complex!(CommonType!(T, R)) opBinaryRight(string op : "+", R)(const R rhs)
if (isNumeric!R);
Complex!(CommonType!(T, R)) opBinaryRight(string op : "-", R)(const R rhs)
if (isNumeric!R);
Complex!(CommonType!(T, R)) opBinaryRight(string op : "*", R)(const R rhs)
if (isNumeric!R);
Complex!(CommonType!(T, R)) opBinaryRight(string op : "/", R)(const R rhs)
if (isNumeric!R);
R opCast(R)()
if (isNumeric!R || isComplex!R);
pure nothrow @nogc @safe Complex!T fromPolar(T)(const T modulus, const T argument)
if (__traits(isFloating, T));
Constructs a complex number given its absolute value and argument.
Parameters:
T modulus The modulus
T argument The argument
Returns:
The complex number with the given modulus and argument.
Examples:
import mir.math : approxEqual, PI, sqrt;
auto z = fromPolar(sqrt(2.0), double(PI / 4));
assert(approxEqual(z.re, 1.0));
assert(approxEqual(z.im, 1.0));
pure nothrow @nogc @safe Complex!T conj(T)(Complex!T z);
Parameters:
Complex!T z A complex number.
Returns:
The complex conjugate of z.
Examples:
assert(conj(complex(1.0)) == complex(1.0));
assert(conj(complex(1.0, 2.0)) == complex(1.0, -2.0));
pure nothrow @nogc @safe T arg(T)(Complex!T z);
Parameters:
Complex!T z A complex number.
Returns:
The argument (or phase) of z.
Examples:
import mir.math.constant: PI_2, PI_4;
assert(arg(complex(1.0)) == 0.0);
assert(arg(complex(0.0L, 1.0L)) == PI_2);
assert(arg(complex(1.0L, 1.0L)) == PI_4);
pure nothrow @nogc @safe T cabs(T)(Complex!T z);
Parameters:
Complex!T z A complex number.
Returns:
The absolute value (or modulus) of z.
Examples:
import mir.math.common: sqrt;
assert(cabs(complex(1.0)) == 1.0);
assert(cabs(complex(0.0, 1.0)) == 1.0);
assert(cabs(complex(1.0L, -2.0L)) == sqrt(5.0L));