Extreme Optimization >
QuickStart Samples >
Matrix Decompositions QuickStart Sample (VB.NET)
Extreme Optimization QuickStart Samples
Matrix Decompositions QuickStart Sample (VB.NET)
Illustrates the use of matrix decomposition classes
(Extreme.Mathematics.LinearAlgebra namespace) in Visual Basic .NET.
C# code Back to
QuickStart Samples
' The GeneralMatrix and DoubleVector classes resides in the
' Extreme.Mathematics.LinearAlgebra namespace.
Imports Extreme.Mathematics.LinearAlgebra
Namespace Extreme.Mathematics.QuickStart.VB
'/ Illustrates the use of matrix decompositions for solving systems of
'/ simultaneous linear equations and related operations using the
'/ Decomposition class and its derived classes from the
'/ Extreme.Mathematics.LinearAlgebra namespace of the Extreme Optimization
'/ Mathematics Library for .NET.
Module MatrixDecompositions
Sub Main()
' For details on the basic workings of Vector
' objects, including constructing, copying and
' cloning vectors, see the BasicVectors QuickStart
' Sample.
'
' For details on the basic workings of Matrix
' objects, including constructing, copying and
' cloning vectors, see the BasicVectors QuickStart
' Sample.
'
'
' LU Decomposition
'
' The LU decomposition of a matrix rewrites a matrix A in the
' form A = PLU with P a permutation matrix, L a unit-
' lower triangular matrix, and U an upper triangular matrix.
Dim aLU As GeneralMatrix = New GeneralMatrix(4, 4, _
New Double() _
{ _
1.8, 5.25, 1.58, -1.11, _
2.88, -2.95, -2.69, -0.66, _
2.05, -0.95, -2.9, -0.59, _
-0.89, -3.8, -1.04, 0.8 _
})
Dim bLU As GeneralMatrix = New GeneralMatrix(4, 2, New Double() _
{ _
9.52, 24.35, 0.77, -6.22, _
18.47, 2.25, -13.28, -6.21 _
})
' The constructor takes one or two parameters. The second
' parameter is a bool value that indicates whether the
' matrix should be overwritten with its decomposition.
Dim lu As LUDecomposition = New LUDecomposition(aLU, True)
Console.WriteLine("A = {0}", aLU)
' The Decompose method performs the decomposition. You don't need
' to call it explicitly, as it is called automatically as needed.
' The IsSingular method checks for singularity.
Console.WriteLine("'A is singular' is {0}.", lu.IsSingular())
' GetInverse() gives the matrix inverse,
' Determinant() the determinant:
Console.WriteLine("Inv A = {0}", lu.GetInverse().ToString("F4"))
Console.WriteLine("Det A = {0}", lu.GetDeterminant())
' The Solve method solves a system of simultaneous linear equations
' with one or more right-hand-sides:
Dim xLU As Matrix = lu.Solve(bLU)
Console.WriteLine("x = {0}", xLU)
' The LowerTriangularFactor and UpperTriangularFactor properties
' return the two main components of the decomposition.
Console.WriteLine("L = {0}", lu.UpperTriangularFactor.ToString("F4"))
Console.WriteLine("U = {0}", lu.UpperTriangularFactor.ToString("F4"))
' The permutation matrix P is not available directly.
' You can apply the permutation by calling the ApplyP method:
Console.WriteLine("Px = {0}", lu.ApplyP(xLU))
'
' QR Decomposition
'
' The QR decomposition of a matrix A rewrites the matrix
' in the form A = QR, with Q a square, orthogonal matrix,
' and R an upper triangular matrix.
Dim aQR As GeneralMatrix = New GeneralMatrix(5, 3, _
New Double() _
{ _
2.0, 2.0, 1.6, 2.0, 1.2, _
2.5, 2.5, -0.4, -0.5, -0.3, _
2.5, 2.5, 2.8, 0.5, -2.9 _
})
Dim bQR As GeneralVector = _
New GeneralVector(1.1, 0.9, 0.6, 0.0, -0.8)
' The constructor takes one or two parameters. The second
' parameter is a bool value that indicates whether the
' matrix should be overwritten with its decomposition.
Dim qr As QRDecomposition = New QRDecomposition(aQR, True)
Console.WriteLine("A = {0}", aQR)
' The Decompose method performs the decomposition. You don't need
' to call it explicitly, as it is called automatically as needed.
' The IsSingular method checks for singularity.
Console.WriteLine("'A is singular' is {0}.", qr.IsSingular())
' GetInverse() gives the matrix inverse,
' Determinant() the determinant:
Console.WriteLine("Inv A = {0}", qr.GetInverse().ToString("F4"))
Console.WriteLine("Det A = {0}", qr.GetDeterminant())
' The Solve method solves a system of simultaneous linear
' equations with one or more right-hand-sides. If the matrix
' is over-determined, you can use the LeastSquaresSolve
' method to find a least squares solution:
Dim xQR As GeneralVector = _
CType(qr.LeastSquaresSolve(bQR), GeneralVector)
Console.WriteLine("x = {0}", xQR)
' You can also
' The OrthogonalFactor and UpperTriangularFactor properties
' return the two main components of the decomposition.
Console.WriteLine("Q = {0}", qr.OrthogonalFactor.ToString("F4"))
Console.WriteLine("R = {0}", qr.UpperTriangularFactor.ToString("F4"))
' You don't usually need to form Q explicitly. You can multiply
' a vector or matrix on either side by Q using the ApplyQ method:
Console.WriteLine("Qx = {0}", qr.ApplyQ(xQR))
Console.WriteLine("transpose(Q)x = {0}", _
qr.ApplyQ(TransposeOperation.Transpose, xQR))
'
' Singular Value Decomposition
'
' The singular value decomposition of a matrix A rewrites
' the matrix in the form A = USVt, with U and V orthogonal
' matrices, S a diagonal matrix. The diagonal elements of S
' are called the singular values.
Dim aSvd As GeneralMatrix = New GeneralMatrix(3, 5, _
New Double() _
{ _
2.0, 2.0, 1.6, 2.0, 1.2, _
2.5, 2.5, -0.4, -0.5, -0.3, _
2.5, 2.5, 2.8, 0.5, -2.9 _
}, MatrixElementOrder.RowMajor)
Dim bSvd As GeneralVector = New GeneralVector(1.1, 0.9, 0.6)
' The constructor takes one or two parameters. The second
' parameter indicates which parts of the decomposition
' are to be calculated. The default is All.
Dim svd As SingularValueDecomposition = _
New SingularValueDecomposition(aSvd)
Console.WriteLine("A = {0:F1}", aSvd)
' The Decompose method performs the decomposition. You don't need
' to call it explicitly, as it is called automatically as needed.
' The IsSingular method checks for singularity.
Console.WriteLine("'A is singular' is {0:F6}.", svd.IsSingular())
' Several methods are specific to this class. The GetPseudoInverse
' method returns the Moore-Penrose pseudo-inverse, a generalization
' of the inverse of a matrix to rectangular and/or singular matrices:
Dim aInv As Matrix = svd.GetPseudoInverse()
' It can be used to solve over- or under-determined systems.
Dim xSvd As Vector = Matrix.Multiply(aInv, bSvd)
Console.WriteLine("x = {0:F6}", xSvd)
' The SingularValues property returns a vector that contains
' the singular values in descending order:
Console.WriteLine("S = {0:F6}", svd.SingularValues)
' The LeftSingularVectors and RightSingularVectors properties
' return matrices that contain the U and V factors
' of the decomposition.
Console.WriteLine("U = {0:F6}", svd.LeftSingularVectors)
Console.WriteLine("V = {0:F6}", svd.RightSingularVectors)
'
' Cholesky decomposition
'
' The Cholesky decomposition of a symmetric matrix A
' rewrites the matrix in the form A = GGt with
' G a lower-triangular matrix.
' Remember the column-major storage mode: each line of
' components contains one COLUMN of the matrix.
Dim aC As SymmetricMatrix = New SymmetricMatrix(4, _
New Double() _
{ _
4.16, -3.12, 0.56, -0.1, _
0, 5.03, -0.83, 1.18, _
0, 0, 0.76, 0.34, _
0, 0, 0, 1.18 _
}, MatrixTriangleMode.Lower)
Dim bC As GeneralMatrix = New GeneralMatrix(4, 2, _
New Double() {8.7, -13.35, 1.89, -4.14, 8.3, 2.13, 1.61, 5.0})
' The constructor takes one or two parameters. The second
' parameter is a bool value that indicates whether the
' matrix should be overwritten with its decomposition.
Dim c As CholeskyDecomposition = New CholeskyDecomposition(aC, True)
Console.WriteLine("A = {0:F4}", aC)
' The Decompose method performs the decomposition. You don't need
' to call it explicitly, as it is called automatically as needed.
' The IsSingular method checks for singularity.
Console.WriteLine("'A is singular' is {0}.", c.IsSingular())
' The LowerTriangularFactor returns the component
' of the decomposition.
Console.WriteLine("L = {0:F4}", c.LowerTriangularFactor)
' GetInverse() gives the matrix inverse,
' Determinant() the determinant:
Console.WriteLine("Inv A = {0:F4}", c.GetInverse())
Console.WriteLine("Det A = {0:F4}", c.GetDeterminant())
' The Solve method solves a system of simultaneous linear equations
' with one or more right-hand-sides:
Dim xC As Matrix = c.Solve(bC)
Console.WriteLine("x = {0:F4}", xC)
'
' Symmetric eigenvalue decomposition
'
' The eigenvalue decomposition of a symmetric matrix A
' rewrites the matrix in the form A = XLXt with
' X an orthogonal matrix and L a diagonal matrix.
' The diagonal elements of L are the eigenvalues.
' The columns of X are the eigenvectors.
' Remember the column-major storage mode: each line of
' components contains one COLUMN of the matrix.
Dim aEig As SymmetricMatrix = New SymmetricMatrix(4, _
New Double() _
{ _
0.5, 0.0, 2.3, -2.6, _
0.0, 0.5, -1.4, -0.7, _
2.3, -1.4, 0.5, 0.0, _
-2.6, -0.7, 0.0, 0.5 _
}, MatrixTriangleMode.Lower)
' The constructor takes one or two parameters. The second
' parameter is a bool value that indicates whether the
' matrix should be overwritten with its decomposition.
Dim eig As SymmetricEigenvalueDecomposition = _
New SymmetricEigenvalueDecomposition(aEig)
Console.WriteLine("A = {0:F2}", aEig)
' The Decompose method performs the decomposition. You don't need
' to call it explicitly, as it is called automatically as needed.
' The IsSingular method checks for singularity.
Console.WriteLine("'A is singular' is {0:F6}.", eig.IsSingular())
' The Eigenvalues property returns a vector containing
' the eigenvalues:
Console.WriteLine("L = {0:F6}", eig.Eigenvalues)
' The Eigenvectors property returns a matrix whose columns
' contain the corresponding eigenvectors:
Console.WriteLine("X = {0:F6}", eig.Eigenvectors)
' GetInverse() gives the matrix inverse,
' Determinant() the determinant:
Console.WriteLine("Inv A = {0:F6}", eig.GetInverse())
Console.WriteLine("Det A = {0:F6}", eig.GetDeterminant())
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