octave: Sparse Linear Algebra

 
 22.2 Linear Algebra on Sparse Matrices
 ======================================
 
 Octave includes a polymorphic solver for sparse matrices, where the
 exact solver used to factorize the matrix, depends on the properties of
 the sparse matrix itself.  Generally, the cost of determining the matrix
 type is small relative to the cost of factorizing the matrix itself, but
 in any case the matrix type is cached once it is calculated, so that it
 is not re-determined each time it is used in a linear equation.
 
    The selection tree for how the linear equation is solve is
 
   1. If the matrix is diagonal, solve directly and goto 8
 
   2. If the matrix is a permuted diagonal, solve directly taking into
      account the permutations.  Goto 8
 
   3. If the matrix is square, banded and if the band density is less
      than that given by ‘spparms ("bandden")’ continue, else goto 4.
 
        a. If the matrix is tridiagonal and the right-hand side is not
           sparse continue, else goto 3b.
 
             1. If the matrix is Hermitian, with a positive real
                diagonal, attempt Cholesky factorization using LAPACK
                xPTSV.
 
             2. If the above failed or the matrix is not Hermitian with a
                positive real diagonal use Gaussian elimination with
                pivoting using LAPACK xGTSV, and goto 8.
 
        b. If the matrix is Hermitian with a positive real diagonal,
           attempt Cholesky factorization using LAPACK xPBTRF.
 
        c. if the above failed or the matrix is not Hermitian with a
           positive real diagonal use Gaussian elimination with pivoting
           using LAPACK xGBTRF, and goto 8.
 
   4. If the matrix is upper or lower triangular perform a sparse forward
      or backward substitution, and goto 8
 
   5. If the matrix is an upper triangular matrix with column
      permutations or lower triangular matrix with row permutations,
      perform a sparse forward or backward substitution, and goto 8
 
   6. If the matrix is square, Hermitian with a real positive diagonal,
      attempt sparse Cholesky factorization using CHOLMOD.
 
   7. If the sparse Cholesky factorization failed or the matrix is not
      Hermitian with a real positive diagonal, and the matrix is square,
      factorize using UMFPACK.
 
   8. If the matrix is not square, or any of the previous solvers flags a
      singular or near singular matrix, find a minimum norm solution
      using CXSPARSE(1).
 
    The band density is defined as the number of nonzero values in the
 band divided by the total number of values in the full band.  The banded
 matrix solvers can be entirely disabled by using “spparms” to set
 ‘bandden’ to 1 (i.e., ‘spparms ("bandden", 1)’).
 
    The QR solver factorizes the problem with a Dulmage-Mendelsohn
 decomposition, to separate the problem into blocks that can be treated
 as over-determined, multiple well determined blocks, and a final
 over-determined block.  For matrices with blocks of strongly connected
 nodes this is a big win as LU decomposition can be used for many blocks.
 It also significantly improves the chance of finding a solution to
 over-determined problems rather than just returning a vector of “NaN”’s.
 
    All of the solvers above, can calculate an estimate of the condition
 number.  This can be used to detect numerical stability problems in the
 solution and force a minimum norm solution to be used.  However, for
 narrow banded, triangular or diagonal matrices, the cost of calculating
 the condition number is significant, and can in fact exceed the cost of
 factoring the matrix.  Therefore the condition number is not calculated
 in these cases, and Octave relies on simpler techniques to detect
 singular matrices or the underlying LAPACK code in the case of banded
 matrices.
 
    The user can force the type of the matrix with the ‘matrix_type’
 function.  This overcomes the cost of discovering the type of the
 matrix.  However, it should be noted that identifying the type of the
 matrix incorrectly will lead to unpredictable results, and so
 ‘matrix_type’ should be used with care.
 
  -- : NEST = normest (A)
  -- : NEST = normest (A, TOL)
  -- : [NEST, ITER] = normest (...)
      Estimate the 2-norm of the matrix A using a power series analysis.
 
      This is typically used for large matrices, where the cost of
      calculating ‘norm (A)’ is prohibitive and an approximation to the
      2-norm is acceptable.
 
      TOL is the tolerance to which the 2-norm is calculated.  By default
      TOL is 1e-6.
 
      The optional output ITER returns the number of iterations that were
      required for ‘normest’ to converge.
 
DONTPRINTYET       See also: Seenormest1 XREFnormest1, Seenorm XREFnorm, *noteDONTPRINTYET       See also: Seenormest1 XREFnormest1, Seenorm XREFnorm, See
      cond XREFcond, Seecondest XREFcondest.
 
  -- : NEST = normest1 (A)
  -- : NEST = normest1 (A, T)
  -- : NEST = normest1 (A, T, X0)
  -- : NEST = normest1 (AFUN, T, X0, P1, P2, ...)
  -- : [NEST, V] = normest1 (A, ...)
  -- : [NEST, V, W] = normest1 (A, ...)
  -- : [NEST, V, W, ITER] = normest1 (A, ...)
      Estimate the 1-norm of the matrix A using a block algorithm.
 
      ‘normest1’ is best for large sparse matrices where only an estimate
      of the norm is required.  For small to medium sized matrices,
      consider using ‘norm (A, 1)’.  In addition, ‘normest1’ can be used
      for the estimate of the 1-norm of a linear operator A when
      matrix-vector products ‘A * X’ and ‘A' * X’ can be cheaply
      computed.  In this case, instead of the matrix A, a function ‘AFUN
      (FLAG, X)’ is used; it must return:
 
         • the dimension N of A, if FLAG is "dim"
 
         • true if A is a real operator, if FLAG is "real"
 
         • the result ‘A * X’, if FLAG is "notransp"
 
         • the result ‘A' * X’, if FLAG is "transp"
 
      A typical case is A defined by ‘B ^ M’, in which the result ‘A * X’
      can be computed without even forming explicitly ‘B ^ M’ by:
 
           Y = X;
           for I = 1:M
             Y = B * Y;
           endfor
 
      The parameters P1, P2, ... are arguments of ‘AFUN (FLAG, X, P1, P2,
      ...)’.
 
      The default value for T is 2.  The algorithm requires matrix-matrix
      products with sizes N x N and N x T.
 
      The initial matrix X0 should have columns of unit 1-norm.  The
      default initial matrix X0 has the first column ‘ones (N, 1) / N’
      and, if T > 1, the remaining columns with random elements ‘-1 / N’,
      ‘1 / N’, divided by N.
 
      On output, NEST is the desired estimate, V and W are vectors such
      that ‘W = A * V’, with ‘norm (W, 1)’ = ‘C * norm (V, 1)’.  ITER
      contains in ‘ITER(1)’ the number of iterations (the maximum is
      hardcoded to 5) and in ‘ITER(2)’ the total number of products ‘A *
      X’ or ‘A' * X’ performed by the algorithm.
 
      Algorithm Note: ‘normest1’ uses random numbers during evaluation.
      Therefore, if consistent results are required, the "state" of the
      random generator should be fixed before invoking ‘normest1’.
 
      Reference: N. J. Higham and F. Tisseur, ‘A block algorithm for
      matrix 1-norm estimation, with and application to 1-norm
      pseudospectra’, SIAM J. Matrix Anal.  Appl., pp.  1185–1201, Vol
      21, No.  4, 2000.
 
DONTPRINTYET       See also: Seenormest XREFnormest, Seenorm XREFnorm, *noteDONTPRINTYET       See also: Seenormest XREFnormest, Seenorm XREFnorm, See
      cond XREFcond, Seecondest XREFcondest.
 
  -- : CEST = condest (A)
  -- : CEST = condest (A, T)
  -- : CEST = condest (A, SOLVEFUN, T, P1, P2, ...)
  -- : CEST = condest (AFCN, SOLVEFUN, T, P1, P2, ...)
  -- : [CEST, V] = condest (...)
 
      Estimate the 1-norm condition number of a square matrix A using T
      test vectors and a randomized 1-norm estimator.
 
      The optional input T specifies the number of test vectors (default
      5).
 
      If the matrix is not explicit, e.g., when estimating the condition
      number of A given an LU factorization, ‘condest’ uses the following
      functions:
 
         − AFCN which must return
 
              • the dimension N of A, if FLAG is "dim"
 
              • true if A is a real operator, if FLAG is "real"
 
              • the result ‘A * X’, if FLAG is "notransp"
 
              • the result ‘A' * X’, if FLAG is "transp"
 
         − SOLVEFUN which must return
 
              • the dimension N of A, if FLAG is "dim"
 
              • true if A is a real operator, if FLAG is "real"
 
              • the result ‘A \ X’, if FLAG is "notransp"
 
              • the result ‘A' \ X’, if FLAG is "transp"
 
      The parameters P1, P2, ... are arguments of ‘AFCN (FLAG, X, P1, P2,
      ...)’ and ‘SOLVEFCN (FLAG, X, P1, P2, ...)’.
 
      The principal output is the 1-norm condition number estimate CEST.
 
      The optional second output is an approximate null vector when CEST
      is large; it satisfies the equation ‘norm (A*v, 1) == norm (A, 1) *
      norm (V, 1) / EST’.
 
      Algorithm Note: ‘condest’ uses a randomized algorithm to
      approximate the 1-norms.  Therefore, if consistent results are
      required, the "state" of the random generator should be fixed
      before invoking ‘condest’.
 
      References:
 
         • N.J. Higham and F. Tisseur, ‘A Block Algorithm for Matrix
           1-Norm Estimation, with an Application to 1-Norm
           Pseudospectra’.  SIMAX vol 21, no 4, pp 1185-1201.
           <http://dx.doi.org/10.1137/S0895479899356080>
 
         • N.J. Higham and F. Tisseur, ‘A Block Algorithm for Matrix
           1-Norm Estimation, with an Application to 1-Norm
           Pseudospectra’.  <http://citeseer.ist.psu.edu/223007.html>
 
DONTPRINTYET       See also: Seecond XREFcond, Seenorm XREFnorm, *noteDONTPRINTYET       See also: Seecond XREFcond, Seenorm XREFnorm, See
      normest1 XREFnormest1, Seenormest XREFnormest.
 
  -- : spparms ()
  -- : VALS = spparms ()
  -- : [KEYS, VALS] = spparms ()
  -- : VAL = spparms (KEY)
  -- : spparms (VALS)
  -- : spparms ("default")
  -- : spparms ("tight")
  -- : spparms (KEY, VAL)
      Query or set the parameters used by the sparse solvers and
      factorization functions.
 
      The first four calls above get information about the current
      settings, while the others change the current settings.  The
      parameters are stored as pairs of keys and values, where the values
      are all floats and the keys are one of the following strings:
 
      ‘spumoni’
           Printing level of debugging information of the solvers
           (default 0)
 
      ‘ths_rel’
           Included for compatibility.  Not used.  (default 1)
 
      ‘ths_abs’
           Included for compatibility.  Not used.  (default 1)
 
      ‘exact_d’
           Included for compatibility.  Not used.  (default 0)
 
      ‘supernd’
           Included for compatibility.  Not used.  (default 3)
 
      ‘rreduce’
           Included for compatibility.  Not used.  (default 3)
 
      ‘wh_frac’
           Included for compatibility.  Not used.  (default 0.5)
 
      ‘autommd’
           Flag whether the LU/QR and the ’\’ and ’/’ operators will
           automatically use the sparsity preserving mmd functions
           (default 1)
 
      ‘autoamd’
           Flag whether the LU and the ’\’ and ’/’ operators will
           automatically use the sparsity preserving amd functions
           (default 1)
 
      ‘piv_tol’
           The pivot tolerance of the UMFPACK solvers (default 0.1)
 
      ‘sym_tol’
           The pivot tolerance of the UMFPACK symmetric solvers (default
           0.001)
 
      ‘bandden’
           The density of nonzero elements in a banded matrix before it
           is treated by the LAPACK banded solvers (default 0.5)
 
      ‘umfpack’
           Flag whether the UMFPACK or mmd solvers are used for the LU,
           ’\’ and ’/’ operations (default 1)
 
      The value of individual keys can be set with ‘spparms (KEY, VAL)’.
      The default values can be restored with the special keyword
      "default".  The special keyword "tight" can be used to set the mmd
      solvers to attempt a sparser solution at the potential cost of
      longer running time.
 
DONTPRINTYET       See also: Seechol XREFchol, Seecolamd XREFcolamd, *notelu:
DONTPRINTYET       See also: Seechol XREFchol, Seecolamd XREFcolamd, Seelu

      XREFlu, Seeqr XREFqr, Seesymamd XREFsymamd.
 
  -- : P = sprank (S)
 
      Calculate the structural rank of the sparse matrix S.
 
      Note that only the structure of the matrix is used in this
      calculation based on a Dulmage-Mendelsohn permutation to block
      triangular form.  As such the numerical rank of the matrix S is
      bounded by ‘sprank (S) >= rank (S)’.  Ignoring floating point
      errors ‘sprank (S) == rank (S)’.
 
      See also: Seedmperm XREFdmperm.
 
  -- : [COUNT, H, PARENT, POST, R] = symbfact (S)
  -- : [...] = symbfact (S, TYP)
  -- : [...] = symbfact (S, TYP, MODE)
 
      Perform a symbolic factorization analysis of the sparse matrix S.
 
      The input variables are
 
      S
           S is a real or complex sparse matrix.
 
      TYP
           Is the type of the factorization and can be one of
 
           "sym" (default)
                Factorize S.  Assumes S is symmetric and uses the upper
                triangular portion of the matrix.
 
           "col"
                Factorize S’ * S.
 
           "row"
                Factorize S * S’.
 
           "lo"
                Factorize S’.  Assumes S is symmetric and uses the lower
                triangular portion of the matrix.
 
      MODE
           When MODE is unspecified return the Cholesky factorization for
           R.  If MODE is "lower" or "L" then return the conjugate
           transpose R’ which is a lower triangular factor.  The
           conjugate transpose version is faster and uses less memory,
           but still returns the same values for all other outputs:
           COUNT, H, PARENT, and POST.
 
      The output variables are:
 
      COUNT
           The row counts of the Cholesky factorization as determined by
           TYP.  The computational difficulty of performing the true
           factorization using ‘chol’ is ‘sum (COUNT .^ 2)’.
 
      H
           The height of the elimination tree.
 
      PARENT
           The elimination tree itself.
 
      POST
           A sparse boolean matrix whose structure is that of the
           Cholesky factorization as determined by TYP.
 
DONTPRINTYET       See also: Seechol XREFchol, Seeetree XREFetree, *noteDONTPRINTYET       See also: Seechol XREFchol, Seeetree XREFetree, See
      treelayout XREFtreelayout.
 
    For non square matrices, the user can also utilize the ‘spaugment’
 function to find a least squares solution to a linear equation.
 
  -- : S = spaugment (A, C)
      Create the augmented matrix of A.
 
      This is given by
 
           [C * eye(M, M), A;
                       A', zeros(N, N)]
 
      This is related to the least squares solution of ‘A \ B’, by
 
           S * [ R / C; x] = [ B, zeros(N, columns(B)) ]
 
      where R is the residual error
 
           R = B - A * X
 
      As the matrix S is symmetric indefinite it can be factorized with
      ‘lu’, and the minimum norm solution can therefore be found without
      the need for a ‘qr’ factorization.  As the residual error will be
      ‘zeros (M, M)’ for underdetermined problems, and example can be
 
           m = 11; n = 10; mn = max (m, n);
           A = spdiags ([ones(mn,1), 10*ones(mn,1), -ones(mn,1)],
                        [-1, 0, 1], m, n);
           x0 = A \ ones (m,1);
           s = spaugment (A);
           [L, U, P, Q] = lu (s);
           x1 = Q * (U \ (L \ (P  * [ones(m,1); zeros(n,1)])));
           x1 = x1(end - n + 1 : end);
 
      To find the solution of an overdetermined problem needs an estimate
      of the residual error R and so it is more complex to formulate a
      minimum norm solution using the ‘spaugment’ function.
 
      In general the left division operator is more stable and faster
      than using the ‘spaugment’ function.
 
      See also: Seemldivide XREFmldivide.
 
    Finally, the function ‘eigs’ can be used to calculate a limited
 number of eigenvalues and eigenvectors based on a selection criteria and
 likewise for ‘svds’ which calculates a limited number of singular values
 and vectors.
 
  -- : D = eigs (A)
  -- : D = eigs (A, K)
  -- : D = eigs (A, K, SIGMA)
  -- : D = eigs (A, K, SIGMA, OPTS)
  -- : D = eigs (A, B)
  -- : D = eigs (A, B, K)
  -- : D = eigs (A, B, K, SIGMA)
  -- : D = eigs (A, B, K, SIGMA, OPTS)
  -- : D = eigs (AF, N)
  -- : D = eigs (AF, N, B)
  -- : D = eigs (AF, N, K)
  -- : D = eigs (AF, N, B, K)
  -- : D = eigs (AF, N, K, SIGMA)
  -- : D = eigs (AF, N, B, K, SIGMA)
  -- : D = eigs (AF, N, K, SIGMA, OPTS)
  -- : D = eigs (AF, N, B, K, SIGMA, OPTS)
  -- : [V, D] = eigs (A, ...)
  -- : [V, D] = eigs (AF, N, ...)
  -- : [V, D, FLAG] = eigs (A, ...)
  -- : [V, D, FLAG] = eigs (AF, N, ...)
      Calculate a limited number of eigenvalues and eigenvectors of A,
      based on a selection criteria.
 
      The number of eigenvalues and eigenvectors to calculate is given by
      K and defaults to 6.
 
      By default, ‘eigs’ solve the equation ‘A * v = lambda * v’, where
      ‘lambda’ is a scalar representing one of the eigenvalues, and ‘v’
      is the corresponding eigenvector.  If given the positive definite
      matrix B then ‘eigs’ solves the general eigenvalue equation ‘A * v
      = lambda * B * v’.
 
      The argument SIGMA determines which eigenvalues are returned.
      SIGMA can be either a scalar or a string.  When SIGMA is a scalar,
      the K eigenvalues closest to SIGMA are returned.  If SIGMA is a
      string, it must have one of the following values.
 
      "lm"
           Largest Magnitude (default).
 
      "sm"
           Smallest Magnitude.
 
      "la"
           Largest Algebraic (valid only for real symmetric problems).
 
      "sa"
           Smallest Algebraic (valid only for real symmetric problems).
 
      "be"
           Both Ends, with one more from the high-end if K is odd (valid
           only for real symmetric problems).
 
      "lr"
           Largest Real part (valid only for complex or unsymmetric
           problems).
 
      "sr"
           Smallest Real part (valid only for complex or unsymmetric
           problems).
 
      "li"
           Largest Imaginary part (valid only for complex or unsymmetric
           problems).
 
      "si"
           Smallest Imaginary part (valid only for complex or unsymmetric
           problems).
 
      If OPTS is given, it is a structure defining possible options that
      ‘eigs’ should use.  The fields of the OPTS structure are:
 
      ‘issym’
           If AF is given, then flags whether the function AF defines a
           symmetric problem.  It is ignored if A is given.  The default
           is false.
 
      ‘isreal’
           If AF is given, then flags whether the function AF defines a
           real problem.  It is ignored if A is given.  The default is
           true.
 
      ‘tol’
           Defines the required convergence tolerance, calculated as ‘tol
           * norm (A)’.  The default is ‘eps’.
 
      ‘maxit’
           The maximum number of iterations.  The default is 300.
 
      ‘p’
           The number of Lanzcos basis vectors to use.  More vectors will
           result in faster convergence, but a greater use of memory.
           The optimal value of ‘p’ is problem dependent and should be in
           the range K to N.  The default value is ‘2 * K’.
 
      ‘v0’
           The starting vector for the algorithm.  An initial vector
           close to the final vector will speed up convergence.  The
           default is for ARPACK to randomly generate a starting vector.
           If specified, ‘v0’ must be an N-by-1 vector where ‘N = rows
           (A)’
 
      ‘disp’
           The level of diagnostic printout (0|1|2).  If ‘disp’ is 0 then
           diagnostics are disabled.  The default value is 0.
 
      ‘cholB’
           Flag if ‘chol (B)’ is passed rather than B.  The default is
           false.
 
      ‘permB’
           The permutation vector of the Cholesky factorization of B if
           ‘cholB’ is true.  It is obtained by ‘[R, ~, permB] = chol (B,
           "vector")’.  The default is ‘1:N’.
 
      It is also possible to represent A by a function denoted AF.  AF
      must be followed by a scalar argument N defining the length of the
      vector argument accepted by AF.  AF can be a function handle, an
      inline function, or a string.  When AF is a string it holds the
      name of the function to use.
 
      AF is a function of the form ‘y = af (x)’ where the required return
      value of AF is determined by the value of SIGMA.  The four possible
      forms are
 
      ‘A * x’
           if SIGMA is not given or is a string other than "sm".
 
      ‘A \ x’
           if SIGMA is 0 or "sm".
 
      ‘(A - sigma * I) \ x’
           for the standard eigenvalue problem, where ‘I’ is the identity
           matrix of the same size as A.
 
      ‘(A - sigma * B) \ x’
           for the general eigenvalue problem.
 
      The return arguments of ‘eigs’ depend on the number of return
      arguments requested.  With a single return argument, a vector D of
      length K is returned containing the K eigenvalues that have been
      found.  With two return arguments, V is a N-by-K matrix whose
      columns are the K eigenvectors corresponding to the returned
      eigenvalues.  The eigenvalues themselves are returned in D in the
      form of a N-by-K matrix, where the elements on the diagonal are the
      eigenvalues.
 
      Given a third return argument FLAG, ‘eigs’ returns the status of
      the convergence.  If FLAG is 0 then all eigenvalues have converged.
      Any other value indicates a failure to converge.
 
      This function is based on the ARPACK package, written by R.
      Lehoucq, K. Maschhoff, D. Sorensen, and C. Yang.  For more
      information see <http://www.caam.rice.edu/software/ARPACK/>.
 
      See also: Seeeig XREFeig, Seesvds XREFsvds.
 
  -- : S = svds (A)
  -- : S = svds (A, K)
  -- : S = svds (A, K, SIGMA)
  -- : S = svds (A, K, SIGMA, OPTS)
  -- : [U, S, V] = svds (...)
  -- : [U, S, V, FLAG] = svds (...)
 
      Find a few singular values of the matrix A.
 
      The singular values are calculated using
 
           [M, N] = size (A);
           S = eigs ([sparse(M, M), A;
                                A', sparse(N, N)])
 
      The eigenvalues returned by ‘eigs’ correspond to the singular
      values of A.  The number of singular values to calculate is given
      by K and defaults to 6.
 
      The argument SIGMA specifies which singular values to find.  When
      SIGMA is the string ’L’, the default, the largest singular values
      of A are found.  Otherwise, SIGMA must be a real scalar and the
      singular values closest to SIGMA are found.  As a corollary, ‘SIGMA
      = 0’ finds the smallest singular values.  Note that for relatively
      small values of SIGMA, there is a chance that the requested number
      of singular values will not be found.  In that case SIGMA should be
      increased.
 
      OPTS is a structure defining options that ‘svds’ will pass to
      ‘eigs’.  The possible fields of this structure are documented in
      ‘eigs’.  By default, ‘svds’ sets the following three fields:
 
      ‘tol’
           The required convergence tolerance for the singular values.
           The default value is 1e-10.  ‘eigs’ is passed ‘TOL / sqrt(2)’.
 
      ‘maxit’
           The maximum number of iterations.  The default is 300.
 
      ‘disp’
           The level of diagnostic printout (0|1|2).  If ‘disp’ is 0 then
           diagnostics are disabled.  The default value is 0.
 
      If more than one output is requested then ‘svds’ will return an
      approximation of the singular value decomposition of A
 
           A_approx = U*S*V'
 
      where A_approx is a matrix of size A but only rank K.
 
      FLAG returns 0 if the algorithm has succesfully converged, and 1
      otherwise.  The test for convergence is
 
           norm (A*V - U*S, 1) <= TOL * norm (A, 1)
 
      ‘svds’ is best for finding only a few singular values from a large
      sparse matrix.  Otherwise, ‘svd (full (A))’ will likely be more
      efficient.
 
 See also: Seesvd XREFsvd, Seeeigs XREFeigs.
 
    ---------- Footnotes ----------
 
    (1) The CHOLMOD, UMFPACK and CXSPARSE packages were written by Tim
 Davis and are available at <http://www.cise.ufl.edu/research/sparse/>