Multiplication of two matrix in vb.net

Vb program for Multiplication of two matrix. Here mentioned other language solution.
' Include namespace system
Imports System
' Vb.net program for
' Two matrix multiplication
public Class Multiply
' Display the element of given 2d matrix
Public Shared Sub printRecord(ByVal matrix As Integer(,))
Console.WriteLine(" --------------")
' Assume N x N Matrix size
Dim row As Integer = matrix.GetLength(0)
Dim col As Integer = matrix.GetLength(1)
' Iterate the row element
Dim i As Integer = 0
While i < row
' Iterate the column element
Dim j As Integer = 0
While j < col
' Display element value
Console.Write(" {0}", matrix(i,j))
j += 1
End While
' Add new line
Console.WriteLine()
i += 1
End While
Console.WriteLine()
End Sub
Public Shared Sub multiplication(ByVal a As Integer(,),
ByVal b As Integer(,))
' Get the size
Dim row As Integer = a.GetLength(0)
Dim col As Integer = a.GetLength(1)
' This matrix are store the result of multiplication
Dim result As Integer(,) = New Integer(row-1,col-1){}
Dim i As Integer = 0
While i < row
Dim j As Integer = 0
While j < col
' Set the initial value of new matrix element
result(i,j) = 0
Dim k As Integer = 0
While k < row
' Multiply matrix A [i] row and [k] columns to
' the Matrix B [k] columns and [j] rows.
result(i,j) += a(i,k) * b(k,j)
k += 1
End While
j += 1
End While
i += 1
End While
Console.WriteLine(" Matrix A")
' Print element of matrix x
printRecord(a)
Console.WriteLine(" Matrix B")
' Print element of matrix y
printRecord(b)
Console.WriteLine(" Matrix [(A) x (B)]")
' Display resultant matrix
printRecord(result)
End Sub
Public Shared Sub Main(ByVal args As String())
' Define matrix A
Dim a As Integer(,) =
{
{1, 2, 3},
{6, 1, 2},
{5, 4, 3}
}
' Define matrix B
Dim b As Integer(,) =
{
{3, 1, 3},
{1, 1, 2},
{2, 2, 3}
}
multiplication(a, b)
End Sub
End Class
Output
Matrix A
--------------
1 2 3
6 1 2
5 4 3
Matrix B
--------------
3 1 3
1 1 2
2 2 3
Matrix [(A) x (B)]
--------------
11 9 16
23 11 26
25 15 32
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment