How to implement matrix multiplication in VB.NET

2 Answers

0 votes
Imports System

Module Module1

    Sub Main()

        Dim a(,) As Integer = New Integer(,) {{1, 8, 5}, {6, 7, 1}, {8, 7, 6}}
        Dim b(,) As Integer = New Integer(,) {{4, 8, 1}, {6, 5, 3}, {4, 6, 5}}
        Dim bound0 As Integer = a.GetUpperBound(0)
        Dim bound1 As Integer = a.GetUpperBound(1)
        Dim c(bound0, bound1) As Integer

        Print(a)
        Console.WriteLine()
        Print(b)
        Console.WriteLine()

        '// c[0, 0] = (a[0, 0] * b[0, 0]) + (a[0, 1] * b[1, 0]) + (a[0, 2] * b[2, 0])

        For i As Integer = 0 To a.GetUpperBound(0)
            For j As Integer = 0 To a.GetUpperBound(1)
                c(i, j) = Calc(a, b, i, j)
            Next
        Next
        Print(c)

    End Sub

    Sub Print(arr2d(,) As Integer)
        For i As Integer = 0 To arr2d.GetUpperBound(0)
            For j As Integer = 0 To arr2d.GetUpperBound(1)
                Console.Write("{0,4}", arr2d(i, j))
            Next
            Console.WriteLine()
        Next
    End Sub

    Function Calc(a(,) As Integer, b(,) As Integer, i As Integer, j As Integer) As Integer
        Dim sum As Integer = 0

        For x As Integer = 0 To a.GetUpperBound(0)
            sum = sum + a(i, x) * b(x, j)
        Next

        Return sum
    End Function

End Module



' run:
' 
'   1   8   5
'   6   7   1
'   8   7   6

'   4   8   1
'   6   5   3
'   4   6   5

'  72  78  50
'  70  89  32
'  98 135  59
'

 



answered Feb 28, 2016 by avibootz
edited 3 days ago by avibootz
0 votes
Imports System

Module Module1
 
    Sub Main()
 
        Dim a(,) As Integer = New Integer(,) {{1, 8, 5}, {6, 7, 1}, {8, 7, 6}}
        Dim b(,) As Integer = New Integer(,) {{4, 8, 1}, {6, 5, 3}, {4, 6, 5}}
        Dim bound0 As Integer = a.GetUpperBound(0)
        Dim bound1 As Integer = a.GetUpperBound(1)
        Dim c(bound0, bound1) As Integer
 
        Print(a)
        Console.WriteLine()
        Print(b)
        Console.WriteLine()
 
        '// c[0, 0] = (a[0, 0] * b[0, 0]) + (a[0, 1] * b[1, 0]) + (a[0, 2] * b[2, 0])
 
        multiple_matrix(a, b, c)
        Print(c)
 
    End Sub
 
    Sub Print(arr2d(,) As Integer)
        For i As Integer = 0 To arr2d.GetUpperBound(0)
            For j As Integer = 0 To arr2d.GetUpperBound(1)
                Console.Write("{0,4}", arr2d(i, j))
            Next
            Console.WriteLine()
        Next
    End Sub
 
    Sub multiple_matrix(a(,) As Integer, b(,) As Integer, c(,) As Integer)
        For i As Integer = 0 To c.GetUpperBound(0)
            For j As Integer = 0 To c.GetUpperBound(0)
                For k As Integer = 0 To c.GetUpperBound(0)
                    c(i, j) = c(i, j) + a(i, k) * b(k, j)
                Next
            Next
        Next
    End Sub
 
End Module
 
 
 
' run:
' 
'   1   8   5
'   6   7   1
'   8   7   6
 
'   4   8   1
'   6   5   3
'   4   6   5
 
'  72  78  50
'  70  89  32
'  98 135  59
'


 



answered Feb 28, 2016 by avibootz
edited 3 days ago by avibootz
...