INV Function

INV (matrix) ;

The INV function computes the inverse of a square and nonsingular matrix.

For $\mb {G} = \mbox{INV}(\mb {A})$ the inverse has the properties

\[  \mb {GA}= \mb {AG} = \mbox{identity}  \]

To solve a system of linear equations $\mb {AX}=\mb {B}$ for $\mb {X}$, 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;

Figure 24.168: Solving a Linear System

x1 x2
3 3
1 1
4 4
1 1
5 5


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, $\mathbf{A} = [a_{ij}]_{i,j=1,\ldots ,n}$, is singular:

\[  \mbox{sing} = 100 \times \mbox{MACHEPS} \times \max _{1 \leq i,j \leq n} |a_{ij}|  \]

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 $\mb {A}$ is an $n\times n$ matrix, the INV function allocates an $n\times n$ matrix in order to return the inverse. It also temporarily allocates an $n^2$ array in order to compute the inverse.