Dual

Dual number for automatic differentation

Constructors

this
this(Dual!R d)
this(R re)
this(Rr re, Rd du)

Construct a dual number with the specified real and dual parts. In the case where a single argument is passed that is not a dual number, the imaginary part of the result will be zero.

Members

Functions

opAssign
Dual opAssign(R rhs)
Undocumented in source. Be warned that the author may not have intended to support it.
opAssign
Dual opAssign(Dual!R rhs)
Undocumented in source. Be warned that the author may not have intended to support it.
opBinary
Dual!(CommonType!(T, R)) opBinary(Dual!R rhs)
Undocumented in source. Be warned that the author may not have intended to support it.
opBinary
Dual!(CommonType!(T, R)) opBinary(Dual!R rhs)
Undocumented in source. Be warned that the author may not have intended to support it.
opBinary
Dual!(CommonType!(T, R)) opBinary(Dual!R rhs)
Undocumented in source. Be warned that the author may not have intended to support it.
opBinary
Dual!(CommonType!(T, R)) opBinary(R rhs)
Undocumented in source. Be warned that the author may not have intended to support it.
opBinary
Dual!(CommonType!(T, R)) opBinary(R rhs)
Undocumented in source. Be warned that the author may not have intended to support it.
opBinary
Dual!T opBinary(R rhs)
Undocumented in source. Be warned that the author may not have intended to support it.
opBinaryRight
Dual!(CommonType!(T, R)) opBinaryRight(R lhs)
Undocumented in source. Be warned that the author may not have intended to support it.
opBinaryRight
Dual!(CommonType!(T, R)) opBinaryRight(R lhs)
Undocumented in source. Be warned that the author may not have intended to support it.
opBinaryRight
Dual!(CommonType!(T, R)) opBinaryRight(R lhs)
Undocumented in source. Be warned that the author may not have intended to support it.
opCmp
int opCmp(Dual!R z)
Undocumented in source. Be warned that the author may not have intended to support it.
opCmp
int opCmp(R r)
Undocumented in source. Be warned that the author may not have intended to support it.
opEquals
bool opEquals(Dual!R z)
Undocumented in source. Be warned that the author may not have intended to support it.
opEquals
bool opEquals(R r)
Undocumented in source. Be warned that the author may not have intended to support it.
opOpAssign
Dual opOpAssign(D rhs)
Undocumented in source. Be warned that the author may not have intended to support it.
opOpAssign
Dual opOpAssign(R rhs)
Undocumented in source. Be warned that the author may not have intended to support it.
opOpAssign
Dual opOpAssign(R rhs)
Undocumented in source. Be warned that the author may not have intended to support it.
opUnary
Dual opUnary()
Undocumented in source. Be warned that the author may not have intended to support it.
toString
string toString()
void toString(Writer w, FormatSpec!Char formatSpec)

Converts the dual 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.

Variables

du
T du;

The dual part

re
T re;

The real part

Examples

To calculate the derivate of a function, set the dual part of the variable by which you want to derive to 1. Other dual numbers should have dual part of 0. Then just calculate the function like you would with normal numbers. The real part is always identically to the result with normal numbers. After the calculation the dual part holds the derivate of the function.

import std.math;
// f(x) = x⁵ f'(x) = 5x⁴ for x = 2
const x = Dual!double(2.0, 1.0);
auto f2 = x^^5;
assert(approxEqual(f2.re, 2.0^^5));
assert(approxEqual(f2.du, 5.0 * 2.0^^4));

// f(x) = 3x² f'(x) = 6x for x = 2
f2 = 3.0 * x * x;
assert(approxEqual(f2.re, 3.0 * 2.0 * 2.0));
assert(approxEqual(f2.du, 6.0 * 2.0));

// f(x) = 3/(1-x) f'(x) = 3/(1-x)^2 for x = 2
f2 = 3.0/(1.0 - x);
assert(approxEqual(f2.re, -3.0));
assert(approxEqual(f2.du, 3.0));

// f(x) = 3exp(2x), f'(x) = 6exp(2x) for x = 2
f2 = 3 * exp(2 * x);
assert(approxEqual(f2.re, 3 * std.math.exp(4.0)));
assert(approxEqual(f2.du, 6 * std.math.exp(4.0)));

Meta