[VB] - Matrix Operations (Multiply, Transposee)
Hi
I've search the forums and I didn't find any code for multiplying matrix. So here the code to do so (and to make the transposee)
Ps.: errors are catch at a higher level
How it work:
Calling them with the two matrix, and it returns the result. Both calling matrix must be with 2 dimensions or else it fell.
The transposee only switch (i,j) position of the calling matrix
VB Code:
' effectue la matrice transposée
Private Function transposeMatrix(theMatrix() As Double) As Double()
Dim i As Integer
Dim j As Integer
Dim matrixResult() As Double
' on redimensionne la matrix résultante au format précis
ReDim matrixResult(UBound(theMatrix, 2), UBound(theMatrix, 1)) As Double
For i = 0 To UBound(theMatrix, 1)
For j = 0 To UBound(theMatrix, 2)
matrixResult(j, i) = theMatrix(i, j)
Next j
Next i
transposeMatrix = matrixResult
End Function
Private Function multiplyMatrix(firstMatrix() As Double, _
secondMatrix() As Double) As Double()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim leTotal As Double
Dim matrixResult() As Double
' on vérifie que les 2 matrices peuvent bien être multiplié
If (UBound(firstMatrix, 2) <> UBound(secondMatrix, 1)) Then Exit Function
' on redimensionne la matrix résultante au format précis
ReDim matrixResult(UBound(firstMatrix, 1), UBound(secondMatrix, 2)) As Double
For i = 0 To UBound(firstMatrix, 1)
For j = 0 To UBound(secondMatrix, 2)
leTotal = 0
For k = 0 To UBound(firstMatrix, 2) ' égal à UBound(Secondmatrix,1)
leTotal = leTotal + firstMatrix(i, k) * secondMatrix(k, j)
Next k
matrixResult(i, j) = leTotal
Next j
Next i
multiplyMatrix = matrixResult
End Function
@+