Theoretica
Scientific Computing
Loading...
Searching...
No Matches
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
19namespace 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
42
46
49 : a(real_part), b(0), c(0) {}
50
51 ~dual2() = default;
52
54 dual2(const vec3& v) {
55 a = v[0];
56 b = v[1];
57 c = v[2];
58 }
59
61 inline dual2& operator=(const vec3& v) {
62 a = v[0];
63 b = v[1];
64 c = v[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, MathError::DivByZero);
109 return dual2(nan(), nan(), nan());
110 }
111
112 return dual2(1.0 / a, -b / square(a), 2.0 * square(b) / cube(a) - c / square(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, MathError::DivByZero);
167 return dual2(nan(), nan(), nan());
168 }
169
170 return dual2(a / r, b / r, c / r);
171 }
172
173
175 inline dual2& operator+=(const dual2& other) {
176
177 a += other.a;
178 b += other.b;
179 c += other.c;
180 return *this;
181 }
182
183
186
187 a += r;
188 return *this;
189 }
190
192 inline dual2& operator-=(const dual2& other) {
193
194 a -= other.a;
195 b -= other.b;
196 c -= other.c;
197 return *this;
198 }
199
202
203 a -= r;
204 return *this;
205 }
206
208 inline dual2& operator*=(const dual2& other) {
209
210 const real a_old = a;
211 const real b_old = b;
212 const real c_old = c;
213
214 a = (a_old * other.a);
215 b = (a_old * other.b) + (b_old * other.a);
216 c = (a_old * other.c) + (2 * b_old * other.b) + (c_old * other.a);
217 return *this;
218 }
219
222 a *= r;
223 b *= r;
224 c *= r;
225 return *this;
226 }
227
229 inline dual2& operator/=(const dual2& other) {
230 *this = operator*(other.inverse());
231 return *this;
232 }
233
236
237 if(r == 0) {
238 TH_MATH_ERROR("dual::operator/=", 0, MathError::DivByZero);
239 a = nan();
240 b = nan();
241 c = nan();
242 return *this;
243 }
244
245 a /= r;
246 b /= r;
247 c /= r;
248
249 return *this;
250 }
251
252
255 inline bool operator==(const dual2& other) const {
256 return (a == other.a) && (b == other.b) && (c == other.c);
257 }
258
259
261 inline vec3 to_vec() const {
262 vec3 res;
263 res[0] = a;
264 res[1] = b;
265 res[2] = c;
266 return res;
267 }
268
270 inline void from_vec(const vec3& v) {
271 a = v[0];
272 b = v[1];
273 c = v[2];
274 }
275
276
277 // Friend operators to enable equations of the form
278 // (real) op. (dual2)
279
280 inline friend dual2 operator+(real a, const dual2& d) {
281 return d + a;
282 }
283
284 inline friend dual2 operator-(real a, const dual2& d) {
285 return -d + a;
286 }
287
288 inline friend dual2 operator*(real a, const dual2& d) {
289 return d * a;
290 }
291
292 inline friend dual2 operator/(real a, const dual2& d) {
293 return dual2(a, 0, 0) / d;
294 }
295
296
297#ifndef THEORETICA_NO_PRINT
298
302 inline std::string to_string(const std::string& epsilon1 = "ε",
303 const std::string& epsilon2 = "ε²") const {
304
305 std::stringstream res;
306
307 res << a;
308 res << (b >= 0 ? " + " : " - ");
309 res << abs(b);
310
311 res << epsilon1;
312
313 res << (c >= 0 ? " + " : " - ");
314 res << abs(c);
315
316 res << epsilon2;
317
318 return res.str();
319 }
320
321
323 inline operator std::string() {
324 return to_string();
325 }
326
327
330 inline friend std::ostream& operator<<(std::ostream& out, const dual2& obj) {
331 return out << obj.to_string();
332 }
333
334#endif
335
336 };
337
338}
339
340
341#endif
Second order dual number class.
Definition dual2.h:29
dual2(real real_part, real dual1_part)
Initialize from two real numbers.
Definition dual2.h:44
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-=(const dual2 &other)
Subtract a real number from this one.
Definition dual2.h:192
dual2 & operator*=(const dual2 &other)
Multiply this dual number by another one.
Definition dual2.h:208
dual2 operator+(real r) const
Sum a real number to a dual number.
Definition dual2.h:126
dual2 & operator*=(real r)
Multiply this dual number by a real number.
Definition dual2.h:221
dual2 operator+(const dual2 &other) const
Sum two dual numbers.
Definition dual2.h:121
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:261
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
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:330
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:185
dual2(const vec3 &v)
Initialize from a vec3.
Definition dual2.h:54
dual2(real real_part)
Initialize from a real number.
Definition dual2.h:48
real Dual2() const
Return second order dual part.
Definition dual2.h:95
dual2 & operator/=(real r)
Divide a dual number by a real number.
Definition dual2.h:235
void from_vec(const vec3 &v)
Initialize from a vector.
Definition dual2.h:270
dual2 operator*(real r) const
Multiply a dual number by a real number.
Definition dual2.h:153
dual2 & operator+=(const dual2 &other)
Add a dual2 number from this one.
Definition dual2.h:175
dual2 operator-(real r) const
Subtract a real number from a dual number.
Definition dual2.h:141
dual2 & operator=(real x)
Initialize a dual number from a real number.
Definition dual2.h:69
dual2(real real_part, real dual1_part, real dual2_part)
Initialize from two real numbers.
Definition dual2.h:40
dual2 & operator=(const std::array< real, 3 > &v)
Initialize a dual number from a std::array.
Definition dual2.h:77
dual2 & operator-=(real r)
Subtract a real number from this dual number.
Definition dual2.h:201
dual2 & operator=(const vec3 &v)
Initialize a dual number from a vec3.
Definition dual2.h:61
dual2 operator-() const
Get the opposite of a dual number.
Definition dual2.h:131
dual2 & operator/=(const dual2 &other)
Divide this dual number by another one.
Definition dual2.h:229
bool operator==(const dual2 &other) const
Check whether two dual numbers have the same real and dual parts.
Definition dual2.h:255
real Re() const
Return real part.
Definition dual2.h:85
std::string to_string(const std::string &epsilon1="ε", const std::string &epsilon2="ε²") const
Convert the dual number to string representation.
Definition dual2.h:302
dual2 operator+() const
Identity (for consistency)
Definition dual2.h:116
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:92
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compilation op...
Definition error.h:219
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:207
dual2 abs(dual2 x)
Compute the absolute value of a second order dual number.
Definition dual2_functions.h:242
Vector make_error()
Create a vector representing an error state, with all NaN values.
Definition algebra.h:103
TH_CONSTEXPR real nan()
Return a quiet NaN number in floating point representation.
Definition error.h:74
@ DivByZero
Division by zero.
dual2 square(dual2 x)
Return the square of a second order dual number.
Definition dual2_functions.h:23
dual2 cube(dual2 x)
Return the cube of a second order dual number.
Definition dual2_functions.h:29