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# CopyCode imageCopy Code
Matrix t1 = new DiagonalMatrix(MatrixTriangleMode.Upper, 5);
Visual Basic CopyCode imageCopy 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# CopyCode imageCopy Code
Matrix t2 = new DiagonalMatrix(MatrixTriangleMode.Upper, 5, 7);
Visual Basic CopyCode imageCopy 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# CopyCode imageCopy Code
Matrix t3 = new DiagonalMatrix(MatrixTriangleMode.Lower, 5, 5,
    MatrixDiagonalMode.UnitDiagonal);
Visual Basic CopyCode imageCopy 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# CopyCode imageCopy Code
double[] components = {1, 0, 0, 2, 3, 0, 4, 5, 6};
Matrix t4 = new DiagonalMatrix(MatrixTriangleMode.Upper,
    3, components);
Visual Basic CopyCode imageCopy 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# CopyCode imageCopy Code
double[] components = {1, 0, 0, 2, 3, 0, 4, 5, 6};
Matrix t5 = new DiagonalMatrix(MatrixTriangleMode.Upper,
    3, components, MatrixElementOrder.RowMajor);
Visual Basic CopyCode imageCopy 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# CopyCode imageCopy 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 CopyCode imageCopy 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# CopyCode imageCopy 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 CopyCode imageCopy 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

Overview
Introduction
Features
Documentation
QuickStart Samples
Sample Applications
Downloads
Get it now!
Download trial version
How to Buy
Information
Resources
Contact Us
Search

"The Extreme Optimization Statistics Library for .NET is a major boon for those doing statistical work in .NET. I strongly recommend this product."
- Marc Brooks

"I have made it my mission to institutionalize the value of good API design.  I strongly believe that this is key to making developers more productive and happy on our platform. It is clear that you value good API design in your work, and take to heart developer productivity and synergy with the .NET framework."
- Brad Abrams,
Lead Program Manager, Microsoft.

This is a partial list of companies who are using our libraries:
ABB Robotics
Allstate
Applied Materials
Arcam
Astra Schedule
Babson College
Canadian Council on Learning
Canyon Associates
Caxton Associates
CECity
Constellation Energy
CreditSights
DeepOcean
Duke University
Dynamotive
Elecsoft
Engelhard Corporation
Epcor
Equipoise Software
Galileo International
GAM UK
Gammex
GlaxoSmithKline
Global Matrix
The Hartford
Infinera Corporation
Intel
JDS Uniphase
LaBranche & Co.
Learning & Skills Council
Jacobs Consultancy
Litman Gregory
Lucas Systems
Malvern Instruments
Medrio
Merck & Co.
Mintera.
Monitor Software
MorningStar
NanoString Technologies
Paletta Invent
Parametric Portfolio Associates
Prosanos
RATA Associates
RiskShield
Ramboll
Standard & Poor's
Strategic Analysis Corporation
Univ. of Alicante
Univ. of South Carolina
vielife
Xerox
US Army