Theoretica
Scientific Computing
Loading...
Searching...
No Matches
dual2_functions.h
Go to the documentation of this file.
1
6
7
8#ifndef THEORETICA_DUAL2_FUNCTIONS_H
9#define THEORETICA_DUAL2_FUNCTIONS_H
10
11#include "./dual2.h"
12#include "../core/real_analysis.h"
13
14
15namespace theoretica {
16
17
18 // Second derivative of the composite function:
19 // (f(g))'' = f''(g) * g'' + f'(g) * g''
20
21
23 inline dual2 square(dual2 x) {
24 return x * x;
25 }
26
27
29 inline dual2 cube(dual2 x) {
30 return x * x * x;
31 }
32
33
35 inline dual2 conjugate(dual2 x) {
36 return x.conjugate();
37 }
38
39
41 inline dual2 pow(dual2 x, int n) {
42
43 const real pow_n_2_x = pow(x.Re(), n - 2);
44
45 real f = pow_n_2_x * square(x.Re());
46 real df = pow_n_2_x * x.Re() * n * x.Dual1();
47 real d2f = (pow_n_2_x * n * (n - 1)) * square(x.Dual1()) + df * x.Dual2();
48
49 return dual2(f, df, d2f);
50 }
51
52
54 inline dual2 sqrt(dual2 x) {
55
56 const real sqrt_x = sqrt(x.Re());
57
58 if(sqrt_x == 0) {
60 return dual2(nan(), nan());
61 }
62
63 return dual2(
64 sqrt_x,
65 0.5 / sqrt_x * x.Dual1(),
66 -0.25 / x.Re() / sqrt_x * square(x.Dual1()) + 0.5 / sqrt_x * x.Dual2()
67 );
68 }
69
70
72 inline dual2 sin(dual2 x) {
73
74 const real sin_x = sin(x.Re());
75 const real cos_x = cos(x.Re());
76
77 return dual2(
78 sin_x,
79 cos_x * x.Dual1(),
80 -sin_x * square(x.Dual1()) + cos_x * x.Dual2()
81 );
82 }
83
84
86 inline dual2 cos(dual2 x) {
87
88 const real sin_x = sin(x.Re());
89 const real cos_x = cos(x.Re());
90
91 return dual2(
92 cos_x,
93 -sin_x * x.Dual1(),
94 -cos_x * square(x.Dual1()) - sin_x * x.Dual2()
95 );
96 }
97
98
100 inline dual2 tan(dual2 x) {
101
102 const real sin_x = sin(x.Re());
103 const real cos_x = cos(x.Re());
104
105 if(cos_x == 0) {
107 return dual2(nan(), nan());
108 }
109
110 return dual2(
111 tan(x.Re()),
112 x.Dual1() / square(cos_x),
113 2.0 * sin_x / cube(cos_x) * square(x.Dual1()) + x.Dual2() / square(cos_x)
114 );
115 }
116
117
119 inline dual2 cot(dual2 x) {
120
121 const real sin_x = sin(x.Re());
122 const real cos_x = cos(x.Re());
123
124 if(sin_x == 0) {
126 return dual2(nan(), nan());
127 }
128
129 return dual2(
130 cot(x.Re()),
131 -x.Dual1() / square(sin_x),
132 -2.0 * cos_x / cube(sin_x) * square(x.Dual1()) - x.Dual2() / square(sin_x)
133 );
134 }
135
136
138 inline dual2 exp(dual2 x) {
139
140 const real exp_x = exp(x.Re());
141
142 return dual2(
143 exp_x,
144 x.Dual1() * exp_x,
145 square(x.Dual1()) * exp_x + x.Dual2() * exp_x
146 );
147 }
148
149
151 inline dual2 sinh(dual2 x) {
152
153 const real sinh_x = sinh(x.Re());
154 const real cosh_x = cosh(x.Re());
155
156 return dual2(
157 sinh_x,
158 cosh_x * x.Dual1(),
159 cosh_x * square(x.Dual1()) + sinh_x * x.Dual2()
160 );
161 }
162
163
165 inline dual2 cosh(dual2 x) {
166
167 const real sinh_x = sinh(x.Re());
168 const real cosh_x = cosh(x.Re());
169
170 return dual2(
171 cosh_x,
172 sinh_x * x.Dual1(),
173 sinh_x * square(x.Dual1()) + cosh_x * x.Dual2()
174 );
175 }
176
177
179 inline dual2 tanh(dual2 x) {
180
181 const real sinh_x = sinh(x.Re());
182 const real cosh_x = cosh(x.Re());
183
184 const real tanh_x = sinh_x / cosh_x;
185
186 return dual2(
187 tanh_x,
188 x.Dual1() / square(cosh_x),
189 2.0 * sinh_x / cube(cosh_x) * square(x.Dual1()) + x.Dual2() / square(cosh_x)
190 );
191 }
192
193
195 inline dual2 ln(dual2 x) {
196
197 if(x.Re() <= 0) {
198 TH_MATH_ERROR("ln(dual2)", x.Re(), MathError::OutOfDomain);
199 return dual2(nan(), nan());
200 }
201
202 return dual2(ln(x.Re()),
203 x.Dual1() / x.Re(),
204 -square(x.Dual1()) / square(x.Re()) + x.Dual2() / x.Re()
205 );
206 }
207
208
210 inline dual2 log2(dual2 x) {
211
212 if(x.Re() <= 0) {
213 TH_MATH_ERROR("log2(dual2)", x.Re(), MathError::OutOfDomain);
214 return dual2(nan(), nan());
215 }
216
217 return dual2(
218 log2(x.Re()),
219 x.Dual1() * LOG2E / x.Re(),
220 -square(x.Dual1()) * LOG2E / square(x.Re()) + x.Dual2() * LOG2E / x.Re()
221 );
222 }
223
224
226 inline dual2 log10(dual2 x) {
227
228 if(x.Re() <= 0) {
229 TH_MATH_ERROR("log10(dual2)", x.Re(), MathError::OutOfDomain);
230 return dual2(nan(), nan());
231 }
232
233 return dual2(
234 log10(x.Re()),
235 x.Dual1() * LOG10E / x.Re(),
236 -square(x.Dual1()) * LOG10E / square(x.Re()) + x.Dual2() * LOG10E / x.Re()
237 );
238 }
239
240
242 inline dual2 abs(dual2 x) {
243 return dual2(abs(x.Re()), x.Dual1() * sgn(x.Re()), 0);
244 }
245
246
248 inline dual2 asin(dual2 x) {
249
250 if(x.Re() >= 1) {
251 TH_MATH_ERROR("asin(dual2)", x.Re(), MathError::OutOfDomain);
252 return dual2(nan(), nan());
253 }
254
255 const real sqrt_1mx2 = sqrt(1 - square(x.Re()));
256
257 return dual2(
258 asin(x.Re()),
259 x.Dual1() / sqrt_1mx2,
260 square(x.Dual1()) * x.Re() / ((1 - square(x.Re())) * sqrt_1mx2)
261 + x.Dual2() / sqrt_1mx2
262 );
263 }
264
265
267 inline dual2 acos(dual2 x) {
268
269 if(x.Re() >= 1) {
270 TH_MATH_ERROR("acos(dual2)", x.Re(), MathError::OutOfDomain);
271 return dual2(nan(), nan());
272 }
273
274 const real sqrt_1mx2 = sqrt(1 - square(x.Re()));
275
276 return dual2(
277 acos(x.Re()),
278 -x.Dual1() / sqrt_1mx2,
279 -square(x.Dual1()) * x.Re() / ((1 - square(x.Re())) * sqrt_1mx2)
280 - x.Dual2() / sqrt_1mx2
281 );
282 }
283
284
286 inline dual2 atan(dual2 x) {
287
288 return dual2(
289 atan(x.Re()),
290 x.Dual1() / (1 + square(x.Re())),
291 -square(x.Dual1()) * 2 * x.Re() / square(1 + square(x.Re()))
292 + x.Dual2() / (1 + square(x.Re()))
293 );
294 }
295
296}
297
298
299#endif
Second order dual number class.
Definition dual2.h:29
dual2 conjugate() const
Get the dual conjugate.
Definition dual2.h:100
real Dual2() const
Return second order dual part.
Definition dual2.h:95
real Re() const
Return real part.
Definition dual2.h:85
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 compilation op...
Definition error.h:219
Second order dual number class.
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 sqrt(dual2 x)
Compute the square root of a second order dual number.
Definition dual2_functions.h:54
dual2 ln(dual2 x)
Compute the natural logarithm of a second order dual number.
Definition dual2_functions.h:195
dual2 abs(dual2 x)
Compute the absolute value of a second order dual number.
Definition dual2_functions.h:242
dual2 log2(dual2 x)
Compute the natural logarithm of a second order dual number.
Definition dual2_functions.h:210
dual2 asin(dual2 x)
Compute the arcsine of a second order dual number.
Definition dual2_functions.h:248
dual2 exp(dual2 x)
Compute the exponential of a second order dual number.
Definition dual2_functions.h:138
Vector make_error()
Create a vector representing an error state, with all NaN values.
Definition algebra.h:103
dual2 log10(dual2 x)
Compute the natural logarithm of a second order dual number.
Definition dual2_functions.h:226
dual2 conjugate(dual2 x)
Return the conjugate of a second order dual number.
Definition dual2_functions.h:35
constexpr real LOG10E
The base-10 logarithm of e.
Definition constants.h:255
constexpr real LOG2E
The binary logarithm of e.
Definition constants.h:249
dual2 tanh(dual2 x)
Compute the hyperbolic tangent of a second order dual number.
Definition dual2_functions.h:179
TH_CONSTEXPR real nan()
Return a quiet NaN number in floating point representation.
Definition error.h:74
dual2 sinh(dual2 x)
Compute the hyperbolic sine of a second order dual number.
Definition dual2_functions.h:151
dual2 cos(dual2 x)
Compute the cosine of a second order dual number.
Definition dual2_functions.h:86
dual2 cot(dual2 x)
Compute the cotangent of a second order dual number.
Definition dual2_functions.h:119
@ OutOfDomain
Argument out of domain.
@ DivByZero
Division by zero.
dual2 tan(dual2 x)
Compute the tangent of a second order dual number.
Definition dual2_functions.h:100
dual2 cosh(dual2 x)
Compute the hyperbolic cosine of a second order dual number.
Definition dual2_functions.h:165
dual2 acos(dual2 x)
Compute the arcosine of a second order dual number.
Definition dual2_functions.h:267
int sgn(real x)
Return the sign of x (1 if positive, -1 if negative, 0 if null)
Definition real_analysis.h:259
dual2 sin(dual2 x)
Compute the sine of a second order dual number.
Definition dual2_functions.h:72
dual2 square(dual2 x)
Return the square of a second order dual number.
Definition dual2_functions.h:23
dual2 atan(dual2 x)
Compute the arctangent of a second order dual number.
Definition dual2_functions.h:286
dual2 pow(dual2 x, int n)
Compute the n-th power of a second order dual number.
Definition dual2_functions.h:41
dual2 cube(dual2 x)
Return the cube of a second order dual number.
Definition dual2_functions.h:29