6#ifndef THEORETICA_AUTODIFF_H
7#define THEORETICA_AUTODIFF_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) {
117#ifdef THEORETICA_HAS_CPP20
118 template<ADScalarField Function, VectorType Vector>
121 typename Function,
typename Vector =
vec<real>,
122 enable_scalar_field<Function> =
true,
123 enable_vector<Vector> =
true
126 inline auto gradient(Function f,
const Vector& x) {
129 using MultidualType = return_type_t<Function>;
131 constexpr size_t N = MultidualType::size_argument;
137 for (
unsigned int i = 0; i < x.size(); ++i) {
138 arg[i] = MultidualType(
143 return f(arg).Dual();
158#ifdef THEORETICA_HAS_CPP20
159 template<ADScalarField Function>
163 enable_scalar_field<Function> =
true
168 constexpr size_t N = return_type_t<Function>::size_argument;
187#ifdef THEORETICA_HAS_CPP20
188 template<ADVectorField Function, VectorType Vector>
191 typename Function,
typename Vector =
vec<real>,
192 enable_vector_field<Function> =
true,
193 enable_vector<Vector> =
true
198 using MultidualT = return_type_t<Function>;
199 const size_t N = MultidualT::size_argument;
205 for (
unsigned int i = 0; i < res.
size(); ++i) {
209 if (res[i].v.
size() > i)
210 div += res[i].Dual()[i];
227#ifdef THEORETICA_HAS_CPP20
228 template<ADVectorField Function>
232 enable_scalar_field<Function> =
true
237 constexpr size_t N = return_type_t<Function>::size_argument;
240 return [V](
const Vector& x) {
253#ifdef THEORETICA_HAS_CPP20
254 template<ADVectorField Mult
idualFunction, VectorType Vector>
257 typename MultidualFunction,
typename Vector,
258 enable_vector<Vector> =
true,
259 enable_vector_field<MultidualFunction> =
true
262 inline auto jacobian(MultidualFunction f,
const Vector& x) {
264 constexpr size_t M = return_type_t<MultidualFunction>::size_argument;
265 constexpr size_t N = first_arg_t<MultidualFunction>::size_argument;
273 for (
unsigned int j = 0; j < J.
rows(); ++j) {
275 const unsigned int dual_size = res[j].v.
size();
277 if (dual_size > J.
cols()) {
283 for (; i < dual_size; ++i)
284 J(j, i) = res[j].v[i];
286 for (; i < J.
cols(); ++i)
301#ifdef THEORETICA_HAS_CPP20
302 template<ADVectorField Mult
idualFunction>
305 typename MultidualFunction,
306 enable_vector_field<MultidualFunction> =
true
311 constexpr size_t N = return_type_t<MultidualFunction>::size_argument;
314 return [f](
const Vector& x) {
328#ifdef THEORETICA_HAS_CPP20
329 template<ADVectorField Mult
idualFunction, VectorType Vector>
332 typename MultidualFunction,
typename Vector,
333 enable_vector<Vector> =
true,
334 enable_vector_field<MultidualFunction> =
true
337 inline auto curl(MultidualFunction f,
const Vector& x) {
347 constexpr size_t N = return_type_t<MultidualFunction>::size_argument;
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);
366#ifdef THEORETICA_HAS_CPP20
367 template<ADVectorField Mult
idualFunction>
370 typename MultidualFunction,
371 enable_vector_field<MultidualFunction> =
true
374 inline auto curl(MultidualFunction f) {
376 constexpr size_t N = return_type_t<MultidualFunction>::size_argument;
379 return [f](
const Vector& x) {
394 typename Dual2Function,
typename Vector,
395 enable_vector<Vector> =
true
400 constexpr size_t N = first_arg_t<Dual2Function>::size_argument;
405 for (
unsigned int i = 0; i < d.
size(); ++i)
410 for (
unsigned int i = 0; i < d.
size(); ++i) {
428 template <
typename Dual2Function>
431 constexpr size_t N = first_arg_t<Dual2Function>::size_argument;
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
Second order dual number class.
Matrix class and operations.
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.