6 #ifndef THEORETICA_DUAL_H
7 #define THEORETICA_DUAL_H
9 #ifndef THEORETICA_NO_PRINT
14 #include "../core/error.h"
15 #include "../core/constants.h"
16 #include "../algebra/algebra_types.h"
39 : a(real_part), b(dual_part) {}
43 : a(real_part), b(0) {}
139 return dual(a + other.a, b + other.b);
144 return dual(a + r, b);
154 return dual(a - other.a, b - other.b);
159 return dual(a - r, b);
164 return dual(a * other.a, a * other.b + b * other.a);
169 return dual(a * r, b * r);
174 return dual(a / other.a,
175 (b * other.a - a * other.b) /
square(other.a));
180 return dual(a / r, b / r);
210 b = (a * other.b) + (b * other.a);
226 b = (b * other.a - a * other.b) /
square(other.a);
250 return (a == other.a) && (b == other.b);
297 return dual(a, 0) / d;
301 #ifndef THEORETICA_NO_PRINT
307 std::stringstream res;
310 res << (b >= 0 ?
" + " :
" - ");
311 res <<
abs(b) << epsilon;
Dual number class.
Definition: dual.h:28
vec2 to_vec() const
Convert a dual number to a vector.
Definition: dual.h:255
dual operator/(const dual &other) const
Dual division.
Definition: dual.h:173
dual & operator=(real x)
Initialize a dual number from a real number.
Definition: dual.h:61
dual(const vec2 &v)
Initialize from a vec2.
Definition: dual.h:48
dual & operator*=(real r)
Multiply this dual number by a real number.
Definition: dual.h:215
dual conjugate() const
Get the dual conjugate.
Definition: dual.h:117
dual & operator-=(real r)
Subtract a real number from this dual number.
Definition: dual.h:200
dual operator*(real r) const
Multiply a dual number by a real number.
Definition: dual.h:168
friend std::ostream & operator<<(std::ostream &out, const dual &obj)
Stream the dual number in string representation to an output stream (std::ostream)
Definition: dual.h:325
dual & operator-=(const dual &other)
Subtract a real number from this one.
Definition: dual.h:192
dual operator-(const dual &other) const
Subtract two dual numbers.
Definition: dual.h:153
std::string to_string(const std::string &epsilon="e") const
Convert the dual number to string representation.
Definition: dual.h:305
real & Dual()
Return dual part.
Definition: dual.h:101
mat2 to_mat() const
Convert a dual number to matrix form.
Definition: dual.h:270
friend real & Dual(dual &d)
Extract the real part of the dual number.
Definition: dual.h:112
dual operator+(const dual &other) const
Sum two dual numbers.
Definition: dual.h:138
dual & operator*=(const dual &other)
Multiply this dual number by another one.
Definition: dual.h:207
friend real & Re(dual &d)
Extract the real part of the dual number.
Definition: dual.h:91
dual inverse() const
Get the inverse of a dual number.
Definition: dual.h:122
friend real Re(const dual &d)
Extract the real part of the dual number.
Definition: dual.h:85
void from_vec(const vec2 &v)
Initialize from a vector.
Definition: dual.h:263
real & Re()
Return real part.
Definition: dual.h:80
dual operator/(real r) const
Divide a dual number by a real number.
Definition: dual.h:179
dual & operator/=(const dual &other)
Divide this dual number by another one.
Definition: dual.h:223
dual operator-() const
Get the opposite of a dual number.
Definition: dual.h:148
const real & Re() const
Return real part.
Definition: dual.h:75
dual operator-(real r) const
Subtract a real number from a dual number.
Definition: dual.h:158
bool operator==(const dual &other)
Check whether two dual numbers have the same real and dual parts.
Definition: dual.h:249
dual(real real_part, real dual_part)
Initialize from two real numbers.
Definition: dual.h:38
dual(real real_part)
Initialize from a real number.
Definition: dual.h:42
dual operator*(const dual &other) const
Multiply two dual numbers.
Definition: dual.h:163
dual operator+() const
Identity (for consistency)
Definition: dual.h:133
dual operator+(real r) const
Sum a real number to a dual number.
Definition: dual.h:143
dual & operator=(const std::array< real, 2 > &v)
Initialize a dual number from a std::array.
Definition: dual.h:68
dual()
Default constructor, initialize with null values.
Definition: dual.h:35
friend real Dual(const dual &d)
Extract the real part of the dual number.
Definition: dual.h:106
dual & operator+=(real r)
Sum a real number to this dual number.
Definition: dual.h:185
const real & Dual() const
Return dual part.
Definition: dual.h:96
dual & operator=(const vec2 &v)
Initialize a dual number from a vec2.
Definition: dual.h:54
dual & operator/=(real r)
Divide a dual number by a real number.
Definition: dual.h:231
Type & at(unsigned int i, unsigned int j)
Access the element at the i-th row and j-th column.
Definition: mat.h:386
A statically allocated N-dimensional vector with elements of the given type.
Definition: vec.h:88
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compiling opti...
Definition: error.h:219
std::string string(size_t length)
Generate a random string made of human-readable ASCII characters.
Definition: random.h:102
Main namespace of the library which contains all functions and objects.
Definition: algebra.h:27
double real
A real number, defined as a floating point type.
Definition: constants.h:188
dual2 abs(dual2 x)
Compute the absolute value of a second order dual number.
Definition: dual2_functions.h:183
dual2 square(dual2 x)
Return the square of a second order dual number.
Definition: dual2_functions.h:23
real nan()
Return a quiet NaN number in floating point representation.
Definition: error.h:54