Plus and PlusEquals methods can be used for matrix addition. Unlike the Plus Method, the PlusEquals method does not create a new matrix but instead adds to current matrix elements.
In the same way, Minus and MinusEquals methods can be used for matrix subtraction.
Matrix addition and subtraction is possible only between matrices of the same size.
Times method performs matrix multiplication. TimesScalar multiplies matrix elements by a scalar value.
'Declare 2 matrices and fill them with random values Dim A As Matrix, B As Matrix Set A = New Matrix A.Size 4, 4 A.FillRandom Set B = New Matrix B.Size 4, 4 B.FillRandom 'Print the result A + B Debug.Print "A + B =" Debug.Print A.Plus(B).GetString 'Print the result A - B Debug.Print "A - B =" Debug.Print A.Minus(B).GetString 'Print the result A * B Debug.Print "A * B =" Debug.Print A.Times(B).GetString
Inverse and Transpose methods return the matrix inverse and transpose respectively. Not all matrices are invertible. It is a good practice to use error handling to trap the error that would result from this situation.
On Error Resume Next
Set B = A.Inverse
If Err.Number = 1306 + vbObjectError Then
'put your error handling code here
Debug.Print "Singular Matrix"
End If
Debug.Print "Inverse of A = "
Debug.Print A.GetString
Using the Plus, Minus, Times, TimesScalar, Inverse and Transpose methods leaves original matrix unaffected. This gives you the choice to preserve the original matrix for later use or to replace current instance:
Set B = A.Inverse 'Matrix B contains the inverse of matrix A, while A remains unaffected. Set A = A.Inverse 'Matrix variable A now contains the inverse of the original matrix.
Finally the Determinant method computes and returns the determinant of current matrix.
Debug.Print A.Determinant
Referencing ShortName 3.1 | Declaring Matrices | Matrix Properties and Methods | Matrices and Arrays | Eigenvalues and Eigenvectors