Report a bug
If you spot a problem with this page, click here to create a Bugzilla 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.
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:
Source: std/complex.d
- pure nothrow @nogc @safe auto
complex
(R)(Rre
)
if (is(R : double));
pure nothrow @nogc @safe autocomplex
(R, I)(Rre
, Iim
)
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
numberI (template parameter) type of imaginary part of complex
numberR re
real part of complex
number to be constructedI im
(optional) imaginary part of complex
numberReturns:Complex instance with real and imaginary parts set to the values provided as input. If neitherre
norim
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 @safe string
toString
();
const voidtoString
(Writer, Char)(scope Writerw
, FormatSpec!CharformatSpec
)
if (isOutputRange!(Writer, const(Char)[])); - 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.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!Tz
); - Parameters:
Complex!T z
A complex number. Returns:The absolute value (or modulus) ofz
.Examples:static import std.math; assert (abs(complex(1.0)) == 1.0); assert (abs(complex(0.0, 1.0)) == 1.0); assert (abs(complex(1.0L, -2.0L)) == std.math.sqrt(5.0L));
- pure nothrow @nogc @safe T
sqAbs
(T)(Complex!Tz
);
pure nothrow @nogc @safe TsqAbs
(T)(Tx
)
if (isFloatingPoint!T); - Parameters:
Complex!T z
A complex number. T x
A real number. Returns:The squared modulus ofz
. For genericity, if called on a real number, returns its square.Examples:import std.math; assert (sqAbs(complex(0.0)) == 0.0); assert (sqAbs(complex(1.0)) == 1.0); assert (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));
- pure nothrow @nogc @safe T
arg
(T)(Complex!Tz
); - Parameters:
Complex!T z
A complex number. Returns:The argument (or phase) ofz
.Examples:import std.math; 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 Complex!T
conj
(T)(Complex!Tz
); - Parameters:
Complex!T z
A complex number. Returns:The complex conjugate ofz
.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 Complex!(CommonType!(T, U))
fromPolar
(T, U)(Tmodulus
, Uargument
); - Constructs a complex number given its absolute value and
argument
.Parameters:T modulus
The modulus
U argument
The argument
Returns:The complex number with the givenmodulus
andargument
.Examples: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));
- pure nothrow @nogc @safe Complex!T
sin
(T)(Complex!Tz
);
pure nothrow @nogc @safe Complex!Tcos
(T)(Complex!Tz
); - Trigonometric functions on complex numbers.Parameters:
Complex!T z
A complex number. Returns:The sine and cosine ofz
, respectively.Examples:static import std.math; assert(sin(complex(0.0)) == 0.0); assert(sin(complex(2.0L, 0)) == std.math.sin(2.0L));
Examples:import std.math; import std.complex; assert(cos(complex(0.0)) == 1.0); assert(cos(complex(1.3L)) == std.math.cos(1.3L)); assert(cos(complex(0, 5.2L)) == cosh(5.2L));
- pure nothrow @nogc @trusted Complex!real
expi
(realy
); - Parameters:
real y
A real number. Returns:The value of 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.Examples:static import std.math; assert(expi(1.3e5L) == complex(std.math.cos(1.3e5L), std.math.sin(1.3e5L))); assert(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);
- pure nothrow @nogc @safe Complex!T
sqrt
(T)(Complex!Tz
); - Parameters:
Complex!T z
A complex number. Returns:The square root ofz
.Examples:static import std.math; assert (sqrt(complex(0.0)) == 0.0); assert (sqrt(complex(1.0L, 0)) == std.math.sqrt(1.0L)); assert (sqrt(complex(-1.0L, 0)) == complex(0, 1.0L));
Copyright (c) 2010, Lars T. Kyllingstad.
| Page generated by
Ddoc on (no date time)