NMath User's Guide

TOC | Previous | Next | Index

4.1 DataBlock Classes (.NET, C#, CSharp, VB, Visual Basic, F#)

This section describes the data block classes that underlie the NMath matrix and vector types.

NOTE—You will rarely need to deal directly with data block objects.

Class Names

The classes that encapsulate blocks of data in NMath are named <Type>DataBlock, where <Type> is Float, Double, FloatComplex, or DoubleComplex. (See Chapter 3 for a description of the complex number structures.) Thus:

The FloatDataBlock class represents an array of single-precision floating point numbers.

The DoubleDataBlock class represents an array of double-precision floating point numbers.

The FloatComplexDataBlock class represents an array of single-precision complex numbers.

The DoubleComplexDataBlock class represents an array of double-precision complex numbers.

The data referenced by the NMath vector and matrix classes is in the form of an instance of one of the data block classes.

Data Block Properties

Each data block object contains a reference to an array of the appropriate datatype, and an offset into the array. For instance, a FloatComplexDataBlock object contains a reference to an array of FloatComplex instances.

Think of a data block as encapsulating the concept of a pointer without using unsafe code. The value of an equivalent pointer is the address of the first element of the array, plus the offset.

Data block classes have the following public, read-only properties:

The Data property returns the array referenced by the data block.

The Offset property returns the current offset into the array.

The Length property returns the number of elements currently referenced by the data block.

Accessing the Underlying Data

You rarely need to deal directly with data block objects. However, for applications that need to interface with native or legacy code, the NMath vector and matrix classes can be used to obtain a pointer to the underlying data. Each of these classes has a property called DataBlock that returns the data block object being viewed. As mentioned above, each data block class contains an array and an offset that allows you to extract a pointer to the beginning of the data. For example:

Code Example – C# data block

var v = new DoubleVector( 12, 0, 1 );



DoubleDataBlock dataBlock = v.DataBlock;
unsafe
{
  double *ptr = &(dataBlock.Data[dataBlock.Offset]);
  
  // Do something with *ptr here
}

NOTE—Exercise caution when using raw data pointers.

Vector and matrix classes also provide ToArray() methods that return data copied into an array. Thus:

Code Example – C# data block

var v = new DoubleVector( "1 2 3 4 5" );
double[] d = v.ToArray();



var A = new DoubleMatrix( "3x3 [1 2 3  4 5 6  7 8 9]" );
double[,] d2 = A.ToArray();

Code Example – VB data block

Dim V As New DoubleVector("1 2 3 4 5")
Dim D() As Double = V.ToArray()



Dim A As New DoubleMatrix("3x3 [1 2 3  4 5 6  7 8 9]")
Dim D2(,) As Double = A.ToArray()

Top

Top