Mathematical functions
The following functions are implemented computing the truncated Taylor series of the argument:
exp()
,log()
,pow()
(4 overloads),sqrt()
,cbrt()
sin()
,cos()
,sin_and_cos()
,tan()
atan()
,asin()
,acos()
sinh()
,cosh()
,sinh_and_cosh()
,tanh()
atanh()
,asinh()
,acosh()
abs()
The following functions are implemented using the definition of their derivative:
erf()
: error function
#include <audi/functions.hpp>
-
template<typename T, typename M>
inline gdual<T, M> audi::exp(const gdual<T, M> &d) Overload for the exponential.
Implements the exponential of a audi::gdual. Essentially it performs the following computations in the \(\mathcal P_{n,m}\) algebra:
\[T_{(\exp f)} = \exp f_0 \sum_{i=0}^m \frac{\hat f^i}{i!} = \exp f_0 \left( 1 + \hat f + \frac {\hat f^2}{2!} + ... \right) \]where \(T_f = f_0 + \hat f\).
- Parameters:
d – audi::gdual argument
- Returns:
an audi:gdual containing the Taylor expansion of the exponential of
d
-
template<typename T, typename M>
inline gdual<T, M> audi::log(const gdual<T, M> &d) Overload for the logarithm.
Implements the logarithm of a audi::gdual. Essentially it performs the following computations in the \(\mathcal P_{n,m}\) algebra:
\[T_{(\log f)} = \log f_0 + \sum_{i=1}^m (-1)^{i+1} \frac 1i \left(\frac{\hat f}{f_0}\right)^i = \log f_0 + \frac{\hat f}{f_0} - \frac 12 \left(\frac{\hat f}{f_0}\right)^2 + ... \]where \(T_f = f_0 + \hat f\).
- Parameters:
d – audi::gdual argument
- Returns:
an audi:gdual containing the Taylor expansion of the logarithm of
d
-
template<typename T, typename U, typename M, enable_if_t<audi::is_arithmetic<U>::value, int> = 0>
inline gdual<T, M> audi::pow(U base, const gdual<T, M> &d) Overload for the exponentiation to a gdual power.
Computes the exponentiation to the power of an audi::gdual. If the exponent is an integer constant, it calls the std::pow overload. Otherwise it converts \(a^{T_f}\) to \(\exp(T_g*\log(a))\) and computes this last expression instead.
- Parameters:
base – the base for the exponent
d – audi::gdual argument
-
template<typename T, typename U, typename M, enable_if_t<audi::is_arithmetic<U>::value, int> = 0>
inline gdual<T, M> audi::pow(const gdual<T, M> &d, U alpha) Overload for the exponentiation.
Implements the exponentiation of a audi::gdual. Essentially it performs the following computations in the \(\mathcal P_{n,m}\) algebra:
\[T_{(f^\alpha)} = f_0^\alpha \sum_{k=0}^m {\alpha \choose k} \left(\hat f / f_0\right)^k \]where \(T_f = f_0 + \hat f\).
- Parameters:
d – audi::gdual argument
alpha – exponent
- Returns:
an audi:gdual containing the Taylor expansion of
d
elevated to the poweralpha
-
template<typename T, typename M>
inline gdual<T, M> audi::pow(const gdual<T, M> &d, int n) Overload for the integer exponentiation.
Implements the integer exponentiation of a audi::gdual. Essentially, it uses the \(\mathcal P_{n,m}\) multiplication on
d
n
times- Parameters:
d – audi::gdual argument
n – integer exponent
-
template<typename T, typename M>
inline gdual<T, M> audi::pow(const gdual<T, M> &d1, const gdual<T, M> &d2) Overload for the exponentiation of a gdual to a gdual power.
Computes the exponentiation of an audi::gdual to the power of an audi::gdual. If the exponent is an integer constant, it calls a different overload. Otherwise it converts \(T_f^{T_g}\) to \(\exp(T_g*\log(T_f))\) and computes this last expression instead.
- Parameters:
d1 – audi::gdual argument
d2 – audi::gdual argument
- Throws:
std::domain_error – if std::log( \(f_0\)) is not finite (uses std::isfinite)
-
template<typename T, typename M>
inline gdual<T, M> audi::sqrt(const gdual<T, M> &d) Overload for the square root.
Implements the square root of a audi::gdual. Essentially it performs the following computations in the \(\mathcal P_{n,m}\) algebra:
\[T_{\sqrt{f}} = \sqrt{f_0} \sum_{k=0}^m {\frac 12 \choose k} \left(\hat f / f_0\right)^k \]where \(T_f = f_0 + \hat f\).
- Parameters:
d – audi::gdual argument
- Returns:
an audi:gdual containing the Taylor expansion of the square root of
d
-
template<typename T, typename M>
inline gdual<T, M> audi::cbrt(const gdual<T, M> &d) Overload for the cubic root.
Implements the cubic root of a audi::gdual. Essentially it performs the following computations in the \(\mathcal P_{n,m}\) algebra:
\[T_{\sqrt[3]{f}} = \sqrt[3]{f_0} \sum_{k=0}^m {\frac 13 \choose k} \left(\hat f / f_0\right)^k \]where \(T_f = f_0 + \hat f\).
- Parameters:
d – audi::gdual argument
- Returns:
an audi:gdual containing the Taylor expansion of the square root of
d
-
template<typename T, typename M>
inline gdual<T, M> audi::sin(const gdual<T, M> &d) Overload for the sine.
Implements the sine of a audi::gdual. Essentially it performs the following computations in the \(\mathcal P_{n,m}\) algebra:
\[\begin{split}T_{(\sin f)} = \sin f_0 \left(\sum_{i=0}^{2i\le m} (-1)^{i} \frac{\hat f^{2i}}{(2i)!}\right) + \cos f_0 \left(\sum_{i=0}^{(2i+1)\le m} (-1)^{i} \frac{\hat f^{2i+1}}{(2i+1)!}\right) \\ \end{split}\]where \(T_f = f_0 + \hat f\).
- Parameters:
d – audi::gdual argument
- Returns:
an audi:gdual containing the Taylor expansion of the sine of
d
-
template<typename T, typename M>
inline gdual<T, M> audi::cos(const gdual<T, M> &d) Overload for the cosine.
Implements the cosine of a audi::gdual. Essentially it performs the following computations in the \(\mathcal P_{n,m}\) algebra:
\[T_{(\cos f)} = \cos f_0 \left(\sum_{i=0}^{2i\le m} (-1)^{i} \frac{\hat f^{2i}}{(2i)!}\right) - \sin f_0 \left(\sum_{i=0}^{(2i+1)\le m} (-1)^{i} \frac{\hat f^{2i+1}}{(2i+1)!}\right) \]where \(T_f = f_0 + \hat f\).
- Parameters:
d – audi::gdual argument
- Returns:
an audi:gdual containing the Taylor expansion of the cosine of
d
-
template<typename T, typename M>
std::array<gdual<T, M>, 2> audi::sin_and_cos(const gdual<T, M> &d) Computes both sine and cosine.
As most of the computations for the sine and cosine is the same, it is twice as fast to get both sine and cosine at once rather than computing them in sequence. Use this function when both sine and cosine are needed.
- Parameters:
d – audi::gdual argument
- Returns:
an std::array containing the Taylor expansions of sine and the cosine (first element, second element)
-
template<typename T, typename M>
inline gdual<T, M> audi::tan(const gdual<T, M> &d) Overload for the tangent.
Implements the tangent of a audi::gdual. Essentially, it performs the following computations in the \(\mathcal P_{n,m}\) algebra:
\[T_{(\tan f)} = \frac{\tan f_0 + \sum_{k=1}^{k \le 2k+1} B_{2k} \frac{(-4)^k(1-4^k)}{2k!}x^{2k - 1}}{1 - \tan f_0 \sum_{k=1}^{k \le 2k+1} \frac{B_{2k}(-4)^k(1-4^k)}{2k!}x^{2k - 1} } \]where \(T_f = f_0 + \hat f\) and \( B_{2k}\) are the Bernoulli numbers.
- Parameters:
d – audi::gdual argument
- Returns:
an audi:gdual containing the Taylor expansion of the tangent of
d
-
template<typename T, typename M>
inline gdual<T, M> audi::sinh(const gdual<T, M> &d) Overload for the hyperbolic sine.
Implements the hyperbolic sine of a audi::gdual. Essentially it performs the following computations in the \(\mathcal P_{n,m}\) algebra:
\[\begin{split}T_{(\sin f)} = \sinh f_0 \left(\sum_{i=0}^{2i\le m} \frac{\hat f^{2i}}{(2i)!}\right) + \cosh f_0 \left(\sum_{i=0}^{(2i+1)\le m} \frac{\hat f^{2i+1}}{(2i+1)!}\right) \\ \end{split}\]where \(T_f = f_0 + \hat f\).
- Parameters:
d – audi::gdual argument
- Returns:
an audi:gdual containing the Taylor expansion of the hyperbolic sine of
d
-
template<typename T, typename M>
inline gdual<T, M> audi::cosh(const gdual<T, M> &d) Overload for the hyperbolic cosine.
Implements the hyperbolic cosine of a audi::gdual. Essentially it performs the following computations in the \(\mathcal P_{n,m}\) algebra:
\[\begin{split}T_{(\sin f)} = \cosh f_0 \left(\sum_{i=0}^{2i\le m} \frac{\hat f^{2i}}{(2i)!}\right) + \sinh f_0 \left(\sum_{i=0}^{(2i+1)\le m} \frac{\hat f^{2i+1}}{(2i+1)!}\right) \\ \end{split}\]where \(T_f = f_0 + \hat f\).
- Parameters:
d – audi::gdual argument
- Returns:
an audi:gdual containing the Taylor expansion of the hyperbolic cosine of
d
-
template<typename T, typename M>
std::array<gdual<T, M>, 2> audi::sinh_and_cosh(const gdual<T, M> &d) Computes both the hyperbolic sine and the hyperbolic cosine.
As most of the computations for the hyperbolic sine and hyperbolic cosine is the same, it is twice as fast to get them both at once rather than computing them in sequence. Use this function when both the hyperbolic sine and the hyperbolic cosine are needed.
- Parameters:
d – audi::gdual argument
- Returns:
an std::array containing the Taylor expansions of hyperbolic sine and cosine (first element, second element)
-
template<typename T, typename M>
inline gdual<T, M> audi::tanh(const gdual<T, M> &d) Overload for the hyperbolic tangent.
Implements the hyperbolic tangent of a audi::gdual. Essentially, it performs the following computations in the \(\mathcal P_{n,m}\) algebra:
\[T_{(\tan f)} = \frac{\tanh f_0 + \sum_{k=1}^{k \le 2k+1} B_{2k} \frac{4^k(4^k-1)}{2k!}x^{2k - 1}}{1 + \tanh f_0 \sum_{k=1}^{k \le 2k+1} \frac{B_{2k}4^k(4^k-1)}{2k!}x^{2k - 1} } \]where \(T_f = f_0 + \hat f\) and \( B_{2k}\) are the Bernoulli numbers.
- Parameters:
d – audi::gdual argument
- Returns:
an audi::gdual containing the Taylor expansion of the hyperbolic tangent of
d
-
template<typename T, typename M>
inline gdual<T, M> audi::atanh(const gdual<T, M> &d) Overload for the inverse hyperbolic tangent.
Implements the inverse hyperbolic tangent of an audi::gdual. Essentially, it performs the following computations in the \(\mathcal P_{n,m}\) algebra:
\[T_{(\mbox{atanh} f)} = \mbox{atanh} f_0 +\frac 12 \sum_{k=1}^m \left(\frac{1}{(1-f_0)^k} + \frac{(-1)^{k+1}}{(1+f_0)^k}\right) \frac {\hat f^k}{k} \]- Parameters:
d – audi::gdual argument
- Returns:
an audi::gdual containing the Taylor expansion of the inverse hyperbolic tangent of
d
-
template<typename T, typename M>
inline gdual<T, M> audi::atan(const gdual<T, M> &d) Overload for the inverse tangent.
Implements the inverse tangent of an audi::gdual. Essentially, it performs the following computations in the \(\mathcal P_{n,m}\) algebra:
\[T_{(\mbox{atan} f)} = \mbox{atan} f_0 + \sum_{k=1}^{2k-1\le m} \left(\frac{1 + \sum_{j=1}^{2j\le 2k-1} {2k-1 \choose 2j} f_0^{2j}(-1)^j}{(1+f_0^2)^{2k-1}}\right) \frac {\hat f^{2k-1}}{2k-1}(-1)^{k+1} + \sum_{k=1}^{2k\le m} \left(\frac{\sum_{j=1}^{2j-1\le 2k} {2k \choose 2j-1} f_0^{2j-1}(-1)^{j+1}}{(1+f_0^2)^{2k}}\right) \frac {\hat f^{2k}}{2k}(-1)^k \]This formula derives directly from the formula for audi::atanh noting that: \( \mbox{atan}(z) = i \mbox{atanh}(-iz)\)
- Parameters:
d – audi::gdual argument
- Returns:
an audi::gdual containing the Taylor expansion of the inverse tangent of
d
-
template<typename T, typename M>
inline gdual<T, M> audi::asinh(const gdual<T, M> &d) Overload for the inverse hyperbolic sine.
Implements the inverse inverse hyperbolic sine of an audi::gdual. Essentially, it performs the following computations in the \(\mathcal P_{n,m}\) algebra:
\[T_{(\mbox{asinh} f)} = T_{\left(\log\left(f + \sqrt{1 + f^2}\right)\right)} \]- Parameters:
d – audi::gdual argument
- Returns:
an audi::gdual containing the Taylor expansion of the inverse hyperbolic sine of
d
-
template<typename T, typename M>
inline gdual<T, M> audi::acosh(const gdual<T, M> &d) Overload for the inverse hyperbolic cosine.
Implements the inverse inverse hyperbolic cosine of an audi::gdual. Essentially, it performs the following computations in the \(\mathcal P_{n,m}\) algebra:
\[T_{(\mbox{acosh} f)} = T_{\left(\log\left(f + \sqrt{f^2 - 1}\right)\right)} \]- Parameters:
d – audi::gdual argument
- Returns:
an audi::gdual containing the Taylor expansion of the inverse hyperbolic cosine of
d
-
template<typename T, typename M>
inline gdual<T, M> audi::asin(const gdual<T, M> &d) Overload for the inverse sine.
Implements the inverse inverse sine of an audi::gdual. Essentially, it performs the following computations in the \(\mathcal P_{n,m}\) algebra:
\[T_{(\mbox{asin} f)} = T_{\left(\mbox{atan} \left(f / \sqrt{1 - f^2}\right)\right)} \]- Parameters:
d – audi::gdual argument
- Returns:
an audi::gdual containing the Taylor expansion of the inverse sine of
d
-
template<typename T, typename M>
inline gdual<T, M> audi::acos(const gdual<T, M> &d) Overload for the inverse cosine.
Implements the inverse inverse cosine of an audi::gdual. Essentially, it performs the following computations in the \(\mathcal P_{n,m}\) algebra:
\[T_{(\mbox{acos} f)} = T_{\left(\mbox{atan} \left(\sqrt{1 - f^2} / f\right)\right)} \]- Parameters:
d – audi::gdual argument
- Returns:
an audi::gdual containing the Taylor expansion of the inverse cosine of
d
-
template<typename T, typename M>
inline gdual<T, M> audi::abs(const gdual<T, M> &d) Overload for the absolute value.
Implements the absolute value of a audi::gdual. Essentially, it inverts the sign of \(T_f\) as follows:
\[\begin{split}T_{(\mbox{abs} f)} = \left\{ \begin{array}{ll} T_f & f_0 \ge 0 \\ -T_f & f_0 < 0 \end{array} \right. \end{split}\]where \(T_f = f_0 + \hat f\).
Note
If \(f_0\) is zero, the right Taylor expansion will be returned rather than nans.
Note
This operation is not availiable for std::complex types.
- Parameters:
d – audi::gdual argument
- Returns:
an audi:gdual containing the Taylor expansion of the absoute value of
d