Theoretica
A C++ numerical and automatic mathematical library
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.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+=(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 a = (a * other.a);
211 b = (a * other.b) + (b * other.a);
212 c = (a * other.c) + (2 * b * other.b) + (c * other.a);
213 return *this;
214 }
215
218 a *= r;
219 b *= r;
220 c *= r;
221 return *this;
222 }
223
225 inline dual2& operator/=(const dual2& other) {
226 *this = operator*(other.inverse());
227 return *this;
228 }
229
232
233 if(r == 0) {
234 TH_MATH_ERROR("dual::operator/=", 0, DIV_BY_ZERO);
235 a = nan();
236 b = nan();
237 c = nan();
238 return *this;
239 }
240
241 a /= r;
242 b /= r;
243 c /= r;
244
245 return *this;
246 }
247
248
251 inline bool operator==(const dual2& other) {
252 return (a == other.a) && (b == other.b) && (c == other.c);
253 }
254
255
257 inline vec3 to_vec() const {
258 vec3 res;
259 res.at(0) = a;
260 res.at(1) = b;
261 res.at(2) = c;
262 return res;
263 }
264
266 inline void from_vec(const vec3& v) {
267 a = v.get(0);
268 b = v.get(1);
269 c = v.get(2);
270 }
271
272
273 // Friend operators to enable equations of the form
274 // (real) op. (dual2)
275
276 inline friend dual2 operator+(real a, const dual2& d) {
277 return d + a;
278 }
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 dual2(a, 0, 0) / d;
290 }
291
292
293#ifndef THEORETICA_NO_PRINT
294
298 inline std::string to_string(const std::string& epsilon1 = "e1",
299 const std::string& epsilon2 = "e2") const {
300
301 std::stringstream res;
302
303 res << a;
304 res << (b >= 0 ? " + " : " - ");
305 res << abs(b);
306
307 res << epsilon1;
308
309 res << (c >= 0 ? " + " : " - ");
310 res << abs(c);
311
312 res << epsilon2;
313
314 return res.str();
315 }
316
317
319 inline operator std::string() {
320 return to_string();
321 }
322
323
326 inline friend std::ostream& operator<<(std::ostream& out, const dual2& obj) {
327 return out << obj.to_string();
328 }
329
330#endif
331
332 };
333
334}
335
336
337#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:217
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:257
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:298
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:326
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:231
void from_vec(const vec3 &v)
Initialize from a vector.
Definition dual2.h:266
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:225
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:251
real Dual1() const
Return first order dual part.
Definition dual2.h:90
#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
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:198
dual2 abs(dual2 x)
Compute the absolute value of a second order dual number.
Definition dual2_functions.h:198
std::remove_reference_t< decltype(std::declval< Structure >()[0])> vector_element_t
Extract the type of a vector (or any indexable container) from its operator[].
Definition core_traits.h:134
vec< real, 3 > vec3
A 3-dimensional vector with real elements.
Definition algebra_types.h:42
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