Extreme Optimization >
QuickStart Samples >
Symmetric Matrices QuickStart Sample (VB.NET)
Extreme Optimization QuickStart Samples
Symmetric Matrices QuickStart Sample (VB.NET)
Illustrates the use of the SymmetricMatrix class
(Extreme.Mathematics.LinearAlgebra namespace) in Visual Basic .NET.
C# code Back
to QuickStart Samples
' The SymmetricMatrix class resides in the Extreme.Mathematics.LinearAlgebra
' namespace.
Imports Extreme.Mathematics.LinearAlgebra
Namespace Extreme.Mathematics.QuickStart.VB
' Illustrates the use of the SymmetricMatrix class in the
' Extreme.Mathematics.LinearAlgebra namespace of the Extreme Optimization
' Mathematics Library for .NET.
Module SymmetricMatrices
Sub Main()
' Symmetric matrices are matrices whose elements
' are symmetrical around the main diagonal.
' Symmetric matrices are always square, and are
' equal to their own transpose.
'
' Constructing symmetric matrices
'
' Constructing symmetric matrices is similar to
' constructing general matrices. See the
' BasicMatrices QuickStart samples for a more
' complete discussion.
' Symmetric matrices are always square. You don't
' have to specify both the number of rows and the
' number of columns.
'
' The following creates a 5x5 symmetric matrix:
Dim s1 As SymmetricMatrix = New SymmetricMatrix(5)
' Symmetric matrices access and modify only the
' elements on and either above or below the
' main diagonal. When initializing a
' symmetric matrix in a constructor, you must
' specify a triangleMode parameter that specifies
' whether to use the upper or lower triangle:
Dim components As Double() = New Double() _
{11, 12, 13, 14, 15, _
21, 22, 23, 24, 25, _
31, 32, 33, 34, 35, _
41, 42, 43, 44, 45, _
51, 52, 53, 54, 55}
Dim s2 As SymmetricMatrix = New SymmetricMatrix( _
MatrixTriangleMode.Upper, components, 5)
Console.WriteLine("s2 = {0}", s2)
' You can also create a symmetric matrix by
' multiplying any matrix by its transpose:
Dim m As GeneralMatrix = New GeneralMatrix(3, 4, New Double() _
{1, 2, 3, _
2, 3, 4, _
3, 4, 5, _
4, 5, 6})
Console.WriteLine("m = {0}", m)
' This calculates transpose(m) times m:
Dim s3 As SymmetricMatrix = _
SymmetricMatrix.FromOuterProduct(m)
Console.WriteLine("s3 = {0}", s3)
' An optional 'side' parameter lets you specify
' whether the left or right operand of the
' multiplication is the transposed matrix.
' This calculates m times transpose(m):
Dim s4 As SymmetricMatrix = _
SymmetricMatrix.FromOuterProduct(m, _
MatrixOperationSide.Right)
Console.WriteLine("s4 = {0}", s4)
'
' SymmetricMatrix methods
'
' The GetEigenvalues method returns a vector
' containing the eigenvalues.
Dim l As Vector = s4.GetEigenvalues()
Console.WriteLine("Eigenvalues: {0:F4}", l)
' The ApplyMatrixFunction calculates a function
' of the entire matrix. For example, to calculate
' the 'sine' of a matrix:
Dim sinS As SymmetricMatrix = _
s4.ApplyMatrixFunction(New RealFunction(AddressOf Math.Sin))
Console.WriteLine("sin(s4): {0:F4}", sinS)
' Symmetric matrices don't have any specific
' properties.
'
' You can get and set matrix elements:
s1(1, 3) = 55
Console.WriteLine("s1(1, 3) = {0}", s1(1, 3))
' And the change will automatically be reflected
' in the symmetric element:
Console.WriteLine("s1(3, 1) = {0}", s1(3, 1))
'
' Row and column views
'
' The GetRow and GetColumn methods are
' available.
Dim row As Vector = s2.GetRow(1)
Console.WriteLine("row 1 of s2 = {0}", row)
Dim column As Vector = s2.GetColumn(1, 3, 4)
Console.WriteLine("column of s2 of length 4 at ")
Console.WriteLine(" row 1, column 3 = {0}", column)
Console.Write("Press Enter key to exit...")
Console.ReadLine()
End Sub
End Module
End Namespace
Copyright 2004-2007,
Extreme Optimization. All rights reserved.
Extreme Optimization, Complexity made simple, M#, and M
Sharp are trademarks of ExoAnalytics Inc.
Microsoft, Visual C#, Visual Basic, Visual Studio, and Visual
Studio.NET are registered trademarks of Microsoft Corporation