Extreme Optimization >
User's Guide >
Vector and Matrix Library >
Matrices >
General Matrices
Extreme Optimization User's Guide
User's Guide
Up: Matrices Next: Accessing Matrix Components Previous: Matrix Basics Contents
General matrices
A GeneralMatrix
represents a dense matrix with arbitrary components.
How general matrices are stored
The components of a GeneralMatrix are stored in a
one-dimensional Double array. Matrix components can be
stored in two ways: column major and row major. Column major order
means that the components of each column are stored in adjacent
elements of the array. Row major order means that the components of
each row are stored in adjacent elements of the array.
For historical reasons, most linear algebra algorithms have been
optimized for column major storage. For this reason, column major
order is the default. Row major order is supported transparently.
Conversions are performed when necessary. Very often, row major
order can be used without any significant loss of speed.
The element order is usually only important when initializing
the components of a matrix. No application should have any other
explicit depency on the way matrix components are stored.
The element order is given by the ElementOrder
property, which is of type MatrixElementOrder.
Possible values are ColumnMajor, RowMajor
and NotApplicable.
Constructing general matrices
The constructors for GeneralMatrix fall into three
clear groups.
The first group has two members. The first constructor has two
parameters: the number of rows and the number of columns in the
matrix. This creates a matrix with the specified number of rows and
columns. All components are initially set to zero. The example
below creates a matrix with 3 rows and 4 columns:
| C# | Copy Code |
GeneralMatrix m1 = new GeneralMatrix(3, 4); |
| Visual Basic | Copy Code |
Dim m1 As GeneralMatrix = New GeneralMatrix(3, 4) |
A third parameter can be added that specifies the order in which
matrix components are stored. This parameter is of the
MatrixElementOrder enumeration type. The following
creates a matrix with 3 rows and 4 columns, whose elements are
stored in row major order:
| C# | Copy Code |
GeneralMatrix m2 = new GeneralMatrix(3, 4, MatrixElementOrder.RowMajor); |
| Visual Basic | Copy Code |
Dim m2 As GeneralMatrix = New GeneralMatrix(3, 4, MatrixElementOrder.RowMajor) |
The second group of constructors lets you initialize the matrix.
The first and second parameters are once again the number of rows
and the number of columns. The matrix is initialized using the
values in a Double array. For example, to create a
matrix with 3 rows and 2 columns, you need:
| C# | Copy Code |
double[] components = {11, 21, 31, 12, 22, 32};
GeneralMatrix m3 = new GeneralMatrix(3, 2, components); |
| Visual Basic | Copy Code |
Dim components As Double() = {11, 21, 31, 12, 22, 32}
Dim m3 As GeneralMatrix = New GeneralMatrix(3, 2, components) |
The elements are stored in column major order by default. This
means that the first column has as its components 11, 21, and 31.
The first row has components 11 and 12.
You can specify the order of the componets by adding a parameter
of type MatrixElementOrder. The following creates a
matrix that is identical to the previous one, but its elements are
provided and stored in row major order:
| C# | Copy Code |
double[] componentsByRow = {11, 12, 21, 22, 31, 32};
GeneralMatrix m4 = new GeneralMatrix(3, 2, components,
MatrixElementOrder.RowMajor); |
| Visual Basic | Copy Code |
Dim componentsByRow As Double() = {11, 21, 31, 12, 22, 32}
Dim m4 As GeneralMatrix = New GeneralMatrix(3, 2, components, _
MatrixElementOrder.RowMajor) |
By default, the matrix elements are copied from the component
array to a private storage array. This may become expensive when
the number of components is large. You can let the new matrix reuse
the component array by supplying a Boolean parameter
with a value of True. The example below creates
matrices identical to the two examples above, but the
componentarray is reused:
| C# | Copy Code |
// Column major order:
double[] components = {11, 21, 31, 12, 22, 32};
GeneralMatrix m5 = new GeneralMatrix(3, 2, components, true);
// Row major order:
double[] componentsByRow = {11, 12, 21, 22, 31, 32};
GeneralMatrix m6 = new GeneralMatrix(3, 2, components,
MatrixElementOrder.RowMajor, true); |
| Visual Basic | Copy Code |
' Column major order:
Dim components As Double() = {11, 21, 31, 12, 22, 32}
Dim m5 As GeneralMatrix = New GeneralMatrix(3, 2, components, True)
' Row major order:
Dim componentsByRow As Double() = {11, 21, 31, 12, 22, 32}
Dim m6 As GeneralMatrix = New GeneralMatrix(3, 2, componentsByRow, _
MatrixElementOrder.RowMajor, True) |
It is important to note the effects of reusing the component
array. Any change to an element of the component array will change
the corresponding component of the matrix, and vice versa. In
general, the component array should not be used for any other
purpose.
One last constructor uses a two-dimensional Double
array that contains the components of the new matrix. It is not
necessary to specify the number of rows and columns. These are
taken from the dimensions of the component array. Let's once again
construct the same matrix using this last constructor:
| C# | Copy Code |
double[,] components = {{11, 12}, {21, 22}, {31, 32}};
GeneralMatrix m7 = new GeneralMatrix(components); |
| Visual Basic | Copy Code |
Dim components As Double(,) = {{11, 12}, {21, 22}, {31, 32}}
Dim m7 As GeneralMatrix = New GeneralMatrix(components) |
It is generally recommended to not use two-dimensional arrays.
This constructor is mostly supplied for convenience.
Up: Matrices Next: Accessing Matrix Components Previous: Matrix Basics 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