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# CopyCode imageCopy Code
GeneralMatrix m1 = new GeneralMatrix(3, 4);
Visual Basic CopyCode imageCopy 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# CopyCode imageCopy Code
GeneralMatrix m2 = new GeneralMatrix(3, 4, MatrixElementOrder.RowMajor);
Visual Basic CopyCode imageCopy 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# CopyCode imageCopy Code
double[] components = {11, 21, 31, 12, 22, 32};
GeneralMatrix m3 = new GeneralMatrix(3, 2, components);
Visual Basic CopyCode imageCopy 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# CopyCode imageCopy Code
double[] componentsByRow = {11, 12, 21, 22, 31, 32};
GeneralMatrix m4 = new GeneralMatrix(3, 2, components,
        MatrixElementOrder.RowMajor);
Visual Basic CopyCode imageCopy 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# CopyCode imageCopy 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 CopyCode imageCopy 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# CopyCode imageCopy Code
double[,] components = {{11, 12}, {21, 22}, {31, 32}};
GeneralMatrix m7 = new GeneralMatrix(components);
Visual Basic CopyCode imageCopy 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

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