Theoretica
Scientific Computing
Loading...
Searching...
No Matches
autodiff.h
Go to the documentation of this file.
1
5
6#ifndef THEORETICA_AUTODIFF_H
7#define THEORETICA_AUTODIFF_H
8
9#include "dual.h"
10#include "dual2.h"
11#include "multidual.h"
12#include "../algebra/vec.h"
13#include "../algebra/mat.h"
14#include "../core/error.h"
15#include "../core/core_traits.h"
16#include "./autodiff_types.h"
17
18#include <functional>
19
20
21namespace theoretica {
22
24 namespace autodiff {
25
26
27 // Univariate automatic differentiation
28
29
37 template <
38 typename DualFunction = std::function<dual(dual)>,
39 enable_dual_func<DualFunction> = true
40 >
41 inline real deriv(DualFunction f, real x) {
42 return f(dual(x, 1.0)).Dual();
43 }
44
45
54 template <
55 typename DualFunction = std::function<dual(dual)>,
56 enable_dual_func<DualFunction> = true
57 >
58 inline auto deriv(DualFunction f) {
59
60 return [f](real x) -> real {
61 return autodiff::deriv(f, x);
62 };
63 }
64
65
73 template <
74 typename Dual2Function = std::function<dual2(dual2)>,
75 enable_dual2_func<Dual2Function> = true
76 >
77 inline real deriv2(Dual2Function f, real x) {
78 return f(dual2(x, 1.0, 0.0)).Dual2();
79 }
80
81
90 template <
91 typename Dual2Function = std::function<dual2(dual2)>,
92 enable_dual2_func<Dual2Function> = true
93 >
94 inline auto deriv2(Dual2Function f) {
95
96 return [f](real x) -> real {
97 return autodiff::deriv2(f, x);
98 };
99 }
100
101
102 // Differential operators
103
104
117#ifdef THEORETICA_HAS_CPP20
118 template<ADScalarField Function, VectorType Vector>
119#else
120 template <
121 typename Function, typename Vector = vec<real>,
122 enable_scalar_field<Function> = true,
123 enable_vector<Vector> = true
124 >
125#endif
126 inline auto gradient(Function f, const Vector& x) {
127
128 // Extract the return type of the function
129 using MultidualType = return_type_t<Function>;
130
131 constexpr size_t N = MultidualType::size_argument;
133 arg.resize(x.size());
134
135 // Iterate over each element, setting the dual part to
136 // the i-th element of the canonical base
137 for (unsigned int i = 0; i < x.size(); ++i) {
138 arg[i] = MultidualType(
139 x[i], vec<real, N>::euclidean_base(i, x.size())
140 );
141 }
142
143 return f(arg).Dual();
144 }
145
146
158#ifdef THEORETICA_HAS_CPP20
159 template<ADScalarField Function>
160#else
161 template <
162 typename Function,
163 enable_scalar_field<Function> = true
164 >
165#endif
166 inline auto gradient(Function f) {
167
168 constexpr size_t N = return_type_t<Function>::size_argument;
169
170 return [f](const vec<real, N>& x) {
171 return gradient(f, x);
172 };
173 }
174
175
187#ifdef THEORETICA_HAS_CPP20
188 template<ADVectorField Function, VectorType Vector>
189#else
190 template <
191 typename Function, typename Vector = vec<real>,
192 enable_vector_field<Function> = true,
193 enable_vector<Vector> = true
194 >
195#endif
196 inline real divergence(Function V, const Vector& x) {
197
198 using MultidualT = return_type_t<Function>;
199 const size_t N = MultidualT::size_argument;
200
201 // Sum the diagonal elements of the jacobian
202 const vec<multidual<N>, N> res = V(multidual<N>::make_argument(x));
203
204 real div = 0.0;
205 for (unsigned int i = 0; i < res.size(); ++i) {
206
207 // dreal numbers initialized to a scalar
208 // have an empty dual part, check the size before summing
209 if (res[i].v.size() > i)
210 div += res[i].Dual()[i];
211 }
212
213 return div;
214 }
215
216
227#ifdef THEORETICA_HAS_CPP20
228 template<ADVectorField Function>
229#else
230 template <
231 typename Function,
232 enable_scalar_field<Function> = true
233 >
234#endif
235 inline auto divergence(Function V) {
236
237 constexpr size_t N = return_type_t<Function>::size_argument;
238 using Vector = vec<real, N>;
239
240 return [V](const Vector& x) {
241 return divergence(V, x);
242 };
243 }
244
245
253#ifdef THEORETICA_HAS_CPP20
254 template<ADVectorField MultidualFunction, VectorType Vector>
255#else
256 template <
257 typename MultidualFunction, typename Vector,
258 enable_vector<Vector> = true,
259 enable_vector_field<MultidualFunction> = true
260 >
261#endif
262 inline auto jacobian(MultidualFunction f, const Vector& x) {
263
264 constexpr size_t M = return_type_t<MultidualFunction>::size_argument;
265 constexpr size_t N = first_arg_t<MultidualFunction>::size_argument;
266
267 const vec<multidual<N>, N> res = f(multidual<N>::make_argument(x));
268
269 // Construct the jacobian matrix
271 J.resize(res.size(), x.size());
272
273 for (unsigned int j = 0; j < J.rows(); ++j) {
274
275 const unsigned int dual_size = res[j].v.size();
276
277 if (dual_size > J.cols()) {
278 TH_MATH_ERROR("autodiff::jacobian", dual_size, MathError::InvalidArgument);
279 return algebra::mat_error(J);
280 }
281
282 unsigned int i = 0;
283 for (; i < dual_size; ++i)
284 J(j, i) = res[j].v[i];
285
286 for (; i < J.cols(); ++i)
287 J(j, i) = 0.0;
288 }
289
290 return J;
291 }
292
293
301#ifdef THEORETICA_HAS_CPP20
302 template<ADVectorField MultidualFunction>
303#else
304 template<
305 typename MultidualFunction,
306 enable_vector_field<MultidualFunction> = true
307 >
308#endif
309 inline auto jacobian(MultidualFunction f) {
310
311 constexpr size_t N = return_type_t<MultidualFunction>::size_argument;
312 using Vector = vec<real, N>;
313
314 return [f](const Vector& x) {
315 return jacobian(f, x);
316 };
317 }
318
319
328#ifdef THEORETICA_HAS_CPP20
329 template<ADVectorField MultidualFunction, VectorType Vector>
330#else
331 template <
332 typename MultidualFunction, typename Vector,
333 enable_vector<Vector> = true,
334 enable_vector_field<MultidualFunction> = true
335 >
336#endif
337 inline auto curl(MultidualFunction f, const Vector& x) {
338
339 Vector res;
340 res.resize(3);
341
342 if(x.size() != 3) {
343 TH_MATH_ERROR("th::curl", x.size(), MathError::InvalidArgument);
344 return algebra::vec_error(res);
345 }
346
347 constexpr size_t N = return_type_t<MultidualFunction>::size_argument;
348 const mat<real, N, N> J = jacobian(f, x);
349
350 res[0] = J(2, 1) - J(1, 2);
351 res[1] = J(0, 2) - J(2, 0);
352 res[2] = J(1, 0) - J(0, 1);
353
354 return res;
355 }
356
357
366#ifdef THEORETICA_HAS_CPP20
367 template<ADVectorField MultidualFunction>
368#else
369 template<
370 typename MultidualFunction,
371 enable_vector_field<MultidualFunction> = true
372 >
373#endif
374 inline auto curl(MultidualFunction f) {
375
376 constexpr size_t N = return_type_t<MultidualFunction>::size_argument;
377 using Vector = vec<real, N>;
378
379 return [f](const Vector& x) {
380 return curl(f, x);
381 };
382 }
383
384
393 template <
394 typename Dual2Function, typename Vector,
395 enable_vector<Vector> = true
396 >
397 inline real laplacian(Dual2Function f, const Vector& x) {
398
399 // Extract size template argument of the input vector
400 constexpr size_t N = first_arg_t<Dual2Function>::size_argument;
401
403 d.resize(x.size());
404
405 for (unsigned int i = 0; i < d.size(); ++i)
406 d[i].a = x[i];
407
408 // Accumulate second derivatives
409 real res = 0;
410 for (unsigned int i = 0; i < d.size(); ++i) {
411 d[i].b = 1;
412 res += f(d).Dual2();
413 d[i].b = 0;
414 }
415
416 return res;
417 }
418
419
428 template <typename Dual2Function>
429 inline auto laplacian(Dual2Function f) {
430
431 constexpr size_t N = first_arg_t<Dual2Function>::size_argument;
432
433 return [f](const vec<real, N>& x) {
434 return laplacian(f, x);
435 };
436 }
437
438 }
439}
440
441#endif
Types and traits for automatic differentiation.
Second order dual number class.
Definition dual2.h:29
Dual number class.
Definition dual.h:28
A generic matrix with a fixed number of rows and columns.
Definition mat.h:136
TH_CONSTEXPR unsigned int rows() const
Returns the number of rows in the matrix.
Definition mat.h:641
TH_CONSTEXPR unsigned int cols() const
Returns the number of columns in the matrix.
Definition mat.h:648
mat< Type, N, K > resize(unsigned int n, unsigned int k)
Compatibility function to allow for allocation or resizing of dynamic matrices.
Definition mat.h:730
static vec< multidual< N >, N > make_argument(const Vector &x)
Construct an N-dimensional vector of multidual numbers to be passed as argument to a multidual functi...
Definition multidual.h:401
A statically allocated N-dimensional vector with elements of the given type.
Definition vec.h:92
void resize(size_t n) const
Compatibility function to allow for allocation or resizing of dynamic vectors.
Definition vec.h:459
static vec< Type, N > euclidean_base(unsigned int i, unsigned int n=N)
Returns an N-dimensional euclidean base unit vector with the i-th element set to 1.
Definition vec.h:484
TH_CONSTEXPR unsigned int size() const
Returns the size of the vector (N).
Definition vec.h:449
Error handling with errno and exceptions.
#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
Fundamental type traits.
Second order dual number class.
Dual number class.
Matrix class and operations.
Multidual numbers.
Matrix & mat_error(Matrix &m)
Overwrite the given matrix with the error matrix with NaN values on the diagonal and zeroes everywher...
Definition algebra.h:40
Vector & vec_error(Vector &v)
Overwrite the given vector with the error vector with NaN values.
Definition algebra.h:58
Differential operators with automatic differentiation.
real laplacian(Dual2Function f, const Vector &x)
Compute the Laplacian differential operator for a generic function of the form at a given $\vec x$.
Definition autodiff.h:397
auto jacobian(MultidualFunction f, const Vector &x)
Compute the jacobian of a vector field of the form .
Definition autodiff.h:262
real deriv(DualFunction f, real x)
Compute the derivative of a function at the given point using univariate automatic differentiation.
Definition autodiff.h:41
auto curl(MultidualFunction f, const Vector &x)
Compute the curl for a given of a vector field defined by using automatic differentiation.
Definition autodiff.h:337
real divergence(Function V, const Vector &x)
Compute the divergence for a given of a vector field of the form using automatic differentiation.
Definition autodiff.h:196
real deriv2(Dual2Function f, real x)
Compute the second derivative of a function at the given point using univariate automatic differentia...
Definition autodiff.h:77
auto gradient(Function f, const Vector &x)
Compute the gradient for a given of a scalar field of the form using automatic differentiation.
Definition autodiff.h:126
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
@ InvalidArgument
Invalid argument.
Definition error.h:32
Vector class and operations.