How to calculate the normal and trace of square matrix in VB.NET

1 Answer

0 votes
Imports System

Public Class Program
    Private Shared Function CalculateNormal(ByVal matrix As Integer()()) As Double
        Dim normal As Integer = 0

        For i As Integer = 0 To matrix.Length - 1
            For j As Integer = 0 To matrix.Length - 1
                normal += matrix(i)(j) * matrix(i)(j)
            Next
        Next

        Return Math.Sqrt(normal)
    End Function

    Private Shared Function CalculateTrace(ByVal matrix As Integer()()) As Integer
        Dim trace As Integer = 0

        For i As Integer = 0 To matrix.Length - 1
            trace += matrix(i)(i)
        Next

        Return trace
    End Function

    Public Shared Sub Main(ByVal args As String())
        Dim matrix As Integer()() = New Integer()() {
										New Integer() {1, 1, 1, 1, 1}, 
										New Integer() {2, 2, 2, 2, 2}, 
										New Integer() {3, 3, 3, 3, 3}, 
										New Integer() {4, 4, 4, 4, 4}, 
										New Integer() {5, 5, 5, 5, 5}}
        
		Console.WriteLine(CalculateTrace(matrix))
        
		Console.WriteLine(CalculateNormal(matrix))
    End Sub
End Class




' run:
'
' 15
' 16.583123951777
'

 



answered Feb 27, 2023 by avibootz

Related questions

1 answer 126 views
1 answer 175 views
1 answer 138 views
1 answer 155 views
1 answer 131 views
...