<?xml version="1.0" encoding="UTF-8"?>

<CRANTaskView>

  <name>NumericalMathematics</name>
  <topic>Numerical Mathematics</topic>
  <maintainer email="hwb@mailbox.org">Hans W. Borchers</maintainer>
  <version>2016-02-09</version>
  
  <info>

    <p>This task view on numerical mathematics lists R packages and functions 
    that are useful for solving numerical problems in linear algebra and 
    analysis. It shows that R is a viable computing environment for 
    implementing and applying numerical methods, also outside the realm of 
    statistics.</p>

    <p>The task view will <em>not</em> cover differential equations, 
    optimization problems and solvers, or packages and functions operating 
    on times series, because all these topics are treated extensively in 
    the corresponding task views <view>DifferentialEquations</view>, 
    <view>Optimization</view>, and <view>TimeSeries</view>.
    All these task views together will provide a good selection of what is
    available in R for the area of numerical mathematics.
    The <view>HighPerformanceComputing</view> task view with its many
    links for parallel computing may also be of interest.</p>

    <p>The task view has been created to provide an overview of the topic. 
    If some packages are missing or certain topics in numerical math 
    should be treated in more detail, please let the maintainer know.</p>

    <h4>Numerical Linear Algebra</h4>

    <p>As statistics is based to a large extent on linear algebra, many 
    numerical linear algebra routines are present in R, and some only 
    implicitly. Examples of explicitly available functions are vector and 
    matrix operations, matrix (QR) decompositions, solving linear equations, 
    eigenvalues/-vectors, singular value decomposition, or least-squares 
    approximation.</p>

    <ul>
    <li>The recommended package <pkg>Matrix</pkg> provides classes and methods 
        for dense and sparse matrices and operations on them, for example 
        Cholesky and Schur decomposition, matrix exponential, or norms and 
        conditional numbers for sparse matrices.</li>
    <li>Recommended package <pkg>MASS</pkg> adds generalized (Penrose) 
        inverses and null spaces of matrices.</li>
    <li><pkg>expm</pkg> computes the exponential, logarithm, and square root 
        of square matrices, but also powers of matrices or the Frechet 
        derivative. <code>expm()</code> is to be preferred to the function 
        with the same name in <pkg>Matrix</pkg>.</li>
    <li><pkg>SparseM</pkg> provides classes and methods for sparse matrices 
        and for solving linear and least-squares problems in sparse linear 
        algebra</li>
    <li><pkg>svd</pkg> provides R bindings to state-of-the-art implementations 
        of singular value decomposition (SVD) and eigenvalue/eigenvector 
        computations. Package <pkg>ssvd</pkg> will obtain sparse SVDs using an 
        iterative thresholding method, while <pkg>irlba</pkg> will compute 
        approximate singular values/vectors of large matrices.</li>
    <li>The packages <pkg>geigen</pkg> and <pkg>QZ</pkg> compute generalized 
        eigenvalues and -vectors for pairs of matrices, and QZ (generalized 
        Schur) decompositions.</li>
    <li><pkg>eigeninv</pkg> generates matrices with a given set of 
        eigenvalues ('inverse eigenvalue problem').</li>
    <li>Package <pkg>rARPACK</pkg>, a wrapper for the ARPACK library, is 
        typically used to compute only a few eigenvalues/vectors, e.g., a 
        small number of largest eigenvalues.</li>
    <li><pkg>optR</pkg> uses elementary methods of linear algebra (Gauss, LU, 
        CGM, Cholesky) to solve linear systems.</li>
    <li><pkg>matrixcalc</pkg> contains a collection of functions for matrix 
        calculations, special matrices, and tests for matrix properties, 
        e.g., (semi-)positive definiteness.</li>
    <li>Package <pkg>onion</pkg> contains routines for manipulating 
        quaternions and octonians (normed division algebras over the real 
        numbers); quaternions can be useful for handling rotations in 
        three-dimensional space.</li>
    <li>Packages <pkg>RcppArmadillo</pkg> and <pkg>RcppEigen</pkg> enable the 
        integration of the C++ template libraries 'Armadillo' resp. 'Eigen' 
        for linear algebra applications written in C++ and integrated in R 
        using <pkg>Rcpp</pkg> for performance and ease of use.</li>
    </ul>

    <h4>Special Functions</h4>

    <p>Many special mathematical functions are present in R, especially 
    logarithms and exponentials, trigonometric and hyperbolic functions, or 
    Bessel and Gamma functions. Many more special functions are available in 
    contributed packages.</p>

    <ul>
    <li>Package <pkg>gsl</pkg> provides an interface to the 'GNU Scientific 
        Library' that contains implementations of many special functions, 
        for example the Airy and Bessel functions, elliptic and exponential 
        integrals, the hypergeometric function, Lambert's W function, and
        many more.</li>
    <li>Airy and Bessel functions, for real and complex numbers, are also 
        computed in package <pkg>Bessel</pkg>, with approximations for large 
        arguments.</li>
    <li>Package <pkg>pracma</pkg> includes special functions, such as 
        error functions and inverses, incomplete and complex gamma function, 
        exponential and logarithmic integrals, Fresnel integrals, the 
        polygamma and the Riemann zeta function.</li>
    <li><pkg>appell</pkg> computes Gauss' 2F1 and Appell's F1 hypergeometric 
        functions for complex parameters and arguments quite accurately.</li>
    <li>The hypergeometric (and generalized hypergeometric) function, is 
        computed in <pkg>hypergeo</pkg>, including transformation formulas 
        and special values of the parameters.</li>
    <li>Elliptic and modular functions are available in package 
        <pkg>elliptic</pkg>, including the Weierstrass P function and 
        Jacobi's theta functions.
        There are tools for visualizing complex functions.</li>
    <li>Package <pkg>lamW</pkg> implements both real-valued branches of the 
        Lambert W function (using Rcpp).</li>
    </ul>

    <h4>Polynomials</h4>

    <p>Function polyroot() in base R determines all zeros of a polynomial,  
    based on the Jenkins-Traub algorithm. Linear regression function lm() 
    can perform polynomial fitting when using <code>poly()</code> in the model 
    formula (with option <code>raw = TRUE</code>).</p>

    <ul>
    <li>Packages <pkg>polynom</pkg> and <pkg>PolynomF</pkg> provide similar 
        functionality for manipulating univariate polynomials, like 
        evaluating polynomials (Horner scheme), differentiating or integrating 
        them, or solving polynomials, i.e. finding all roots (based on an 
        eigenvalue computation).</li>
    <li>Package <pkg>MonoPoly</pkg> fits univariate polynomials to given data, 
        applying different algorithms.</li>
    <li>For multivariate polynomials, package <pkg>multipol</pkg> provides 
        various tools to manipulate and combine these polynomials of several 
        variables.</li>
    <li>Package <pkg>mpoly</pkg> facilitates symbolic manipulations on 
        multivariate polynomials, including basic differential calculus 
        operations on polynomials, plus some Groebner basis calculations.</li>
    <li>Package <pkg>orthopolynom</pkg> consists of a collection of functions 
        to construct orthogonal polynomials and their recurrence relations, 
        among them Chebyshev, Hermite, and Legendre polynomials, as well as 
        spherical and ultraspherical polynomials. There are functions to 
        operate on these polynomials.</li>
    </ul>

    <h4>Differentiation and Integration</h4>

    <p><code>D()</code> and <code>deriv()</code> in base R compute 
    derivatives of simple expressions symbolically.
    Function <code>integrate()</code> implements an approach for numerically 
    integrating univariate functions in R. It applies adaptive Gauss-Kronrod 
    quadrature and can handle singularities and unbounded domains to a certain 
    extent.</p>

    <ul>
    <li>Package <pkg>Deriv</pkg> provides an extended solution for symbolic 
        differentiation in R; the user can add custom derivative rules, and the 
        output for a function will be an executable function again.</li>
    <li><pkg>numDeriv</pkg> sets the standard for numerical differentiation 
        in R, providing numerical gradients, Jacobians, and Hessians, computed 
        by simple finite differences, Richardson extrapolation, or the highly 
        accurate complex step approach.</li>
    <li>Package <pkg>pracma</pkg> contains functions for computing numerical 
        derivatives, including Richardson extrapolation or complex step. 
        <code>fderiv()</code> computes numerical derivatives of higher orders.
        <pkg>pracma</pkg> has several routines for numerical integration: 
        adaptive Lobatto quadrature, Romberg integration, Newton-Cotes 
        formulas, Clenshaw-Curtis quadrature rules. <code>integral2()</code> 
        integrates functions in two dimensions, also for domains characterized 
        by polar coordinates or with variable interval limits.</li>
    <li>Package <pkg>gaussquad</pkg> contains a collection of functions to 
        perform Gaussian quadrature, among them Chebyshev, Hermite, Laguerre, 
        and Legendre quadrature rules, explicitly returning nodes and weights 
        in each case. Function <code>gaussquad()</code> in package 
        <pkg>statmod</pkg> does a similar job.</li>
    <li>Package <pkg>fastGHQuad</pkg> provides a fast <pkg>Rcpp</pkg>-based
        implementation of (adaptive) Gauss-Hermite quadrature.</li>
    <li>Adaptive multivariate integration over hyper-rectangles in 
        n-dimensional space is available in package <pkg>cubature</pkg> as 
        function <code>adaptIntegrate()</code>, based on a C library of the
        same name. The integrand functions can even be multi-valued.</li>
    <li>Multi-dimensional numerical integration is also covered in package 
        <pkg>R2Cuba</pkg>, a wrapper around the C library Cuba.
        With <code>vegas()</code> it includes an approach to Monte Carlo 
        integration based on importance sampling.</li>
    <li><pkg>mvQuad</pkg> provides methods for generating multivariate grids 
        that can be used for multivariate integration. These grids will be 
        based on different quadrature rules such as Newton-Cotes or Gauss 
        quadrature formulas.</li>
    <li>Package <pkg>SparseGrid</pkg> provides another approach to 
        multivariate integration in high-dimensional spaces. It creates sparse 
        n-dimensional grids that can be used as with quadrature rules.</li>
    <li>Package <pkg>SphericalCubature</pkg> employs <pkg>cubature</pkg> to 
        integrate functions over unit spheres and balls in n-dimensional 
        space; <pkg>SimplicialCubature</pkg> provides methods to integrate
        functions over m-dimensional simplices in n-dimensional space.
        Both packages comprise exact methods for polynomials.</li>
    <li>Package <pkg>polyCub</pkg> holds some routines for numerical 
        integration over polygonal domains in two dimensions.</li>
    <li>Package <pkg>Pade</pkg> calculates the numerator and denominator 
        coefficients of the Pade approximation, given the Taylor series 
        coefficients of sufficient length.</li>
    <li><pkg>madness</pkg> allows to compute derivatives of multivariate 
        functions, defined by matrix operations, via forward differentiation 
        and the chain rule.</li>
    <li><pkg>features</pkg> extracts features from functional data, such as 
        first and second derivatives, or curvature at critical points, while
        <pkg>RootsExtremaInflections</pkg> finds roots, extrema and 
        inflection points of curves defined by discrete points.</li>
    </ul>

    <h4>Interpolation and Approximation</h4>

    <p>Base R provides functions <code>approx()</code> for constant and linear 
    interpolation, and <code>spline()</code> for cubic (Hermite) spline 
    interpolation, while <code>smooth.spline()</code> performs cubic spline 
    approximation. Base package splines creates periodic interpolation 
    splines in function <code>periodicSpline()</code>.</p>

    <ul>
    <li>Interpolation of irregularly spaced data is possible with the 
        <pkg>akima</pkg> package: <code>aspline()</code> for univariate data, 
        <code>bicubic()</code> or <code>interp()</code> for data on a 2D 
        rectangular domain. (This package is distributed under ACM license and 
        not available for commercial use.)</li>
    <li>Package <pkg>signal</pkg> contains several <em>filters</em> to smooth 
        discrete data, notably <code>interp1()</code> for linear, spline, and 
        cubic interpolation, <code>pchip()</code> for piecewise cubic Hermite 
        interpolation, and <code>sgolay()</code> for Savitzky-Golay 
        smoothing.</li>
    <li>Package <pkg>pracma</pkg> provides barycentric Lagrange interpolation 
        (in 1 and 2 dimensions) in <code>barylag()</code> resp. 
        <code>barylag2d()</code>, 1-dim. akima in <code>akimaInterp()</code>, 
        and interpolation and approximation of data with rational functions, 
        i.e. in the presence of singularities, in <code>ratinterp()</code> and 
        <code>rationalfit()</code>.</li>
    <li><code>sinterp()</code> in package <pkg>stinepack</pkg> realizes 
        interpolation based on piecewise rational functions by applying 
        Stineman's algorithm. The interpolating function will be monoton in 
        regions where the specified points change monotonically.</li>
    <li>Package <pkg>conicfit</pkg> provides several (geometric and algebraic) 
        algorithms for fitting circles, ellipses, and conics in general.</li>
    </ul>

    <h4>Root Finding</h4>

    <p><code>uniroot()</code>, implementing the Brent-Decker algorithm, is the 
    basic routine in R to find roots of univariate functions. There are 
    implementations of the bisection algorithm in several contributed 
    packages. For root finding with higher precision there is function 
    <code>unirootR()</code> in the multi-precision package <pkg>Rmpfr</pkg>. 
    And for finding roots of multivariate functions see the following two 
    packages:</p>

    <ul>
    <li>For solving nonlinear systems of equations the <pkg>BB</pkg> package 
        provides (non-monotone) Barzilai-Borwein spectral methods in 
        <code>sane()</code>, including a derivative-free variant in 
        <code>dfsane()</code>, and multi-start features with sensitivity 
        analysis.</li>
    <li>Package <pkg>nleqslv</pkg> solves nonlinear systems of equations 
        using alternatively the Broyden or Newton method, supported by 
        strategies such as line searches or trust regions.</li>
    <li><pkg>ktsolve</pkg> defines a common interface for solving a set of
        equations with <code>BB</code> or <code>nleqslv</code>.</li>
    </ul>

    <h4>Discrete Mathematics and Number Theory</h4>

    <p>Not so many functions are available for computational number theory.
    Note that integers in double precision can be represented exactly up to 
    <code>2^53 - 1</code>, above that limit a multi-precision package such as
    <pkg>gmp</pkg> is needed, see below.</p>

    <ul>
    <li>Package <pkg>numbers</pkg> provides functions for factorization, prime 
        numbers, twin primes, primitive roots, modular inverses, extended GCD, 
        etc. Included are some number-theoretic functions like divisor 
        functions or Euler's Phi function.</li>
    <li><pkg>contfrac</pkg> contains various utilities for evaluating 
        continued fractions and partial convergents.</li>
    <li>The <pkg>partitions</pkg> package enumerates additive partitions 
        of integers, including restricted and unequal partitions.</li>
    <li><pkg>permutations</pkg> treats permutations as invertible functions of 
        finite sets and includes several mathematical operations on them.</li>
    <li>Package <pkg>combinat</pkg> generates all permutations or all 
        combinations of a certain length of a set of elements (i.e. a vector); 
        it also computes binomial coefficients.</li>
    <li><pkg>magic</pkg> creates and investigates magical squares.</li>
    </ul>

    <h4>Multi-Precision Arithmetic and Symbolic Mathematics</h4>

    <ul>
    <li>Multiple precision arithmetic is available in R through package 
        <pkg>gmp</pkg> that interfaces to the GMP C library. Examples are 
        factorization of integers, a probabilistic prime number test, or 
        operations on big rationals -- for which linear systems of equations 
        can be solved.</li>
    <li>Multiple precision floating point operations and functions are 
        provided through package <pkg>Rmpfr</pkg> using the MPFR and GMP 
        libraries. Special numbers and some special functions are included, 
        as well as routines for root finding, integration, and optimization 
        in arbitrary precision.</li>
    <li><pkg>Brobdingnag</pkg> handles very large numbers by holding their
        logarithm plus a flag indicating their sign. (An excellent vignette
        explains how this is done using S4 methods.)</li>
    <li>Package <pkg>rSymPy</pkg> accesses the symbolic algebra system 'SymPy' 
        (written in Python) from R. 
        It supports arbitrary precision computations, linear algebra and 
        calculus, solving equations, discrete mathematics, and much more.</li>
    <li>Package <pkg>Ryacas</pkg> interfaces the computer algebra system 
        'Yacas'. It supports symbolic and arbitrary precision computations 
        in calculus and linear algebra.</li>
    </ul>

    <h4>MATLAB, Octave, and Python Interfaces</h4>

    <p>Interfaces to numerical computation software such as MATLAB 
    (commercial) or Octave (free) will be important when solving difficult 
    numerical problems. (Please note that the commercial programs SAS and 
    Mathematica do have facilities to call R functions.)</p>

    <ul>
    <li>The <pkg>matlab</pkg> emulation package contains about 30 simple 
        functions, replicating MATLAB functions, using the respective MATLAB 
        names and being implemented in pure R.</li>
    <li>Package <pkg>R.matlab</pkg> provides tools to read and write MAT 
        files, which is the MATLAB data format. It also enables a 
        one-directional interface with a MATLAB process, sending and 
        retrieving objects through a TCP/IP connection.</li>
    <li>Package <pkg>RcppOctave</pkg> provides an interface to Octave (a 
        MATLAB clone). It enables calling Octave functions, passing variables 
        between R and Octave, and browsing Octave documentation.</li>
    <li>Python, through its modules 'NumPy', 'SciPy', 'Matplotlib', 'SymPy', 
    	and 'pandas', has elaborate and efficient numerical and graphical 
    	tools available. R package <pkg>rPython</pkg> permits calls from R to 
    	Python, while <a href="http://sourceforge.net/projects/rpy/">RPy</a> 
    	(with Python module 'rpy2') interfaces R from Python.</li>
    <li>'pyRserve' is a Python module for connecting Python to an R process 
        running <pkg>Rserve</pkg> as an RPC gateway. This R process can run on 
        a remote machine, variable access and function calls will be delegated 
        through the network.</li>
    </ul>
        
  </info>
  
  <packagelist>

    <pkg>akima</pkg>
    <pkg>appell</pkg>
    <pkg>BB</pkg>
    <pkg>Bessel</pkg>
    <pkg>Brobdingnag</pkg>
    <pkg>combinat</pkg>
    <pkg>conicfit</pkg>
    <pkg>contfrac</pkg>
    <pkg>cubature</pkg>
    <pkg priority="core">Deriv</pkg>
    <pkg>eigeninv</pkg>
    <pkg>elliptic</pkg>
    <pkg>expm</pkg>
    <pkg>fastGHQuad</pkg>
    <pkg>features</pkg>
    <pkg>gaussquad</pkg>
    <pkg>geigen</pkg>
    <pkg>gmp</pkg>
    <pkg>gsl</pkg>
    <pkg>hypergeo</pkg>
    <pkg>irlba</pkg>
    <pkg>ktsolve</pkg>
    <pkg>lamW</pkg>
    <pkg>madness</pkg>
    <pkg>magic</pkg>
    <pkg>MASS</pkg>
    <pkg>matlab</pkg>
    <pkg priority="core">Matrix</pkg>
    <pkg>matrixcalc</pkg>
    <pkg>MonoPoly</pkg>
    <pkg>mpoly</pkg>
    <pkg>multipol</pkg>
    <pkg>mvQuad</pkg>
    <pkg>nleqslv</pkg>
    <pkg>numbers</pkg>
    <pkg priority="core">numDeriv</pkg>
    <pkg>onion</pkg>
    <pkg>optR</pkg>
    <pkg>orthopolynom</pkg>
    <pkg>Pade</pkg>
    <pkg>partitions</pkg>
    <pkg>permutations</pkg>
    <pkg>polyCub</pkg>
    <pkg priority="core">polynom</pkg>
    <pkg>PolynomF</pkg>
    <pkg priority="core">pracma</pkg>
    <pkg>QZ</pkg>
    <pkg>R.matlab</pkg>
    <pkg>R2Cuba</pkg>
    <pkg>rARPACK</pkg>
    <pkg>Rcpp</pkg>
    <pkg>RcppArmadillo</pkg>
    <pkg>RcppEigen</pkg>
    <pkg>RcppOctave</pkg>
    <pkg>Rmpfr</pkg>
    <pkg>RootsExtremaInflections</pkg>
    <pkg>rPython</pkg>
    <pkg>Rserve</pkg>
    <pkg>rSymPy</pkg>
    <pkg>Ryacas</pkg>
    <pkg>signal</pkg>
    <pkg>SimplicialCubature</pkg>
    <pkg>SparseGrid</pkg>
    <pkg>SparseM</pkg>
    <pkg>SphericalCubature</pkg>
    <pkg>statmod</pkg>
    <pkg>stinepack</pkg>
    <pkg>ssvd</pkg>
    <pkg>svd</pkg>

  </packagelist>
  
  <links>
    <view>DifferentialEquations</view>
    <view>Optimization</view>
    <view>TimeSeries</view>
    <view>HighPerformanceComputing</view>

    <span>Textbook:
    <a href="http://www.worldscientific.com/worldscibooks/10.1142/7814">
        Hands-On Matrix Algebra Using R</a></span>
    <span>Textbook: 
    <a href="http://www.ms.unimelb.edu.au/spuRs/">Introduction 
        to Scientific Programming and Simulation Using R</a></span>
    <span>Textbook:
    <a href="http://www.crcpress.com/product/isbn/9781439884485">
    Numerical Methods in Science and Engineering Using R</a></span>

    <a href="http://www.nr.com/aands/">Abramowitz and Stegun. 
        Handbook of Mathematical Functions</a>
    <a href="http://www.nrbook.com/a/bookcpdf.php">Numerical Recipes:
        The Art of Numerical Computing</a>
    <a href="http://mathworld.wolfram.com/">E. Weisstein's 
        Wolfram MathWorld</a>
  </links>

</CRANTaskView>
