/usr/include/boost/math/differentiation
Edit: /usr/include/boost/math/differentiation/autodiff.hpp (82588B)
// Copyright Matthew Pulver 2018 - 2019.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_MATH_DIFFERENTIATION_AUTODIFF_HPP
#define BOOST_MATH_DIFFERENTIATION_AUTODIFF_HPP
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
namespace boost {
namespace math {
namespace differentiation {
// Automatic Differentiation v1
inline namespace autodiff_v1 {
namespace detail {
template
struct promote_args_n {
using type = typename tools::promote_args_2::type>::type;
};
template
struct promote_args_n {
using type = typename tools::promote_arg::type;
};
} // namespace detail
template
using promote = typename detail::promote_args_n::type;
namespace detail {
template
class fvar;
template
struct is_fvar_impl : std::false_type {};
template
struct is_fvar_impl> : std::true_type {};
template
using is_fvar = is_fvar_impl::type>;
template
struct nest_fvar {
using type = fvar::type, Order>;
};
template
struct nest_fvar {
using type = fvar;
};
template
struct get_depth_impl : std::integral_constant {};
template
struct get_depth_impl>
: std::integral_constant::value + 1> {};
template
using get_depth = get_depth_impl::type>;
template
struct get_order_sum_t : std::integral_constant {};
template
struct get_order_sum_t>
: std::integral_constant::value + Order> {};
template
using get_order_sum = get_order_sum_t::type>;
template
struct get_root_type {
using type = RealType;
};
template
struct get_root_type> {
using type = typename get_root_type::type;
};
template
struct type_at {
using type = RealType;
};
template
struct type_at, Depth> {
using type = typename conditional,
typename type_at::type>::type;
};
template
using get_type_at = typename type_at::type;
// Satisfies Boost's Conceptual Requirements for Real Number Types.
// https://www.boost.org/libs/math/doc/html/math_toolkit/real_concepts.html
template
class fvar {
std::array v;
public:
using root_type = typename get_root_type::type; // RealType in the root fvar.
fvar() = default;
// Initialize a variable or constant.
fvar(root_type const&, bool const is_variable);
// RealType(cr) | RealType | RealType is copy constructible.
fvar(fvar const&) = default;
// Be aware of implicit casting from one fvar<> type to another by this copy constructor.
template
fvar(fvar const&);
// RealType(ca) | RealType | RealType is copy constructible from the arithmetic types.
explicit fvar(root_type const&); // Initialize a constant. (No epsilon terms.)
template
fvar(RealType2 const& ca); // Supports any RealType2 for which static_cast(ca) compiles.
// r = cr | RealType& | Assignment operator.
fvar& operator=(fvar const&) = default;
// r = ca | RealType& | Assignment operator from the arithmetic types.
// Handled by constructor that takes a single parameter of generic type.
// fvar& operator=(root_type const&); // Set a constant.
// r += cr | RealType& | Adds cr to r.
template
fvar& operator+=(fvar const&);
// r += ca | RealType& | Adds ar to r.
fvar& operator+=(root_type const&);
// r -= cr | RealType& | Subtracts cr from r.
template
fvar& operator-=(fvar const&);
// r -= ca | RealType& | Subtracts ca from r.
fvar& operator-=(root_type const&);
// r *= cr | RealType& | Multiplies r by cr.
template
fvar& operator*=(fvar const&);
// r *= ca | RealType& | Multiplies r by ca.
fvar& operator*=(root_type const&);
// r /= cr | RealType& | Divides r by cr.
template
fvar& operator/=(fvar const&);
// r /= ca | RealType& | Divides r by ca.
fvar& operator/=(root_type const&);
// -r | RealType | Unary Negation.
fvar operator-() const;
// +r | RealType& | Identity Operation.
fvar const& operator+() const;
// cr + cr2 | RealType | Binary Addition
template
promote> operator+(fvar const&) const;
// cr + ca | RealType | Binary Addition
fvar operator+(root_type const&) const;
// ca + cr | RealType | Binary Addition
template
friend fvar operator+(typename fvar::root_type const&,
fvar const&);
// cr - cr2 | RealType | Binary Subtraction
template
promote> operator-(fvar const&) const;
// cr - ca | RealType | Binary Subtraction
fvar operator-(root_type const&) const;
// ca - cr | RealType | Binary Subtraction
template
friend fvar operator-(typename fvar::root_type const&,
fvar const&);
// cr * cr2 | RealType | Binary Multiplication
template
promote> operator*(fvar const&)const;
// cr * ca | RealType | Binary Multiplication
fvar operator*(root_type const&)const;
// ca * cr | RealType | Binary Multiplication
template
friend fvar operator*(typename fvar::root_type const&,
fvar const&);
// cr / cr2 | RealType | Binary Subtraction
template
promote> operator/(fvar const&) const;
// cr / ca | RealType | Binary Subtraction
fvar operator/(root_type const&) const;
// ca / cr | RealType | Binary Subtraction
template
friend fvar operator/(typename fvar::root_type const&,
fvar const&);
// For all comparison overloads, only the root term is compared.
// cr == cr2 | bool | Equality Comparison
template
bool operator==(fvar const&) const;
// cr == ca | bool | Equality Comparison
bool operator==(root_type const&) const;
// ca == cr | bool | Equality Comparison
template
friend bool operator==(typename fvar::root_type const&, fvar const&);
// cr != cr2 | bool | Inequality Comparison
template
bool operator!=(fvar const&) const;
// cr != ca | bool | Inequality Comparison
bool operator!=(root_type const&) const;
// ca != cr | bool | Inequality Comparison
template
friend bool operator!=(typename fvar::root_type const&, fvar const&);
// cr <= cr2 | bool | Less than equal to.
template
bool operator<=(fvar const&) const;
// cr <= ca | bool | Less than equal to.
bool operator<=(root_type const&) const;
// ca <= cr | bool | Less than equal to.
template
friend bool operator<=(typename fvar::root_type const&, fvar const&);
// cr >= cr2 | bool | Greater than equal to.
template
bool operator>=(fvar const&) const;
// cr >= ca | bool | Greater than equal to.
bool operator>=(root_type const&) const;
// ca >= cr | bool | Greater than equal to.
template
friend bool operator>=(typename fvar::root_type const&, fvar const&);
// cr < cr2 | bool | Less than comparison.
template
bool operator<(fvar const&) const;
// cr < ca | bool | Less than comparison.
bool operator<(root_type const&) const;
// ca < cr | bool | Less than comparison.
template
friend bool operator<(typename fvar::root_type const&, fvar const&);
// cr > cr2 | bool | Greater than comparison.
template
bool operator>(fvar const&) const;
// cr > ca | bool | Greater than comparison.
bool operator>(root_type const&) const;
// ca > cr | bool | Greater than comparison.
template
friend bool operator>(typename fvar::root_type const&, fvar const&);
// Will throw std::out_of_range if Order < order.
template
get_type_at at(size_t order, Orders... orders) const;
template
get_type_at derivative(Orders... orders) const;
const RealType& operator[](size_t) const;
fvar inverse() const; // Multiplicative inverse.
fvar& negate(); // Negate and return reference to *this.
static constexpr size_t depth = get_depth::value; // Number of nested std::array.
static constexpr size_t order_sum = get_order_sum::value;
explicit operator root_type() const; // Must be explicit, otherwise overloaded operators are ambiguous.
template ::type>::value>>
explicit operator T() const; // Must be explicit; multiprecision has trouble without the std::enable_if
fvar& set_root(root_type const&);
// Apply coefficients using horner method.
template
promote, Fvar, Fvars...> apply_coefficients(size_t const order,
Func const& f,
Fvar const& cr,
Fvars&&... fvars) const;
template
fvar apply_coefficients(size_t const order, Func const& f) const;
// Use when function returns derivative(i)/factorial(i) and may have some infinite derivatives.
template
promote, Fvar, Fvars...> apply_coefficients_nonhorner(size_t const order,
Func const& f,
Fvar const& cr,
Fvars&&... fvars) const;
template
fvar apply_coefficients_nonhorner(size_t const order, Func const& f) const;
// Apply derivatives using horner method.
template
promote, Fvar, Fvars...> apply_derivatives(size_t const order,
Func const& f,
Fvar const& cr,
Fvars&&... fvars) const;
template
fvar apply_derivatives(size_t const order, Func const& f) const;
// Use when function returns derivative(i) and may have some infinite derivatives.
template
promote, Fvar, Fvars...> apply_derivatives_nonhorner(size_t const order,
Func const& f,
Fvar const& cr,
Fvars&&... fvars) const;
template
fvar apply_derivatives_nonhorner(size_t const order, Func const& f) const;
private:
RealType epsilon_inner_product(size_t z0,
size_t isum0,
size_t m0,
fvar const& cr,
size_t z1,
size_t isum1,
size_t m1,
size_t j) const;
fvar epsilon_multiply(size_t z0, size_t isum0, fvar const& cr, size_t z1, size_t isum1) const;
fvar epsilon_multiply(size_t z0, size_t isum0, root_type const& ca) const;
fvar inverse_apply() const;
fvar& multiply_assign_by_root_type(bool is_root, root_type const&);
template
friend class fvar;
template
friend std::ostream& operator<<(std::ostream&, fvar const&);
// C++11 Compatibility
#ifdef BOOST_NO_CXX17_IF_CONSTEXPR
template
void fvar_cpp11(std::true_type, RootType const& ca, bool const is_variable);
template
void fvar_cpp11(std::false_type, RootType const& ca, bool const is_variable);
template
get_type_at at_cpp11(std::true_type, size_t order, Orders... orders) const;
template
get_type_at at_cpp11(std::false_type, size_t order, Orders... orders) const;
template
fvar epsilon_multiply_cpp11(std::true_type,
SizeType z0,
size_t isum0,
fvar const& cr,
size_t z1,
size_t isum1) const;
template
fvar epsilon_multiply_cpp11(std::false_type,
SizeType z0,
size_t isum0,
fvar const& cr,
size_t z1,
size_t isum1) const;
template
fvar epsilon_multiply_cpp11(std::true_type, SizeType z0, size_t isum0, root_type const& ca) const;
template
fvar epsilon_multiply_cpp11(std::false_type, SizeType z0, size_t isum0, root_type const& ca) const;
template
fvar& multiply_assign_by_root_type_cpp11(std::true_type, bool is_root, RootType const& ca);
template
fvar& multiply_assign_by_root_type_cpp11(std::false_type, bool is_root, RootType const& ca);
template
fvar& negate_cpp11(std::true_type, RootType const&);
template
fvar& negate_cpp11(std::false_type, RootType const&);
template
fvar& set_root_cpp11(std::true_type, RootType const& root);
template
fvar& set_root_cpp11(std::false_type, RootType const& root);
#endif
};
// C++11 compatibility
#ifdef BOOST_NO_CXX17_IF_CONSTEXPR
#define BOOST_AUTODIFF_IF_CONSTEXPR
#else
#define BOOST_AUTODIFF_IF_CONSTEXPR constexpr
#endif
// Standard Library Support Requirements
// fabs(cr1) | RealType
template
fvar fabs(fvar const&);
// abs(cr1) | RealType
template
fvar abs(fvar const&);
// ceil(cr1) | RealType
template
fvar ceil(fvar const&);
// floor(cr1) | RealType
template
fvar floor(fvar const&);
// exp(cr1) | RealType
template
fvar exp(fvar const&);
// pow(cr, ca) | RealType
template
fvar pow(fvar const&, typename fvar::root_type const&);
// pow(ca, cr) | RealType
template
fvar pow(typename fvar::root_type const&, fvar const&);
// pow(cr1, cr2) | RealType
template
promote, fvar> pow(fvar const&,
fvar const&);
// sqrt(cr1) | RealType
template
fvar sqrt(fvar const&);
// log(cr1) | RealType
template
fvar log(fvar const&);
// frexp(cr1, &i) | RealType
template
fvar frexp(fvar const&, int*);
// ldexp(cr1, i) | RealType
template
fvar ldexp(fvar const&, int);
// cos(cr1) | RealType
template
fvar cos(fvar const&);
// sin(cr1) | RealType
template
fvar sin(fvar const&);
// asin(cr1) | RealType
template
fvar asin(fvar const&);
// tan(cr1) | RealType
template
fvar tan(fvar const&);
// atan(cr1) | RealType
template
fvar atan(fvar const&);
// atan2(cr, ca) | RealType
template
fvar atan2(fvar const&, typename fvar::root_type const&);
// atan2(ca, cr) | RealType
template
fvar atan2(typename fvar::root_type const&, fvar const&);
// atan2(cr1, cr2) | RealType
template
promote, fvar> atan2(fvar const&,
fvar const&);
// fmod(cr1,cr2) | RealType
template
promote, fvar> fmod(fvar const&,
fvar const&);
// round(cr1) | RealType
template
fvar round(fvar