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
local clone.
Page wiki
View or edit the community-maintained wiki page associated with this page.
std.complex
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. Authors:Lars Tandle Kyllingstad, Don Clugston License:
Boost License 1.0 Source:
std/complex.d
- pure nothrow @safe auto complex(T)(T re);
pure nothrow @safe auto complex(R, I)(R re, I im); - Helper function that returns a complex number with the specified
real and imaginary parts.
If neither re nor im are floating-point numbers, this
function returns a Complex!double. Otherwise, the return type
is deduced using std.traits.CommonType!(R, I).
Examples:
auto c = complex(2.0); static assert (is(typeof(c) == Complex!double)); assert (c.re == 2.0); assert (c.im == 0.0); auto w = complex(2); static assert (is(typeof(w) == Complex!double)); assert (w == c); auto z = complex(1, 3.14L); static assert (is(typeof(z) == Complex!real)); assert (z.re == 1.0L); assert (z.im == 3.14L);
- struct Complex(T) if (isFloatingPoint!(T));
- A complex number parametrised by a type T, which must be either
float, double or real.
- T re;
- The real part of the number.
- T im;
- The imaginary part of the number.
- const string toString(scope void delegate(const(char)[]) sink = null, string formatSpec = "%s");
- Converts the complex number to a string representation. If a sink delegate is specified, the string is passed to it and this function returns null. Otherwise, this function returns the string representation directly. The output format is controlled via formatSpec, which should consist of a single POSIX format specifier, including the percent (%) character. Note that complex numbers are floating point numbers, so the only valid format characters are 'e', 'f', 'g', 'a', and 's', where 's' gives the default behaviour. Positional parameters are not valid in this context. See the std.format documentation for more information.
- pure nothrow @safe T abs(T)(Complex!(T) z);
- Calculates the absolute value (or modulus) of a complex number.
- pure nothrow @safe T arg(T)(Complex!(T) z);
- Calculates the argument (or phase) of a complex number.
- pure nothrow @safe Complex!(T) conj(T)(Complex!(T) z);
- Returns the complex conjugate of a complex number.
- pure nothrow @safe Complex!(CommonType!(T, U)) fromPolar(T, U)(T modulus, U argument);
- Constructs a complex number given its absolute value and argument.
- pure nothrow @safe Complex!(T) sin(T)(Complex!(T) z);
pure nothrow @safe Complex!(T) cos(T)(Complex!(T) z); - Trigonometric functions.
- pure nothrow @trusted Complex!(real) expi(real y);
- Calculates cos(y) + i sin(y).
Note:
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. - pure nothrow @safe Complex!(T) sqrt(T)(Complex!(T) z);
- Square root.