Extreme Optimization >
User's Guide >
Vector and Matrix Library >
Structured Matrix Types >
Diagonal Matrices
Extreme Optimization User's Guide
User's Guide
Up: Structured Matrix Types Next: Matrix Decompositions Previous: Band Matrices Contents
Diagonal Matrices
A diagonal matrix is a matrix whose only nonzero components lie
on a diagonal. Diagonal matrices are implemented by the
DiagonalMatrix
class.
How diagonal matrices are stored
Diagonal matrices most often have the nonzero components on the
main diagonal, but this is not necessary. The Diagonal
property is an integer that specifies which diagonal contains the
nonzero components. A value of zero indicates the main diagonal. A
value greater than zero specifies a superdiagonal. A value less
than zero specifies a subdiagonal.
Constructing diagonal matrices
Constructors for diagonal 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 diagonal.
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
diagonal matrix, we have:
| C# | Copy Code |
Matrix t1 = new DiagonalMatrix(MatrixTriangleMode.Upper, 5); |
| Visual Basic | Copy Code |
Dim t1 As Matrix = New DiagonalMatrix(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 DiagonalMatrix(MatrixTriangleMode.Upper, 5, 7); |
| Visual Basic | Copy Code |
Dim t2 As Matrix = New DiagonalMatrix(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 DiagonalMatrix(MatrixTriangleMode.Lower, 5, 5,
MatrixDiagonalMode.UnitDiagonal); |
| Visual Basic | Copy Code |
Dim t3 As Matrix = New DiagonalMatrix(MatrixTriangleMode.Lower, 5, 5, _
MatrixDiagonalMode.UnitDiagonal) |
creates a 5x5 lower diagonal 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 diagonal matrix:
| C# | Copy Code |
double[] components = {1, 0, 0, 2, 3, 0, 4, 5, 6};
Matrix t4 = new DiagonalMatrix(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 DiagonalMatrix(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 DiagonalMatrix(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 DiagonalMatrix(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 DiagonalMatrix(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 DiagonalMatrix(MatrixTriangleMode.Upper, _
3, components, MatrixDiagonalMode.UnitDiagonal, _
MatrixElementOrder.RowMajor, true) |
Extracting diagonal matrices
You may have an existing matrix whose upper or lower diagonal
part you wish to reuse as if it were a diagonal matrix with all
other elements being zero. The
ExtractLowerTriangle and
ExtractUpperTriangle methods provide this
functionality. You can extract diagonal 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
diagonal and a unit-diagonal lower diagonal part:
| C# | Copy Code |
double[] components = {1, 4, 7, 2, 5, 8, 3, 6, 9};
GeneralMatrix m = new GeneralMatrix(components, 3, 3);
DiagonalMatrix u = DiagonalMatrix.ExtractUpperTriangle(m);
DiagonalMatrix l = DiagonalMatrix.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 DiagonalMatrix = DiagonalMatrix.ExtractUpperTriangle(m)
Dim l As DiagonalMatrix = DiagonalMatrix.ExtractLowerTriangle(m, _
DiagonalMode.UnitDiagonal) |
Rows and Columns
The GetRow
and
GetColumn methods of DiagonalMatrix
return a vector of type BandVector.
Up: Structured Matrix Types Next: Matrix Decompositions Previous: Band Matrices 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