NMath User's Guide

TOC |  Previous |  Next |  Index

6.9 Matrix Enumeration (.NET, C#, CSharp, Visual Basic, VB.NET)

NMath matrix classes provide standard .NET GetEnumerator() methods for returning IEnumerator objects. For example:

int rows = 13, cols = 3;
DoubleMatrix A = new DoubleMatrix( rows, cols, 0, .25 );
IEnumerator elements = A.GetEnumerator();

double[] data = new double[rows*cols];
i = 0;
while ( elements.MoveNext() )
{
  data[i++] = (double) elements.Current;
}

Note that the Current property on an IEnumerator returns the current object in the collection, which must then be cast to the appropriate type. NMath also provides custom strongly-typed enumerators: IFloatEnumerator, IDoubleEnumerator, IFloatComplexEnumerator, and IDoubleComplexEnumerator. These avoid casting, and are therefore much faster.

For instance:

int rows = 13, cols = 3;
DoubleMatrix A = new DoubleMatrix( rows, cols, 0, .25 );
IDoubleEnumerator elements = A.GetDoubleEnumerator();

double[] data = new double[rows*cols];
i = 0;
while ( elements.MoveNext() )
{
  data[i++] = elements.Current;       // No need to cast to double
}

TOC |  Previous |  Next |  Index