Elementary Functions

GNSTLIB includes exponential, logarithmic, powers, trigonometric and hyperbolic functions. These elementary functions are included in the following header file:

#include <gnstlib_basic.hpp>

Exponentials functions

GNSTLIB::exp

double exp(const double x)

Computes the exponential function \(e^x\) for real \(x\). The exponential function is defined by

\[e^x = \sum_{k=0}^{\infty} \frac{x^k}{k!}.\]
  1. Arguments:

    • x: (double) - argument.

  2. Example:

    #include <iomanip>
    #include <iostream>
    
    #include <gnstlib_basic.hpp>
    
    void test_exp()
    {
       double x = 3.2;
       double result = GNSTLIB::exp(x);
       std::cout << std::setprecision(16) << result << std::endl;
    }
    
std::complex<double> exp(const std::complex<double> z)

Computes the exponential function \(e^z\) for complex \(z\). The exponential function for complex argument is defined by the above power series and satisfies the following functional relation

\[e^{(x + iy)} = e^x(\cos(y) + i \sin(y)).\]
  1. Arguments:

    • z: (complex) - argument.

  2. Example:

    #include <complex>
    #include <iomanip>
    #include <iostream>
    
    #include <gnstlib_basic.hpp>
    
    void test_exp()
    {
       std::complex<double> z = std::complex<double>(3.2, 2.1);
       std::complex<double> result = GNSTLIB::exp(z);
       std::cout << std::setprecision(16) << result << std::endl;
    }
    

GNSTLIB::exp2

double exp2(const double x)

Computes \(2^x\) for real power \(x\).

  1. Arguments:

    • x: (double) - argument.

GNSTLIB::exp10

double exp10(const double x)

Computes \(10^x\) for real power \(x\).

  1. Arguments:

    • x: (double) - argument.

GNSTLIB::expm1

double expm1(const double x)

Computes \(e^x - 1\) for real power \(x\). The function is more accurate than evaluating exp(x)-1.0 for \(x\) close to 0.

  1. Arguments:

    • x: (double) - argument.

GNSTLIB::expm1x

double expm1x(const double x)

Computes \(\frac{e^x - 1}{x}\) for real \(x\). The function is more accurate than evaluating (exp(x)-1.0)/x for \(x\) close to 0.

  1. Arguments:

    • x: (double) - argument.

GNSTLIB::expm1mx

double expm1mx(const double x)

Computes \(\frac{2(e^x - 1)}{x^2}\) for real \(x\). The function is more accurate than evaluating 2.0*(exp(x)-1.0-x)/(x*x) for \(x\) close to 0.

  1. Arguments:

    • x: (double) - argument.

Logarithmic functions

GNSTLIB::log

double log(const double x)

Computes the natural logarithm \(\log(x)\) for real \(x \ge 0\).

  1. Arguments:

    • x: (double) - argument.

      Constraint: x \(\ge\) 0.0.

  2. Errors and Warnings:

    • Not err_id is used. The natural logarithm is real for x > 0.0. For x \(=\) 0.0, \(-\infty\) is returned. If x < 0.0 the result is complex and it is returned as NaN.

std::complex<double> log(const std::complex<double> z)

Computes the natural logarithm \(\log(z)\) for complex \(z\) or when \(z\) is real and \(z <0\).

  1. Arguments:

    • z: (complex) - argument.

GNSTLIB::log2

double log2(const double x)

Computes the binary (base-2) logarithm \(\mathrm{log}_2(x)\) for real \(x \ge 0\).

  1. Arguments:

    • x: (double) - argument.

      Constraint: x \(\ge\) 0.0.

  2. Errors and Warnings:

    • Not err_id is used. The result is real for x > 0.0. For x \(=\) 0.0, \(-\infty\) is returned. If x < 0.0 the result is complex and it is returned as NaN.

GNSTLIB::log10

double log10(const double x)

Computes the base-10 logarithm \(\mathrm{log}_{10}(x)\) for real \(x \ge 0\).

  1. Arguments:

    • x: (double) - argument.

      Constraint: x \(\ge\) 0.0.

  2. Errors and Warnings:

    • Not err_id is used. The result is real for x > 0.0. For x \(=\) 0.0, \(-\infty\) is returned. If x < 0.0 the result is complex and it is returned as NaN.

GNSTLIB::log1p

double log1p(const double x)

Computes the shifted logarithmic function \(\log(1+x)\) for real \(x \ge -1\). The function is more accurate than evaluating log(1+x) for \(x\) close to 0.

  1. Arguments:

    • x: (double) - argument.

      Constraint: x \(\ge\) -1.0.

  2. Errors and Warnings:

    • Not err_id is used. The result is real for x > -1.0. For x \(=\) -1.0, \(-\infty\) is returned. If x < -1.0 the result is complex and it is returned as NaN.

GNSTLIB::log1pmx

double log1pmx(const double x)

Computes \(\log(1+x)-x\) for real \(x \ge -1\). The function is more accurate than evaluating log(1+x)-x for \(x\) close to 0.

  1. Arguments:

    • x: (double) - argument.

      Constraint: x \(\ge\) -1.0.

  2. Errors and Warnings:

    • Not err_id is used. The result is real for x > -1.0. For x math:= -1.0, \(-\infty\) is returned. If x < -1.0 the result is complex and it is returned as NaN.

GNSTLIB::log1pexp

double log1pexp(const double x)

Computes \(\log(1+e^x)\) for real \(x\).

  1. Algorithm:

    log1pexp() is derived from the algorithm in [1]. The scheme of computation is given by

    \[\begin{split}\mathrm{log1pexp}(x) = \begin{cases} \exp(x) & x \le -37\\ \mathrm{log1p}(\exp(x)) & -37 < x \le 18\\ x + \exp(-x) & 18 < x \le 33.3\\ x & x > 33.3 \end{cases}\end{split}\]

    See log1p().

  2. Arguments:

    • x: (double) - argument.

GNSTLIB::log1mexp

double log1mexp(const double x, int &err_id)

Computes \(\log(1-e^x)\) for real \(x < 0\).

  1. Algorithm:

    log1mexp() is derived from the algorithm in [1]. The scheme of computation is given by

    \[\begin{split}\mathrm{log1mexp}(x) = \begin{cases} \log(-\mathrm{expm1(x)}) & 0 \le -x \le \log(2)\\ \mathrm{log1p}(-\mathrm{exp(x)}) & -x > \log(2)\\ \end{cases}\end{split}\]

    See expm1() and log1p().

  2. Arguments:

    • x: (double) - argument.

      Constraint: x < 0.0.

    • err_id: (int) - error identifier.

  3. Errors and Warnings:

    • err_id = 1:

      Argument x \(\ge\) 0.0. The result is complex and it is returned as NaN.

  4. Example:

    #include <iomanip>
    #include <iostream>
    
    #include <gnstlib_basic.hpp>
    
    void test_log1mexp()
    {
       double x = 1.5;
       int err_id = 0;
       double result = GNSTLIB::log1mexp(x, err_id);
       std::cout << std::setprecision(16) << result << std::endl;
       std::cout << err_id = << err_id << std::endl;
    }
    

    The above example produces the following output:

    nan
    err_id = 1
    

Trigonometric functions

GNSTLIB::cos

double cos(const double x)

Computes the cosine function \(\cos(x)\) for real \(x\).

  1. Arguments:

    • x: (double) - argument.

std::complex<double> cos(const std::complex<double> z)

Computes \(\cos(z)\) for complex \(z\).

  1. Arguments:

    • z: (complex) - argument.

GNSTLIB::sin

double sin(const double x)

Computes the sine function \(\sin(x)\) for real \(x\).

  1. Arguments:

    • x: (double) - argument.

std::complex<double> sin(const std::complex<double> z)

Computes \(\sin(z)\) for complex \(z\).

  1. Arguments:

    • z: (complex) - argument.

GNSTLIB::tan

double tan(const double x)

Computes the tangent function \(\tan(x)\) for real \(x\). The tangent function has poles at \(x = \pi (n + 1/2)\), but \(\pi/2\) cannot be exactly represented in common floating-point arithmetic.

  1. Arguments:

    • x: (double) - argument.

std::complex<double> tan(const std::complex<double> z)

Computes \(\tan(z)\) for complex \(z\).

  1. Arguments:

    • z: (complex) - argument.

GNSTLIB::sec

double sec(const double x)

Computes the secant function \(\sec(x)\) for real \(x\). The secant function has poles at \(x = \pi (n + 1/2)\), but \(\pi/2\) cannot be exactly represented in common floating-point arithmetic.

  1. Arguments:

    • x: (double) - argument.

std::complex<double> sec(const std::complex<double> z)

Computes \(\sec(z)\) for complex \(z\).

  1. Arguments:

    • z: (complex) - argument.

GNSTLIB::csc

double csc(const double x)

Computes the cosecant function \(\csc(x)\) for real \(x\). The cosecant function has poles at \(x = \pi n\), but \(\pi n\) cannot be exactly represented in common floating-point arithmetic.

  1. Arguments:

    • x: (double) - argument.

std::complex<double> csc(const std::complex<double> z)

Computes \(\csc(z)\) for complex \(z\).

  1. Arguments:

    • z: (complex) - argument.

GNSTLIB::cot

double cot(const double x)

Computes the cotangent function \(\csc(x)\) for real \(x\). The cotangent function has poles at \(x = \pi n\), but \(\pi n\) cannot be exactly represented in common floating-point arithmetic.

  1. Arguments:

    • x: (double) - argument.

std::complex<double> cot(const std::complex<double> z)

Computes \(\cot(z)\) for complex \(z\).

  1. Arguments:

    • z: (complex) - argument.

GNSTLIB::acos

double acos(const double x)

Computes arccosine function \(\text{acos}(x)\) for real \(x \in [-1, 1]\). Outside this domain NaN is returned.

  1. Arguments:

    • x: (double) - argument.

std::complex<double> acos(const std::complex<double> z)

Computes \(\text{acos}(z)\) for complex \(z\).

  1. Arguments:

    • z: (complex) - argument.

GNSTLIB::asin

double asin(const double x)

Computes arcsine function \(\text{asin}(x)\) for real \(x \in [-1, 1]\). Outside this domain NaN is returned.

  1. Arguments:

    • x: (double) - argument.

std::complex<double> asin(const std::complex<double> z)

Computes \(\text{asin}(z)\) for complex \(z\).

  1. Arguments:

    • z: (complex) - argument.

GNSTLIB::atan

double atan(const double x)

Computes the arctangent function \(\text{atan}(x)\) for real argument \(x \in (-\pi /2, \pi / 2)\).

  1. Arguments:

    • x: (double) - argument.

std::complex<double> atan(const std::complex<double> z)

Computes \(\text{atan}(z)\) for complex \(z\).

  1. Arguments:

    • z: (complex) - argument.

GNSTLIB::atan2

double atan2(const double y, const double x)

Computes the arctangent function \(\text{atan}(y/x)\) using the signs of arguments to determine the angle for the correct quadrant.

  1. Arguments:

    • x: (double) - argument.

    • y: (double) - argument.

GNSTLIB::asec

double asec(const double x)

Computes the inverse secant function \(\text{asec}(x) = \text{acos}(1/x)\) for real \(x\).

  1. Arguments:

    • x: (double) - argument.

std::complex<double> asec(const std::complex<double> z)

Computes \(\text{asec}(z)\) for complex \(z\).

  1. Arguments:

    • z: (complex) - argument.

GNSTLIB::acsc

double acsc(const double x)

Computes the inverse cosecant function \(\text{acsc}(x) = \text{asin}(1/x)\) for real \(x\).

  1. Arguments:

    • x: (double) - argument.

std::complex<double> acsc(const std::complex<double> z)

Computes \(\text{acsc}(z)\) for complex \(z\).

  1. Arguments:

    • z: (complex) - argument.

GNSTLIB::acot

double acot(const double x)

Computes the inverse cotangent function \(\text{acsc}(x) = \text{atan}(1/x)\) for real \(x\).

  1. Arguments:

    • x: (double) - argument.

std::complex<double> acot(const std::complex<double> z)

Computes \(\text{acot}(z)\) for complex \(z\).

  1. Arguments:

    • z: (complex) - argument.

GNSTLIB::cospi

double cospi(const double x)

Computes \(\cos(\pi x)\) for real \(x\). This function is more accurate than evaluating cos(pi * x).

  1. Arguments:

    • x: (double) - argument.

std::complex<double> cospi(const std::complex<double> z)

Computes \(\cos(\pi z)\) for complex \(z\).

  1. Arguments:

    • z: (complex) - argument.

GNSTLIB::sinpi

double sinpi(const double x)

Computes \(\sin(\pi x)\) for real \(x\). This function is more accurate than evaluating sin(pi * x).

  1. Arguments:

    • x: (double) - argument.

std::complex<double> sinpi(const std::complex<double> z)

Computes \(\sin(\pi z)\) for complex \(z\).

  1. Arguments:

    • z: (complex) - argument.

Hyperbolic functions

GNSTLIB::cosh

double cosh(const double x)

Computes the hyperbolic cosine function \(\cosh(x)\) for real \(x\).

  1. Arguments:

    • x: (double) - argument.

std::complex<double> cosh(const std::complex<double> z)

Computes \(\cosh(z)\) for complex \(z\).

  1. Arguments:

    • z: (complex) - argument.

GNSTLIB::sinh

double sinh(const double x)

Computes the hyperbolic sine function \(\sinh(x)\) for real \(x\).

  1. Arguments:

    • x: (double) - argument.

std::complex<double> sinh(const std::complex<double> z)

Computes \(\sinh(z)\) for complex \(z\).

  1. Arguments:

    • z: (complex) - argument.

GNSTLIB::tanh

double tanh(const double x)

Computes the hyperbolic tangent function \(\tanh(x)\) for real \(x\).

  1. Arguments:

    • x: (double) - argument.

std::complex<double> tanh(const std::complex<double> z)

Computes \(\tanh(z)\) for complex \(z\).

  1. Arguments:

    • z: (complex) - argument.

GNSTLIB::sech

double sech(const double x)

Computes the hyperbolic secant function \(\text{sech}(x)\) for real \(x\).

  1. Arguments:

    • x: (double) - argument.

std::complex<double> sech(const std::complex<double> z)

Computes \(\text{sech}(z)\) for complex \(z\).

  1. Arguments:

    • z: (complex) - argument.

GNSTLIB::csch

double csch(const double x)

Computes the hyperbolic cosecant function \(\text{csch}(x)\) for real \(x\).

  1. Arguments:

    • x: (double) - argument.

std::complex<double> csch(const std::complex<double> z)

Computes \(\text{csch}(z)\) for complex \(z\).

  1. Arguments:

    • z: (complex) - argument.

GNSTLIB::coth

double coth(const double x)

Computes the hyperbolic cotangent function \(\coth(x)\) for real \(x\).

  1. Arguments:

    • x: (double) - argument.

std::complex<double> coth(const std::complex<double> z)

Computes \(\coth(z)\) for complex \(z\).

  1. Arguments:

    • z: (complex) - argument.

GNSTLIB::acosh

double acosh(const double x)

Computes the inverse hyperbolic cosine function \(\text{acosh}(x)\) for real \(x\).

  1. Arguments:

    • x: (double) - argument.

std::complex<double> acosh(const std::complex<double> z)

Computes \(\text{acosh}(x)\) for complex \(z\).

  1. Arguments:

    • z: (complex) - argument.

GNSTLIB::asinh

double asinh(const double x)

Computes the inverse hyperbolic sine function \(\text{asinh}(x)\) for real \(x\).

  1. Arguments:

    • x: (double) - argument.

std::complex<double> asinh(const std::complex<double> z)

Computes \(\text{asinh}(x)\) for complex \(z\).

  1. Arguments:

    • z: (complex) - argument.

GNSTLIB::atanh

double atanh(const double x)

Computes the inverse hyperbolic tangent function \(\text{atanh}(x)\) for real \(x\).

  1. Arguments:

    • x: (double) - argument.

std::complex<double> atanh(const std::complex<double> z)

Computes \(\text{atanh}(x)\) for complex \(z\).

  1. Arguments:

    • z: (complex) - argument.

GNSTLIB::asech

double asech(const double x)

Computes the inverse hyperbolic secant function \(\text{asech}(x)\) for real \(x\).

  1. Arguments:

    • x: (double) - argument.

std::complex<double> asech(const std::complex<double> z)

Computes \(\text{asech}(x)\) for complex \(z\).

  1. Arguments:

    • z: (complex) - argument.

GNSTLIB::acsch

double acsch(const double x)

Computes the inverse hyperbolic cosecant function \(\text{acsch}(x)\) for real \(x\).

  1. Arguments:

    • x: (double) - argument.

std::complex<double> acsch(const std::complex<double> z)

Computes \(\text{acsch}(x)\) for complex \(z\).

  1. Arguments:

    • z: (complex) - argument.

GNSTLIB::acoth

double acoth(const double x)

Computes the inverse hyperbolic cotangent function \(\text{acoth}(x)\) for real \(x\).

  1. Arguments:

    • x: (double) - argument.

std::complex<double> acoth(const std::complex<double> z)

Computes \(\text{acoth}(x)\) for complex \(z\).

  1. Arguments:

    • z: (complex) - argument.

Power functions

GNSTLIB::pow

double pow(const double x, const double y)

Computes \(x^y\) for real \(x\) and \(y\).

  1. Arguments:

    • x: (double) - argument.

      Constraint: If x \(<\) 0.0, y must be an integer.

    • y: (double) - argument.

  2. Errors and Warnings:

    • Not err_id is used. For x \(<\) 0.0 and non-integer y the result is complex and it is returned as NaN. For x \(=\) 0.0 and y \(<\) 0.0, \(\infty\) is returned. For x \(=\) 0.0 and y \(=\) 0.0, 1.0 is returned.

std::complex<double> pow(const std::complex<double> x, const std::complex<double> y)

Computes \(x^y\) for complex \(x\) and \(y\) using std::exp(y*std::log(x)).

  1. Arguments:

    • x: (complex) - argument.

    • y: (complex) - argument.

GNSTLIB::powm1

double powm1(const double x, const double y, int &err_id)

Computes \(x^y - 1\) for real \(x\) and \(y\). The function is more accurate than evaluating x^y - 1 for \(x\) close to 1 (equivalently for \(y\) close to 0).

  1. Arguments:

    • x: (double) - argument.

      Constraint: If x \(<\) 0.0, y must be an integer.

    • y: (double) - argument.

    • err_id: (int) - error identifier.

  2. Errors and Warnings:

    • err_id = 1:

      Argument x \(<\) 0.0 and y non-integer. The result is complex and it is returned as NaN.

GNSTLIB::sqrt

double sqrt(const double x)

Computes the square root of \(x\).

  1. Arguments:

    • x: (double) - argument.

      Constraint: x \(\ge\) 0.0.

  2. Errors and Warnings:

    • Not err_id is used. For x \(<\) 0.0 the result is complex and it is returned as NaN.

std::complex<double> sqrt(const std::complex<double> z)

Computes \(\sqrt{z}\) for complex \(z\).

  1. Arguments:

    • z: (complex) - argument.

GNSTLIB::cbrt

double cbrt(const double x)

Computes the cubic root of \(x\).

  1. Arguments:

    • x: (double) - argument.

GNSTLIB::hypot

double hypot(const double x, const double y)

Computes the length of the hypotenuse of a right-angled triangle \(\sqrt{x^2 + y^2}\) with real sides \(x\) and \(y\).

  1. Arguments:

    • x: (double) - argument.

    • y: (double) - argument.

References

[1]
  1. Mächler (2012). Accurately Computing \(\log(1-\exp(-|a|))\) Assessed by the Rmpfr package. [https://cran.r-project.org/web/packages/Rmpfr/vignettes/log1mexp-note.pdf]