Extreme Optimization > User's Guide > Vector and Matrix Library > Structured Matrix Types > Symmetrical Matrices

Extreme Optimization User's Guide

User's Guide

Up: Structured Matrix Types Next: Band Matrices Previous: Triangular Matrices Contents

Symmetrical Matrices

A SymmetricMatrix represents a matrix whose elements are symmetrical about the main diagonal. A symmetric matrix is equal to its transpose.

How symmetrical matrices are stored

Symmetrical matrices are stored in the upper or lower triangular part of a general matrix. The TriangleMode property indicates how elements are stored. It is of enumeration type MatrixTriangleMode and can have values Upper and Lower. The element order is always column major order. Row major storage in the upper triangle is exactly equivalent to column major storage in the lower triangle.

Constructing symmetrical matrices

Constructors for symmetrical matrices are analogous to constructors for triangular matrices. The fact that symmetrical matrices are always square means only one dimension needs to be passed to the constructor.

The simplest constructor has just one parameter. It constructs a symmetrical matrix with the specified number of rows and columns. All elements are initally set to zero. For example, for a 5x5 symmetrical matrix, we have:

C# CopyCode imageCopy Code
Matrix s1 = new SymmetricMatrix(5);
Visual Basic CopyCode imageCopy Code
Dim s1 As Matrix = New SymmetricMatrix(5)

The two remaining constructors allow you to initialize the components of the matrix. The first parameter once again specifies the number of rows and columns. The second parameter is a Double array containing the components of the new matrix. The third parameter is of type MatrixTriangleMode, and indicates whether the elements are taken from the upper or lower triangular part of the component array. The components are assumed to be given in column major order.

C# CopyCode imageCopy Code
double[] components = {1, 0, 0, 2, 3, 0, 4, 5, 6};
SymmetricMatrix s2 = new SymmetricMatrix(3, components,
    MatrixTriangleMode.Upper);
Visual Basic CopyCode imageCopy Code
Dim components As Double() components = {1, 0, 0, 2, 3, 0, 4, 5, 6}
Dim s2 As SymmetricMatrix = New SymmetricMatrix(3, components, _
    MatrixTriangleMode.Upper)

The last constructor adds one Boolean parameter that lets you specify whether the component array should be reused or copied. If the component array is reused, any changes to its elements will result in changes to the corresponding components of the matrix.

C# CopyCode imageCopy Code
double[] components = {1, 0, 0, 2, 3, 0, 4, 5, 6};
SymmetricMatrix s3 = new SymmetricMatrix(3, components,
    MatrixTriangleMode.Upper, true);
Visual Basic CopyCode imageCopy Code
Dim components As Double() = {1, 0, 0, 2, 3, 0, 4, 5, 6}
Dim s3 As SymmetricMatrix = New SymmetricMatrix(3, components, _
    MatrixTriangleMode.Upper, True)

Methods specific to symmetrical matrices

The static FromOuterProduct methods return a symmetrical matrix that is the product of a matrix with its transpose. An optional second parameter of type MatrixOperationSide specifies which of the two operands should be transposed. The default is MatrixOperationSide.Left The following example creates a 2x2 symmetrical matrix that is the product of a 2x3 matrix with its transpose.

C# CopyCode imageCopy Code
double[] components = {1, 2, 3, 4, 5, 6};
GeneralMatrix a = new GeneralMatrix(3, 2, components);
SymmetricMatrix s = SymmetricMatrix.FromOuterProduct(a);
Console.WriteLine("aTa = {0}", s);
Visual Basic CopyCode imageCopy Code
Dim components As Double() = {1, 2, 3, 4, 5, 6}
Dim a As GeneralMatrix = New GeneralMatrix(3, 2, components)
Dim s As SymmetricMatrix = SymmetricMatrix.FromOuterProduct(a)
Console.WriteLine("aTa = {0}", s)

The SymmetricMatrix class has two specific instance methods. The AddOuterProduct method adds the outer product of a vector or a matrix with its transpose to a SymmetricMatrix. You may supply a multiplier for the outer product, as in the following example:

C# CopyCode imageCopy Code
SymmetricMatrix a = new SymmetricMatrix(4);
GeneralVector v = new GeneralVector(1, 2, 3, 4);
a.AddOuterProduct(2, v);
// This prints "[2 4 6 8]":
Console.WriteLine("1st row of a = {0}", a.GetRow(0));
Visual Basic CopyCode imageCopy Code
Dim a As SymmetricMatrix = New SymmetricMatrix(4)
Dim v As GeneralVector = New GeneralVector(1, 2, 3, 4)
a.AddOuterProduct(2, v)
' This prints "[2 4 6 8]":
Console.WriteLine("1st row of a = {0}", a.GetRow(0))

Complementary to this method is the SubtractOuterProduct method, which subtracts an outer product.

The GetEigenValues method returns a GeneralVector that contains the eigenvalues of the matrix. If you also need the eigenvectors, it is more efficient to create a SymmetricEigenvalueDecomposition object from the matrix and use its Eigenvalues and Eigenvectors properties.

The ApplyMatrixFunction calculates a matrix function. Its only parameter is a RealFunction delegate that specifies the function to calculate. The matrix function is computed by computing the eigenvalue decomposition and applying the function to each of the eigenvalues, and finally computing the matrix with the same eigenvectors but the transformed eigenvalues. The example below calculates the exponential of a 2x2 symmetrical matrix:

C# CopyCode imageCopy Code
double[] components = {1, 0, 3, 4};
SymmetricMatrix s = new SymmetricMatrix(2, components, MatrixTriangleMode.Upper);
RealFunction exp = new RealFunction(Math.Exp);
SymmetricMatrix expS = s.ApplyMatrixFunction(exp);
Console.WriteLine("Exp(s) = {0}", expS);
Visual Basic CopyCode imageCopy Code
Dim components As Double() = {1, 0, 3, 4}
Dim s As SymmetricMatrix = New SymmetricMatrix(2, components, MatrixTriangleMode.Upper)
Dim exp As RealFunction = New RealFunction(AddressOf Math.Exp)
Dim expS As SymmetricMatrix = s.ApplyMatrixFunction(exp)
Console.WriteLine("Exp(s) = {0}", expS)

Rows and columns

The rows and columns of a symmetrical matrix are of type RowVector and ColumnVector, respectively.

Up: Structured Matrix Types Next: Band Matrices Previous: Triangular 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