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
78 return f(
dual2(x, 1.0, 0.0)).Dual2();
91 typename Dual2Function = std::function<
dual2(
dual2)>,
92 enable_dual2_func<Dual2Function> =
true
94 inline auto deriv2(Dual2Function f) {
110 typename MultidualType,
115 constexpr
size_t N = MultidualType::vector_argument;
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,
148 inline auto gradient(Function f,
const Vector& x) {
153 return f(make_autodiff_arg<R>(x)).Dual();
170 enable_scalar_field<Function> =
true
194 typename Function,
typename Vector =
vec<real>,
195 enable_scalar_field<Function> =
true,
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
242 template<
unsigned int N = 0,
unsigned int M = 0>
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>
285 template<
unsigned int N = 0>
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>
340 template<
unsigned int N = 0>
364 template<
unsigned int N = 0>
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>
433 template<
unsigned int N = 0>
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:132
mat< Type, N, K > resize(unsigned int n, unsigned int k) const
Compatibility function to allow for allocation or resizing of dynamic matrices.
Definition: mat.h:579
TH_CONSTEXPR unsigned int rows() const
Get the number of rows of the matrix.
Definition: mat.h:465
static mat< Type, N, K > symplectic(unsigned int n=0, unsigned int k=0)
A symplectic NxN matrix, where for some natural K.
Definition: mat.h:684
Multidual number algebra for functions of the form .
Definition: multidual.h:26
void resize(size_t n) const
Compatibility function to allow for allocation or resizing of dynamic vectors.
Definition: vec.h:412
TH_CONSTEXPR unsigned int size() const
Returns the size of the vector (N)
Definition: vec.h:402
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:219
auto dot(const Vector1 &v, const Vector2 &w)
Computes the dot product between two vectors, using the Hermitian form if needed.
Definition: algebra.h:351
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
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
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 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
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
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
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:188
typename _internal::func_helper< Function >::return_type return_type_t
Extract the return type of a Callable object, such as a function pointer or lambda function.
Definition: core_traits.h:246
std::enable_if_t< is_vector< Structure >::value, T > enable_vector
Enable a function overload if the template typename is considerable a vector.
Definition: core_traits.h:167
real nan()
Return a quiet NaN number in floating point representation.
Definition: error.h:54