Creates the inverse of a matrix and returns a reference to it.
MatrixObject.Inverse
Returns a matrix object reference to the inverse of the current matrix.
Inverse method does not alter the current matrix, rather it creates a new matrix and returns a reference to it.
Matrix inversion is only possible for square matrices.
Error handling is recommended, in order to trap errors that might arise from Inverse method, in case the current matrix is found to be a singular matrix.
Error 1305 will be returned if the matrix is not square.
Error 1306 will be returned if matrix inversion is not possible (i.e. singular matrix).
Private Sub MatrixInverse()
Dim A As Matrix, B As Matrix, C As Matrix
'Generate a matrix, fill it with random values
'and print it in debug window
Set A = New Matrix
A.Size 5, 5
A.FillRandom
Debug.Print "Matrix A = "
Debug.Print A.GetString
'Use Inverse method to get its inverse
On Error Resume Next
Set B = A.Inverse
If (Err.Number = (vbObjectError + 1306)) Then
Debug.Print "Matrix is singular"
Exit Sub
End If
On Error GoTo 0
Debug.Print "Matrix B = A.Inverse"
Debug.Print B.GetString
'Now verify the result by forming the product
' C = A x B. Matrix C is a unary matrix
Set C = A.Times(B)
Debug.Print "C = A x B "
Debug.Print C.GetString
End Sub
Matrix A = | 6.000 2.000 8.000 6.000 5.000 | | 4.000 9.000 8.000 7.000 2.000 | | 9.000 7.000 5.000 3.000 0.000 | | 1.000 4.000 1.000 2.000 10.000 | | 4.000 1.000 0.000 0.000 4.000 | Matrix B = A.Inverse | -0.048 0.176 -0.158 -0.235 0.560 | | -0.076 -0.069 0.198 0.174 -0.305 | | 0.410 -1.035 1.032 0.847 -2.112 | | -0.362 1.360 -1.374 -1.112 2.552 | | 0.067 -0.158 0.108 0.192 -0.233 | C = A x B | 1.000 0.000 0.000 0.000 0.000 | | 0.000 1.000 0.000 0.000 0.000 | | 0.000 0.000 1.000 0.000 0.000 | | 0.000 0.000 0.000 1.000 0.000 | | 0.000 0.000 0.000 0.000 1.000 |