NMath User's Guide

TOC |  Previous |  Next |  Index

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

The vector classes have the following read-only properties:

For instance, if v is a DoubleComplexVector instance:

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

NOTE- As described in Section 4.1, use caution when accessing a data block referenced 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 indicated by a given slice or range to a specified value.

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

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

Range evenElements = new Range( 0, Position.End, 2 );
Range oddElements = 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.

Resizing a Vector

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

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:

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

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

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

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

TOC |  Previous |  Next |  Index