Theoretica
A C++ numerical and automatic mathematical library
theoretica::autodiff Namespace Reference

Differential operators with automatic differentiation. More...

Classes

struct  is_dual_type
 Type trait to check whether the given type is a multidual number. More...
 
struct  is_dual_type< dual >
 Type trait to check whether the given type is a multidual number. More...
 
struct  is_dual2_type
 Type trait to check whether the given type is a multidual number. More...
 
struct  is_dual2_type< dual2 >
 Type trait to check whether the given type is a multidual number. More...
 
struct  is_multidual_type
 Type trait to check whether the given type is a multidual number. More...
 
struct  is_multidual_type< multidual< N > >
 Type trait to check whether the given type is a multidual number. More...
 

Typedefs

template<typename Function >
using is_dual_func = std::conditional_t< is_dual_type< typename _internal::return_type_or_void< Function, dual >::type >::value, std::true_type, std::false_type >
 Type trait to check whether the given function takes a dual number as its first argument.
 
template<typename Function , typename T = bool>
using enable_dual_func = typename std::enable_if< is_dual_func< Function >::value, T >::type
 Enable a certain function overload if the given type is a function taking as first argument a dual number.
 
template<typename Function >
using is_dual2_func = std::conditional_t< is_dual2_type< typename _internal::return_type_or_void< Function, dual2 >::type >::value, std::true_type, std::false_type >
 Type trait to check whether the given function takes a dual2 number as its first argument.
 
template<typename Function , typename T = bool>
using enable_dual2_func = typename std::enable_if< is_dual2_func< Function >::value, T >::type
 Enable a certain function overload if the given type is a function taking as first argument a dual2 number.
 
template<typename Type , typename T = bool>
using enable_multidual = typename std::enable_if< is_multidual_type< Type >::value, T >::type
 Enable a certain function overload if the given type is an instantiation of the multidual template class.
 
template<typename Function , typename T = bool>
using enable_scalar_field = typename std::enable_if< is_multidual_type< return_type_t< Function > >::value, T >::type
 Enable a certain function overload if the given type is a Callable object corresponding to a multidual function representing a scalar field, that is, a function taking a dvec_t and returning a dreal_t.
 
template<typename Function , typename T = bool>
using enable_vector_field = typename std::enable_if< is_multidual_type< vector_element_t< return_type_t< Function > > >::value, T >::type
 Enable a certain function overload if the given type is a Callable object corresponding to a multidual function representing a vector field, that is, a function taking a dvec_t and returning a dvec_t.
 
template<unsigned int N = 0>
using dreal_t = multidual< N >
 Real type for multivariate automatic differentiation (read "differential real").
 
template<unsigned int N = 0>
using dvec_t = vec< dreal_t< N >, N >
 Vector type for multivariate automatic differentiation (read "differential vector").
 
using dreal = dreal_t< 0 >
 Real type for multivariate automatic differentiation with dynamically allocated vectors.
 
using dvec = dvec_t< 0 >
 Vector type for multivariate automatic differentiation with dynamically allocated vectors.
 
using dreal2 = dreal_t< 2 >
 Real type for multivariate automatic differentiation with two-dimensional statically allocated vectors.
 
using dvec2 = dvec_t< 2 >
 Vector type for multivariate automatic differentiation with two-dimensional statically allocated vectors.
 
using dreal3 = dreal_t< 3 >
 Real type for multivariate automatic differentiation with three-dimensional statically allocated vectors.
 
using dvec3 = dvec_t< 3 >
 Vector type for multivariate automatic differentiation with three-dimensional statically allocated vectors.
 
using dreal4 = dreal_t< 4 >
 Real type for multivariate automatic differentiation with four-dimensional statically allocated vectors.
 
using dvec4 = dvec_t< 4 >
 Vector type for multivariate automatic differentiation with four-dimensional statically allocated vectors.
 

Functions

template<typename DualFunction = std::function<dual(dual)>, enable_dual_func< DualFunction > = true>
real deriv (DualFunction f, real x)
 Compute the derivative of a function at the given point using univariate automatic differentiation. More...
 
template<typename DualFunction = std::function<dual(dual)>, enable_dual_func< DualFunction > = true>
auto deriv (DualFunction f)
 Get a lambda function which computes the derivative of the given function at the given point, using automatic differentiation. More...
 
template<typename Dual2Function = std::function<dual2(dual2)>, enable_dual2_func< Dual2Function > = true>
real deriv2 (Dual2Function f, real x)
 Compute the second derivative of a function at the given point using univariate automatic differentiation. More...
 
template<typename Dual2Function = std::function<dual2(dual2)>, enable_dual2_func< Dual2Function > = true>
auto deriv2 (Dual2Function f)
 Get a lambda function which computes the second derivative of the given function at the given point, using automatic differentiation. More...
 
template<typename MultidualType , typename Vector = vec<real>>
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 dual part which is the i-th canonical vector. More...
 
template<typename Function , typename Vector = vec<real>, enable_scalar_field< Function > = true, enable_vector< Vector > = true>
auto gradient (Function f, const Vector &x)
 Compute the gradient \(\nabla f = \sum_i^n \vec e_i \frac{\partial}{\partial x_i} f(\vec x)\) for a given \(\vec x\) of a scalar field of the form \(f: \mathbb{R}^N \rightarrow \mathbb{R}\) using automatic differentiation. More...
 
template<typename Function , enable_scalar_field< Function > = true>
auto gradient (Function f)
 Get a lambda function which computes the gradient \(\nabla f = \sum_i^n \vec e_i \frac{\partial}{\partial x_i} f(\vec x)\) of a given scalar field of the form \(f: \mathbb{R}^N \rightarrow \mathbb{R}\) at \(\vec x\) using automatic differentiation. More...
 
template<typename Function , typename Vector = vec<real>, enable_scalar_field< Function > = true, enable_vector< Vector > = true>
real divergence (Function f, const Vector &x)
 Compute the divergence \(\sum_i^n \frac{\partial}{\partial x_i} f(\vec x)\) for a given \(\vec x\) of a scalar field of the form \(f: \mathbb{R}^N \rightarrow \mathbb{R}\) using automatic differentiation. More...
 
template<typename Function , enable_scalar_field< Function > = true>
auto divergence (Function f)
 Get a lambda function which computes the divergence of a given function of the form \(f: \mathbb{R}^N \rightarrow \mathbb{R}\) at a given \(\vec x\) using automatic differentiation. More...
 
template<unsigned int N = 0, unsigned int M = 0>
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 \(f: \mathbb{R}^N \rightarrow \mathbb{R}^M\). More...
 
template<unsigned int N = 0, unsigned int M = 0>
auto jacobian (vec< multidual< N >, M >(*f)(vec< multidual< N >, N >))
 Get a lambda function which computes the jacobian of a generic function of the form \(f: \mathbb{R}^N \rightarrow \mathbb{R}^M\) for a given $\vec x$. More...
 
template<unsigned int N = 0>
vec< real, N > curl (vec< multidual< N >, N >(*f)(vec< multidual< N >, N >), const vec< real, N > &x)
 Compute the curl for a given \(\vec x\) of a vector field defined by \(f: \mathbb{R}^3 \rightarrow \mathbb{R}^3\) using automatic differentiation. More...
 
template<unsigned int N = 0>
auto curl (vec< multidual< N >, N >(*f)(vec< multidual< N >, N >))
 Get a lambda function which computes the curl for a given \(\vec x\) of a vector field defined by \(f: \mathbb{R}^3 \rightarrow \mathbb{R}^3\) using automatic differentiation. More...
 
template<unsigned int N = 0>
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 \(f: \mathbb{R}^N \rightarrow \mathbb{R}\). More...
 
template<unsigned int N = 0>
auto directional_derivative (multidual< N >(*f)(vec< multidual< N >, N >), const vec< real, N > &v)
 Get a lambda function which computes the directional derivative of a generic function of the form \(f: \mathbb{R}^N \rightarrow \mathbb{R}\). More...
 
template<unsigned int N = 0>
real laplacian (dual2(*f)(vec< dual2, N >), const vec< real, N > &x)
 Compute the Laplacian differential operator for a generic function of the form \(f: \mathbb{R}^N \rightarrow \mathbb{R}\) at a given $\vec x$. More...
 
template<unsigned int N = 0>
auto laplacian (dual2(*f)(vec< dual2, N >))
 Get a lambda function which computes the Laplacian differential operator for a generic function of the form \(f: \mathbb{R}^N \rightarrow \mathbb{R}\) at a given $\vec x$. More...
 
template<unsigned int N = 0>
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 \(f: \mathbb{R}^{2N} \rightarrow \mathbb{R}\) with respect to a given Hamiltonian function of the form \(H: \mathbb{R}^{2N} \rightarrow \mathbb{R}\) where the first N arguments are the coordinates in phase space and the last N arguments are the conjugate momenta, for a given point in phase space. More...
 

Detailed Description

Differential operators with automatic differentiation.

Function Documentation

◆ curl() [1/2]

template<unsigned int N = 0>
auto theoretica::autodiff::curl ( vec< multidual< N >, N >(*)(vec< multidual< N >, N >)  f)
inline

Get a lambda function which computes the curl for a given \(\vec x\) of a vector field defined by \(f: \mathbb{R}^3 \rightarrow \mathbb{R}^3\) using automatic differentiation.

Parameters
fA function with a vector of multidual numbers as input and a vector of multidual numbers as output.
Returns
A lambda function which computes the curl of f.

◆ curl() [2/2]

template<unsigned int N = 0>
vec<real, N> theoretica::autodiff::curl ( vec< multidual< N >, N >(*)(vec< multidual< N >, N >)  f,
const vec< real, N > &  x 
)
inline

Compute the curl for a given \(\vec x\) of a vector field defined by \(f: \mathbb{R}^3 \rightarrow \mathbb{R}^3\) using automatic differentiation.

Parameters
fA function with a vector of multidual numbers as input and a vector of multidual numbers as output.
xThe point to compute the curl at.
Returns
The curl of f at x.

◆ deriv() [1/2]

template<typename DualFunction = std::function<dual(dual)>, enable_dual_func< DualFunction > = true>
auto theoretica::autodiff::deriv ( DualFunction  f)
inline

Get a lambda function which computes the derivative of the given function at the given point, using automatic differentiation.

Parameters
fThe function to differentiate, with dual argument and return value.
Returns
A lambda function which computes the derivative of f using automatic differentiation.

◆ deriv() [2/2]

template<typename DualFunction = std::function<dual(dual)>, enable_dual_func< DualFunction > = true>
real theoretica::autodiff::deriv ( DualFunction  f,
real  x 
)
inline

Compute the derivative of a function at the given point using univariate automatic differentiation.

Parameters
fThe function to differentiate, with dual argument and return value.
xThe coordinate to compute the derivative at.
Returns
The derivative of f at x.

◆ deriv2() [1/2]

template<typename Dual2Function = std::function<dual2(dual2)>, enable_dual2_func< Dual2Function > = true>
auto theoretica::autodiff::deriv2 ( Dual2Function  f)
inline

Get a lambda function which computes the second derivative of the given function at the given point, using automatic differentiation.

Parameters
fThe function to differentiate, with dual2 argument and return value.
Returns
A lambda function which computes the derivative of f using automatic differentiation.

◆ deriv2() [2/2]

template<typename Dual2Function = std::function<dual2(dual2)>, enable_dual2_func< Dual2Function > = true>
real theoretica::autodiff::deriv2 ( Dual2Function  f,
real  x 
)
inline

Compute the second derivative of a function at the given point using univariate automatic differentiation.

Parameters
fThe function to differentiate, with dual2 argument and return value.
xThe coordinate to compute the derivative at.
Returns
The derivative of f at x.

◆ directional_derivative() [1/2]

template<unsigned int N = 0>
auto theoretica::autodiff::directional_derivative ( multidual< N >(*)(vec< multidual< N >, N >)  f,
const vec< real, N > &  v 
)
inline

Get a lambda function which computes the directional derivative of a generic function of the form \(f: \mathbb{R}^N \rightarrow \mathbb{R}\).

Parameters
fThe function to partially differentiate
xThe point to compute the derivative at
vThe direction to compute the derivative on
Note
In most applications, the vector v should be a unit vector, but the function does not control whether the vector has unit length or not.
Parameters
fA function with a vector of multidual numbers as input and a vector of multidual numbers as output.
vThe direction of the derivative.
Returns
A lambda function which computes the directional derivative over v of f.

◆ directional_derivative() [2/2]

template<unsigned int N = 0>
vec<real, N> theoretica::autodiff::directional_derivative ( multidual< N >(*)(vec< multidual< N >, N >)  f,
const vec< real, N > &  x,
const vec< real, N > &  v 
)
inline

Compute the directional derivative of a generic function of the form \(f: \mathbb{R}^N \rightarrow \mathbb{R}\).

Parameters
fThe function to partially differentiate
xThe point to compute the derivative at
vThe direction to compute the derivative on
Note
In most applications, the vector v should be a unit vector, but the function does not control whether the vector has unit length or not.
Parameters
fA function with a vector of multidual numbers as input and a vector of multidual numbers as output.
xThe point to compute the directional derivative at.
vThe direction of the derivative.
Returns
The directional derivative over v of f at x.

◆ divergence() [1/2]

template<typename Function , enable_scalar_field< Function > = true>
auto theoretica::autodiff::divergence ( Function  f)
inline

Get a lambda function which computes the divergence of a given function of the form \(f: \mathbb{R}^N \rightarrow \mathbb{R}\) at a given \(\vec x\) using automatic differentiation.

The returned lambda function accepts a vec<real, N> argument. The argument function may be a function pointer or lambda function, with the type dvec_t as first argument and dreal_t return type.

Parameters
fA function with a vector of multidual numbers as input and a vector of multidual numbers as output.
Returns
A lambda function which computes the divergence of f at x.

◆ divergence() [2/2]

template<typename Function , typename Vector = vec<real>, enable_scalar_field< Function > = true, enable_vector< Vector > = true>
real theoretica::autodiff::divergence ( Function  f,
const Vector &  x 
)
inline

Compute the divergence \(\sum_i^n \frac{\partial}{\partial x_i} f(\vec x)\) for a given \(\vec x\) of a scalar field of the form \(f: \mathbb{R}^N \rightarrow \mathbb{R}\) using automatic differentiation.

The argument function may be a function pointer or lambda function, with the type dvec_t as first argument and dreal_t return type.

Parameters
fA function with a vector of multidual numbers as input and a vector of multidual numbers as output.
xThe point to compute the divergence at.
Returns
The divergence of f at x.

◆ gradient() [1/2]

template<typename Function , enable_scalar_field< Function > = true>
auto theoretica::autodiff::gradient ( Function  f)
inline

Get a lambda function which computes the gradient \(\nabla f = \sum_i^n \vec e_i \frac{\partial}{\partial x_i} f(\vec x)\) of a given scalar field of the form \(f: \mathbb{R}^N \rightarrow \mathbb{R}\) at \(\vec x\) using automatic differentiation.

The returned lambda function accepts a vec<real, N> argument. The argument function may be a function pointer or lambda function, with the type dvec_t as first argument and dreal_t return type.

Parameters
fA function with a vector of multidual numbers as input and a multidual number as output.
Returns
A lambda function which computes the gradient of f.

◆ gradient() [2/2]

template<typename Function , typename Vector = vec<real>, enable_scalar_field< Function > = true, enable_vector< Vector > = true>
auto theoretica::autodiff::gradient ( Function  f,
const Vector &  x 
)
inline

Compute the gradient \(\nabla f = \sum_i^n \vec e_i \frac{\partial}{\partial x_i} f(\vec x)\) for a given \(\vec x\) of a scalar field of the form \(f: \mathbb{R}^N \rightarrow \mathbb{R}\) using automatic differentiation.

The argument function may be a function pointer or lambda function, with the type dvec_t as first argument and dreal_t return type.

Parameters
fA function with a vector of multidual numbers as input and a multidual number as output.
xThe point to compute the gradient at.
Returns
The gradient of f computed at x.

◆ jacobian() [1/2]

template<unsigned int N = 0, unsigned int M = 0>
auto theoretica::autodiff::jacobian ( vec< multidual< N >, M >(*)(vec< multidual< N >, N >)  f)
inline

Get a lambda function which computes the jacobian of a generic function of the form \(f: \mathbb{R}^N \rightarrow \mathbb{R}^M\) for a given $\vec x$.

Parameters
fA function with a vector of multidual numbers as input and a vector of multidual numbers as output.
Returns
A lambda function which computes the Jacobian matrix of f.

◆ jacobian() [2/2]

template<unsigned int N = 0, unsigned int M = 0>
mat<real, M, N> theoretica::autodiff::jacobian ( vec< multidual< N >, M >(*)(vec< multidual< N >, N >)  f,
const vec< real, N > &  x 
)
inline

Compute the jacobian of a vector field of the form \(f: \mathbb{R}^N \rightarrow \mathbb{R}^M\).

Parameters
fA function with a vector of multidual numbers as input and a vector of multidual numbers as output.
xThe point to compute the Jacobian at.
Returns
The Jacobian matrix of f at x.

◆ laplacian() [1/2]

template<unsigned int N = 0>
auto theoretica::autodiff::laplacian ( dual2(*)(vec< dual2, N >)  f)
inline

Get a lambda function which computes the Laplacian differential operator for a generic function of the form \(f: \mathbb{R}^N \rightarrow \mathbb{R}\) at a given $\vec x$.

Parameters
fA function taking a vector of dual2 numbers and returning a dual2 number.
Returns
A lambda function which computes the Laplacian of f at x.

◆ laplacian() [2/2]

template<unsigned int N = 0>
real theoretica::autodiff::laplacian ( dual2(*)(vec< dual2, N >)  f,
const vec< real, N > &  x 
)
inline

Compute the Laplacian differential operator for a generic function of the form \(f: \mathbb{R}^N \rightarrow \mathbb{R}\) at a given $\vec x$.

Parameters
fA function taking a vector of dual2 numbers and returning a dual2 number.
xThe point to compute the Laplacian at.
Returns
The Laplacian of f at x.

◆ make_autodiff_arg()

template<typename MultidualType , typename Vector = vec<real>>
auto theoretica::autodiff::make_autodiff_arg ( const Vector &  x)
inline

Prepare a vector of multidual numbers in "canonical" form, where the i-th element of the vector has a dual part which is the i-th canonical vector.

This form is used to evaluate a multidual function for automatic differentiation.

◆ sturm_liouville()

template<unsigned int N = 0>
real theoretica::autodiff::sturm_liouville ( multidual< N >(*)(vec< multidual< N >, N >)  f,
multidual< N >(*)(vec< multidual< N >, N >)  H,
vec< real, N >  eta 
)
inline

Compute the Sturm-Liouville operator on a generic function of the form \(f: \mathbb{R}^{2N} \rightarrow \mathbb{R}\) with respect to a given Hamiltonian function of the form \(H: \mathbb{R}^{2N} \rightarrow \mathbb{R}\) where the first N arguments are the coordinates in phase space and the last N arguments are the conjugate momenta, for a given point in phase space.

Parameters
fA function taking a vector of multidual numbers and returning a multidual number.
HThe Hamiltonian of the system.
etaA vector containing N = 2K elements, where the first K elements are the coordinates and the last K elements are the conjugate momenta.