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# | Copy Code |
Matrix s1 = new SymmetricMatrix(5); |
| Visual Basic | Copy 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# | Copy Code |
double[] components = {1, 0, 0, 2, 3, 0, 4, 5, 6};
SymmetricMatrix s2 = new SymmetricMatrix(3, components,
MatrixTriangleMode.Upper); |
| Visual Basic | Copy 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# | Copy Code |
double[] components = {1, 0, 0, 2, 3, 0, 4, 5, 6};
SymmetricMatrix s3 = new SymmetricMatrix(3, components,
MatrixTriangleMode.Upper, true); |
| Visual Basic | Copy 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# | Copy 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 | Copy 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# | Copy 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 | Copy 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# | Copy 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 | Copy 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
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