NMath User's Guide

TOC | Previous | Next | Index

5.3 Value Operations on Vectors (.NET, C#, CSharp, VB, Visual Basic, F#)

The vector classes have the following read-only properties:

Length gets the number of data elements in a vector.

Stride gets the step between successive elements in the data block that a vector is viewing.

DataBlock gets a reference to the data block that a vector is viewing.

For instance, if v is a DoubleComplexVector instance:

Code Example – C# vector

int length = v.Length;
int stride = v.Stride;
DoubleComplexDataBlock block = v.DataBlock;

Code Example – VB vector

Dim Length As Integer = V.Length
Dim Stride As Integer = V.Stride
Dim Block As DoubleComplexDataBlock = V.DataBlock

NOTE—As described in Section 4.1, use caution when accessing a data block refer­enced by a vector. Other objects may be viewing the same data.

Accessing and Modifying Vector Values

The vector classes provide standard indexing operators for getting and setting element values. Thus, v[i] always returns the ith element of vector v's view of the data.

NOTE—Indexing starts at 0.

You can also use the Set() member function to set the data elements of a vector to a specified value.

For example, this code changes the contents of v to alternate values of 0 and 1:

Code Example – C# vector

var v = new FloatVector(10, 0, 1);

var evenElements = new Range( 0, Position.End, 2 );
var oddElements = new Range( 1, Position.End, 2 );

v.Set( evenElements, 0 );
v.Set( oddElements, 1 );

Code Example – VB vector

Dim V As New FloatVector(10, 0, 1)

Dim EvenElements As New Range(0, Position.End, 2)
Dim OddElements As New Range(1, Position.End, 2)

V.Set(EvenElements, 0)
V.Set(OddElements, 1)

NOTE—Any method that returns a vector view of the data referenced by a vector can be used to modify the values of the original vector, since the returned vector and the original vector share the data.

Clearing and Resizing a Vector

The vector classes provide two methods for changing the length of a vector after it has been created:

Clear()resetsthe value of all data elements to zero.

Resize() changes the size of a vector to the specified length, adding zeros or truncating as necessary.

ResizeAndClear() performs the same function as Resize(), but also resets the value of all remaining data elements to zero.

Appending to a Vector

You can add new elements to the end of a vector using the Append() methods. Thus, this code adds a single element to the end of a vector:

Code Example – C# vector

var v = new FloatVector( 10, 0, 0.5F );
float x = 5.5F;
v.Append( x );

Code Example – VB vector

Dim V As New FloatVector(10, 0, 0.5F)
Dim X As Single = 5.5F

This code appends another vector to the end of a vector:

Code Example – C# vector

var v = new DoubleVector( 10, 0, 1 );
var w = new DoubleVector( 5, 11, 1 );
v.Append( w );

Code Example – VB vector

Dim V As New DoubleVector(10, 0, 1)
Dim W As New DoubleVector(5, 11, 1)

Note that a new vector is allocated by the Append() methods, and data is copied.