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

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