Theoretica
A C++ numerical and automatic mathematical library
dual2.h
Go to the documentation of this file.
1 
5 
6 #ifndef THEORETICA_DUAL2_H
7 #define THEORETICA_DUAL2_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 
29  class dual2 {
30  public:
31 
32  real a; // Real part
33  real b; // First order dual part
34  real c; // Second order dual part
35 
37  dual2() : a(0), b(0), c(0) {}
38 
40  dual2(real real_part, real dual1_part, real dual2_part)
41  : a(real_part), b(dual1_part), c(dual2_part) {}
42 
44  dual2(real real_part, real dual1_part)
45  : a(real_part), b(dual1_part), c(0) {}
46 
48  dual2(real real_part)
49  : a(real_part), b(0), c(0) {}
50 
51  ~dual2() = default;
52 
54  dual2(const vec3& v) {
55  a = v.get(0);
56  b = v.get(1);
57  c = v.get(2);
58  }
59 
61  inline dual2& operator=(const vec3& v) {
62  a = v.get(0);
63  b = v.get(1);
64  c = v.get(2);
65  return *this;
66  }
67 
69  inline dual2& operator=(real x) {
70  a = x;
71  b = 0;
72  c = 0;
73  return *this;
74  }
75 
77  inline dual2& operator=(const std::array<real, 3>& v) {
78  a = v[0];
79  b = v[1];
80  c = v[2];
81  return *this;
82  }
83 
85  inline real Re() const {
86  return a;
87  }
88 
90  inline real Dual1() const {
91  return b;
92  }
93 
95  inline real Dual2() const {
96  return c;
97  }
98 
100  inline dual2 conjugate() const {
101  return dual2(a, -b, -c);
102  }
103 
105  inline dual2 inverse() const {
106 
107  if(a == 0) {
108  TH_MATH_ERROR("dual2::inverse", 0, DIV_BY_ZERO);
109  return dual2(nan(), nan(), nan());
110  }
111 
112  return dual2(1.0 / a, -b / square(a), 2 * b / cube(a));
113  }
114 
116  inline dual2 operator+() const {
117  return dual2(a, b, c);
118  }
119 
121  inline dual2 operator+(const dual2& other) const {
122  return dual2(a + other.a, b + other.b, c + other.c);
123  }
124 
126  inline dual2 operator+(real r) const {
127  return dual2(a + r, b, c);
128  }
129 
131  inline dual2 operator-() const {
132  return dual2(-a, -b, -c);
133  }
134 
136  inline dual2 operator-(const dual2& other) const {
137  return dual2(a - other.a, b - other.b, c - other.c);
138  }
139 
141  inline dual2 operator-(real r) const {
142  return dual2(a - r, b, c);
143  }
144 
146  inline dual2 operator*(const dual2& other) const {
147  return dual2(a * other.a,
148  a * other.b + b * other.a,
149  a * other.c + 2 * b * other.b + c * other.a);
150  }
151 
153  inline dual2 operator*(real r) const {
154  return dual2(a * r, b * r, c * r);
155  }
156 
158  inline dual2 operator/(const dual2& other) const {
159  return operator*(other.inverse());
160  }
161 
163  inline dual2 operator/(real r) const {
164 
165  if(r == 0) {
166  TH_MATH_ERROR("dual2::operator/", r, DIV_BY_ZERO);
167  return dual2(nan(), nan(), nan());
168  }
169 
170  return dual2(a / r, b / r, c / r);
171  }
172 
173 
175  inline dual2& operator+=(real r) {
176 
177  a += r;
178  return *this;
179  }
180 
182  inline dual2& operator-=(const dual2& other) {
183 
184  a -= other.a;
185  b -= other.b;
186  c -= other.c;
187  return *this;
188  }
189 
191  inline dual2& operator-=(real r) {
192 
193  a -= r;
194  return *this;
195  }
196 
198  inline dual2& operator*=(const dual2& other) {
199 
200  a = (a * other.a);
201  b = (a * other.b) + (b * other.a);
202  c = (a * other.c) + (2 * b * other.b) + (c * other.a);
203  return *this;
204  }
205 
207  inline dual2& operator*=(real r) {
208  a *= r;
209  b *= r;
210  c *= r;
211  return *this;
212  }
213 
215  inline dual2& operator/=(const dual2& other) {
216  *this = operator*(other.inverse());
217  return *this;
218  }
219 
221  inline dual2& operator/=(real r) {
222 
223  if(r == 0) {
224  TH_MATH_ERROR("dual::operator/=", 0, DIV_BY_ZERO);
225  a = nan();
226  b = nan();
227  c = nan();
228  return *this;
229  }
230 
231  a /= r;
232  b /= r;
233  c /= r;
234 
235  return *this;
236  }
237 
238 
241  inline bool operator==(const dual2& other) {
242  return (a == other.a) && (b == other.b) && (c == other.c);
243  }
244 
245 
247  inline vec3 to_vec() const {
248  vec3 res;
249  res.at(0) = a;
250  res.at(1) = b;
251  res.at(2) = c;
252  return res;
253  }
254 
256  inline void from_vec(const vec3& v) {
257  a = v.get(0);
258  b = v.get(1);
259  c = v.get(2);
260  }
261 
262 
263  // Friend operators to enable equations of the form
264  // (real) op. (dual2)
265 
266  inline friend dual2 operator+(real a, const dual2& d) {
267  return d + a;
268  }
269 
270  inline friend dual2 operator-(real a, const dual2& d) {
271  return -d + a;
272  }
273 
274  inline friend dual2 operator*(real a, const dual2& d) {
275  return d * a;
276  }
277 
278  inline friend dual2 operator/(real a, const dual2& d) {
279  return dual2(a, 0, 0) / d;
280  }
281 
282 
283 #ifndef THEORETICA_NO_PRINT
284 
288  inline std::string to_string(const std::string& epsilon1 = "e1",
289  const std::string& epsilon2 = "e2") const {
290 
291  std::stringstream res;
292 
293  res << a;
294  res << (b >= 0 ? " + " : " - ");
295  res << abs(b);
296 
297  res << epsilon1;
298 
299  res << (c >= 0 ? " + " : " - ");
300  res << abs(c);
301 
302  res << epsilon2;
303 
304  return res.str();
305  }
306 
307 
309  inline operator std::string() {
310  return to_string();
311  }
312 
313 
316  inline friend std::ostream& operator<<(std::ostream& out, const dual2& obj) {
317  return out << obj.to_string();
318  }
319 
320 #endif
321 
322  };
323 
324 }
325 
326 
327 #endif
Second order dual number class.
Definition: dual2.h:29
dual2 & operator/=(real r)
Divide a dual number by a real number.
Definition: dual2.h:221
dual2 & operator*=(real r)
Multiply this dual number by a real number.
Definition: dual2.h:207
dual2(real real_part, real dual1_part)
Initialize from two real numbers.
Definition: dual2.h:44
friend std::ostream & operator<<(std::ostream &out, const dual2 &obj)
Stream the dual number in string representation to an output stream (std::ostream)
Definition: dual2.h:316
dual2 & operator=(const vec3 &v)
Initialize a dual number from a vec3.
Definition: dual2.h:61
dual2 operator*(const dual2 &other) const
Multiply two dual numbers.
Definition: dual2.h:146
dual2 conjugate() const
Get the dual conjugate.
Definition: dual2.h:100
dual2 operator+(real r) const
Sum a real number to a dual number.
Definition: dual2.h:126
dual2 operator+(const dual2 &other) const
Sum two dual numbers.
Definition: dual2.h:121
dual2 & operator-=(real r)
Subtract a real number from this dual number.
Definition: dual2.h:191
dual2 operator/(const dual2 &other) const
Dual division.
Definition: dual2.h:158
vec3 to_vec() const
Convert a dual number to a vector.
Definition: dual2.h:247
std::string to_string(const std::string &epsilon1="e1", const std::string &epsilon2="e2") const
Convert the dual number to string representation.
Definition: dual2.h:288
dual2 inverse() const
Get the inverse of a dual number.
Definition: dual2.h:105
dual2 operator/(real r) const
Divide a dual number by a real number.
Definition: dual2.h:163
dual2()
Default constructor, initialize with null values.
Definition: dual2.h:37
dual2 operator-(const dual2 &other) const
Subtract two dual numbers.
Definition: dual2.h:136
dual2 & operator+=(real r)
Sum a real number to this dual number.
Definition: dual2.h:175
dual2 & operator=(const std::array< real, 3 > &v)
Initialize a dual number from a std::array.
Definition: dual2.h:77
dual2(const vec3 &v)
Initialize from a vec3.
Definition: dual2.h:54
dual2 & operator-=(const dual2 &other)
Subtract a real number from this one.
Definition: dual2.h:182
dual2(real real_part)
Initialize from a real number.
Definition: dual2.h:48
dual2 & operator=(real x)
Initialize a dual number from a real number.
Definition: dual2.h:69
real Dual2() const
Return second order dual part.
Definition: dual2.h:95
void from_vec(const vec3 &v)
Initialize from a vector.
Definition: dual2.h:256
dual2 operator*(real r) const
Multiply a dual number by a real number.
Definition: dual2.h:153
dual2 & operator/=(const dual2 &other)
Divide this dual number by another one.
Definition: dual2.h:215
dual2 & operator*=(const dual2 &other)
Multiply this dual number by another one.
Definition: dual2.h:198
dual2 operator-(real r) const
Subtract a real number from a dual number.
Definition: dual2.h:141
dual2(real real_part, real dual1_part, real dual2_part)
Initialize from two real numbers.
Definition: dual2.h:40
dual2 operator-() const
Get the opposite of a dual number.
Definition: dual2.h:131
real Re() const
Return real part.
Definition: dual2.h:85
dual2 operator+() const
Identity (for consistency)
Definition: dual2.h:116
bool operator==(const dual2 &other)
Check whether two dual numbers have the same real and dual parts.
Definition: dual2.h:241
real Dual1() const
Return first order dual part.
Definition: dual2.h:90
A statically allocated N-dimensional vector with elements of the given type.
Definition: vec.h:88
Type get(unsigned int i) const
Getters and setters.
Definition: vec.h:322
Type & at(unsigned int i)
Access i-th element.
Definition: vec.h:316
#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
dual2 cube(dual2 x)
Return the cube of a second order dual number.
Definition: dual2_functions.h:29