Debian Codegraph @

src/Base/DualNumber.h

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2019 Viktor Titov (DeepSOIC) <[email protected]> *
5 * *
6 * This file is part of the FreeCAD CAx development system. *
7 * *
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU Library General Public *
10 * License as published by the Free Software Foundation; either *
11 * version 2 of the License, or (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Library General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Library General Public *
19 * License along with this library; see the file COPYING.LIB. If not, *
20 * write to the Free Software Foundation, Inc., 59 Temple Place, *
21 * Suite 330, Boston, MA 02111-1307, USA *
22 * *
23 ***************************************************************************/
24
25 #ifndef FREECAD_BASE_DUAL_NUMBER_H
26 #define FREECAD_BASE_DUAL_NUMBER_H
27
28 #include <cmath>
29
30 // NOLINTBEGIN(readability-identifier-length)
31 namespace Base
32 {
33
34
35 /**
36 * @brief Dual Numbers are 2-part numbers like complex numbers, but different
37 * algebra. They are denoted as a + b*eps, where eps^2 = 0. eps, the nilpotent,
38 * is like imaginary unit of complex numbers. The neat utility of dual numbers
39 * is that if you use them instead of normal numbers in a function like sin(),
40 * derivative is implicitly calculated as a multiplier to the dual part.
41 */
42 class DualNumber
43 {
44 public:
45 double re = 0.0;
46 double du = 0.0;
47
48 public:
49 DualNumber() = default;
50 DualNumber(double re, double du = 0.0) // NOLINT
51 : re(re)
52 , du(du)
53 {}
54 DualNumber operator-() const
55 {
56 return {-re, -du};
57 }
58 };
59
60 inline DualNumber operator+(DualNumber a, DualNumber b)
61 {
62 return {a.re + b.re, a.du + b.du};
63 }
64 inline DualNumber operator+(DualNumber a, double b)
65 {
66 return {a.re + b, a.du};
67 }
68 inline DualNumber operator+(double a, DualNumber b)
69 {
70 return {a + b.re, b.du};
71 }
72
73 inline DualNumber operator-(DualNumber a, DualNumber b)
74 {
75 return {a.re - b.re, a.du - b.du};
76 }
77 inline DualNumber operator-(DualNumber a, double b)
78 {
79 return {a.re - b, a.du};
80 }
81 inline DualNumber operator-(double a, DualNumber b)
82 {
83 return {a - b.re, -b.du};
84 }
85
86 inline DualNumber operator*(DualNumber a, DualNumber b)
87 {
88 return {a.re * b.re, a.re * b.du + a.du * b.re};
89 }
90 inline DualNumber operator*(double a, DualNumber b)
91 {
92 return {a * b.re, a * b.du};
93 }
94 inline DualNumber operator*(DualNumber a, double b)
95 {
96 return {a.re * b, a.du * b};
97 }
98
99 inline DualNumber operator/(DualNumber a, DualNumber b)
100 {
101 return {a.re / b.re, (a.du * b.re - a.re * b.du) / (b.re * b.re)};
102 }
103 inline DualNumber operator/(DualNumber a, double b)
104 {
105 return {a.re / b, a.du / b};
106 }
107
108 inline DualNumber pow(DualNumber a, double pw)
109 {
110 return {std::pow(a.re, pw), pw * std::pow(a.re, pw - 1.0) * a.du};
111 }
112 } // namespace Base
113 // NOLINTEND(readability-identifier-length)
114
115
116 #endif