Theoretica
Scientific Computing
Loading...
Searching...
No Matches
multidual_functions.h
Go to the documentation of this file.
1
5
6
7#ifndef THEORETICA_MULTIDUAL_FUNCTIONS_H
8#define THEORETICA_MULTIDUAL_FUNCTIONS_H
9
10#include "./multidual.h"
11#include "../core/real_analysis.h"
12
13
14namespace theoretica {
15
16
18 template<unsigned int N>
20 return x * x;
21 }
22
23
25 template<unsigned int N>
27 return x * x * x;
28 }
29
30
32 template<unsigned int N>
34 return x.conjugate();
35 }
36
37
39 template<unsigned int N>
41
42 const real pow_n_1_x = pow(x.Re(), n - 1);
43 return multidual<N>(pow_n_1_x * x.Re(), x.Dual() * pow_n_1_x * n);
44 }
45
46
48 template<unsigned int N>
50
51 real sqrt_x = sqrt(x.Re());
52
53 if(sqrt_x == 0) {
54 TH_MATH_ERROR("sqrt(multidual)", sqrt_x, MathError::DivByZero);
55 return multidual<N>(nan(), vec<real, N>(nan()));
56 }
57
58 return multidual<N>(sqrt_x, x.Dual() * 0.5 / sqrt_x);
59 }
60
61
63 template<unsigned int N>
65 return multidual<N>(sin(x.Re()), x.Dual() * cos(x.Re()));
66 }
67
68
70 template<unsigned int N>
72 return multidual<N>(cos(x.Re()), x.Dual() * -sin(x.Re()));
73 }
74
75
77 template<unsigned int N>
79
80 real cos_x = cos(x.Re());
81
82 if(cos_x == 0) {
83 TH_MATH_ERROR("tan(multidual)", cos_x, MathError::DivByZero);
84 return multidual<N>(nan(), vec<real, N>(nan()));
85 }
86
87 return multidual<N>(tan(x.Re()), x.Dual() / square(cos_x));
88 }
89
90
92 template<unsigned int N>
94
95 real sin_x = sin(x.Re());
96
97 if(sin_x == 0) {
98 TH_MATH_ERROR("cot(multidual)", sin_x, MathError::DivByZero);
99 return multidual<N>(nan(), vec<real, N>(nan()));
100 }
101
102 return multidual<N>(cot(x.Re()), x.Dual() * (-1 / square(sin_x)));
103 }
104
105
107 template<unsigned int N>
109 real exp_x = exp(x.Re());
110 return multidual<N>(exp_x, x.Dual() * exp_x);
111 }
112
113
115 template<unsigned int N>
117
118 if(x.Re() <= 0) {
119 TH_MATH_ERROR("ln(multidual)", x.Re(), MathError::OutOfDomain);
120 return multidual<N>(nan(), vec<real, N>(nan()));
121 }
122
123 return multidual<N>(ln(x.Re()), x.Dual() / x.Re());
124 }
125
126
128 template<unsigned int N>
130
131 if(x.Re() <= 0) {
132 TH_MATH_ERROR("log2(multidual)", x.Re(), MathError::OutOfDomain);
133 return multidual<N>(nan(), vec<real, N>(nan()));
134 }
135
136 return multidual<N>(log2(x.Re()), x.Dual() * LOG2E / x.Re());
137 }
138
139
141 template<unsigned int N>
143
144 if(x.Re() <= 0) {
145 TH_MATH_ERROR("log10(multidual)", x.Re(), MathError::OutOfDomain);
146 return multidual<N>(nan(), vec<real, N>(nan()));
147 }
148
149 return multidual<N>(log10(x.Re()), x.Dual() * LOG10E / x.Re());
150 }
151
152
154 template<unsigned int N>
156 return multidual<N>(abs(x.Re()), x.Dual() * sgn(x.Re()));
157 }
158
159
161 template<unsigned int N>
163
164 if(x.Re() >= 1) {
165 TH_MATH_ERROR("asin(multidual)", x.Re(), MathError::OutOfDomain);
166 return multidual<N>(nan(), vec<real, N>(nan()));
167 }
168
169 return multidual<N>(asin(x.Re()), x.Dual() / sqrt(1 - square(x.Re())));
170 }
171
172
174 template<unsigned int N>
176
177 if(x.Re() >= 1) {
178 TH_MATH_ERROR("acos(multidual)", x.Re(), MathError::OutOfDomain);
179 return multidual<N>(nan(), vec<real, N>(nan()));
180 }
181
182 return multidual<N>(acos(x.Re()), x.Dual() * (-1 / sqrt(1 - square(x.Re()))));
183 }
184
186 template<unsigned int N>
188 return multidual<N>(atan(x.Re()), x.Dual() / (1 + square(x.Re())));
189 }
190
191
193 template<unsigned int N>
195
196 real exp_x = exp(x.Re());
197 return multidual<N>((exp_x - 1.0 / exp_x) / 2.0, x.Dual() * (exp_x + 1.0 / exp_x) / 2.0);
198 }
199
200
202 template<unsigned int N>
204
205 real exp_x = exp(x.Re());
206 return multidual<N>((exp_x + 1.0 / exp_x) / 2.0, x.Dual() * (exp_x - 1.0 / exp_x) / 2.0);
207 }
208
209
211 template<unsigned int N>
213
214 const real exp_2x = exp(-2.0 * abs(x.Re()));
215
216 real t;
217 if (x.Re() >= 0.0)
218 t = (1.0 - exp_2x) / (1.0 + exp_2x);
219 else
220 t = (exp_2x - 1.0) / (1.0 + exp_2x);
221
222 const real dt = (4.0 * exp_2x) / square(1.0 + exp_2x);
223
224 return multidual<N>(t, x.Dual() * dt);
225 }
226
227}
228
229
230#endif
Multidual number algebra for functions of the form .
Definition multidual.h:26
vec< real, N > Dual() const
Get the multidual part.
Definition multidual.h:79
real Re() const
Get the real part.
Definition multidual.h:67
multidual conjugate() const
Get the multidual conjugate.
Definition multidual.h:107
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
Multidual numbers.
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