The INV function computes the inverse of a square and nonsingular matrix.
For the inverse has the properties
To solve a system of linear equations for , you can use the expression x = inv(a)*b
. However, the SOLVE function
is more accurate and efficient for this task.
The following statements compute a matrix inverse and solve a linear system:
A = {0 0 1 0 1, 1 0 0 1 0, 0 1 1 0 1, 1 0 0 0 1, 0 1 0 1 0}; b = {9, 4, 10, 8, 2}; /* find inverse and solve linear system */ Ainv = inv(A); x1 = Ainv*b; /* solve by using a more efficient algorithm */ x2 = solve(A,b); print x1 x2;
The INV function uses an LU decomposition followed by back substitution to solve for the inverse, as described in Forsythe, Malcom, and Moler (1967).
The INV function (in addition to the DET and SOLVE functions ) uses the following criterion to decide whether the input matrix, , is singular:
where MACHEPS is the relative machine precision.
All matrix elements less than or equal to sing are considered rounding errors of the largest matrix elements, so they are taken to be zero in subsequent computations. For example, if a diagonal or triangular coefficient matrix has a diagonal value that is less than or equal to sing, the matrix is considered singular by the DET, INV, and SOLVE functions.
The criterion is used by some functions to detect a singular matrix and to abort a computation that cannot be performed on a singular matrix. The typical error message is as follows:
ERROR: (execution) Matrix should be non-singular.
If you are getting this error message but believe that your matrix is actually nonsingular, you can try one of the following:
Center and scale the data.
Use the GINV function to compute the generalized inverse.
Examine the size of the singular values returned by the SVD call . The SVD call can be used to compute a generalized inverse with a user-specified singularity criterion.
If is an matrix, the INV function allocates an matrix in order to return the inverse. It also temporarily allocates an array in order to compute the inverse.