📄 triangularmatrices.vb
字号:
' The TriangularMatrix class resides in the Extreme.Mathematics.LinearAlgebra
' namespace.
Imports Extreme.Mathematics.LinearAlgebra
Namespace Extreme.Mathematics.QuickStart.VB
' Illustrates the use of the TriangularMatrix class in the
' Extreme.Mathematics.LinearAlgebra namespace of the Extreme Optimization
' Mathematics Library for .NET.
Module TriangularMatrices
Sub Main()
' Triangular matrices are matrices whose elements
' above or below the diagonal are all zero. The
' former is called lower triangular, the latter
' lower triangular. In addition, triangular matrices
' can have all 1's on the diagonal.
'
' Constructing triangular matrices
'
' Constructing triangular matrices is similar to
' constructing general matrices. See the
' BasicMatrices QuickStart samples for a more
' complete discussion.
'
' All constructors take a MatrixTriangleMode
' value as their first parameter. This indicates
' whether an upper or lower triangular matrix
' should be created. The following creates a
' 5x5 lower triangular matrix:
Dim t1 As TriangularMatrix = New TriangularMatrix( _
MatrixTriangleMode.Lower, 5, 5)
' You can also specify whether the diagonal
' consists of all 1's using a unitDiagonal parameter:
Dim t2 As TriangularMatrix = New TriangularMatrix( _
MatrixTriangleMode.Lower, _
MatrixDiagonalMode.UnitDiagonal, 5, 5)
' Triangular matrices access and modify only the
' elements that are non-zero. If the diagonal
' mode is UnitDiagonal, the diagonal elements
' are not used, since they are all equal to 1.
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}
' The following creates a matrix using the
' upper triangular part of the above.
Dim t3 As TriangularMatrix = New TriangularMatrix( _
MatrixTriangleMode.Upper, 5, 5, _
components, MatrixElementOrder.RowMajor)
Console.WriteLine("t3 = {0:F4}", t3)
' Same as above, but unit diagonal:
Dim t4 As TriangularMatrix = New TriangularMatrix( _
MatrixTriangleMode.Upper, 5, 5, components, _
MatrixDiagonalMode.UnitDiagonal, _
MatrixElementOrder.RowMajor, True)
Console.WriteLine("t4 = {0:F4}", t4)
'
' Extracting triangular matrices
'
' You may want to use part of a dense matrix
' as a triangular matrix. The static
' ExtractUpperTriangleand ExtractLowerTriangle
' methods perform this task.
Dim m As GeneralMatrix = New GeneralMatrix(5, 5, components)
Console.WriteLine("m = {0:F4}", m)
' Both methods are overloaded. The simplest
' returns a triangular matrix of the same dimension:
Dim t5 As TriangularMatrix = _
TriangularMatrix.ExtractLowerTriangle(m)
Console.WriteLine("t5 = {0:F4}", t5)
' You can also specify if the matrix is unit diagonal:
Dim t6 As TriangularMatrix = _
TriangularMatrix.ExtractUpperTriangle( _
m, MatrixDiagonalMode.UnitDiagonal)
Console.WriteLine("t6 = {0:F4}", t6)
' Or the dimensions of the matrix if they don't
' match the original:
Dim t7 As TriangularMatrix = _
TriangularMatrix.ExtractUpperTriangle( _
m, 3, 3, MatrixDiagonalMode.UnitDiagonal)
Console.WriteLine("t7 = {0:F4}", t7)
Console.WriteLine()
'
' TriangularMatrix properties
'
' The IsLowerTriangular and IsUpperTriangular return
' a boolean value:
Console.WriteLine("t4 is lower triangular? - {0}", _
t4.IsLowerTriangular)
Console.WriteLine("t4 is upper triangular? - {0}", _
t4.IsUpperTriangular)
' The IsUnitDiagonal property indicates whether the
' matrix has all 1's on its diagonal:
Console.WriteLine("t3 is unit diagonal? - {0}", _
t3.IsUnitDiagonal)
Console.WriteLine("t4 is unit diagonal? - {0}", _
t4.IsUnitDiagonal)
Console.WriteLine()
' You can get and set matrix elements:
t3(1, 3) = 55
Console.WriteLine("t3(1, 3) = {0}", t3(1, 3))
' But trying to set an element that is zero or
' is on the diagonal for a unit diagonal matrix
' causes an exception to be thrown:
Try
t3(3, 1) = 100
Catch e As ComponentReadOnlyException
Console.WriteLine("Error accessing element: {0:F4}", _
e.Message)
End Try
'
' Row and column views
'
' The GetRowView and GetColumnView methods are
' available.
Dim row As Vector = t3.GetRow(1)
Console.WriteLine("row 1 of t3 = {0:F4}", row)
Dim column As Vector = t4.GetColumn(2, 1, 3)
Console.WriteLine("column 3 of t4 from row 2 to row 4:")
Console.WriteLine(" {0:F4}", column)
Console.Write("Press Enter key to exit...")
Console.ReadLine()
End Sub
End Module
End Namespace
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -