Extreme Optimization > User's Guide > Vector and Matrix Library > Matrices > Matrix Basics

Extreme Optimization User's Guide

User's Guide

Up: Matrices Next: General Matrices Previous: Matrices Contents

Matrix Basics

Matrices come in many shapes and sizes. When the matrix exhibits a definite structure, calculations can often be speeded up by several orders of magnitude. Storage requirements may also be significantly reduced. It is therefore useful to define different matrix types to take advantage of these improvements.

The Extreme Optimization Mathematics Library for .NET includes classes for general matrices, upper- and lower-triangular matrices, and symmetrical matrices.

The Matrix class is an abstract base class that exposes the mathematical methods and properties of matrices of Double values. It also contains a default implementation for these methods and properties. A series of derived classes implement specific types of matrices. These are listed in the table below.

Class Description
GeneralMatrix A standard dense matrix. All components can take on any value. This is the most common type.
TriangularMatrix A matrix whose elements above or below the main diagonal are all zero.
SymmetricMatrix A matrix whose elements are symmetrical about the main diagonal. A symmetric matrix is equal to its transpose.
MatrixView A matrix that represents a range or slice of another matrix.

The Matrix class does not specify how matrix components should be stored. This is left entirely up to the derived classes. The derived matrix types provide their own optimized implementations for many of the base methods and properties. Wherever possible, use is made of optimized implementations of the standard Basic Linear Algebra Subroutines (BLAS) and the Linear Algebra PACKage (LAPACK).

Some matrrix types put restrictions on the components that can be changed. For example, you can only modify the non-zero elements of a TriangularMatrix. If you try to assign a value to a read-only component, an exception of type ComponentReadonlyException is thrown.

GeneralMatrix, as the name suggests, can be used to represent any matrix. It is the only matrix type whose components are all guaranteed to be writable. For this reason, it is used by methods that overwrite their matrix argument

Stand-alone and derived matrices

Like their vector relatives, matrices also come in two guises: stand-alone matrices and derived matrices.

Stand-alone matrices 'own' their components. Changing the value of a component does not have any side-effects. If there are no vectors or matrices derived from it, no operation unrelated to the stand-alone matrix can change any of its components.

Derived matrices take their components from another matrix. Changing the value of a component also changes the value of the corresponding component in the matrix from which it is derived. Changes to this underlying matrix are reflected in the derived matrix.

Many methods return matrices, so how are we to know whether the return value is a stand-alone matrix or a derived matrix? The rule is simple:

If a method returns a new matrix whose components consists of the unmodified components of another vector or matrix, then this return value is a derived matrix.

If a method returns a new matrix whose components are the result of a calculation involving the components of one or more vectors or matrices, then the return value is a stand-alone matrix.

Some methods return a reference to the instance the method was called on. In this case, the return value is not a new matrix, and the above rule does not apply. A method call on a matrix has no effect on whether the instance is a stand-alone or a derived matrix. There is just one exception: the CloneData method. This method converts a derived matrix to a stand-alone matrix.

Note also that certain methods return rows or columns of a matrix. These vectors are always derived vectors. Modifying the components of such a vector also changes the corresponding component of the matrix.

Copying matrices

There are three ways to make a copy of a matrix.

The ShallowCopy method creates a derived matrix that is a memberwise clone of the original. The new matrix has the same type and dimensions as the original matrix. When you change the value of a component of this matrix, the value of the corresponding component in the original matrix changes as well, and vice versa.

The Clone method, which implements the ICloneable interface, creates a stand-alone matrix that is a complete copy of the original. This method creates a matrix of the same type as the original, and makes its own copy of the component storage. When you change the value of a component of this matrix, the value of the corresponding component in the original matrix does not change. Likewise, if you change the value of a component of the original matrix, the value of the corresponding component in the copied matrix does not change.

You can convert a shallow copy to a full copy by calling the CloneData method. This method ensures that an instance has its own copy of the data. After a call to this method, any changes to the elements of this matrix will only affect this instance.

C# CopyCode imageCopy Code
// Create a vector, a shallow copy, and a clone:
Matrix m = new GeneralMatrix(2, 2);
m[1,1] = 2;
Matrix mShallowCopy = m.ShallowCopy();
Matrix mClone = m.Clone();
// This prints '2':
Console.WriteLine("m[1,1] = {0}", m[1,1]);
// Now change the value of this component.
m[1,1] = -1;
// Component changed. This prints '-1':
Console.WriteLine("m[1,1] = {0}", m[1,1]);
// Shallow copy changed, also. This prints '-1':
Console.WriteLine("mShallowCopy[1,1] = {0}", mShallowCopy[1,1]);
// Clone is left unchanged. This prints '2':
Console.WriteLine("mClone[1,1] = {0}", mClone[1,1]);
Visual Basic CopyCode imageCopy Code
' Create a vector, a shallow copy, and a clone:
Dim m1 As Matrix = New GeneralMatrix(2, 2)
m(1,1) = 2;
Dim mShallowCopy As Matrix = m.ShallowCopy();
Dim mClone As Matrix = m.Clone();
' This prints '2':
Console.WriteLine("m(1,1) = {0}", m(1,1));
' Now change the value of this component.
m(1,1) = -1;
' Component changed. This prints '-1':
Console.WriteLine("m(1,1) = {0}", m(1,1));
' Shallow copy changed, also. This prints '-1':
Console.WriteLine("mShallowCopy(1,1) = {0}", mShallowCopy(1,1));
' Clone is left unchanged. This prints '2':
Console.WriteLine("mClone(1,1) = {0}", mClone(1,1));

The third way to copy a vector is by calling the vector's ToGeneralMatrix method. This method returns a stand-alone GeneralMatrix whose components are copied from the original matrix. If this method is called on a GeneralMatrix, a new matrix is still created. In all cases, the components of the new matrix are guaranteed to be stored in a contiguous block of memory.

You can copy the components of a matrix to another, previously created vector using the CopyTo method. Care should be taken that you don't try to modify elements that are read-only. Any such attempt will result in a ComponentReadOnlyException.

Up: Matrices Next: General Matrices Previous: 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