Extreme Optimization >
User's Guide >
Vector and Matrix Library >
Vectors >
Accessing Vector Components
Extreme Optimization User's Guide
User's Guide
Up: Vectors Next: Mathematical Properties Previous: Constructing Vectors Contents
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.
| C# | Copy Code |
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); |
| Visual Basic | Copy Code |
Dim v1 As Vector = 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) |
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:
| C# | Copy Code |
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]); |
| Visual Basic | Copy Code |
Dim v As Vector = 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)) |
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.
| C# | Copy Code |
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); |
| Visual Basic | Copy Code |
Dim v As Vector = new GeneralVector(6);
Dim w As Vector = new GeneralVector(1, 2, 3);
v[New Range(2, 4)] = w;
' This prints '[0, 0, 1, 2, 3, 0]:
Console.WriteLine("v = {0}", v);
Dim x As Vector = 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); |
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:
| C# | Copy Code |
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); |
| Visual Basic | Copy Code |
Dim v As Vector = New GeneralVector(0, 1, 2, 3, 4, 5, 6)
Dim s1 As Vector = v.GetSubvector(2, 3)
' This prints '[2, 3]':
Console.WriteLine("s1 = {0}", s1);
Dim s2 As Vector = v.GetSubvector(2, 4, 2)
' This prints '[2, 4]':
Console.WriteLine("s2 = {0}", s2);
Dim s3 As Vector = v.GetSubvector(6, 0, -1)
' This prints '[6, 5, 4, 3, 2, 1, 0]':
Console.WriteLine("s3 = {0}", s3)
Dim s4 As Vector = v.GetSubvector(New Range(6, 0, -1));
' This also prints '[6, 5, 4, 3, 2, 1, 0]':
Console.WriteLine("s4 = {0}", s4) |
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:
| C# | Copy Code |
double ProductOfComponents(Vector v)
{
double product = 1;
foreach(double component in v)
product *= component;
return product;
} |
| Visual Basic | Copy Code |
Function ProductOfComponents(v As Vector) As Double
Dim product As Double = 1
Dim component As Double
For Each component In v
product = product * component
Next
Return product
End Function |
Up: Vectors Next: Mathematical Properties Previous: Constructing Vectors Contents
Copyright 2004-2008,
Extreme Optimization. All rights reserved.
Extreme Optimization, Complexity made simple, M#, and M
Sharp are trademarks of ExoAnalytics Inc.
Microsoft, Visual C#, Visual Basic, Visual Studio, Visual
Studio.NET, and the Visual Studio Logo are registered trademarks of Microsoft Corporation