Extreme Optimization > Mathematics Library for .NET > User's Guide > Current Page > Accessing vector components

Extreme Optimization Mathematics Library for .NET

Accessing vector components

Different applications need to access vector components in different ways. The Extreme Optimization Mathematics Library for .NET accommodates these needs by providing four different options.

Direct indexed access

The Vector class has an indexer property. In languages that don't support indexers, you can use the indexed Item property. The index is zero based. If the index is greater than or equal to the length, an exception of type IndexOutOfRangeException is thrown.

Vector v = new GeneralVector(1, 2, 4, 8, 16);
// This prints "2":
Console.WriteLine("v[1] = {0}", v[1]);
v[1] = 7;
// This prints "[1 7 4 8 16]":
Console.WriteLine("v = {0}", v);
C#VB.NET 

The Range structure

A Range structure represents a range of indices. The following table illustrates the different ways to specify a range:

Expression Description
new Range(startIndex, endIndex) A range that runs from startIndex up to and including endIndex.
new Range(startIndex, endIndex, stride) A range that runs from startIndex to endIndex with an increments of stride.
Range.All A range that spans the entire vector.

StartIndex, EndIndex and Stride are properties of the Range structure and can be set directly. StartIndex is guaranteed to be the first index in the range. In the second example, the last index in the range is the largest number of the form StartIndex + kStride that is less than or equal to EndIndex. The Stride can also be negative. In this case, the last index in the range is the smallest number of the form StartIndex + kStride that is greater than or equal to EndIndex.

The special range Range.All can be used to represent a range over an entire row, column, or vector, without having to specify the EndIndex explicitly.

Indexed range access

Some vector operations operate on a range of components of a vector.

The indexer property of Vector is overloaded to allow a Range structure as an index. When getting this property, it returns a vector that spans the range specified by the index:

Vector v = new GeneralVector(0, 1, 2, 3, 4, 5, 6);
// This prints '[2, 3]':
Console.WriteLine("v[new Range(2, 3)] = {0}", v[new Range(2, 3)]);
// This prints '[2, 4]':
Console.WriteLine("v[new Range(2, 5, 2)] = {0}", v[new Range(2, 5, 2)]);
// This prints '[0, 1, 2, 3, 4, 5, 6]':
Console.WriteLine("v[Range.All] = {0}", v[Range.All]);
C#VB.NET 

The first example selects the 3rd and 4th elements of the vector v. (Indexes are 0-based!) The second example prints the components with index 2 and 4. The last example prints the entire vector.

When setting this property, the components in the range specified by the index are set to the components of the right-hand side.

Vector v = new GeneralVector(6);
Vector w = new GeneralVector(1, 2, 3);
v[new Range(2, 4)] = w;
// This prints '[0, 0, 1, 2, 3, 0]:
Console.WriteLine("v = {0}", v);
Vector x = new GeneralVector(6);
// x is the reverse of v:
x[new Range(5, 0, -1)] = v;
// This prints '[0, 3, 2, 1, 0, 0]:
Console.WriteLine("x = {0}", x);
C#VB.NET 

The first example sets the 3rd to 5th elements of a vector v to the components of w. The second example sets the reverse range of x equal to v. This, in effect, sets x equal to the reverse of v.

Subvector access

If more elaborate operations need to be performed on a range of components, it may be more appropriate to construct a subvector. A subvector is derived from the original. Changing a component of the subvector changes the corresponding component in the original, and vice versa.

Subvectors are created using the GetSubvector method. It has three overloads. The first one takes the index of the first and last elements as its parameter. A second variant has a third parameter that specifies the stride. A third overload takes a Range structure as its only argument. The following example illustrates the three variants:

Vector v = new GeneralVector(0, 1, 2, 3, 4, 5, 6);
Vector s1 = v.GetSubvector(2, 3);
// This prints '[2, 3]':
Console.WriteLine("s1 = {0}", s1);
Vector s2 = v.GetSubvector(2, 4, 2);
// This prints '[2, 4]':
Console.WriteLine("s2 = {0}", s2);
Vector s3 = v.GetSubvector(6, 0, -1);
// This prints '[6, 5, 4, 3, 2, 1, 0]':
Console.WriteLine("s3 = {0}", s3);
Vector s4 = v.GetSubvector(new Range(6, 0, -1));
// This also prints '[6, 5, 4, 3, 2, 1, 0]':
Console.WriteLine("s4 = {0}", s4);
C#VB.NET 

By default, a vector of type VectorView is created. Whenever possible, a more efficient vector is returned. For example, the GetSubvector method of the GeneralVector class always returns another GeneralVector.

Enumeration

The Vector class also supports the IEnumerable interface. A call to GetEnumerator returns a Vector.VectorEnumerator object that can be used to iterate through the components of the vector. For example, the code below calculates the product of the components of a vector:

double ProductOfComponents(Vector v)
{
    double product = 1;
    foreach(double component in v)
        product *= component;
        
    return product;
}
C#VB.NET 
Overview
Introduction
Features
Documentation
QuickStart Samples
Sample Applications
Downloads
Get it now!
Download trial version
How to Buy
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