6#ifndef THEORETICA_AUTODIFF_H
7#define THEORETICA_AUTODIFF_H
12#include "../algebra/vec.h"
13#include "../algebra/mat.h"
14#include "../core/error.h"
15#include "../core/core_traits.h"
38 typename DualFunction = std::function<dual(dual)>,
39 enable_dual_func<DualFunction> =
true
42 return f(
dual(x, 1.0)).Dual();
55 typename DualFunction = std::function<
dual(
dual)>,
56 enable_dual_func<DualFunction> =
true
58 inline auto deriv(DualFunction f) {
74 typename Dual2Function = std::function<
dual2(
dual2)>,
75 enable_dual2_func<Dual2Function> =
true
91 typename Dual2Function = std::function<
dual2(
dual2)>,
92 enable_dual2_func<Dual2Function> =
true
94 inline auto deriv2(Dual2Function f) {
110 typename MultidualType,
111 typename Vector = vec<real>
115 constexpr size_t N = MultidualType::vector_argument;
116 vec<MultidualType, N> arg;
121 for (
unsigned int i = 0; i < x.size(); ++i) {
122 arg[i] = MultidualType(
144 typename Function,
typename Vector = vec<real>,
145 enable_scalar_field<Function> =
true,
146 enable_vector<Vector> =
true
148 inline auto gradient(Function f,
const Vector& x) {
151 using R = return_type_t<Function>;
153 return f(make_autodiff_arg<R>(x)).Dual();
170 enable_scalar_field<Function> =
true
174 constexpr size_t N = return_type_t<Function>::vector_argument;
176 return [f](vec<real, N> x) {
194 typename Function,
typename Vector = vec<real>,
195 enable_scalar_field<Function> =
true,
196 enable_vector<Vector> =
true
200 using MultidualT = return_type_t<Function>;
201 MultidualT d = f(MultidualT::make_argument(x));
204 for (
unsigned int i = 0; i < d.v.size(); ++i)
223 enable_scalar_field<Function> =
true
227 constexpr size_t N = return_type_t<Function>::vector_argument;
229 return [f](vec<real, N> x) {
242 template<
unsigned int N = 0,
unsigned int M = 0>
244 vec<multidual<N>, M>(*f)(
vec<multidual<N>, N>),
const vec<real, N>& x) {
250 J.resize(res.size(), x.
size());
252 for (
unsigned int j = 0; j < J.rows(); ++j)
253 for (
unsigned int i = 0; i < res[j].v.size(); ++i)
254 J(j, i) = res[j].v[i];
267 template<
unsigned int N = 0,
unsigned int M = 0>
269 vec<multidual<N>, M>(*f)(
vec<multidual<N>, N>)) {
271 return [f](vec<real, N> x) {
285 template<
unsigned int N = 0>
287 vec<multidual<N>, N>(*f)(
vec<multidual<N>, N>),
const vec<real, N>& x) {
291 return vec<real, N>(
nan(), x.
size());
294 mat<real, N, N> J = jacobian<N, N>(f, x);
300 res[0] = J(2, 1) - J(1, 2);
301 res[1] = J(0, 2) - J(2, 0);
302 res[2] = J(1, 0) - J(0, 1);
316 template<
unsigned int N = 0>
317 inline auto curl(
vec<multidual<N>, N>(*f)(
vec<multidual<N>, N>)) {
319 return [f](vec<real, N> x) {
340 template<
unsigned int N = 0>
342 const vec<real, N>& x,
const vec<real, N>& v) {
364 template<
unsigned int N = 0>
366 multidual<N>(*f)(
vec<multidual<N>, N>),
const vec<real, N>& v) {
368 return [f, v](vec<real, N> x) {
382 template<
unsigned int N = 0>
389 for (
unsigned int i = 0; i < x.
size(); ++i)
392 for (
unsigned int i = 0; i < x.
size(); ++i) {
410 template<
unsigned int N = 0>
413 return [f](vec<real, N> x) {
433 template<
unsigned int N = 0>
435 multidual<N>(*f)(
vec<multidual<N>, N>),
436 multidual<N>(*H)(
vec<multidual<N>, N>),
Types and traits for automatic differentiation.
Second order dual number class.
Definition dual2.h:29
real Dual2() const
Return second order dual part.
Definition dual2.h:95
Dual number class.
Definition dual.h:28
A generic matrix with a fixed number of rows and columns.
Definition mat.h:136
Multidual number algebra for functions of the form .
Definition multidual.h:26
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:422
TH_CONSTEXPR unsigned int size() const
Returns the size of the vector (N)
Definition vec.h:412
Second order dual number class.
#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:225
vec< real, N > curl(vec< multidual< N >, N >(*f)(vec< multidual< N >, N >), const vec< real, N > &x)
Compute the curl for a given of a vector field defined by using automatic differentiation.
Definition autodiff.h:286
real sturm_liouville(multidual< N >(*f)(vec< multidual< N >, N >), multidual< N >(*H)(vec< multidual< N >, N >), vec< real, N > eta)
Compute the Sturm-Liouville operator on a generic function of the form with respect to a given Hamil...
Definition autodiff.h:434
real deriv(DualFunction f, real x)
Compute the derivative of a function at the given point using univariate automatic differentiation.
Definition autodiff.h:41
vec< real, N > directional_derivative(multidual< N >(*f)(vec< multidual< N >, N >), const vec< real, N > &x, const vec< real, N > &v)
Compute the directional derivative of a generic function of the form .
Definition autodiff.h:341
real laplacian(dual2(*f)(vec< dual2, N >), const vec< real, N > &x)
Compute the Laplacian differential operator for a generic function of the form at a given $\vec x$.
Definition autodiff.h:383
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:148
auto make_autodiff_arg(const Vector &x)
Prepare a vector of multidual numbers in "canonical" form, where the i-th element of the vector has a...
Definition autodiff.h:113
mat< real, M, N > jacobian(vec< multidual< N >, M >(*f)(vec< multidual< N >, N >), const vec< real, N > &x)
Compute the jacobian of a vector field of the form .
Definition autodiff.h:243
real divergence(Function f, const Vector &x)
Compute the divergence for a given of a scalar field of the form using automatic differentiation.
Definition autodiff.h:198
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
TH_CONSTEXPR real nan()
Return a quiet NaN number in floating point representation.
Definition error.h:54