Overload Operators For Matrix Class

PdotWang 0 Tallied Votes 710 Views Share

This is an answer to a question in C++ area.

http://www.daniweb.com/software-development/cpp/threads/355232

It should be ready to be modified to code in C++, C#, or Java, because VB.Net is fully OOP. Even though it can be used for vectors, you may still want to modify it to a Vector Class.

Public Class Matrix
    'Matrix is 0 based.
    Private m_Values As Double(,)
    Public Sub New(ByVal m As Integer, ByVal n As Integer)
        ReDim m_Values(m, n)
    End Sub
    Public Sub New(ByVal a As Double(,))
        'This is not a copy but a reference.
        'Must release the reference of a afterwards.
        m_Values = a
    End Sub
    Public ReadOnly Property M() As Double
        Get
            Return m_Values.GetLength(0) - 1
        End Get
    End Property
    Public ReadOnly Property N() As Double
        Get
            Return m_Values.GetLength(1) - 1
        End Get
    End Property
    Public Property Values() As Double(,)
        Get
            Return m_Values
        End Get
        Set(ByVal a As Double(,))
            'This is not a copy but a reference.
            'Must release the reference of a afterwards.
            m_Values = a
        End Set
    End Property
    Public Overrides Function ToString() As String
        Dim a(M) As String
        Dim b(N) As Double
        For i As Integer = 0 To M
            For j As Integer = 0 To N
                b(j) = Values(i, j)
            Next
            'a(i) = DStrArray(",", b, 8)'Defined in another Module
        Next
        Return String.Join(vbCrLf, a)
    End Function
    Public Function Copy() As Matrix
        Dim c(M, N) As Double
        Dim v As Double(,) = Values
        For i As Integer = 0 To M
            For j As Integer = 0 To N
                c(i, j) = v(i, j)
            Next
        Next
        Return New Matrix(c)
    End Function
    Public Function Trans() As Matrix
        Dim c(N, M) As Double
        Dim v As Double(,) = Values
        For i As Integer = 0 To M
            For j As Integer = 0 To N
                c(j, i) = v(i, j)
            Next
        Next
        Return New Matrix(c)
    End Function
    Public Function MaxAbs() As Double
        Dim s As Double = 0
        For Each a As Double In Values
            s = Math.Max(s, Math.Abs(a))
        Next
        Return s
    End Function
    Public Shared Operator +(ByVal ma As Matrix, ByVal mb As Matrix) As Matrix
        Dim a As Double(,) = ma.Values
        Dim b As Double(,) = mb.Values
        Dim c(ma.M, ma.N) As Double
        For i As Integer = 0 To ma.M
            For j As Integer = 0 To ma.N
                c(i, j) = a(i, j) + b(i, j)
            Next
        Next
        Return New Matrix(c)
    End Operator
    Public Shared Operator -(ByVal ma As Matrix, ByVal mb As Matrix) As Matrix
        Dim a As Double(,) = ma.Values
        Dim b As Double(,) = mb.Values
        Dim c(ma.M, ma.N) As Double
        For i As Integer = 0 To ma.M
            For j As Integer = 0 To ma.N
                c(i, j) = a(i, j) - b(i, j)
            Next
        Next
        Return New Matrix(c)
    End Operator
    Public Shared Operator *(ByVal s As Double, ByVal ma As Matrix) As Matrix
        Dim a As Double(,) = ma.Values
        Dim c(ma.M, ma.N) As Double
        For i As Integer = 0 To ma.M
            For j As Integer = 0 To ma.N
                c(i, j) = s * a(i, j)
            Next
        Next
        Return New Matrix(c)
    End Operator
    Public Shared Operator *(ByVal ma As Matrix, ByVal vb As Vector) As Vector
        Dim a As Double(,) = ma.Values
        Dim b As Double() = vb.Values
        Dim c(ma.M) As Double
        Dim s As Double
        For i As Integer = 0 To ma.M
            s = 0.0
            For k As Integer = 0 To ma.N
                s += a(i, k) * b(k)
            Next
            c(i) = s
        Next
        Return New Vector(c)
    End Operator
    Public Shared Operator *(ByVal ma As Matrix, ByVal mb As Matrix) As Matrix
        Dim a As Double(,) = ma.Values
        Dim b As Double(,) = mb.Values
        Dim c(ma.M, mb.N) As Double
        Dim s As Double
        For i As Integer = 0 To ma.M
            For j As Integer = 0 To mb.N
                s = 0.0
                For k As Integer = 0 To ma.N
                    s += a(i, k) * b(k, j)
                Next
                c(i, j) = s
            Next
        Next
        Return New Matrix(c)
    End Operator
End Class
PdotWang 17 Newbie Poster

Here you are, the test Main()

Module mdlMain
    Sub Main()
        Dim a As New Matrix(3, 5)
        Dim b As New Matrix(5, 4)
        Dim rnd As New Random()

        For i As Integer = 0 To a.M
            For j As Integer = 0 To a.N
                a.Values(i, j) = Math.Round(rnd.NextDouble, 1)
            Next
        Next
        For i As Integer = 0 To b.M
            For j As Integer = 0 To b.N
                b.Values(i, j) = Math.Round(rnd.NextDouble, 1)
            Next
        Next
        Dim c As Matrix = a * b
        For i As Integer = 0 To c.M
            For j As Integer = 0 To c.N
                Console.Write(c.Values(i, j).ToString & ",")
            Next
            Console.WriteLine()
        Next


        Console.ReadKey()
    End Sub
spixy 0 Newbie Poster

i appreciate this man, but its an oops also for vb.net..
i haven't started learning it...

but i hope someone could help me out and translate this to c++ code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.