Extreme Optimization >
User's Guide >
Vector and Matrix Library >
Structured Matrix Types >
Triangular Matrices
Extreme Optimization User's Guide
User's Guide
Up: Structured Matrix Types Next: Symmetrical Matrices Previous: Structured Matrix Types Contents
Triangular Matrices
A matrix whose elements above or below the main diagonal are all
zero. The name derives from the fact that the shape of the non-zero
elements resembles a triangle. Triangular matrices are implemented
by the TriangularMatrix
class.
How triangular matrices are stored
Triangular matrices can be upper or lower triangular. An upper
triangular matrix has non-zero elements on and above the main
diagonal. A lower triangular matrix has non-zero elements on and
below the main diagonal. The
TriangleMode property is of enumeration type
MatrixTriangleMode
and can have values Upper and Lower.
Strictly speaking, triangular matrices are always square. If an
upper triangular matrix has more columns than rows, then the shape
of the block of the non-zero elements is more trapezoidal than
triangular. Such upper-trapezoidal matrices and their
lower-trapezoidal counterparts are also represented using the
TriangularMatrix class.
The main diagonal of a triangular matrix can have arbitrary
elements, or it can be constrained to only consist of 1's. The
DiagonalMode property indicates which of these two is
the case. It is of type MatrixDiagonalMode
and can have values UnitDiagonal and
NonUnitDiagonal (the default).
Constructing triangular matrices
Constructors for triangular matrices are analogous to
constructors for general matrices. All constructors have a
MatrixTriangleMode value as their first parameter.
This indicates whether the matrix is upper or lower triangular.
The simplest constructor has one more parameter. It constructs a
square matrix with the specified number of rows and columns. All
elements are initally set to zero. For example, for a 5x5 upper
triangular matrix, we have:
| C# | Copy Code |
Matrix t1 = new TriangularMatrix(MatrixTriangleMode.Upper, 5); |
| Visual Basic | Copy Code |
Dim t1 As Matrix = New TriangularMatrix(MatrixTriangleMode.Upper, 5) |
You can also specify both the number of rows and columns. The
following creates a 5x7 upper-trapezoidal matrix:
| C# | Copy Code |
Matrix t2 = new TriangularMatrix(MatrixTriangleMode.Upper, 5, 7); |
| Visual Basic | Copy Code |
Dim t2 As Matrix = New TriangularMatrix(MatrixTriangleMode.Upper, 5, 7) |
If the matrix is unit diagonal, you can specify this by adding a
fourth parameter of type MatrixDiagonalMode. If this
parameter has the value UnitDiagonal, all elements
except the elemenets on the main diagonal are initially set to
zero. The elements on the diagonal are all equal to one and cannot
be changed. For example:
| C# | Copy Code |
Matrix t3 = new TriangularMatrix(MatrixTriangleMode.Lower, 5, 5,
MatrixDiagonalMode.UnitDiagonal); |
| Visual Basic | Copy Code |
Dim t3 As Matrix = New TriangularMatrix(MatrixTriangleMode.Lower, 5, 5, _
MatrixDiagonalMode.UnitDiagonal) |
creates a 5x5 lower triangular matrix with unit diagonal.
The three remaining constructors are similar to the previous
three, but also let you initialize the components of the
matrix. This is done through an extra parameter: a
Double array containing the components of the new
matrix. The simplest one creates a square triangular matrix:
| C# | Copy Code |
double[] components = {1, 0, 0, 2, 3, 0, 4, 5, 6};
Matrix t4 = new TriangularMatrix(MatrixTriangleMode.Upper,
3, components); |
| Visual Basic | Copy Code |
Dim components As Double() = {1, 0, 0, 2, 3, 0, 4, 5, 6}
Dim t4 As Matrix t4 = New TriangularMatrix(MatrixTriangleMode.Upper, _
3, components) |
This creates a matrix whose components are stored in column
major order. You can specify that the components are given in
row major order by passing an extra parameter:
| C# | Copy Code |
double[] components = {1, 0, 0, 2, 3, 0, 4, 5, 6};
Matrix t5 = new TriangularMatrix(MatrixTriangleMode.Upper,
3, components, MatrixElementOrder.RowMajor); |
| Visual Basic | Copy Code |
Dim components As Double() = {1, 0, 0, 2, 3, 0, 4, 5, 6}
Dim t5 As Matrix t4 = New TriangularMatrix(MatrixTriangleMode.Upper, _
3, components, MatrixElementOrder.RowMajor) |
Finally, the most complete and flexible constructor lets you
specify the order of the elements, whether the matrix is unit
diagonal, and whether to reuse the component array for storage.
| C# | Copy Code |
double[] components = {1, 0, 0, 2, 3, 0, 4, 5, 6};
Matrix t5 = new TriangularMatrix(MatrixTriangleMode.Upper,
3, components, MatrixDiagonalMode.UnitDiagonal,
MatrixElementOrder.RowMajor, true); |
| Visual Basic | Copy Code |
Dim components As Double() = {1, 0, 0, 2, 3, 0, 4, 5, 6}
Dim t5 As Matrix t4 = New TriangularMatrix(MatrixTriangleMode.Upper, _
3, components, MatrixDiagonalMode.UnitDiagonal, _
MatrixElementOrder.RowMajor, true) |
Extracting triangular matrices
You may have an existing matrix whose upper or lower triangular
part you wish to reuse as if it were a triangular matrix with all
other elements being zero. The
ExtractLowerTriangle and
ExtractUpperTriangle methods provide this
functionality. You can extract triangular matrices from general and
symmetrical matrices. You can also specify whether the new matrix
is unit diagonal.
The following example splits a general matrix into an upper
triangular and a unit-diagonal lower triangular part:
| C# | Copy Code |
double[] components = {1, 4, 7, 2, 5, 8, 3, 6, 9};
GeneralMatrix m = new GeneralMatrix(components, 3, 3);
TriangularMatrix u = TriangularMatrix.ExtractUpperTriangle(m);
TriangularMatrix l = TriangularMatrix.ExtractLowerTriangle(m,
DiagonalMode.UnitDiagonal); |
| Visual Basic | Copy Code |
Dim components As Double() = {1, 4, 7, 2, 5, 8, 3, 6, 9}
Dim m As GeneralMatrix = New GeneralMatrix(components, 3, 3)
Dim u As TriangularMatrix = TriangularMatrix.ExtractUpperTriangle(m)
Dim l As TriangularMatrix = TriangularMatrix.ExtractLowerTriangle(m, _
DiagonalMode.UnitDiagonal) |
Rows and Columns
The
GetRow and
GetColumn methods of TriangularMatrix
return a vector of type BandVector,
or of type GeneralVector
when all components are nonzero and does not include a unit
diagonal component.
Up: Structured Matrix Types Next: Symmetrical Matrices Previous: Structured Matrix Types Contents
Copyright 2004-2008,
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, Visual
Studio.NET, and the Visual Studio Logo are registered trademarks of Microsoft Corporation