If you spot a problem with this page, click here to create a Bugzilla issue. 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.

# 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:

Source: std/complex.d

pure nothrow @nogc @safe auto complex(R)(R re) if (is(R : double));
pure nothrow @nogc @safe auto complex(R, I)(R re, I im) if (is(R : double) && is(I : double));
Helper function that returns a complex number with the specified real and imaginary parts.
Parameters:
 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
Returns:
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).
Examples:
```auto a = complex(1.0);
static assert (is(typeof(a) == Complex!double));
assert (a.re == 1.0);
assert (a.im == 0.0);

auto b = complex(2.0L);
static assert (is(typeof(b) == Complex!real));
assert (b.re == 2.0L);
assert (b.im == 0.0L);

auto c = complex(1.0, 2.0);
static assert (is(typeof(c) == Complex!double));
assert (c.re == 1.0);
assert (c.im == 2.0);

auto d = complex(3.0, 4.0L);
static assert (is(typeof(d) == Complex!real));
assert (d.re == 3.0);
assert (d.im == 4.0L);

auto e = complex(1);
static assert (is(typeof(e) == Complex!double));
assert (e.re == 1);
assert (e.im == 0);

auto f = complex(1L, 2);
static assert (is(typeof(f) == Complex!double));
assert (f.re == 1L);
assert (f.im == 2);

auto g = complex(3, 4.0L);
static assert (is(typeof(g) == Complex!real));
assert (g.re == 3);
assert (g.im == 4.0L);
```
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();
const void toString(Char)(scope void delegate(const(Char)[]) sink, FormatSpec!Char formatSpec);
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'.

Examples:
```auto c = complex(1.2, 3.4);

// Vanilla toString formatting:
assert(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;
assert(format("%.2f", c)  == "1.20+3.40i");
assert(format("%4.1f", c) == " 1.2+ 3.4i");
```
pure nothrow @nogc @safe T abs(T)(Complex!T z);
Calculates the absolute value (or modulus) of a complex number.
pure nothrow @nogc @safe T sqAbs(T)(Complex!T z);
pure nothrow @nogc @safe T sqAbs(T)(T x) if (isFloatingPoint!T);
Calculates the squared modulus of a complex number. For genericity, if called on a real number, sqAbs returns its square.
pure nothrow @nogc @safe T arg(T)(Complex!T z);
Calculates the argument (or phase) of a complex number.
pure nothrow @nogc @safe Complex!T conj(T)(Complex!T z);
Returns the complex conjugate of a complex number.
pure nothrow @nogc @safe Complex!(CommonType!(T, U)) fromPolar(T, U)(T modulus, U argument);
Constructs a complex number given its absolute value and argument.
pure nothrow @nogc @safe Complex!T sin(T)(Complex!T z);
pure nothrow @nogc @safe Complex!T cos(T)(Complex!T z);
Trigonometric functions.
pure nothrow @nogc @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 @nogc @safe Complex!T sqrt(T)(Complex!T z);
Square root.