@c DO NOT EDIT!  Generated automatically by munge-texi.pl.

@c Copyright (C) 1996-2013 John W. Eaton
@c
@c This file is part of Octave.
@c
@c Octave is free software; you can redistribute it and/or modify it
@c under the terms of the GNU General Public License as published by the
@c Free Software Foundation; either version 3 of the License, or (at
@c your option) any later version.
@c 
@c Octave is distributed in the hope that it will be useful, but WITHOUT
@c ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
@c FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
@c for more details.
@c 
@c You should have received a copy of the GNU General Public License
@c along with Octave; see the file COPYING.  If not, see
@c <http://www.gnu.org/licenses/>.

@node Linear Algebra
@chapter Linear Algebra
@cindex linear algebra

This chapter documents the linear algebra functions provided in Octave.
Reference material for many of these functions may be found in Golub and
Van Loan, @cite{Matrix Computations, 2nd Ed.}, Johns Hopkins, 1989, and
in the @cite{@sc{lapack} Users' Guide}, SIAM, 1992. The
@cite{@sc{lapack} Users' Guide} is available at:
@cite{http://www.netlib.org/lapack/lug/}

A common text for engineering courses is G. Strang, @cite{Linear Algebra
and Its Applications, 4th Edition}. It has become a widespread reference
for linear algebra. An alternative is P. Lax @cite{Linear Algebra and
Its Applications}, and also is a good choice. It claims to be suitable
for high school students with substantial mathematical interests as well
as first-year undergraduates.

@menu
* Techniques Used for Linear Algebra::
* Basic Matrix Functions::
* Matrix Factorizations::
* Functions of a Matrix::
* Specialized Solvers::
@end menu

@node Techniques Used for Linear Algebra
@section Techniques Used for Linear Algebra
@cindex linear algebra, techniques

Octave includes a polymorphic solver that selects an appropriate matrix
factorization depending on the properties of the matrix itself.
Generally, the cost of determining the matrix type is small relative to
the cost of factorizing the matrix itself. 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 solved or a matrix
inverse is formed is given by:

@enumerate 1
@item If the matrix is upper or lower triangular sparse use a forward or
backward substitution using the @sc{lapack} xTRTRS function, and goto 4.

@c Permuted triangular matrices currently disabled in the code
@c
@c @item If the matrix is a upper triangular matrix with column permutations
@c or lower triangular matrix with row permutations, perform a forward or
@c backward substitution, and goto 5.

@item If the matrix is square, Hermitian with a real positive diagonal,
attempt Cholesky@tie{}factorization using the @sc{lapack} xPOTRF function.

@item If the Cholesky@tie{}factorization failed or the matrix is not
Hermitian with a real positive diagonal, and the matrix is square, factorize 
using the @sc{lapack} xGETRF function.

@item If the matrix is not square, or any of the previous solvers flags
a singular or near singular matrix, find a least squares solution using
the @sc{lapack} xGELSD function.
@end enumerate

The user can force the type of the matrix with the @code{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 @code{matrix_type} should be
used with care.

It should be noted that the test for whether a matrix is a candidate for
Cholesky@tie{}factorization, performed above, and by the @code{matrix_type}
function, does not make certain that the matrix is
Hermitian.  However, the attempt to factorize the matrix will quickly
detect a non-Hermitian matrix.

@node Basic Matrix Functions
@section Basic Matrix Functions
@cindex matrix functions, basic

@c balance libinterp/corefcn/balance.cc
@anchor{XREFbalance}
@deftypefn  {Built-in Function} {@var{AA} =} balance (@var{A})
@deftypefnx {Built-in Function} {@var{AA} =} balance (@var{A}, @var{opt})
@deftypefnx {Built-in Function} {[@var{DD}, @var{AA}] =} balance (@var{A}, @var{opt})
@deftypefnx {Built-in Function} {[@var{D}, @var{P}, @var{AA}] =} balance (@var{A}, @var{opt})
@deftypefnx {Built-in Function} {[@var{CC}, @var{DD}, @var{AA}, @var{BB}] =} balance (@var{A}, @var{B}, @var{opt})

Compute @code{@var{AA} = @var{DD} \ @var{A} * @var{DD}} in which @var{AA}
is a matrix whose row and column norms are roughly equal in magnitude, and
@code{@var{DD} = @var{P} * @var{D}}, in which @var{P} is a permutation
matrix and @var{D} is a diagonal matrix of powers of two.  This allows the
equilibration to be computed without round-off.  Results of eigenvalue
calculation are typically improved by balancing first.

If two output values are requested, @code{balance} returns
the diagonal @var{D} and the permutation @var{P} separately as vectors.
In this case, @code{@var{DD} = eye(n)(:,@var{P}) * diag (@var{D})}, where
@math{n} is the matrix size.

If four output values are requested, compute @code{@var{AA} =
@var{CC}*@var{A}*@var{DD}} and @code{@var{BB} = @var{CC}*@var{B}*@var{DD}},
in which @var{AA} and @var{BB} have non-zero elements of approximately the
same magnitude and @var{CC} and @var{DD} are permuted diagonal matrices as
in @var{DD} for the algebraic eigenvalue problem.

The eigenvalue balancing option @var{opt} may be one of:

@table @asis
@item @qcode{"noperm"}, @qcode{"S"}
Scale only; do not permute.

@item @qcode{"noscal"}, @qcode{"P"}
Permute only; do not scale.
@end table

Algebraic eigenvalue balancing uses standard @sc{lapack} routines.

Generalized eigenvalue problem balancing uses Ward's algorithm
(SIAM Journal on Scientific and Statistical Computing, 1981).
@end deftypefn


@c cond scripts/linear-algebra/cond.m
@anchor{XREFcond}
@deftypefn  {Function File} {} cond (@var{A})
@deftypefnx {Function File} {} cond (@var{A}, @var{p})
Compute the @var{p}-norm condition number of a matrix.

@code{cond (@var{A})} is defined as
@tex
$ {\parallel A \parallel_p * \parallel A^{-1} \parallel_p .} $
@end tex
@ifnottex
@code{norm (@var{A}, @var{p}) * norm (inv (@var{A}), @var{p})}.
@end ifnottex

By default, @code{@var{p} = 2} is used which implies a (relatively slow)
singular value decomposition.  Other possible selections are
@code{@var{p} = 1, Inf, "fro"} which are generally faster.  See
@code{norm} for a full discussion of possible @var{p} values.

The condition number of a matrix quantifies the sensitivity of the matrix
inversion operation when small changes are made to matrix elements.  Ideally
the condition number will be close to 1.  When the number is large this
indicates small changes (such as underflow or round-off error) will produce
large changes in the resulting output.  In such cases the solution results
from numerical computing are not likely to be accurate.
@seealso{@ref{XREFcondest,,condest}, @ref{XREFrcond,,rcond}, @ref{XREFnorm,,norm}, @ref{XREFsvd,,svd}}
@end deftypefn


@c det libinterp/corefcn/det.cc
@anchor{XREFdet}
@deftypefn  {Built-in Function} {} det (@var{A})
@deftypefnx {Built-in Function} {[@var{d}, @var{rcond}] =} det (@var{A})
Compute the determinant of @var{A}.

Return an estimate of the reciprocal condition number if requested.

Routines from @sc{lapack} are used for full matrices and code from
@sc{umfpack} is used for sparse matrices.

The determinant should not be used to check a matrix for singularity.
For that, use any of the condition number functions: @code{cond},
@code{condest}, @code{rcond}.
@seealso{@ref{XREFcond,,cond}, @ref{XREFcondest,,condest}, @ref{XREFrcond,,rcond}}
@end deftypefn


@c eig libinterp/corefcn/eig.cc
@anchor{XREFeig}
@deftypefn  {Built-in Function} {@var{lambda} =} eig (@var{A})
@deftypefnx {Built-in Function} {@var{lambda} =} eig (@var{A}, @var{B})
@deftypefnx {Built-in Function} {[@var{V}, @var{lambda}] =} eig (@var{A})
@deftypefnx {Built-in Function} {[@var{V}, @var{lambda}] =} eig (@var{A}, @var{B})
Compute the eigenvalues (and optionally the eigenvectors) of a matrix
or a pair of matrices

The algorithm used depends on whether there are one or two input
matrices, if they are real or complex and if they are symmetric
(Hermitian if complex) or non-symmetric.

The eigenvalues returned by @code{eig} are not ordered.
@seealso{@ref{XREFeigs,,eigs}, @ref{XREFsvd,,svd}}
@end deftypefn


@c givens libinterp/corefcn/givens.cc
@anchor{XREFgivens}
@deftypefn  {Built-in Function} {@var{g} =} givens (@var{x}, @var{y})
@deftypefnx {Built-in Function} {[@var{c}, @var{s}] =} givens (@var{x}, @var{y})
@tex
Return a $2\times 2$ orthogonal matrix
$$
 G = \left[\matrix{c & s\cr -s'& c\cr}\right]
$$
such that
$$
 G \left[\matrix{x\cr y}\right] = \left[\matrix{\ast\cr 0}\right]
$$
with $x$ and $y$ scalars.
@end tex
@ifnottex
Return a 2 by 2 orthogonal matrix
@code{@var{g} = [@var{c} @var{s}; -@var{s}' @var{c}]} such that
@code{@var{g} [@var{x}; @var{y}] = [*; 0]} with @var{x} and @var{y} scalars.
@end ifnottex

For example:

@example
@group
givens (1, 1)
   @result{}   0.70711   0.70711
       -0.70711   0.70711
@end group
@end example
@end deftypefn


@c planerot scripts/linear-algebra/planerot.m
@anchor{XREFplanerot}
@deftypefn {Function File} {[@var{g}, @var{y}] =} planerot (@var{x})
Given a two-element column vector, returns the
@tex
$2 \times 2$ orthogonal matrix
@end tex
@ifnottex
2 by 2 orthogonal matrix
@end ifnottex
@var{G} such that
@code{@var{y} = @var{g} * @var{x}} and @code{@var{y}(2) = 0}.
@seealso{@ref{XREFgivens,,givens}}
@end deftypefn


@c inv libinterp/corefcn/inv.cc
@anchor{XREFinv}
@deftypefn  {Built-in Function} {@var{x} =} inv (@var{A})
@deftypefnx {Built-in Function} {[@var{x}, @var{rcond}] =} inv (@var{A})
Compute the inverse of the square matrix @var{A}.  Return an estimate
of the reciprocal condition number if requested, otherwise warn of an
ill-conditioned matrix if the reciprocal condition number is small.

In general it is best to avoid calculating the inverse of a matrix
directly.  For example, it is both faster and more accurate to solve
systems of equations (@var{A}*@math{x} = @math{b}) with
@code{@var{y} = @var{A} \ @math{b}}, rather than
@code{@var{y} = inv (@var{A}) * @math{b}}.

If called with a sparse matrix, then in general @var{x} will be a full
matrix requiring significantly more storage.  Avoid forming the inverse
of a sparse matrix if possible.
@seealso{@ref{XREFldivide,,ldivide}, @ref{XREFrdivide,,rdivide}}
@end deftypefn


@c linsolve scripts/linear-algebra/linsolve.m
@anchor{XREFlinsolve}
@deftypefn  {Function File} {@var{x} =} linsolve (@var{A}, @var{b})
@deftypefnx {Function File} {@var{x} =} linsolve (@var{A}, @var{b}, @var{opts})
@deftypefnx {Function File} {[@var{x}, @var{R}] =} linsolve (@dots{})
Solve the linear system @code{A*x = b}.

With no options, this function is equivalent to the left division operator
@w{(@code{x = A \ b})} or the matrix-left-divide function
@w{(@code{x = mldivide (A, b)})}.

Octave ordinarily examines the properties of the matrix @var{A} and chooses
a solver that best matches the matrix.  By passing a structure @var{opts}
to @code{linsolve} you can inform Octave directly about the matrix @var{A}.
In this case Octave will skip the matrix examination and proceed directly
to solving the linear system.

@strong{Warning:} If the matrix @var{A} does not have the properties
listed in the @var{opts} structure then the result will not be accurate
AND no warning will be given.  When in doubt, let Octave examine the matrix
and choose the appropriate solver as this step takes little time and the
result is cached so that it is only done once per linear system.

Possible @var{opts} fields (set value to true/false):

@table @asis
@item LT
@var{A} is lower triangular

@item UT
@var{A} is upper triangular

@item UHESS
@var{A} is upper Hessenberg (currently makes no difference)

@item SYM
@var{A} is symmetric or complex Hermitian (currently makes no difference)

@item POSDEF
@var{A} is positive definite

@item RECT
@var{A} is general rectangular (currently makes no difference)

@item TRANSA
  Solve @code{A'*x = b} by @code{transpose (A) \ b}
@end table

The optional second output @var{R} is the inverse condition number of
@var{A} (zero if matrix is singular).
@seealso{@ref{XREFmldivide,,mldivide}, @ref{XREFmatrix_type,,matrix_type}, @ref{XREFrcond,,rcond}}
@end deftypefn


@c matrix_type libinterp/corefcn/matrix_type.cc
@anchor{XREFmatrix_type}
@deftypefn  {Built-in Function} {@var{type} =} matrix_type (@var{A})
@deftypefnx {Built-in Function} {@var{type} =} matrix_type (@var{A}, "nocompute")
@deftypefnx {Built-in Function} {@var{A} =} matrix_type (@var{A}, @var{type})
@deftypefnx {Built-in Function} {@var{A} =} matrix_type (@var{A}, "upper", @var{perm})
@deftypefnx {Built-in Function} {@var{A} =} matrix_type (@var{A}, "lower", @var{perm})
@deftypefnx {Built-in Function} {@var{A} =} matrix_type (@var{A}, "banded", @var{nl}, @var{nu})
Identify the matrix type or mark a matrix as a particular type.  This allows
more rapid solutions of linear equations involving @var{A} to be performed.
Called with a single argument, @code{matrix_type} returns the type of the
matrix and caches it for future use.  Called with more than one argument,
@code{matrix_type} allows the type of the matrix to be defined.

If the option @qcode{"nocompute"} is given, the function will not attempt
to guess the type if it is still unknown.  This is useful for debugging
purposes.

The possible matrix types depend on whether the matrix is full or sparse, and
can be one of the following

@table @asis
@item @qcode{"unknown"}
Remove any previously cached matrix type, and mark type as unknown.

@item @qcode{"full"}
Mark the matrix as full.

@item @qcode{"positive definite"}
Probable full positive definite matrix.

@item @qcode{"diagonal"}
Diagonal matrix.  (Sparse matrices only)

@item @qcode{"permuted diagonal"}
Permuted Diagonal matrix.  The permutation does not need to be specifically
indicated, as the structure of the matrix explicitly gives this.  (Sparse
matrices only)

@item @qcode{"upper"}
Upper triangular.  If the optional third argument @var{perm} is given, the
matrix is assumed to be a permuted upper triangular with the permutations
defined by the vector @var{perm}.

@item @qcode{"lower"}
Lower triangular.  If the optional third argument @var{perm} is given, the
matrix is assumed to be a permuted lower triangular with the permutations
defined by the vector @var{perm}.

@item  @qcode{"banded"}
@itemx @qcode{"banded positive definite"}
Banded matrix with the band size of @var{nl} below the diagonal and @var{nu}
above it.  If @var{nl} and @var{nu} are 1, then the matrix is tridiagonal and
treated with specialized code.  In addition the matrix can be marked as
probably a positive definite.  (Sparse matrices only)

@item @qcode{"singular"}
The matrix is assumed to be singular and will be treated with a minimum norm
solution.

@end table

Note that the matrix type will be discovered automatically on the first
attempt to solve a linear equation involving @var{A}.  Therefore
@code{matrix_type} is only useful to give Octave hints of the matrix type.
Incorrectly defining the matrix type will result in incorrect results from
solutions of linear equations; it is entirely @strong{the responsibility of
the user} to correctly identify the matrix type.

Also, the test for positive definiteness is a low-cost test for a Hermitian
matrix with a real positive diagonal.  This does not guarantee that the
matrix is positive definite, but only that it is a probable candidate.  When
such a matrix is factorized, a Cholesky@tie{}factorization is first
attempted, and if that fails the matrix is then treated with an
LU@tie{}factorization.  Once the matrix has been factorized,
@code{matrix_type} will return the correct classification of the matrix.
@end deftypefn


@c norm libinterp/corefcn/data.cc
@anchor{XREFnorm}
@deftypefn  {Built-in Function} {} norm (@var{A})
@deftypefnx {Built-in Function} {} norm (@var{A}, @var{p})
@deftypefnx {Built-in Function} {} norm (@var{A}, @var{p}, @var{opt})
Compute the p-norm of the matrix @var{A}.  If the second argument is
missing, @code{p = 2} is assumed.

If @var{A} is a matrix (or sparse matrix):

@table @asis
@item @var{p} = @code{1}
1-norm, the largest column sum of the absolute values of @var{A}.

@item @var{p} = @code{2}
Largest singular value of @var{A}.

@item @var{p} = @code{Inf} or @qcode{"inf"}
@cindex infinity norm
Infinity norm, the largest row sum of the absolute values of @var{A}.

@item @var{p} = @qcode{"fro"}
@cindex Frobenius norm
Frobenius norm of @var{A}, @code{sqrt (sum (diag (@var{A}' * @var{A})))}.

@item other @var{p}, @code{@var{p} > 1}
@cindex general p-norm
maximum @code{norm (A*x, p)} such that @code{norm (x, p) == 1}
@end table

If @var{A} is a vector or a scalar:

@table @asis
@item @var{p} = @code{Inf} or @qcode{"inf"}
@code{max (abs (@var{A}))}.

@item @var{p} = @code{-Inf}
@code{min (abs (@var{A}))}.

@item @var{p} = @qcode{"fro"}
Frobenius norm of @var{A}, @code{sqrt (sumsq (abs (A)))}.

@item @var{p} = 0
Hamming norm - the number of nonzero elements.

@item other @var{p}, @code{@var{p} > 1}
p-norm of @var{A}, @code{(sum (abs (@var{A}) .^ @var{p})) ^ (1/@var{p})}.

@item other @var{p} @code{@var{p} < 1}
the p-pseudonorm defined as above.
@end table

If @var{opt} is the value @qcode{"rows"}, treat each row as a vector and
compute its norm.  The result is returned as a column vector.
Similarly, if @var{opt} is @qcode{"columns"} or @qcode{"cols"} then
compute the norms of each column and return a row vector.
@seealso{@ref{XREFcond,,cond}, @ref{XREFsvd,,svd}}
@end deftypefn


@c null scripts/linear-algebra/null.m
@anchor{XREFnull}
@deftypefn  {Function File} {} null (@var{A})
@deftypefnx {Function File} {} null (@var{A}, @var{tol})
Return an orthonormal basis of the null space of @var{A}.

The dimension of the null space is taken as the number of singular
values of @var{A} not greater than @var{tol}.  If the argument @var{tol}
is missing, it is computed as

@example
max (size (@var{A})) * max (svd (@var{A})) * eps
@end example
@seealso{@ref{XREForth,,orth}}
@end deftypefn


@c orth scripts/linear-algebra/orth.m
@anchor{XREForth}
@deftypefn  {Function File} {} orth (@var{A})
@deftypefnx {Function File} {} orth (@var{A}, @var{tol})
Return an orthonormal basis of the range space of @var{A}.

The dimension of the range space is taken as the number of singular
values of @var{A} greater than @var{tol}.  If the argument @var{tol} is
missing, it is computed as

@example
max (size (@var{A})) * max (svd (@var{A})) * eps
@end example
@seealso{@ref{XREFnull,,null}}
@end deftypefn


@c mgorth libinterp/corefcn/mgorth.cc
@anchor{XREFmgorth}
@deftypefn {Built-in Function} {[@var{y}, @var{h}] =} mgorth (@var{x}, @var{v})
Orthogonalize a given column vector @var{x} with respect to a set of
orthonormal vectors comprising the columns of @var{v}
using the modified Gram-Schmidt method.
On exit, @var{y} is a unit vector such that:

@example
@group
  norm (@var{y}) = 1
  @var{v}' * @var{y} = 0
  @var{x} = [@var{v}, @var{y}]*@var{h}'
@end group
@end example

@end deftypefn


@c pinv libinterp/corefcn/pinv.cc
@anchor{XREFpinv}
@deftypefn  {Built-in Function} {} pinv (@var{x})
@deftypefnx {Built-in Function} {} pinv (@var{x}, @var{tol})
Return the pseudoinverse of @var{x}.  Singular values less than
@var{tol} are ignored.

If the second argument is omitted, it is taken to be

@example
tol = max (size (@var{x})) * sigma_max (@var{x}) * eps,
@end example

@noindent
where @code{sigma_max (@var{x})} is the maximal singular value of @var{x}.
@end deftypefn

@cindex pseudoinverse

@c rank scripts/linear-algebra/rank.m
@anchor{XREFrank}
@deftypefn  {Function File} {} rank (@var{A})
@deftypefnx {Function File} {} rank (@var{A}, @var{tol})
Compute the rank of matrix @var{A}, using the singular value decomposition.

The rank is taken to be the number of singular values of @var{A} that
are greater than the specified tolerance @var{tol}.  If the second
argument is omitted, it is taken to be

@example
tol = max (size (@var{A})) * sigma(1) * eps;
@end example

@noindent
where @code{eps} is machine precision and @code{sigma(1)} is the largest
singular value of @var{A}.

The rank of a matrix is the number of linearly independent rows or
columns and determines how many particular solutions exist to a system
of equations.  Use @code{null} for finding the remaining homogenous
solutions.

Example:

@example
@group
x = [1 2 3
     4 5 6
     7 8 9];
rank (x)
  @result{} 2
@end group
@end example

@noindent
The number of linearly independent rows is only 2 because the final row
is a linear combination of -1*row1 + 2*row2.

@seealso{@ref{XREFnull,,null}, @ref{XREFsprank,,sprank}, @ref{XREFsvd,,svd}}
@end deftypefn


@c rcond libinterp/corefcn/rcond.cc
@anchor{XREFrcond}
@deftypefn {Built-in Function} {@var{c} =} rcond (@var{A})
Compute the 1-norm estimate of the reciprocal condition number as returned
by @sc{lapack}.  If the matrix is well-conditioned then @var{c} will be near
1 and if the matrix is poorly conditioned it will be close to zero.

The matrix @var{A} must not be sparse.  If the matrix is sparse then
@code{condest (@var{A})} or @code{rcond (full (@var{A}))} should be used
instead.
@seealso{@ref{XREFcond,,cond}, @ref{XREFcondest,,condest}}
@end deftypefn


@c trace scripts/linear-algebra/trace.m
@anchor{XREFtrace}
@deftypefn {Function File} {} trace (@var{A})
Compute the trace of @var{A}, the sum of the elements along the main
diagonal.

The implementation is straightforward: @code{sum (diag (@var{A}))}.
@seealso{@ref{XREFeig,,eig}}
@end deftypefn


@c rref scripts/linear-algebra/rref.m
@anchor{XREFrref}
@deftypefn  {Function File} {} rref (@var{A})
@deftypefnx {Function File} {} rref (@var{A}, @var{tol})
@deftypefnx {Function File} {[@var{r}, @var{k}] =} rref (@dots{})
Return the reduced row echelon form of @var{A}.  @var{tol} defaults
to @code{eps * max (size (@var{A})) * norm (@var{A}, inf)}.

Called with two return arguments, @var{k} returns the vector of
"bound variables", which are those columns on which elimination
has been performed.

@end deftypefn


@node Matrix Factorizations
@section Matrix Factorizations
@cindex matrix factorizations

@c chol libinterp/dldfcn/chol.cc
@anchor{XREFchol}
@deftypefn  {Loadable Function} {@var{R} =} chol (@var{A})
@deftypefnx {Loadable Function} {[@var{R}, @var{p}] =} chol (@var{A})
@deftypefnx {Loadable Function} {[@var{R}, @var{p}, @var{Q}] =} chol (@var{S})
@deftypefnx {Loadable Function} {[@var{R}, @var{p}, @var{Q}] =} chol (@var{S}, "vector")
@deftypefnx {Loadable Function} {[@var{L}, @dots{}] =} chol (@dots{}, "lower")
@deftypefnx {Loadable Function} {[@var{L}, @dots{}] =} chol (@dots{}, "upper")
@cindex Cholesky factorization
Compute the Cholesky@tie{}factor, @var{R}, of the symmetric positive definite
matrix @var{A}, where
@tex
$ R^T R = A $.
@end tex
@ifnottex

@example
@var{R}' * @var{R} = @var{A}.
@end example

@end ifnottex

Called with one output argument @code{chol} fails if @var{A} or @var{S} is
not positive definite.  With two or more output arguments @var{p} flags
whether the matrix was positive definite and @code{chol} does not fail.  A
zero value indicated that the matrix was positive definite and the @var{R}
gives the factorization, and @var{p} will have a positive value otherwise.

If called with 3 outputs then a sparsity preserving row/column permutation
is applied to @var{A} prior to the factorization.  That is @var{R}
is the factorization of @code{@var{A}(@var{Q},@var{Q})} such that
@tex
$ R^T R = Q^T A Q$.
@end tex
@ifnottex

@example
@var{R}' * @var{R} = @var{Q}' * @var{A} * @var{Q}.
@end example

@end ifnottex

The sparsity preserving permutation is generally returned as a matrix.
However, given the flag @qcode{"vector"}, @var{Q} will be returned as a
vector such that
@tex
$ R^T R = A (Q, Q)$.
@end tex
@ifnottex

@example
@var{R}' * @var{R} = @var{A}(@var{Q}, @var{Q}).
@end example

@end ifnottex

Called with either a sparse or full matrix and using the @qcode{"lower"}
flag, @code{chol} returns the lower triangular factorization such that
@tex
$ L L^T = A $.
@end tex
@ifnottex

@example
@var{L} * @var{L}' = @var{A}.
@end example

@end ifnottex

For full matrices, if the @qcode{"lower"} flag is set only the lower
triangular part of the matrix is used for the factorization, otherwise the
upper triangular part is used.

In general the lower triangular factorization is significantly faster for
sparse matrices.
@seealso{@ref{XREFhess,,hess}, @ref{XREFlu,,lu}, @ref{XREFqr,,qr}, @ref{XREFqz,,qz}, @ref{XREFschur,,schur}, @ref{XREFsvd,,svd}, @ref{XREFcholinv,,cholinv}, @ref{XREFchol2inv,,chol2inv}, @ref{XREFcholupdate,,cholupdate}, @ref{XREFcholinsert,,cholinsert}, @ref{XREFcholdelete,,choldelete}, @ref{XREFcholshift,,cholshift}}
@end deftypefn


@c cholinv libinterp/dldfcn/chol.cc
@anchor{XREFcholinv}
@deftypefn {Loadable Function} {} cholinv (@var{A})
Use the Cholesky@tie{}factorization to compute the inverse of the
symmetric positive definite matrix @var{A}.
@seealso{@ref{XREFchol,,chol}, @ref{XREFchol2inv,,chol2inv}, @ref{XREFinv,,inv}}
@end deftypefn


@c chol2inv libinterp/dldfcn/chol.cc
@anchor{XREFchol2inv}
@deftypefn {Loadable Function} {} chol2inv (@var{U})
Invert a symmetric, positive definite square matrix from its Cholesky
decomposition, @var{U}.  Note that @var{U} should be an upper-triangular
matrix with positive diagonal elements.  @code{chol2inv (@var{U})}
provides @code{inv (@var{U}'*@var{U})} but it is much faster than
using @code{inv}.
@seealso{@ref{XREFchol,,chol}, @ref{XREFcholinv,,cholinv}, @ref{XREFinv,,inv}}
@end deftypefn


@c cholupdate libinterp/dldfcn/chol.cc
@anchor{XREFcholupdate}
@deftypefn {Loadable Function} {[@var{R1}, @var{info}] =} cholupdate (@var{R}, @var{u}, @var{op})
Update or downdate a Cholesky@tie{}factorization.  Given an upper triangular
matrix @var{R} and a column vector @var{u}, attempt to determine another
upper triangular matrix @var{R1} such that

@itemize @bullet
@item
@var{R1}'*@var{R1} = @var{R}'*@var{R} + @var{u}*@var{u}'
if @var{op} is @qcode{"+"}

@item
@var{R1}'*@var{R1} = @var{R}'*@var{R} - @var{u}*@var{u}'
if @var{op} is @qcode{"-"}
@end itemize

If @var{op} is @qcode{"-"}, @var{info} is set to

@itemize
@item 0 if the downdate was successful,

@item 1 if @var{R}'*@var{R} - @var{u}*@var{u}' is not positive definite,

@item 2 if @var{R} is singular.
@end itemize

If @var{info} is not present, an error message is printed in cases 1 and 2.
@seealso{@ref{XREFchol,,chol}, @ref{XREFcholinsert,,cholinsert}, @ref{XREFcholdelete,,choldelete}, @ref{XREFcholshift,,cholshift}}
@end deftypefn


@c cholinsert libinterp/dldfcn/chol.cc
@anchor{XREFcholinsert}
@deftypefn  {Loadable Function} {@var{R1} =} cholinsert (@var{R}, @var{j}, @var{u})
@deftypefnx {Loadable Function} {[@var{R1}, @var{info}] =} cholinsert (@var{R}, @var{j}, @var{u})
Given a Cholesky@tie{}factorization of a real symmetric or complex Hermitian
positive definite matrix @w{@var{A} = @var{R}'*@var{R}}, @var{R}@tie{}upper
triangular, return the Cholesky@tie{}factorization of
@var{A1}, where @w{A1(p,p) = A}, @w{A1(:,j) = A1(j,:)' = u} and
@w{p = [1:j-1,j+1:n+1]}.  @w{u(j)} should be positive.
On return, @var{info} is set to

@itemize
@item 0 if the insertion was successful,

@item 1 if @var{A1} is not positive definite,

@item 2 if @var{R} is singular.
@end itemize

If @var{info} is not present, an error message is printed in cases 1 and 2.
@seealso{@ref{XREFchol,,chol}, @ref{XREFcholupdate,,cholupdate}, @ref{XREFcholdelete,,choldelete}, @ref{XREFcholshift,,cholshift}}
@end deftypefn


@c choldelete libinterp/dldfcn/chol.cc
@anchor{XREFcholdelete}
@deftypefn {Loadable Function} {@var{R1} =} choldelete (@var{R}, @var{j})
Given a Cholesky@tie{}factorization of a real symmetric or complex Hermitian
positive definite matrix @w{@var{A} = @var{R}'*@var{R}}, @var{R}@tie{}upper
triangular, return the Cholesky@tie{}factorization of @w{A(p,p)}, where
@w{p = [1:j-1,j+1:n+1]}.
@seealso{@ref{XREFchol,,chol}, @ref{XREFcholupdate,,cholupdate}, @ref{XREFcholinsert,,cholinsert}, @ref{XREFcholshift,,cholshift}}
@end deftypefn


@c cholshift libinterp/dldfcn/chol.cc
@anchor{XREFcholshift}
@deftypefn {Loadable Function} {@var{R1} =} cholshift (@var{R}, @var{i}, @var{j})
Given a Cholesky@tie{}factorization of a real symmetric or complex Hermitian
positive definite matrix @w{@var{A} = @var{R}'*@var{R}}, @var{R}@tie{}upper
triangular, return the Cholesky@tie{}factorization of
@w{@var{A}(p,p)}, where @w{p} is the permutation @*
@code{p = [1:i-1, shift(i:j, 1), j+1:n]} if @w{@var{i} < @var{j}} @*
 or @*
@code{p = [1:j-1, shift(j:i,-1), i+1:n]} if @w{@var{j} < @var{i}}.  @*

@seealso{@ref{XREFchol,,chol}, @ref{XREFcholupdate,,cholupdate}, @ref{XREFcholinsert,,cholinsert}, @ref{XREFcholdelete,,choldelete}}
@end deftypefn


@c hess libinterp/corefcn/hess.cc
@anchor{XREFhess}
@deftypefn  {Built-in Function} {@var{H} =} hess (@var{A})
@deftypefnx {Built-in Function} {[@var{P}, @var{H}] =} hess (@var{A})
@cindex Hessenberg decomposition
Compute the Hessenberg decomposition of the matrix @var{A}.

The Hessenberg decomposition is
@tex
$$
A = PHP^T
$$
where $P$ is a square unitary matrix ($P^TP = I$), and $H$
is upper Hessenberg ($H_{i,j} = 0, \forall i \ge j+1$).
@end tex
@ifnottex
@code{@var{P} * @var{H} * @var{P}' = @var{A}} where @var{P} is a square
unitary matrix (@code{@var{P}' * @var{P} = I}, using complex-conjugate
transposition) and @var{H} is upper Hessenberg
(@code{@var{H}(i, j) = 0 forall i >= j+1)}.
@end ifnottex

The Hessenberg decomposition is usually used as the first step in an
eigenvalue computation, but has other applications as well (see Golub,
Nash, and Van Loan, IEEE Transactions on Automatic Control, 1979).
@seealso{@ref{XREFeig,,eig}, @ref{XREFchol,,chol}, @ref{XREFlu,,lu}, @ref{XREFqr,,qr}, @ref{XREFqz,,qz}, @ref{XREFschur,,schur}, @ref{XREFsvd,,svd}}
@end deftypefn


@c lu libinterp/corefcn/lu.cc
@anchor{XREFlu}
@deftypefn  {Built-in Function} {[@var{L}, @var{U}] =} lu (@var{A})
@deftypefnx {Built-in Function} {[@var{L}, @var{U}, @var{P}] =} lu (@var{A})
@deftypefnx {Built-in Function} {[@var{L}, @var{U}, @var{P}, @var{Q}] =} lu (@var{S})
@deftypefnx {Built-in Function} {[@var{L}, @var{U}, @var{P}, @var{Q}, @var{R}] =} lu (@var{S})
@deftypefnx {Built-in Function} {[@dots{}] =} lu (@var{S}, @var{thres})
@deftypefnx {Built-in Function} {@var{y} =} lu (@dots{})
@deftypefnx {Built-in Function} {[@dots{}] =} lu (@dots{}, "vector")
@cindex LU decomposition
Compute the LU@tie{}decomposition of @var{A}.  If @var{A} is full
subroutines from
@sc{lapack} are used and if @var{A} is sparse then @sc{umfpack} is used.  The
result is returned in a permuted form, according to the optional return
value @var{P}.  For example, given the matrix @code{a = [1, 2; 3, 4]},

@example
[l, u, p] = lu (@var{a})
@end example

@noindent
returns

@example
@group
l =

  1.00000  0.00000
  0.33333  1.00000

u =

  3.00000  4.00000
  0.00000  0.66667

p =

  0  1
  1  0
@end group
@end example

The matrix is not required to be square.

When called with two or three output arguments and a spare input matrix,
@code{lu} does not attempt to perform sparsity preserving column
permutations.  Called with a fourth output argument, the sparsity
preserving column transformation @var{Q} is returned, such that
@code{@var{P} * @var{A} * @var{Q} = @var{L} * @var{U}}.

Called with a fifth output argument and a sparse input matrix,
@code{lu} attempts to use a scaling factor @var{R} on the input matrix
such that
@code{@var{P} * (@var{R} \ @var{A}) * @var{Q} = @var{L} * @var{U}}.
This typically leads to a sparser and more stable factorization.

An additional input argument @var{thres}, that defines the pivoting
threshold can be given.  @var{thres} can be a scalar, in which case
it defines the @sc{umfpack} pivoting tolerance for both symmetric and
unsymmetric cases.  If @var{thres} is a 2-element vector, then the first
element defines the pivoting tolerance for the unsymmetric @sc{umfpack}
pivoting strategy and the second for the symmetric strategy.  By default,
the values defined by @code{spparms} are used ([0.1, 0.001]).

Given the string argument @qcode{"vector"}, @code{lu} returns the values
of @var{P} and @var{Q} as vector values, such that for full matrix,
@code{@var{A} (@var{P},:) = @var{L} * @var{U}}, and @code{@var{R}(@var{P},:)
* @var{A} (:, @var{Q}) = @var{L} * @var{U}}.

With two output arguments, returns the permuted forms of the upper and
lower triangular matrices, such that @code{@var{A} = @var{L} * @var{U}}.
With one output argument @var{y}, then the matrix returned by the @sc{lapack}
routines is returned.  If the input matrix is sparse then the matrix @var{L}
is embedded into @var{U} to give a return value similar to the full case.
For both full and sparse matrices, @code{lu} loses the permutation
information.
@seealso{@ref{XREFluupdate,,luupdate}, @ref{XREFchol,,chol}, @ref{XREFhess,,hess}, @ref{XREFqr,,qr}, @ref{XREFqz,,qz}, @ref{XREFschur,,schur}, @ref{XREFsvd,,svd}}
@end deftypefn


@c luupdate libinterp/corefcn/lu.cc
@anchor{XREFluupdate}
@deftypefn  {Built-in Function} {[@var{L}, @var{U}] =} luupdate (@var{L}, @var{U}, @var{x}, @var{y})
@deftypefnx {Built-in Function} {[@var{L}, @var{U}, @var{P}] =} luupdate (@var{L}, @var{U}, @var{P}, @var{x}, @var{y})
Given an LU@tie{}factorization of a real or complex matrix
@w{@var{A} = @var{L}*@var{U}}, @var{L}@tie{}lower unit trapezoidal and
@var{U}@tie{}upper trapezoidal, return the LU@tie{}factorization
of @w{@var{A} + @var{x}*@var{y}.'}, where @var{x} and @var{y} are
column vectors (rank-1 update) or matrices with equal number of columns
(rank-k update).
Optionally, row-pivoted updating can be used by supplying
a row permutation (pivoting) matrix @var{P};
in that case, an updated permutation matrix is returned.
Note that if @var{L}, @var{U}, @var{P} is a pivoted LU@tie{}factorization
as obtained by @code{lu}:

@example
[@var{L}, @var{U}, @var{P}] = lu (@var{A});
@end example

@noindent
then a factorization of @tcode{@var{A}+@var{x}*@var{y}.'} can be obtained
either as

@example
[@var{L1}, @var{U1}] = lu (@var{L}, @var{U}, @var{P}*@var{x}, @var{y})
@end example

@noindent
or

@example
[@var{L1}, @var{U1}, @var{P1}] = lu (@var{L}, @var{U}, @var{P}, @var{x}, @var{y})
@end example

The first form uses the unpivoted algorithm, which is faster, but less
stable.  The second form uses a slower pivoted algorithm, which is more
stable.

The matrix case is done as a sequence of rank-1 updates;
thus, for large enough k, it will be both faster and more accurate to
recompute the factorization from scratch.
@seealso{@ref{XREFlu,,lu}, @ref{XREFcholupdate,,cholupdate}, @ref{XREFqrupdate,,qrupdate}}
@end deftypefn


@c qr libinterp/dldfcn/qr.cc
@anchor{XREFqr}
@deftypefn  {Loadable Function} {[@var{Q}, @var{R}, @var{P}] =} qr (@var{A})
@deftypefnx {Loadable Function} {[@var{Q}, @var{R}, @var{P}] =} qr (@var{A}, '0')
@deftypefnx {Loadable Function} {[@var{C}, @var{R}] =} qr (@var{A}, @var{B})
@deftypefnx {Loadable Function} {[@var{C}, @var{R}] =} qr (@var{A}, @var{B}, '0')
@cindex QR factorization
Compute the QR@tie{}factorization of @var{A}, using standard @sc{lapack}
subroutines.  For example, given the matrix @code{@var{A} = [1, 2; 3, 4]},

@example
[@var{Q}, @var{R}] = qr (@var{A})
@end example

@noindent
returns

@example
@group
@var{Q} =

  -0.31623  -0.94868
  -0.94868   0.31623

@var{R} =

  -3.16228  -4.42719
   0.00000  -0.63246
@end group
@end example

The @code{qr} factorization has applications in the solution of least
squares problems
@tex
$$
\min_x \left\Vert A x - b \right\Vert_2
$$
@end tex
@ifnottex

@example
min norm(A x - b)
@end example

@end ifnottex
for overdetermined systems of equations (i.e.,
@tex
$A$
@end tex
@ifnottex
@var{A}
@end ifnottex
 is a tall, thin matrix).  The QR@tie{}factorization is
@tex
$QR = A$ where $Q$ is an orthogonal matrix and $R$ is upper triangular.
@end tex
@ifnottex
@code{@var{Q} * @var{R} = @var{A}} where @var{Q} is an orthogonal matrix and
@var{R} is upper triangular.
@end ifnottex

If given a second argument of @qcode{'0'}, @code{qr} returns an economy-sized
QR@tie{}factorization, omitting zero rows of @var{R} and the corresponding
columns of @var{Q}.

If the matrix @var{A} is full, the permuted QR@tie{}factorization
@code{[@var{Q}, @var{R}, @var{P}] = qr (@var{A})} forms the
QR@tie{}factorization such that the diagonal entries of @var{R} are
decreasing in magnitude order.  For example, given the matrix @code{a = [1,
2; 3, 4]},

@example
[@var{Q}, @var{R}, @var{P}] = qr (@var{A})
@end example

@noindent
returns

@example
@group
@var{Q} =

  -0.44721  -0.89443
  -0.89443   0.44721

@var{R} =

  -4.47214  -3.13050
   0.00000   0.44721

@var{P} =

   0  1
   1  0
@end group
@end example

The permuted @code{qr} factorization @code{[@var{Q}, @var{R}, @var{P}] = qr
(@var{A})} factorization allows the construction of an orthogonal basis of
@code{span (A)}.

If the matrix @var{A} is sparse, then compute the sparse
QR@tie{}factorization of @var{A}, using @sc{CSparse}.  As the matrix @var{Q}
is in general a full matrix, this function returns the @var{Q}-less
factorization @var{R} of @var{A}, such that @code{@var{R} = chol (@var{A}' *
@var{A})}.

If the final argument is the scalar @code{0} and the number of rows is
larger than the number of columns, then an economy factorization is
returned.  That is @var{R} will have only @code{size (@var{A},1)} rows.

If an additional matrix @var{B} is supplied, then @code{qr} returns
@var{C}, where @code{@var{C} = @var{Q}' * @var{B}}.  This allows the
least squares approximation of @code{@var{A} \ @var{B}} to be calculated
as

@example
@group
[@var{C}, @var{R}] = qr (@var{A}, @var{B})
x = @var{R} \ @var{C}
@end group
@end example
@seealso{@ref{XREFchol,,chol}, @ref{XREFhess,,hess}, @ref{XREFlu,,lu}, @ref{XREFqz,,qz}, @ref{XREFschur,,schur}, @ref{XREFsvd,,svd}, @ref{XREFqrupdate,,qrupdate}, @ref{XREFqrinsert,,qrinsert}, @ref{XREFqrdelete,,qrdelete}, @ref{XREFqrshift,,qrshift}}
@end deftypefn


@c qrupdate libinterp/dldfcn/qr.cc
@anchor{XREFqrupdate}
@deftypefn {Loadable Function} {[@var{Q1}, @var{R1}] =} qrupdate (@var{Q}, @var{R}, @var{u}, @var{v})
Given a QR@tie{}factorization of a real or complex matrix
@w{@var{A} = @var{Q}*@var{R}}, @var{Q}@tie{}unitary and
@var{R}@tie{}upper trapezoidal, return the QR@tie{}factorization
of @w{@var{A} + @var{u}*@var{v}'}, where @var{u} and @var{v} are
column vectors (rank-1 update) or matrices with equal number of columns
(rank-k update).  Notice that the latter case is done as a sequence of rank-1
updates; thus, for k large enough, it will be both faster and more accurate
to recompute the factorization from scratch.

The QR@tie{}factorization supplied may be either full
(Q is square) or economized (R is square).

@seealso{@ref{XREFqr,,qr}, @ref{XREFqrinsert,,qrinsert}, @ref{XREFqrdelete,,qrdelete}, @ref{XREFqrshift,,qrshift}}
@end deftypefn


@c qrinsert libinterp/dldfcn/qr.cc
@anchor{XREFqrinsert}
@deftypefn {Loadable Function} {[@var{Q1}, @var{R1}] =} qrinsert (@var{Q}, @var{R}, @var{j}, @var{x}, @var{orient})
Given a QR@tie{}factorization of a real or complex matrix
@w{@var{A} = @var{Q}*@var{R}}, @var{Q}@tie{}unitary and
@var{R}@tie{}upper trapezoidal, return the QR@tie{}factorization of
@w{[A(:,1:j-1) x A(:,j:n)]}, where @var{u} is a column vector to be
inserted into @var{A} (if @var{orient} is @qcode{"col"}), or the
QR@tie{}factorization of @w{[A(1:j-1,:);x;A(:,j:n)]}, where @var{x}
is a row vector to be inserted into @var{A} (if @var{orient} is
@qcode{"row"}).

The default value of @var{orient} is @qcode{"col"}.
If @var{orient} is @qcode{"col"},
@var{u} may be a matrix and @var{j} an index vector
resulting in the QR@tie{}factorization of a matrix @var{B} such that
@w{B(:,@var{j})} gives @var{u} and @w{B(:,@var{j}) = []} gives @var{A}.
Notice that the latter case is done as a sequence of k insertions;
thus, for k large enough, it will be both faster and more accurate to
recompute the factorization from scratch.

If @var{orient} is @qcode{"col"},
the QR@tie{}factorization supplied may be either full
(Q is square) or economized (R is square).

If @var{orient} is @qcode{"row"}, full factorization is needed.
@seealso{@ref{XREFqr,,qr}, @ref{XREFqrupdate,,qrupdate}, @ref{XREFqrdelete,,qrdelete}, @ref{XREFqrshift,,qrshift}}
@end deftypefn


@c qrdelete libinterp/dldfcn/qr.cc
@anchor{XREFqrdelete}
@deftypefn {Loadable Function} {[@var{Q1}, @var{R1}] =} qrdelete (@var{Q}, @var{R}, @var{j}, @var{orient})
Given a QR@tie{}factorization of a real or complex matrix
@w{@var{A} = @var{Q}*@var{R}}, @var{Q}@tie{}unitary and
@var{R}@tie{}upper trapezoidal, return the QR@tie{}factorization of
@w{[A(:,1:j-1) A(:,j+1:n)]}, i.e., @var{A} with one column deleted
(if @var{orient} is @qcode{"col"}), or the QR@tie{}factorization of
@w{[A(1:j-1,:);A(j+1:n,:)]}, i.e., @var{A} with one row deleted (if
@var{orient} is @qcode{"row"}).

The default value of @var{orient} is @qcode{"col"}.

If @var{orient} is @qcode{"col"},
@var{j} may be an index vector
resulting in the QR@tie{}factorization of a matrix @var{B} such that
@w{A(:,@var{j}) = []} gives @var{B}.
Notice that the latter case is done as a sequence of k deletions;
thus, for k large enough, it will be both faster and more accurate to
recompute the factorization from scratch.

If @var{orient} is @qcode{"col"},
the QR@tie{}factorization supplied may be either full
(Q is square) or economized (R is square).

If @var{orient} is @qcode{"row"}, full factorization is needed.
@seealso{@ref{XREFqr,,qr}, @ref{XREFqrupdate,,qrupdate}, @ref{XREFqrinsert,,qrinsert}, @ref{XREFqrshift,,qrshift}}
@end deftypefn


@c qrshift libinterp/dldfcn/qr.cc
@anchor{XREFqrshift}
@deftypefn {Loadable Function} {[@var{Q1}, @var{R1}] =} qrshift (@var{Q}, @var{R}, @var{i}, @var{j})
Given a QR@tie{}factorization of a real or complex matrix
@w{@var{A} = @var{Q}*@var{R}}, @var{Q}@tie{}unitary and
@var{R}@tie{}upper trapezoidal, return the QR@tie{}factorization
of @w{@var{A}(:,p)}, where @w{p} is the permutation @*
@code{p = [1:i-1, shift(i:j, 1), j+1:n]} if @w{@var{i} < @var{j}} @*
 or @*
@code{p = [1:j-1, shift(j:i,-1), i+1:n]} if @w{@var{j} < @var{i}}.  @*

@seealso{@ref{XREFqr,,qr}, @ref{XREFqrupdate,,qrupdate}, @ref{XREFqrinsert,,qrinsert}, @ref{XREFqrdelete,,qrdelete}}
@end deftypefn


@c qz libinterp/corefcn/qz.cc
@anchor{XREFqz}
@deftypefn  {Built-in Function} {@var{lambda} =} qz (@var{A}, @var{B})
@deftypefnx {Built-in Function} {@var{lambda} =} qz (@var{A}, @var{B}, @var{opt})
QZ@tie{}decomposition of the generalized eigenvalue problem
(@math{A x = s B x}).  There are three ways to call this function:
@enumerate
@item @code{@var{lambda} = qz (@var{A}, @var{B})}

Computes the generalized eigenvalues
@tex
$\lambda$
@end tex
@ifnottex
@var{lambda}
@end ifnottex
of @math{(A - s B)}.

@item @code{[AA, BB, Q, Z, V, W, @var{lambda}] = qz (@var{A}, @var{B})}

Computes QZ@tie{}decomposition, generalized eigenvectors, and
generalized eigenvalues of @math{(A - s B)}
@tex
$$ AV = BV{ \rm diag }(\lambda) $$
$$ W^T A = { \rm diag }(\lambda)W^T B $$
$$ AA = Q^T AZ, BB = Q^T BZ $$
@end tex
@ifnottex

@example
@group

A * V = B * V * diag (@var{lambda})
W' * A = diag (@var{lambda}) * W' * B
AA = Q * A * Z, BB = Q * B * Z

@end group
@end example

@end ifnottex
with @var{Q} and @var{Z} orthogonal (unitary)= @var{I}

@item @code{[AA,BB,Z@{, @var{lambda}@}] = qz (@var{A}, @var{B}, @var{opt})}

As in form [2], but allows ordering of generalized eigenpairs
for (e.g.) solution of discrete time algebraic Riccati equations.
Form 3 is not available for complex matrices, and does not compute
the generalized eigenvectors @var{V}, @var{W}, nor the orthogonal matrix
@var{Q}.

@table @var
@item opt
for ordering eigenvalues of the @nospell{GEP} pencil.  The leading block
of the revised pencil contains all eigenvalues that satisfy:

@table @asis
@item @qcode{"N"}
= unordered (default)

@item @qcode{"S"}
= small: leading block has all |lambda| @leq{} 1

@item @qcode{"B"}
= big: leading block has all |lambda| @geq{} 1

@item @qcode{"-"}
= negative real part: leading block has all eigenvalues
in the open left half-plane

@item @qcode{"+"}
= non-negative real part: leading block has all eigenvalues
in the closed right half-plane
@end table
@end table
@end enumerate

Note: @code{qz} performs permutation balancing, but not scaling
(@pxref{XREFbalance}).  The order of output arguments was selected for
compatibility with @sc{matlab}.
@seealso{@ref{XREFeig,,eig}, @ref{XREFbalance,,balance}, @ref{XREFlu,,lu}, @ref{XREFchol,,chol}, @ref{XREFhess,,hess}, @ref{XREFqr,,qr}, @ref{XREFqzhess,,qzhess}, @ref{XREFschur,,schur}, @ref{XREFsvd,,svd}}
@end deftypefn


@c qzhess scripts/linear-algebra/qzhess.m
@anchor{XREFqzhess}
@deftypefn {Function File} {[@var{aa}, @var{bb}, @var{q}, @var{z}] =} qzhess (@var{A}, @var{B})
Compute the Hessenberg-triangular decomposition of the matrix pencil
@code{(@var{A}, @var{B})}, returning
@code{@var{aa} = @var{q} * @var{A} * @var{z}},
@code{@var{bb} = @var{q} * @var{B} * @var{z}}, with @var{q} and @var{z}
orthogonal.  For example:

@example
@group
[aa, bb, q, z] = qzhess ([1, 2; 3, 4], [5, 6; 7, 8])
     @result{} aa = [ -3.02244, -4.41741;  0.92998,  0.69749 ]
     @result{} bb = [ -8.60233, -9.99730;  0.00000, -0.23250 ]
     @result{}  q = [ -0.58124, -0.81373; -0.81373,  0.58124 ]
     @result{}  z = [ 1, 0; 0, 1 ]
@end group
@end example

The Hessenberg-triangular decomposition is the first step in
Moler and Stewart's QZ@tie{}decomposition algorithm.

Algorithm taken from Golub and Van Loan,
@cite{Matrix Computations, 2nd edition}.

@seealso{@ref{XREFlu,,lu}, @ref{XREFchol,,chol}, @ref{XREFhess,,hess}, @ref{XREFqr,,qr}, @ref{XREFqz,,qz}, @ref{XREFschur,,schur}, @ref{XREFsvd,,svd}}
@end deftypefn


@c schur libinterp/corefcn/schur.cc
@anchor{XREFschur}
@deftypefn  {Built-in Function} {@var{S} =} schur (@var{A})
@deftypefnx {Built-in Function} {@var{S} =} schur (@var{A}, "real")
@deftypefnx {Built-in Function} {@var{S} =} schur (@var{A}, "complex")
@deftypefnx {Built-in Function} {@var{S} =} schur (@var{A}, @var{opt})
@deftypefnx {Built-in Function} {[@var{U}, @var{S}] =} schur (@var{A}, @dots{})
@cindex Schur decomposition
Compute the Schur@tie{}decomposition of @var{A}
@tex
$$
 S = U^T A U
$$
@end tex
@ifnottex

@example
@code{@var{S} = @var{U}' * @var{A} * @var{U}}
@end example

@end ifnottex
where @var{U} is a unitary matrix
@tex
($U^T U$ is identity)
@end tex
@ifnottex
(@code{@var{U}'* @var{U}} is identity)
@end ifnottex
and @var{S} is upper triangular.  The eigenvalues of @var{A} (and @var{S})
are the diagonal elements of @var{S}.  If the matrix @var{A}
is real, then the real Schur@tie{}decomposition is computed, in which the
matrix @var{U} is orthogonal and @var{S} is block upper triangular
with blocks of size at most
@tex
$2 \times 2$
@end tex
@ifnottex
@code{2 x 2}
@end ifnottex
along the diagonal.  The diagonal elements of @var{S}
(or the eigenvalues of the
@tex
$2 \times 2$
@end tex
@ifnottex
@code{2 x 2}
@end ifnottex
blocks, when appropriate) are the eigenvalues of @var{A} and @var{S}.

The default for real matrices is a real Schur@tie{}decomposition.
A complex decomposition may be forced by passing the flag
@qcode{"complex"}.

The eigenvalues are optionally ordered along the diagonal according to
the value of @var{opt}.  @code{@var{opt} = "a"} indicates that all
eigenvalues with negative real parts should be moved to the leading
block of @var{S}
(used in @code{are}), @code{@var{opt} = "d"} indicates that all eigenvalues
with magnitude less than one should be moved to the leading block of @var{S}
(used in @code{dare}), and @code{@var{opt} = "u"}, the default, indicates
that no ordering of eigenvalues should occur.  The leading @var{k}
columns of @var{U} always span the @var{A}-invariant
subspace corresponding to the @var{k} leading eigenvalues of @var{S}.

The Schur@tie{}decomposition is used to compute eigenvalues of a
square matrix, and has applications in the solution of algebraic
Riccati equations in control (see @code{are} and @code{dare}).
@seealso{@ref{XREFrsf2csf,,rsf2csf}, @ref{XREFlu,,lu}, @ref{XREFchol,,chol}, @ref{XREFhess,,hess}, @ref{XREFqr,,qr}, @ref{XREFqz,,qz}, @ref{XREFsvd,,svd}}
@end deftypefn


@c rsf2csf libinterp/corefcn/schur.cc
@anchor{XREFrsf2csf}
@deftypefn {Function File} {[@var{U}, @var{T}] =} rsf2csf (@var{UR}, @var{TR})
Convert a real, upper quasi-triangular Schur@tie{}form @var{TR} to a complex,
upper triangular Schur@tie{}form @var{T}.

Note that the following relations hold:

@tex
$UR \cdot TR \cdot {UR}^T = U T U^{\dagger}$ and
$U^{\dagger} U$ is the identity matrix I.
@end tex
@ifnottex
@tcode{@var{UR} * @var{TR} * @var{UR}' = @var{U} * @var{T} * @var{U}'} and
@code{@var{U}' * @var{U}} is the identity matrix I.
@end ifnottex

Note also that @var{U} and @var{T} are not unique.
@seealso{@ref{XREFschur,,schur}}
@end deftypefn


@c subspace scripts/linear-algebra/subspace.m
@anchor{XREFsubspace}
@deftypefn {Function File} {@var{angle} =} subspace (@var{A}, @var{B})
Determine the largest principal angle between two subspaces
spanned by the columns of matrices @var{A} and @var{B}.
@end deftypefn


@c svd libinterp/corefcn/svd.cc
@anchor{XREFsvd}
@deftypefn  {Built-in Function} {@var{s} =} svd (@var{A})
@deftypefnx {Built-in Function} {[@var{U}, @var{S}, @var{V}] =} svd (@var{A})
@deftypefnx {Built-in Function} {[@var{U}, @var{S}, @var{V}] =} svd (@var{A}, @var{econ})
@cindex singular value decomposition
Compute the singular value decomposition of @var{A}
@tex
$$
 A = U S V^{\dagger}
$$
@end tex
@ifnottex

@example
A = U*S*V'
@end example

@end ifnottex

The function @code{svd} normally returns only the vector of singular values.
When called with three return values, it computes
@tex
$U$, $S$, and $V$.
@end tex
@ifnottex
@var{U}, @var{S}, and @var{V}.
@end ifnottex
For example,

@example
svd (hilb (3))
@end example

@noindent
returns

@example
@group
ans =

  1.4083189
  0.1223271
  0.0026873
@end group
@end example

@noindent
and

@example
[u, s, v] = svd (hilb (3))
@end example

@noindent
returns

@example
@group
u =

  -0.82704   0.54745   0.12766
  -0.45986  -0.52829  -0.71375
  -0.32330  -0.64901   0.68867

s =

  1.40832  0.00000  0.00000
  0.00000  0.12233  0.00000
  0.00000  0.00000  0.00269

v =

  -0.82704   0.54745   0.12766
  -0.45986  -0.52829  -0.71375
  -0.32330  -0.64901   0.68867
@end group
@end example

If given a second argument, @code{svd} returns an economy-sized
decomposition, eliminating the unnecessary rows or columns of @var{U} or
@var{V}.
@seealso{@ref{XREFsvd_driver,,svd_driver}, @ref{XREFsvds,,svds}, @ref{XREFeig,,eig}, @ref{XREFlu,,lu}, @ref{XREFchol,,chol}, @ref{XREFhess,,hess}, @ref{XREFqr,,qr}, @ref{XREFqz,,qz}}
@end deftypefn


@c svd_driver libinterp/corefcn/svd.cc
@anchor{XREFsvd_driver}
@deftypefn  {Built-in Function} {@var{val} =} svd_driver ()
@deftypefnx {Built-in Function} {@var{old_val} =} svd_driver (@var{new_val})
@deftypefnx {Built-in Function} {} svd_driver (@var{new_val}, "local")
Query or set the underlying @sc{lapack} driver used by @code{svd}.
Currently recognized values are @qcode{"gesvd"} and @qcode{"gesdd"}.  
The default is @qcode{"gesvd"}.

When called from inside a function with the @qcode{"local"} option, the
variable is changed locally for the function and any subroutines it calls.  
The original variable value is restored when exiting the function.
@seealso{@ref{XREFsvd,,svd}}
@end deftypefn


@c FIXME -- should there be a new section here?

@c housh scripts/linear-algebra/housh.m
@anchor{XREFhoush}
@deftypefn {Function File} {[@var{housv}, @var{beta}, @var{zer}] =} housh (@var{x}, @var{j}, @var{z})
Compute Householder reflection vector @var{housv} to reflect @var{x}
to be the j-th column of identity, i.e.,

@example
@group
(I - beta*housv*housv')x =  norm (x)*e(j) if x(j) < 0,
(I - beta*housv*housv')x = -norm (x)*e(j) if x(j) >= 0
@end group
@end example

@noindent
Inputs

@table @var
@item x
vector

@item j
index into vector

@item z
threshold for zero  (usually should be the number 0)
@end table

@noindent
Outputs (see Golub and Van Loan):

@table @var
@item beta
If beta = 0, then no reflection need be applied (@nospell{zer} set to 0)

@item housv
householder vector
@end table
@end deftypefn


@c krylov scripts/linear-algebra/krylov.m
@anchor{XREFkrylov}
@deftypefn {Function File} {[@var{u}, @var{h}, @var{nu}] =} krylov (@var{A}, @var{V}, @var{k}, @var{eps1}, @var{pflg})
Construct an orthogonal basis @var{u} of block Krylov subspace

@example
[v a*v a^2*v @dots{} a^(k+1)*v]
@end example

@noindent
Using Householder reflections to guard against loss of orthogonality.

If @var{V} is a vector, then @var{h} contains the Hessenberg matrix
such that @nospell{@tcode{a*u == u*h+rk*ek'}}, in which @code{rk =
a*u(:,k)-u*h(:,k)}, and @nospell{@tcode{ek'}} is the vector
@code{[0, 0, @dots{}, 1]} of length @code{k}.  Otherwise, @var{h} is
meaningless.

If @var{V} is a vector and @var{k} is greater than
@code{length (A) - 1}, then @var{h} contains the Hessenberg matrix such
that @code{a*u == u*h}.

The value of @var{nu} is the dimension of the span of the Krylov
subspace (based on @var{eps1}).

If @var{b} is a vector and @var{k} is greater than @var{m-1}, then
@var{h} contains the Hessenberg decomposition of @var{A}.

The optional parameter @var{eps1} is the threshold for zero.  The
default value is 1e-12.

If the optional parameter @var{pflg} is nonzero, row pivoting is used
to improve numerical behavior.  The default value is 0.

Reference: A. Hodel, P. Misra, @cite{Partial Pivoting in the Computation of
Krylov Subspaces of Large Sparse Systems}, Proceedings of the 42nd IEEE
Conference on Decision and Control, December 2003.
@end deftypefn


@node Functions of a Matrix
@section Functions of a Matrix
@cindex matrix, functions of

@c expm scripts/linear-algebra/expm.m
@anchor{XREFexpm}
@deftypefn {Function File} {} expm (@var{A})
Return the exponential of a matrix, defined as the infinite Taylor
series
@tex
$$
 \exp (A) = I + A + {A^2 \over 2!} + {A^3 \over 3!} + \cdots
$$
@end tex
@ifnottex

@example
expm (A) = I + A + A^2/2! + A^3/3! + @dots{}
@end example

@end ifnottex
The Taylor series is @emph{not} the way to compute the matrix
exponential; see Moler and Van Loan, @cite{Nineteen Dubious Ways to
Compute the Exponential of a Matrix}, SIAM Review, 1978.  This routine
uses Ward's diagonal Pad@'e approximation method with three step
preconditioning (SIAM Journal on Numerical Analysis, 1977).  Diagonal
Pad@'e approximations are rational polynomials of matrices
@tex
$D_q(A)^{-1}N_q(A)$
@end tex
@ifnottex

@example
@group
     -1
D (A)   N (A)
@end group
@end example

@end ifnottex
whose Taylor series matches the first
@tex
$2 q + 1 $
@end tex
@ifnottex
@code{2q+1}
@end ifnottex
terms of the Taylor series above; direct evaluation of the Taylor series
(with the same preconditioning steps) may be desirable in lieu of the
Pad@'e approximation when
@tex
$D_q(A)$
@end tex
@ifnottex
@code{Dq(A)}
@end ifnottex
is ill-conditioned.
@seealso{@ref{XREFlogm,,logm}, @ref{XREFsqrtm,,sqrtm}}
@end deftypefn


@c logm scripts/linear-algebra/logm.m
@anchor{XREFlogm}
@deftypefn  {Function File} {@var{s} =} logm (@var{A})
@deftypefnx {Function File} {@var{s} =} logm (@var{A}, @var{opt_iters})
@deftypefnx {Function File} {[@var{s}, @var{iters}] =} logm (@dots{})
Compute the matrix logarithm of the square matrix @var{A}.  The
implementation utilizes a Pad@'e approximant and the identity

@example
logm (@var{A}) = 2^k * logm (@var{A}^(1 / 2^k))
@end example

The optional argument @var{opt_iters} is the maximum number of square roots
to compute and defaults to 100.  The optional output @var{iters} is the
number of square roots actually computed.
@seealso{@ref{XREFexpm,,expm}, @ref{XREFsqrtm,,sqrtm}}
@end deftypefn


@c sqrtm libinterp/corefcn/sqrtm.cc
@anchor{XREFsqrtm}
@deftypefn  {Built-in Function} {@var{s} =} sqrtm (@var{A})
@deftypefnx {Built-in Function} {[@var{s}, @var{error_estimate}] =} sqrtm (@var{A})
Compute the matrix square root of the square matrix @var{A}.

Ref: N.J. Higham.  @cite{A New sqrtm for @sc{matlab}}.  Numerical
Analysis Report No. 336, Manchester @nospell{Centre} for Computational
Mathematics, Manchester, England, January 1999.
@seealso{@ref{XREFexpm,,expm}, @ref{XREFlogm,,logm}}
@end deftypefn


@c kron libinterp/corefcn/kron.cc
@anchor{XREFkron}
@deftypefn  {Built-in Function} {} kron (@var{A}, @var{B})
@deftypefnx {Built-in Function} {} kron (@var{A1}, @var{A2}, @dots{})
Form the Kronecker product of two or more matrices, defined block by 
block as

@example
x = [ a(i,j)*b ]
@end example

For example:

@example
@group
kron (1:4, ones (3, 1))
     @result{}  1  2  3  4
         1  2  3  4
         1  2  3  4
@end group
@end example

If there are more than two input arguments @var{A1}, @var{A2}, @dots{}, 
@var{An} the Kronecker product is computed as

@example
kron (kron (@var{A1}, @var{A2}), @dots{}, @var{An})
@end example

@noindent
Since the Kronecker product is associative, this is well-defined.
@end deftypefn


@c blkmm libinterp/corefcn/dot.cc
@anchor{XREFblkmm}
@deftypefn {Built-in Function} {} blkmm (@var{A}, @var{B})
Compute products of matrix blocks.  The blocks are given as
2-dimensional subarrays of the arrays @var{A}, @var{B}.
The size of @var{A} must have the form @code{[m,k,@dots{}]} and
size of @var{B} must be @code{[k,n,@dots{}]}.  The result is
then of size @code{[m,n,@dots{}]} and is computed as follows:

@example
@group
for i = 1:prod (size (@var{A})(3:end))
  @var{C}(:,:,i) = @var{A}(:,:,i) * @var{B}(:,:,i)
endfor
@end group
@end example
@end deftypefn


@c syl libinterp/corefcn/syl.cc
@anchor{XREFsyl}
@deftypefn {Built-in Function} {@var{x} =} syl (@var{A}, @var{B}, @var{C})
Solve the Sylvester equation
@tex
$$
 A X + X B + C = 0
$$
@end tex
@ifnottex

@example
A X + X B + C = 0
@end example

@end ifnottex
using standard @sc{lapack} subroutines.  For example:

@example
@group
syl ([1, 2; 3, 4], [5, 6; 7, 8], [9, 10; 11, 12])
   @result{} [ -0.50000, -0.66667; -0.66667, -0.50000 ]
@end group
@end example
@end deftypefn


@node Specialized Solvers
@section Specialized Solvers
@cindex matrix, specialized solvers

@c bicg scripts/sparse/bicg.m
@anchor{XREFbicg}
@deftypefn  {Function File} {@var{x} =} bicg (@var{A}, @var{b}, @var{rtol}, @var{maxit}, @var{M1}, @var{M2}, @var{x0})
@deftypefnx {Function File} {@var{x} =} bicg (@var{A}, @var{b}, @var{rtol}, @var{maxit}, @var{P})
@deftypefnx {Function File} {[@var{x}, @var{flag}, @var{relres}, @var{iter}, @var{resvec}] =} bicg (@var{A}, @var{b}, @dots{})
Solve @code{A x = b} using the Bi-conjugate gradient iterative method.

@itemize @minus
@item @var{rtol} is the relative tolerance, if not given
or set to [] the default value 1e-6 is used.

@item @var{maxit} the maximum number of outer iterations,
if not given or set to [] the default value
@code{min (20, numel (b))} is used.

@item @var{x0} the initial guess, if not given or set to []
the default value @code{zeros (size (b))} is used.
@end itemize

@var{A} can be passed as a matrix or as a function handle or
inline function @code{f} such that @code{f(x, "notransp") = A*x}
and @code{f(x, "transp") = A'*x}.

The preconditioner @var{P} is given as @code{P = M1 * M2}.
Both @var{M1} and @var{M2} can be passed as a matrix or as
a function handle or inline function @code{g} such that
@code{g(x, "notransp") = M1 \ x} or @code{g(x, "notransp") = M2 \ x} and
@code{g(x, "transp") = M1' \ x} or @code{g(x, "transp") = M2' \ x}.

If called with more than one output parameter

@itemize @minus
@item @var{flag} indicates the exit status:

@itemize @minus
@item 0: iteration converged to the within the chosen tolerance

@item 1: the maximum number of iterations was reached before convergence

@item 3: the algorithm reached stagnation
@end itemize

(the value 2 is unused but skipped for compatibility).

@item @var{relres} is the final value of the relative residual.

@item @var{iter} is the number of iterations performed.

@item @var{resvec} is a vector containing the relative residual at each iteration.
@end itemize

@seealso{@ref{XREFbicgstab,,bicgstab}, @ref{XREFcgs,,cgs}, @ref{XREFgmres,,gmres}, @ref{XREFpcg,,pcg}}

@end deftypefn


@c bicgstab scripts/sparse/bicgstab.m
@anchor{XREFbicgstab}
@deftypefn  {Function File} {@var{x} =} bicgstab (@var{A}, @var{b}, @var{rtol}, @var{maxit}, @var{M1}, @var{M2}, @var{x0})
@deftypefnx {Function File} {@var{x} =} bicgstab (@var{A}, @var{b}, @var{rtol}, @var{maxit}, @var{P})
@deftypefnx {Function File} {[@var{x}, @var{flag}, @var{relres}, @var{iter}, @var{resvec}] =} bicgstab (@var{A}, @var{b}, @dots{})
Solve @code{A x = b} using the stabilizied Bi-conjugate gradient iterative
method.

@itemize @minus
@item @var{rtol} is the relative tolerance, if not given or set to
[] the default value 1e-6 is used.

@item @var{maxit} the maximum number of outer iterations, if not
given or set to [] the default value @code{min (20, numel (b))} is
used.

@item @var{x0} the initial guess, if not given or set to [] the
default value @code{zeros (size (b))} is used.
@end itemize

@var{A} can be passed as a matrix or as a function handle or
inline function @code{f} such that @code{f(x) = A*x}.

The preconditioner @var{P} is given as @code{P = M1 * M2}.
Both @var{M1} and @var{M2} can be passed as a matrix or as a function
handle or inline function @code{g} such that @code{g(x) = M1 \ x} or
@code{g(x) = M2 \ x}.

If called with more than one output parameter

@itemize @minus
@item @var{flag} indicates the exit status:

@itemize @minus
@item 0: iteration converged to the within the chosen tolerance

@item 1: the maximum number of iterations was reached before convergence

@item 3: the algorithm reached stagnation
@end itemize

(the value 2 is unused but skipped for compatibility).

@item @var{relres} is the final value of the relative residual.

@item @var{iter} is the number of iterations performed.

@item @var{resvec} is a vector containing the relative residual at each iteration.
@end itemize

@seealso{@ref{XREFbicg,,bicg}, @ref{XREFcgs,,cgs}, @ref{XREFgmres,,gmres}, @ref{XREFpcg,,pcg}}

@end deftypefn


@c cgs scripts/sparse/cgs.m
@anchor{XREFcgs}
@deftypefn  {Function File} {@var{x} =} cgs (@var{A}, @var{b}, @var{rtol}, @var{maxit}, @var{M1}, @var{M2}, @var{x0})
@deftypefnx {Function File} {@var{x} =} cgs (@var{A}, @var{b}, @var{rtol}, @var{maxit}, @var{P})
@deftypefnx {Function File} {[@var{x}, @var{flag}, @var{relres}, @var{iter}, @var{resvec}] =} cgs (@var{A}, @var{b}, @dots{})
Solve @code{A x = b}, where @var{A} is a square matrix, using the
Conjugate Gradients Squared method.

@itemize @minus
@item @var{rtol} is the relative tolerance, if not given or set to []
the default value 1e-6 is used.

@item @var{maxit} the maximum number of outer iterations, if not
given or set to [] the default value @code{min (20, numel (b))} is
used.

@item @var{x0} the initial guess, if not given or set to [] the
default value @code{zeros (size (b))} is used.
@end itemize

@var{A} can be passed as a matrix or as a function handle or
inline function @code{f} such that @code{f(x) = A*x}.

The preconditioner @var{P} is given as @code{P = M1 * M2}.
Both @var{M1} and @var{M2} can be passed as a matrix or as a function
handle or inline function @code{g} such that @code{g(x) = M1 \ x} or
@code{g(x) = M2 \ x}.

If called with more than one output parameter

@itemize @minus
@item @var{flag} indicates the exit status:

@itemize @minus
@item 0: iteration converged to the within the chosen tolerance

@item 1: the maximum number of iterations was reached before convergence

@item 3: the algorithm reached stagnation
@end itemize

(the value 2 is unused but skipped for compatibility).

@item @var{relres} is the final value of the relative residual.

@item @var{iter} is the number of iterations performed.

@item @var{resvec} is a vector containing the relative residual at
each iteration.
@end itemize

@seealso{@ref{XREFpcg,,pcg}, @ref{XREFbicgstab,,bicgstab}, @ref{XREFbicg,,bicg}, @ref{XREFgmres,,gmres}}
@end deftypefn


@c gmres scripts/sparse/gmres.m
@anchor{XREFgmres}
@deftypefn  {Function File} {@var{x} =} gmres (@var{A}, @var{b}, @var{m}, @var{rtol}, @var{maxit}, @var{M1}, @var{M2}, @var{x0})
@deftypefnx {Function File} {@var{x} =} gmres (@var{A}, @var{b}, @var{m}, @var{rtol}, @var{maxit}, @var{P})
@deftypefnx {Function File} {[@var{x}, @var{flag}, @var{relres}, @var{iter}, @var{resvec}] =} gmres (@dots{})
Solve @code{A x = b} using the Preconditioned GMRES iterative method
with restart, a.k.a. PGMRES(m).

@itemize @minus
@item @var{rtol} is the relative tolerance,
if not given or set to [] the default value 1e-6 is used.

@item @var{maxit} is the maximum number of outer iterations,
if not given or set to [] the default value
@code{min (10, numel (b) / restart)} is used.

@item @var{x0} is the initial guess,
if not given or set to [] the default value @code{zeros (size (b))} is used.

@item @var{m} is the restart parameter,
if not given or set to [] the default value @code{numel (b)} is used.
@end itemize

Argument @var{A} can be passed as a matrix, function handle, or
inline function @code{f} such that @code{f(x) = A*x}.

The preconditioner @var{P} is given as @code{P = M1 * M2}.
Both @var{M1} and @var{M2} can be passed as a matrix, function handle, or
inline function @code{g} such that @code{g(x) = M1\x} or @code{g(x) = M2\x}.

Besides the vector @var{x}, additional outputs are:

@itemize @minus
@item @var{flag} indicates the exit status:

@table @asis
@item 0 : iteration converged to within the specified tolerance

@item 1 : maximum number of iterations exceeded

@item 2 : unused, but skipped for compatibility

@item 3 : algorithm reached stagnation (no change between iterations)
@end table

@item @var{relres} is the final value of the relative residual.

@item @var{iter} is a vector containing the number of outer iterations and
total iterations performed.

@item @var{resvec} is a vector containing the relative residual at each
iteration.
@end itemize

@seealso{@ref{XREFbicg,,bicg}, @ref{XREFbicgstab,,bicgstab}, @ref{XREFcgs,,cgs}, @ref{XREFpcg,,pcg}}
@end deftypefn

