```
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
```

Recommended Topics