Blame doc/math.rst

Packit 67cb25
.. index::
Packit 67cb25
   single: elementary functions
Packit 67cb25
   single: mathematical functions, elementary
Packit 67cb25
Packit 67cb25
**********************
Packit 67cb25
Mathematical Functions
Packit 67cb25
**********************
Packit 67cb25
Packit 67cb25
This chapter describes basic mathematical functions.  Some of these
Packit 67cb25
functions are present in system libraries, but the alternative versions
Packit 67cb25
given here can be used as a substitute when the system functions are not
Packit 67cb25
available.
Packit 67cb25
Packit 67cb25
The functions and macros described in this chapter are defined in the
Packit 67cb25
header file :file:`gsl_math.h`.
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: mathematical constants, defined as macros
Packit 67cb25
   single: numerical constants, defined as macros
Packit 67cb25
   single: constants, mathematical (defined as macros)
Packit 67cb25
   single: macros for mathematical constants
Packit 67cb25
Packit 67cb25
Mathematical Constants
Packit 67cb25
======================
Packit 67cb25
Packit 67cb25
The library ensures that the standard BSD mathematical constants
Packit 67cb25
are defined. For reference, here is a list of the constants:
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: e, defined as a macro
Packit 67cb25
   single: pi, defined as a macro
Packit 67cb25
   single: Euler's constant, defined as a macro
Packit 67cb25
Packit 67cb25
===================== ===================================
Packit 67cb25
:macro:`M_E`          The base of exponentials, :math:`e`
Packit 67cb25
:macro:`M_LOG2E`      The base-2 logarithm of :math:`e`, :math:`\log_2 (e)`
Packit 67cb25
:macro:`M_LOG10E`     The base-10 logarithm of :math:`e`, :math:`\log_{10} (e)`
Packit 67cb25
:macro:`M_SQRT2`      The square root of two, :math:`\sqrt 2`
Packit 67cb25
:macro:`M_SQRT1_2`    The square root of one-half, :math:`\sqrt{1/2}`
Packit 67cb25
:macro:`M_SQRT3`      The square root of three, :math:`\sqrt 3`
Packit 67cb25
:macro:`M_PI`         The constant pi, :math:`\pi`
Packit 67cb25
:macro:`M_PI_2`       Pi divided by two, :math:`\pi/2`
Packit 67cb25
:macro:`M_PI_4`       Pi divided by four, :math:`\pi/4`
Packit 67cb25
:macro:`M_SQRTPI`     The square root of pi, :math:`\sqrt\pi`
Packit 67cb25
:macro:`M_2_SQRTPI`   Two divided by the square root of pi, :math:`2/\sqrt\pi`
Packit 67cb25
:macro:`M_1_PI`       The reciprocal of pi, :math:`1/\pi`
Packit 67cb25
:macro:`M_2_PI`       Twice the reciprocal of pi, :math:`2/\pi`
Packit 67cb25
:macro:`M_LN10`       The natural logarithm of ten, :math:`\ln(10)`
Packit 67cb25
:macro:`M_LN2`        The natural logarithm of two, :math:`\ln(2)`
Packit 67cb25
:macro:`M_LNPI`       The natural logarithm of pi, :math:`\ln(\pi)`
Packit 67cb25
:macro:`M_EULER`      Euler's constant, :math:`\gamma`
Packit 67cb25
===================== ===================================
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: infinity, defined as a macro
Packit 67cb25
   single: IEEE infinity, defined as a macro
Packit 67cb25
Packit 67cb25
Infinities and Not-a-number
Packit 67cb25
===========================
Packit 67cb25
Packit 67cb25
.. macro:: GSL_POSINF
Packit 67cb25
Packit 67cb25
   This macro contains the IEEE representation of positive infinity,
Packit 67cb25
   :math:`+\infty`. It is computed from the expression :code:`+1.0/0.0`.
Packit 67cb25
Packit 67cb25
.. macro:: GSL_NEGINF
Packit 67cb25
Packit 67cb25
   This macro contains the IEEE representation of negative infinity,
Packit 67cb25
   :math:`-\infty`. It is computed from the expression :code:`-1.0/0.0`.
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: NaN, defined as a macro
Packit 67cb25
   single: Not-a-number, defined as a macro
Packit 67cb25
   single: IEEE NaN, defined as a macro
Packit 67cb25
Packit 67cb25
.. macro:: GSL_NAN
Packit 67cb25
Packit 67cb25
   This macro contains the IEEE representation of the Not-a-Number symbol,
Packit 67cb25
   :code:`NaN`. It is computed from the ratio :code:`0.0/0.0`.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_isnan (const double x)
Packit 67cb25
Packit 67cb25
   This function returns 1 if :data:`x` is not-a-number.
Packit 67cb25
Packit 67cb25
.. function:: int gsl_isinf (const double x)
Packit 67cb25
Packit 67cb25
   This function returns :math:`+1` if :data:`x` is positive infinity,
Packit 67cb25
   :math:`-1` if :data:`x` is negative infinity and 0
Packit 67cb25
   otherwise. [#f1]_
Packit 67cb25
Packit 67cb25
.. function:: int gsl_finite (const double x)
Packit 67cb25
Packit 67cb25
   This function returns 1 if :data:`x` is a real number, and 0 if it is
Packit 67cb25
   infinite or not-a-number.
Packit 67cb25
Packit 67cb25
Elementary Functions
Packit 67cb25
====================
Packit 67cb25
Packit 67cb25
The following routines provide portable implementations of functions
Packit 67cb25
found in the BSD math library.  When native versions are not available
Packit 67cb25
the functions described here can be used instead.  The substitution can
Packit 67cb25
be made automatically if you use :code:`autoconf` to compile your
Packit 67cb25
application (see :ref:`portability-functions`).
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: log1p
Packit 67cb25
   single: logarithm, computed accurately near 1
Packit 67cb25
Packit 67cb25
.. function:: double gsl_log1p (const double x)
Packit 67cb25
Packit 67cb25
   This function computes the value of :math:`\log(1+x)` in a way that is
Packit 67cb25
   accurate for small :data:`x`. It provides an alternative to the BSD math
Packit 67cb25
   function :code:`log1p(x)`.
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: expm1
Packit 67cb25
   single: exponential, difference from 1 computed accurately
Packit 67cb25
Packit 67cb25
.. function:: double gsl_expm1 (const double x)
Packit 67cb25
Packit 67cb25
   This function computes the value of :math:`\exp(x)-1` in a way that is
Packit 67cb25
   accurate for small :data:`x`. It provides an alternative to the BSD math
Packit 67cb25
   function :code:`expm1(x)`.
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: hypot
Packit 67cb25
   single: euclidean distance function, hypot
Packit 67cb25
   single: length, computed accurately using hypot
Packit 67cb25
Packit 67cb25
.. function:: double gsl_hypot (const double x, const double y)
Packit 67cb25
Packit 67cb25
   This function computes the value of
Packit 67cb25
   :math:`\sqrt{x^2 + y^2}` in a way that avoids overflow. It provides an
Packit 67cb25
   alternative to the BSD math function :code:`hypot(x,y)`.
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: euclidean distance function, hypot3
Packit 67cb25
   single: length, computed accurately using hypot3
Packit 67cb25
Packit 67cb25
.. function:: double gsl_hypot3 (const double x, const double y, const double z)
Packit 67cb25
Packit 67cb25
   This function computes the value of
Packit 67cb25
   :math:`\sqrt{x^2 + y^2 + z^2}` in a way that avoids overflow.
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: acosh
Packit 67cb25
   single: hyperbolic cosine, inverse
Packit 67cb25
   single: inverse hyperbolic cosine
Packit 67cb25
Packit 67cb25
.. function:: double gsl_acosh (const double x)
Packit 67cb25
Packit 67cb25
   This function computes the value of :math:`\arccosh{(x)}`. It provides an
Packit 67cb25
   alternative to the standard math function :code:`acosh(x)`.
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: asinh
Packit 67cb25
   single: hyperbolic sine, inverse
Packit 67cb25
   single: inverse hyperbolic sine
Packit 67cb25
Packit 67cb25
.. function:: double gsl_asinh (const double x)
Packit 67cb25
Packit 67cb25
   This function computes the value of :math:`\arcsinh{(x)}`. It provides an
Packit 67cb25
   alternative to the standard math function :code:`asinh(x)`.
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: atanh
Packit 67cb25
   single: hyperbolic tangent, inverse
Packit 67cb25
   single: inverse hyperbolic tangent
Packit 67cb25
Packit 67cb25
.. function:: double gsl_atanh (const double x)
Packit 67cb25
Packit 67cb25
   This function computes the value of :math:`\arctanh{(x)}`. It provides an
Packit 67cb25
   alternative to the standard math function :code:`atanh(x)`.
Packit 67cb25
Packit 67cb25
.. index:: ldexp
Packit 67cb25
Packit 67cb25
.. function:: double gsl_ldexp (double x, int e)
Packit 67cb25
Packit 67cb25
   This function computes the value of :math:`x * 2^e`. It provides an
Packit 67cb25
   alternative to the standard math function :code:`ldexp(x,e)`.
Packit 67cb25
Packit 67cb25
.. index:: frexp
Packit 67cb25
Packit 67cb25
.. function:: double gsl_frexp (double x, int * e)
Packit 67cb25
Packit 67cb25
   This function splits the number :data:`x` into its normalized fraction
Packit 67cb25
   :math:`f` and exponent :math:`e`, such that :math:`x = f * 2^e` and
Packit 67cb25
   :math:`0.5 <= f < 1`. The function returns :math:`f` and stores the
Packit 67cb25
   exponent in :math:`e`. If :math:`x` is zero, both :math:`f` and :math:`e`
Packit 67cb25
   are set to zero. This function provides an alternative to the standard
Packit 67cb25
   math function :code:`frexp(x, e)`.
Packit 67cb25
Packit 67cb25
Small integer powers
Packit 67cb25
====================
Packit 67cb25
Packit 67cb25
A common complaint about the standard C library is its lack of a
Packit 67cb25
function for calculating (small) integer powers.  GSL provides some simple
Packit 67cb25
functions to fill this gap.  For reasons of efficiency, these functions
Packit 67cb25
do not check for overflow or underflow conditions. 
Packit 67cb25
Packit 67cb25
.. function::
Packit 67cb25
   double gsl_pow_int (double x, int n)
Packit 67cb25
   double gsl_pow_uint (double x, unsigned int n)
Packit 67cb25
Packit 67cb25
   These routines computes the power :math:`x^n` for integer :data:`n`.  The
Packit 67cb25
   power is computed efficiently---for example, :math:`x^8` is computed as
Packit 67cb25
   :math:`((x^2)^2)^2`, requiring only 3 multiplications.  A version of this
Packit 67cb25
   function which also computes the numerical error in the result is
Packit 67cb25
   available as :func:`gsl_sf_pow_int_e`.
Packit 67cb25
Packit 67cb25
.. function::
Packit 67cb25
   double gsl_pow_2 (const double x)
Packit 67cb25
   double gsl_pow_3 (const double x)
Packit 67cb25
   double gsl_pow_4 (const double x)
Packit 67cb25
   double gsl_pow_5 (const double x)
Packit 67cb25
   double gsl_pow_6 (const double x)
Packit 67cb25
   double gsl_pow_7 (const double x)
Packit 67cb25
   double gsl_pow_8 (const double x)
Packit 67cb25
   double gsl_pow_9 (const double x)
Packit 67cb25
Packit 67cb25
   These functions can be used to compute small integer powers :math:`x^2`,
Packit 67cb25
   :math:`x^3`, etc. efficiently. The functions will be inlined when 
Packit 67cb25
   :macro:`HAVE_INLINE` is defined, so that use of these functions 
Packit 67cb25
   should be as efficient as explicitly writing the corresponding 
Packit 67cb25
   product expression::
Packit 67cb25
Packit 67cb25
     #include <gsl/gsl_math.h>
Packit 67cb25
     double y = gsl_pow_4 (3.141)  /* compute 3.141**4 */
Packit 67cb25
Packit 67cb25
Testing the Sign of Numbers
Packit 67cb25
===========================
Packit 67cb25
Packit 67cb25
.. macro:: GSL_SIGN (x)
Packit 67cb25
Packit 67cb25
   This macro returns the sign of :data:`x`. It is defined as :code:`((x) >= 0
Packit 67cb25
   ? 1 : -1)`. Note that with this definition the sign of zero is positive
Packit 67cb25
   (regardless of its IEEE sign bit).
Packit 67cb25
Packit 67cb25
Testing for Odd and Even Numbers
Packit 67cb25
================================
Packit 67cb25
Packit 67cb25
.. macro:: GSL_IS_ODD (n)
Packit 67cb25
Packit 67cb25
   This macro evaluates to 1 if :data:`n` is odd and 0 if :data:`n` is
Packit 67cb25
   even. The argument :data:`n` must be of integer type.
Packit 67cb25
Packit 67cb25
.. macro:: GSL_IS_EVEN (n)
Packit 67cb25
Packit 67cb25
   This macro is the opposite of :macro:`GSL_IS_ODD`. It evaluates to 1 if
Packit 67cb25
   :data:`n` is even and 0 if :data:`n` is odd. The argument :data:`n` must be of
Packit 67cb25
   integer type.
Packit 67cb25
Packit 67cb25
Maximum and Minimum functions
Packit 67cb25
=============================
Packit 67cb25
Packit 67cb25
Note that the following macros perform multiple evaluations of their
Packit 67cb25
arguments, so they should not be used with arguments that have side
Packit 67cb25
effects (such as a call to a random number generator).
Packit 67cb25
Packit 67cb25
.. index:: maximum of two numbers
Packit 67cb25
Packit 67cb25
.. macro:: GSL_MAX (a, b)
Packit 67cb25
Packit 67cb25
   This macro returns the maximum of :data:`a` and :data:`b`. It is defined
Packit 67cb25
   as :code:`((a) > (b) ? (a):(b))`.
Packit 67cb25
Packit 67cb25
.. index:: minimum of two numbers
Packit 67cb25
Packit 67cb25
.. macro:: GSL_MIN (a, b)
Packit 67cb25
Packit 67cb25
   This macro returns the minimum of :data:`a` and :data:`b`. It is defined as 
Packit 67cb25
   :code:`((a) < (b) ? (a):(b))`.
Packit 67cb25
Packit 67cb25
.. function:: extern inline double GSL_MAX_DBL (double a, double b)
Packit 67cb25
Packit 67cb25
   This function returns the maximum of the double precision numbers
Packit 67cb25
   :data:`a` and :data:`b` using an inline function. The use of a function
Packit 67cb25
   allows for type checking of the arguments as an extra safety feature. On
Packit 67cb25
   platforms where inline functions are not available the macro
Packit 67cb25
   :macro:`GSL_MAX` will be automatically substituted.
Packit 67cb25
Packit 67cb25
.. function:: extern inline double GSL_MIN_DBL (double a, double b)
Packit 67cb25
Packit 67cb25
   This function returns the minimum of the double precision numbers
Packit 67cb25
   :data:`a` and :data:`b` using an inline function. The use of a function
Packit 67cb25
   allows for type checking of the arguments as an extra safety feature. On
Packit 67cb25
   platforms where inline functions are not available the macro
Packit 67cb25
   :macro:`GSL_MIN` will be automatically substituted.
Packit 67cb25
Packit 67cb25
.. function::
Packit 67cb25
   extern inline int GSL_MAX_INT (int a, int b)
Packit 67cb25
   extern inline int GSL_MIN_INT (int a, int b)
Packit 67cb25
Packit 67cb25
   These functions return the maximum or minimum of the integers :data:`a`
Packit 67cb25
   and :data:`b` using an inline function.  On platforms where inline
Packit 67cb25
   functions are not available the macros :macro:`GSL_MAX` or :macro:`GSL_MIN`
Packit 67cb25
   will be automatically substituted.
Packit 67cb25
Packit 67cb25
.. function::
Packit 67cb25
   extern inline long double GSL_MAX_LDBL (long double a, long double b)
Packit 67cb25
   extern inline long double GSL_MIN_LDBL (long double a, long double b)
Packit 67cb25
Packit 67cb25
   These functions return the maximum or minimum of the long doubles :data:`a`
Packit 67cb25
   and :data:`b` using an inline function.  On platforms where inline
Packit 67cb25
   functions are not available the macros :macro:`GSL_MAX` or :macro:`GSL_MIN`
Packit 67cb25
   will be automatically substituted.
Packit 67cb25
Packit 67cb25
Approximate Comparison of Floating Point Numbers
Packit 67cb25
================================================
Packit 67cb25
Packit 67cb25
It is sometimes useful to be able to compare two floating point numbers
Packit 67cb25
approximately, to allow for rounding and truncation errors.  The following
Packit 67cb25
function implements the approximate floating-point comparison algorithm
Packit 67cb25
proposed by D.E. Knuth in Section 4.2.2 of "Seminumerical
Packit 67cb25
Algorithms" (3rd edition).
Packit 67cb25
Packit 67cb25
.. index::
Packit 67cb25
   single: approximate comparison of floating point numbers
Packit 67cb25
   single: safe comparison of floating point numbers
Packit 67cb25
   single: floating point numbers, approximate comparison
Packit 67cb25
Packit 67cb25
.. function:: int gsl_fcmp (double x, double y, double epsilon)
Packit 67cb25
Packit 67cb25
   This function determines whether :data:`x` and :data:`y` are approximately
Packit 67cb25
   equal to a relative accuracy :data:`epsilon`.
Packit 67cb25
Packit 67cb25
   The relative accuracy is measured using an interval of size :math:`2
Packit 67cb25
   \delta`, where :math:`\delta = 2^k \epsilon` and :math:`k` is the
Packit 67cb25
   maximum base-2 exponent of :math:`x` and :math:`y` as computed by the
Packit 67cb25
   function :func:`frexp`.
Packit 67cb25
Packit 67cb25
   If :math:`x` and :math:`y` lie within this interval, they are considered
Packit 67cb25
   approximately equal and the function returns 0. Otherwise if :math:`x <
Packit 67cb25
   y`, the function returns :math:`-1`, or if :math:`x > y`, the function returns
Packit 67cb25
   :math:`+1`.
Packit 67cb25
Packit 67cb25
   Note that :math:`x` and :math:`y` are compared to relative accuracy, so
Packit 67cb25
   this function is not suitable for testing whether a value is
Packit 67cb25
   approximately zero. 
Packit 67cb25
Packit 67cb25
   The implementation is based on the package :code:`fcmp` by T.C. Belding.
Packit 67cb25
Packit 67cb25
.. rubric:: Footnotes
Packit 67cb25
Packit 67cb25
.. [#f1] Note that the C99 standard only requires the
Packit 67cb25
   system :func:`isinf` function to return a non-zero value, without the
Packit 67cb25
   sign of the infinity.  The implementation in some earlier versions of
Packit 67cb25
   GSL used the system :func:`isinf` function and may have this behavior
Packit 67cb25
   on some platforms.  Therefore, it is advisable to test the sign of
Packit 67cb25
   :data:`x` separately, if needed, rather than relying the sign of the
Packit 67cb25
   return value from :func:`gsl_isinf()`.