Extreme Optimization >
User's Guide >
Vector and Matrix Library >
Vectors >
Constructing Vectors
Extreme Optimization User's Guide
User's Guide
Up: Vectors Next: Accessing Vector Components Previous: Vector Basics Contents
Constructing Vectors
Vector objects can be constructed in a number of ways. The
Vector class is an abstract base class and cannot be
instantiated directly. Instead,
create instances of one of its derived
classes.
General vectors
A GeneralVector
represents a vector with arbitrary components.
Passing a single integer to the constructor creates a
GeneralVector with all its components zero. This
example creates a vector of length 5:
| C# | Copy Code |
GeneralVector v1 = new GeneralVector(5); |
| Visual Basic | Copy Code |
Dim v1 As GeneralVector = New GeneralVector(5) |
A second constructor takes a variable number of
Double arguments, which specify the components of the
new vector. The values are copied into the vector's private data
storage.
| C# | Copy Code |
GeneralVector v2 = new GeneralVector(1, 2, 3, 4, 5); |
| Visual Basic | Copy Code |
Dim v2 As GeneralVector = New GeneralVector(1, 2, 3, 4, 5) |
The third variant of the constructor takes a Double
array containing the components. A second boolean parameter,
useExistingArray, may be provided. This specifies
whether to use the component array directly for data
storage (useExistingArray = true), or to copy the data
to a private storage (useExistingArray = false). The
default is false.
| C# | Copy Code |
double[] components = {1, 2, 3, 4, 5};
GeneralVector v3 = new GeneralVector(components); |
| Visual Basic | Copy Code |
Dim components As Double() = {1, 2, 3, 4, 5};
Dim v3 As GeneralVector = New GeneralVector(components) |
An optional boolean
parameter,useExistingArray, specifies whether the
existing component array is to be used (true), or
whether the components are copied. If the former is chosen
(useExistingArray = true), then changing the
components changes the values in the original array, and vice
versa:
| C# | Copy Code |
GeneralVector v4 = new GeneralVector(components, false);
Console.WriteLine("v4 = {0}", v4);
components[3] = 1;
Console.WriteLine("v4 = {0}", v4); |
| Visual Basic | Copy Code |
Dim v4 As GeneralVector = New GeneralVector(components, False)
Console.WriteLine("v4 = {0}", v4)
components(3) = 1
Console.WriteLine("v4 = {0}", v4) |
A fifth constructor takes a Double array containing
the components and an integer containing the length of the desired
vector as arguments. If the length of the components
array is larger than the specified length of the new vector, the
remaining elements are ignored. The last two options can also be
combined.
| C# | Copy Code |
GeneralVector v5 = new GeneralVector(4, components);
GeneralVector v6 = new GeneralVector(4, components, false); |
| Visual Basic | Copy Code |
Dim v5 As GeneralVector = New GeneralVector(4, components)
Dim v6 As GeneralVector = New GeneralVector(4, components, False) |
Band vectors
Band vectors are vectors whose components are zero outside a
contiguous range of components. The presence of the zero components
makes it possible to optimize many calculations. They get their
name from the fact that the rows and columns of a band
matrix are of this format. Band vectors are
implemented by the BandVector
class.
The range of non-zero components must be specified at the time
of creation. Only components within this range can be modified. Any
attempt to set a component outside this range results in a
ComponentReadOnlyException.
The simplest constructor takes three integer parameters. The
first parameter is the length of the vector. The second and third
parameters specify the index of the first and last non-zero
component of the vector. This example creates a vector of the form
[0 x x x 0 0]:
| C# | Copy Code |
BandVector v1 = new BandVector(6, 1, 3); |
| Visual Basic | Copy Code |
Dim v1 As BandVector = New BandVector(6, 1, 3) |
All components are initially set to zero, but only the
components within the specified range can be modified.
A second constructor takes a Double array
as its fourth parameter. This array specifies the
non-zero components of the band vector. The second parameter is the
length of the new vector. The index of the first and last
component are the 3rd and 4th paramters. The components
are copied from the component array starting with the first
element. The following constructs a band vector with value [0 1 2 3
0 0]:
| C# | Copy Code |
double[] components = {1, 2, 3};
BandVector v2 = new BandVector(6, 1, 3, components); |
| Visual Basic | Copy Code |
Dim components As Double() = {1, 2, 3, 4, 5};
Dim v2 As BandVector = New BandVector(6, 1, 3, components) |
Notice that the components array does not contain any zero
elements.
A third constructor has an additional Boolean
parameter, useExistingArray, that specifies
whether to use the component array directly for data
storage (useExistingArray = true), or to copy the data
to a private storage (useExistingArray = false). In
the former case, any changes to the original array cause
the components of the vector to change as well. The following
example illustrates what happens:
| C# | Copy Code |
BandVector v3 = new BandVector(6, 1, 3, components, true);
Console.WriteLine("v3 = {0}", v3);
components[1] = 1;
Console.WriteLine("v3 = {0}", v3); |
| Visual Basic | Copy Code |
Dim v3 As BandVector = New BandVector(6, 1, 3, components, True)
Console.WriteLine("v3 = {0}", v3)
components(1) = 1
Console.WriteLine("v3 = {0}", v3) |
Changing the value of the second element of
Components changes the second non-zero component of
v3, which is the third element overall.
Band vectors are created as the rows or columns of triangular
matrices. Details can be found in the section on triantgular
matrices.
Constant vectors
A constant vector is a vector whose components all have the
same, constant value. Once created, none of the components can be
changed. They are used mainly to represent unit diagonals (all 1's)
or sub- or superdiagonals (all 0's) of triangular matrices.
Constant vectors are implemented by the ConstantVector
class and have only one constructor with two parameters. The first
parameter specifies the length of the vector. The second parameter
specifies the constant value. The following constructs a
ConstantVector that represents the vector [2 2 2
2]:
| C# | Copy Code |
Vector v1 = new ConstantVector(4, 2.0); |
| Visual Basic | Copy Code |
Dim v1 As ConstantVector = New ConstantVector(4, 2.0) |
As mentioned earlier, constant vectors are also returned as
diagonals of triangular matrices.
Other vector types
The VectorView
class was designed to represent a vector whose components are a
subset of the components of another vector. Vector views can only
be created using the GetSubvector method.
See the next section
for more information.
The remaining vector types were designed specifically to
represent rows, columns, or diagonals of matrices. These classes do
not have public constructors. Use the GetRow,
GetColumn
or GetDiagonal
method of the matrix class to create instances of
these. See the section on accessing matrix rows and
columns for details.
Up: Vectors Next: Accessing Vector Components Previous: Vector Basics 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