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# | Copy 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 | Copy 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
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