Theoretica
A C++ numerical and automatic mathematical library
dual.h
Go to the documentation of this file.
1 
5 
6 #ifndef THEORETICA_DUAL_H
7 #define THEORETICA_DUAL_H
8 
9 #ifndef THEORETICA_NO_PRINT
10 #include <sstream>
11 #include <ostream>
12 #endif
13 
14 #include "../core/error.h"
15 #include "../core/constants.h"
16 #include "../algebra/algebra_types.h"
17 
18 
19 namespace theoretica {
20 
21 
28  class dual {
29  public:
30 
31  real a; // Real part
32  real b; // "Dual" part
33 
35  dual() : a(0), b(0) {}
36 
38  dual(real real_part, real dual_part)
39  : a(real_part), b(dual_part) {}
40 
42  dual(real real_part)
43  : a(real_part), b(0) {}
44 
45  ~dual() = default;
46 
48  dual(const vec2& v) {
49  a = v[0];
50  b = v[1];
51  }
52 
54  inline dual& operator=(const vec2& v) {
55  a = v[0];
56  b = v[1];
57  return *this;
58  }
59 
61  inline dual& operator=(real x) {
62  a = x;
63  b = 0;
64  return *this;
65  }
66 
68  inline dual& operator=(const std::array<real, 2>& v) {
69  a = v[0];
70  b = v[1];
71  return *this;
72  }
73 
75  inline const real& Re() const {
76  return a;
77  }
78 
80  inline real& Re() {
81  return a;
82  }
83 
85  inline friend real Re(const dual& d) {
86  return d.a;
87  }
88 
89 
91  inline friend real& Re(dual& d) {
92  return d.a;
93  }
94 
96  inline const real& Dual() const {
97  return b;
98  }
99 
101  inline real& Dual() {
102  return b;
103  }
104 
106  inline friend real Dual(const dual& d) {
107  return d.b;
108  }
109 
110 
112  inline friend real& Dual(dual& d) {
113  return d.b;
114  }
115 
117  inline dual conjugate() const {
118  return dual(a, -b);
119  }
120 
122  inline dual inverse() const {
123 
124  if(a == 0) {
125  TH_MATH_ERROR("dual::inverse", 0, DIV_BY_ZERO);
126  return dual(nan(), nan());
127  }
128 
129  return dual(1.0 / a, -b / square(a));
130  }
131 
133  inline dual operator+() const {
134  return dual(a, b);
135  }
136 
138  inline dual operator+(const dual& other) const {
139  return dual(a + other.a, b + other.b);
140  }
141 
143  inline dual operator+(real r) const {
144  return dual(a + r, b);
145  }
146 
148  inline dual operator-() const {
149  return dual(-a, -b);
150  }
151 
153  inline dual operator-(const dual& other) const {
154  return dual(a - other.a, b - other.b);
155  }
156 
158  inline dual operator-(real r) const {
159  return dual(a - r, b);
160  }
161 
163  inline dual operator*(const dual& other) const {
164  return dual(a * other.a, a * other.b + b * other.a);
165  }
166 
168  inline dual operator*(real r) const {
169  return dual(a * r, b * r);
170  }
171 
173  inline dual operator/(const dual& other) const {
174  return dual(a / other.a,
175  (b * other.a - a * other.b) / square(other.a));
176  }
177 
179  inline dual operator/(real r) const {
180  return dual(a / r, b / r);
181  }
182 
183 
185  inline dual& operator+=(real r) {
186 
187  a += r;
188  return *this;
189  }
190 
192  inline dual& operator-=(const dual& other) {
193 
194  a -= other.a;
195  b -= other.b;
196  return *this;
197  }
198 
200  inline dual& operator-=(real r) {
201 
202  a -= r;
203  return *this;
204  }
205 
207  inline dual& operator*=(const dual& other) {
208 
209  a = (a * other.a);
210  b = (a * other.b) + (b * other.a);
211  return *this;
212  }
213 
215  inline dual& operator*=(real r) {
216 
217  a *= r;
218  b *= r;
219  return *this;
220  }
221 
223  inline dual& operator/=(const dual& other) {
224 
225  a = (a / other.a);
226  b = (b * other.a - a * other.b) / square(other.a);
227  return *this;
228  }
229 
231  inline dual& operator/=(real r) {
232 
233  if(r == 0) {
234  TH_MATH_ERROR("dual::operator/=", 0, DIV_BY_ZERO);
235  a = nan();
236  b = nan();
237  return *this;
238  }
239 
240  a /= r;
241  b /= r;
242 
243  return *this;
244  }
245 
246 
249  inline bool operator==(const dual& other) {
250  return (a == other.a) && (b == other.b);
251  }
252 
253 
255  inline vec2 to_vec() const {
256  vec2 res;
257  res[0] = a;
258  res[1] = b;
259  return res;
260  }
261 
263  inline void from_vec(const vec2& v) {
264  a = v[0];
265  b = v[1];
266  }
267 
268 
270  inline mat2 to_mat() const {
271 
272  mat2 m;
273  m.at(0, 0) = a;
274  m.at(1, 0) = 0;
275  m.at(0, 1) = b;
276  m.at(1, 1) = a;
277  return m;
278  }
279 
280 
281  // Friend operators to enable equations of the form
282  // (real) op. (dual)
283 
284  inline friend dual operator+(real a, const dual& d) {
285  return d + a;
286  }
287 
288  inline friend dual operator-(real a, const dual& d) {
289  return -d + a;
290  }
291 
292  inline friend dual operator*(real a, const dual& d) {
293  return d * a;
294  }
295 
296  inline friend dual operator/(real a, const dual& d) {
297  return dual(a, 0) / d;
298  }
299 
300 
301 #ifndef THEORETICA_NO_PRINT
302 
305  inline std::string to_string(const std::string& epsilon = "e") const {
306 
307  std::stringstream res;
308 
309  res << a;
310  res << (b >= 0 ? " + " : " - ");
311  res << abs(b) << epsilon;
312 
313  return res.str();
314  }
315 
316 
318  inline operator std::string() {
319  return to_string();
320  }
321 
322 
325  inline friend std::ostream& operator<<(std::ostream& out, const dual& obj) {
326  return out << obj.to_string();
327  }
328 
329 #endif
330 
331  };
332 
333 }
334 
335 
336 #endif
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