Multiply Matrices

This algorithm multiplies two given matrices



									Public Structure Matrix
	Public _Matrix As Integer(,)
	Public Rows As Integer
	Public Columns As Integer

	Public Sub New(matrix As Integer(,), rows As Integer, columns As Integer)
		Me._Matrix = matrix
		Me.Rows = rows
		Me.Columns = columns
	End Sub
End Structure

Public Shared Function MultiplyMatrices(firstMatrix As Matrix, secondMatrix As Matrix) As Matrix
	Dim mult As Integer(,) = New Integer(firstMatrix.Rows - 1, secondMatrix.Columns - 1) {}

	If firstMatrix.Columns <> secondMatrix.Rows Then
		Return Nothing
	End If

	For i As Integer = 0 To firstMatrix.Rows - 1
		For j As Integer = 0 To secondMatrix.Columns - 1
			mult(i, j) = 0
		Next
	Next

	For i As Integer = 0 To firstMatrix.Rows - 1
		For j As Integer = 0 To secondMatrix.Columns - 1
			For k As Integer = 0 To firstMatrix.Columns - 1
				mult(i, j) += firstMatrix._Matrix(i, k) * secondMatrix._Matrix(k, j)
			Next
		Next
	Next

	Return New Matrix(mult, firstMatrix.Rows, secondMatrix.Columns)
End Function
								


Example

									Dim m1 As Integer(,) = New Integer(1, 2) {}
m1(0, 0) = 3
m1(0, 1) = -2
m1(0, 2) = 5
m1(1, 0) = 3
m1(1, 1) = 0
m1(1, 2) = 4

Dim m2 As Integer(,) = New Integer(2, 1) {}
m2(0, 0) = 2
m2(0, 1) = 3
m2(1, 0) = -9
m2(1, 1) = 0
m2(2, 0) = 0
m2(2, 1) = 4

Dim matrix1 As New Matrix(m1, 2, 3)
Dim matrix2 As New Matrix(m2, 3, 2)
Dim sumMatrix As Matrix = MultiplyMatrices(matrix1, matrix2)
								


Output

									(0, 0) = 24
(0, 1) = 29
(1, 0) = 6
(1, 1) = 25