### 18.2 Basic Matrix Functions

— Loadable Function: AA = balance (A)
— Loadable Function: AA = balance (A, opt)
— Loadable Function: [DD, AA] = balance (A, opt)
— Loadable Function: [D, P, AA] = balance (A, opt)
— Loadable Function: [CC, DD, AA, BB] = balance (A, B, opt)

> Compute AA`=` DD`\` A`*` DD in which AA is a matrix whose row and column norms are roughly equal in magnitude, and DD`=` P`*` D, in which P is a permutation matrix and 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, `balance` returns the diagonal D and the permutation P separately as vectors. In this case, DD`= eye(n)(:,`P`) * diag (`D`)`, where n is the matrix size.

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

> The eigenvalue balancing option opt may be one of:

> "noperm", "S"
>   Scale only; do not permute.
> "noscal", "P"
>   Permute only; do not scale.

> Algebraic eigenvalue balancing uses standard lapack routines.

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

— Function File: cond (A)
— Function File: cond (A, p)

> Compute the p-norm condition number of a matrix.

> `cond (`A`)` is ## defined as `norm (`A`,` p`) * norm (inv (`A`),` p`)`.

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

> See also: condest, rcond, norm, svd.

— Loadable Function: det (A)
— Loadable Function: [d, rcond] = det (A)

> Compute the determinant of A.

> Return an estimate of the reciprocal condition number if requested.

> Routines from lapack are used for full matrices and code from 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: `cond`, `condest`, `rcond`.

> See also: cond, condest, rcond.

— Loadable Function: lambda = eig (A)
— Loadable Function: lambda = eig (A, B)
— Loadable Function: [V, lambda] = eig (A)
— Loadable Function: [V, lambda] = eig (A, B)

> Compute the eigenvalues and eigenvectors of a matrix.

> Eigenvalues are computed in a several step process which begins with a Hessenberg decomposition, followed by a Schur decomposition, from which the eigenvalues are apparent. The eigenvectors, when desired, are computed by further manipulations of the Schur decomposition.

> The eigenvalues returned by `eig` are not ordered.

> See also: eigs, svd.

— Loadable Function: g = givens (x, y)
— Loadable Function: [c, s] = givens (x, y)

> Return a 2 by 2 orthogonal matrix g`= [`c s`; -`s`'` c`]` such that g`[`x`;` y`] = [*; 0]` with x and y scalars.

> For example:

>               givens (1, 1)
>                    ⇒   0.70711   0.70711
>                        -0.70711   0.70711

— Function File: [g, y] = planerot (x)

> Given a two-element column vector, returns the 2 by 2 orthogonal matrix G such that y`=` g`*` x and y`(2) = 0`.

> See also: givens.

— Loadable Function: x = inv (A)
— Loadable Function: [x, rcond] = inv (A)

> Compute the inverse of the square matrix 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 (A*x = b) with y`=` A`\ b`, rather than y`= inv (`A`) * b`.

> If called with a sparse matrix, then in general x will be a full matrix requiring significantly more storage. Avoid forming the inverse of a sparse matrix if possible.

> See also: ldivide, rdivide.

— Loadable Function: type = matrix_type (A)
— Loadable Function: type = matrix_type (A, 'nocompute')
— Loadable Function: A = matrix_type (A, type)
— Loadable Function: A = matrix_type (A, 'upper', perm)
— Loadable Function: A = matrix_type (A, 'lower', perm)
— Loadable Function: A = matrix_type (A, 'banded', nl, nu)

> Identify the matrix type or mark a matrix as a particular type. This allows more rapid solutions of linear equations involving A to be performed. Called with a single argument, `matrix_type` returns the type of the matrix and caches it for future use. Called with more than one argument, `matrix_type` allows the type of the matrix to be defined.

> If the option '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

> 'unknown'
>   Remove any previously cached matrix type, and mark type as unknown.
> 'full'
>   Mark the matrix as full.
> 'positive definite'
>   Probable full positive definite matrix.
> 'diagonal'
>   Diagonal matrix. (Sparse matrices only)
> '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)
> 'upper'
>   Upper triangular. If the optional third argument perm is given, the matrix is assumed to be a permuted upper triangular with the permutations defined by the vector perm.
> 'lower'
>   Lower triangular. If the optional third argument perm is given, the matrix is assumed to be a permuted lower triangular with the permutations defined by the vector perm.
> 'banded'
> 'banded positive definite'
>   Banded matrix with the band size of nl below the diagonal and nu above it. If nl and 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)
> 'singular'
>   The matrix is assumed to be singular and will be treated with a minimum norm solution.

> Note that the matrix type will be discovered automatically on the first attempt to solve a linear equation involving A. Therefore `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 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 factorization is first attempted, and if that fails the matrix is then treated with an LU factorization. Once the matrix has been factorized, `matrix_type` will return the correct classification of the matrix.

— Built-in Function: norm (A)
— Built-in Function: norm (A, p)
— Built-in Function: norm (A, p, opt)

> Compute the p-norm of the matrix A. If the second argument is missing, `p = 2` is assumed.

> If A is a matrix (or sparse matrix):

> p = `1`
>   1-norm, the largest column sum of the absolute values of A.
> p = `2`
>   Largest singular value of A.
> p = `Inf` or `"inf"`
>   Infinity norm, the largest row sum of the absolute values of A.
> p = `"fro"`
>   Frobenius norm of A, `sqrt (sum (diag (`A`' *` A`)))`.
> other p, p`> 1`
>   maximum `norm (A*x, p)` such that `norm (x, p) == 1`

> If A is a vector or a scalar:

> p = `Inf` or `"inf"`
>   `max (abs (`A`))`.
> p = `-Inf`
>   `min (abs (`A`))`.
> p = `"fro"`
>   Frobenius norm of A, `sqrt (sumsq (abs (A)))`.
> p = 0
>   Hamming norm - the number of nonzero elements.
> other p, p`> 1`
>   p-norm of A, `(sum (abs (`A`) .^` p`)) ^ (1/`p`)`.
> other p p`< 1`
>   the p-pseudonorm defined as above.

> If opt is the value `"rows"`, treat each row as a vector and compute its norm. The result is returned as a column vector. Similarly, if opt is `"columns"` or `"cols"` then compute the norms of each column and return a row vector.

> See also: cond, svd.

— Function File: null (A)
— Function File: null (A, tol)

> Return an orthonormal basis of the null space of A.

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

>               max (size (A)) * max (svd (A)) * eps

> See also: orth.

— Function File: orth (A)
— Function File: orth (A, tol)

> Return an orthonormal basis of the range space of A.

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

>               max (size (A)) * max (svd (A)) * eps

> See also: null.

— Loadable Function: [y, h] = mgorth (x, v)

> Orthogonalize a given column vector x with respect to a given orthonormal basis v using a modified Gram-Schmidt orthogonalization. On exit, y is a unit vector such that:

>                 norm (y) = 1
>                 v' * y = 0
>                 x = h*[v, y]

— Loadable Function: pinv (x)
— Loadable Function: pinv (x, tol)

> Return the pseudoinverse of x. Singular values less than tol are ignored.

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

>               tol = max (size (x)) * sigma_max (x) * eps,

> where `sigma_max (`x`)` is the maximal singular value of x.

— Function File: rank (A)
— Function File: rank (A, tol)

> Compute the rank of A, using the singular value decomposition. The rank is taken to be the number of singular values of A that are greater than the specified tolerance tol. If the second argument is omitted, it is taken to be

>               tol = max (size (A)) * sigma(1) * eps;

> where `eps` is machine precision and `sigma(1)` is the largest singular value of A.

— Loadable Function: c = rcond (A)

> Compute the 1-norm estimate of the reciprocal condition number as returned by lapack. If the matrix is well-conditioned then c will be near 1 and if the matrix is poorly conditioned it will be close to zero.

> The matrix A must not be sparse. If the matrix is sparse then `condest (`A`)` or `rcond (full (`A`))` should be used instead.

> See also: cond, condest.

— Function File: trace (A)

> Compute the trace of A, `sum (diag (`A`))`.

— Function File: rref (A)
— Function File: rref (A, tol)
— Function File: [r, k] = rref (...)

> Return the reduced row echelon form of A. tol defaults to `eps * max (size (`A`)) * norm (`A`, inf)`.

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