The VectorT
class is an abstract base class and cannot be instantiated
directly. Instead, create instances of one of its derived classes by calling one
of the Vector class'
factory methods.
A DenseVectorT
represents a vector with arbitrary elements. Dense vectors are constructed with the
Create
method. This method has many overloads. All overloads take a generic type
argument that specifies the element type. In most cases, type inference eliminates
the need to specify the element type explicitly.
Passing a single integer to the method creates a dense vector of the specified length
with all its elements zero. The element type must be
specified as the generic type parameter.
The example below creates a vector of length 5:
var v1 = Vector.Create<double>(5);
Dim v1 = Vector.Create(Of Double)(5)
No code example is currently available or this language may not be supported.
let v1 = Vector.Create<double>(5)
A second overload takes a variable number of arguments, all of the same type,
that will serve as the elements of the new vector.
The values are copied into the vector's private data storage.
var v2 = Vector.Create(1.0, 2.0, 3.0, 4.0, 5.0);
Dim v2 = Vector.Create(1.0, 2.0, 3.0, 4.0, 5.0)
No code example is currently available or this language may not be supported.
let v2 = Vector.Create(1.0, 2.0, 3.0, 4.0, 5.0)
The third overload takes a parameter array of values.
No copy is made of this array. If you pass in an array directly,
any change in that array will be reflected in the vector.
The fourth overload takes an array containing the elements.
A second boolean parameter, reuseComponentArray,
may be provided. This specifies whether to use the element array directly
for data storage (reuseComponentArray = true),
or to copy the data to a private storage (reuseComponentArray = false).
In the example below, the second
vector is changed when an element of its storage array is changed.
double[] elements = { 1.0, 2.0, 3.0, 4.0, 5.0 };
var v3 = Vector.Create(elements);
var v4 = Vector.Create(elements, true);
elements[3] = 99.0;
Dim elements As Double() = {1.0, 2.0, 3.0, 4.0, 5.0}
Dim v3 = Vector.Create(elements)
Dim v4 = Vector.Create(elements, True)
elements(3) = 99.0
No code example is currently available or this language may not be supported.
let elements = [| 1.0; 2.0; 3.0; 4.0; 5.0 |]
let v3 = Vector.Create(elements)
let v4 = Vector.Create(elements, true)
elements.[3] <- 99.0
The next overload takes 4 arguments and offers the most options when creating
a vector from a part of an array. The first argument is the length of the vector.
The second argument is the storage array. The third and fourth arguments are the
offset of the first element of the vector in the array, and the stride between
vector elements in the array. The storage array is used for the vector's storage.
elements = new[] { 0.0, 1.0, 0.0, 2.0, 0.0, 3.0 };
var v5 = Vector.Create(3, elements, 1, 2, ArrayMutability.MutableValues);
elements = {0.0, 1.0, 0.0, 2.0, 0.0, 3.0}
Dim v5 = Vector.Create(3, elements, 1, 2, ArrayMutability.MutableValues)
No code example is currently available or this language may not be supported.
let elements = [| 0.0; 1.0; 0.0; 2.0; 0.0; 3.0 |]
let v5 = Vector.Create(3, elements, 1, 2, ArrayMutability.MutableValues)
A sixth overload takes a delegate that is used to initialize the elements of the vector.
The delegate maps the index of the element to its value:
var v6 = Vector.Create(5, i => 1.0 + i * i);
Dim v6 = Vector.Create(5, Function(i) 1.0 + i * i)
No code example is currently available or this language may not be supported.
let v6 = Vector.Create(5, fun i -> 1.0 + (float i) * (float i))
Some other methods also create dense vectors:
The CreateRange
method constructs a vector with linearly spaced elements. It takes up to 3 arguments.
When one argument is provided, it returns a vector with integers from 0 up to the argument value.
When two arguments are provided, it returns a vector with values from the first argument value
up to the second argument value with unit increments.
When three arguments are provided, it returns a vector with values from the first argument value
up to the second argument value where the third argument specifies the total number of steps.
var v7 = Vector.CreateRange(10);
var v8 = Vector.CreateRange(5.0, 10.0);
var v9 = Vector.CreateRange(5.0, 10.0, 5);
Dim v7 = Vector.CreateRange(10)
Dim v8 = Vector.CreateRange(5.0, 10.0)
Dim v9 = Vector.CreateRange(5.0, 10.0, 5)
No code example is currently available or this language may not be supported.
let v7 = Vector.CreateRange(10)
let v8 = Vector.CreateRange(5.0, 10.0)
let v9 = Vector.CreateRange(5.0, 10.0, 5)
The CreateRandom
method constructs a vector with random elements. The first argument is the length of the vector.
The second argument is the random number generator to use. This argument is optional.
var v10 = Vector.CreateRandom(10, ArrayMutability.Immutable);
var v11 = Vector.CreateRandom(10, new MersenneTwister(17));
Dim v10 = Vector.CreateRandom(10, ArrayMutability.Immutable)
Dim v11 = Vector.CreateRandom(10, New MersenneTwister(17))
No code example is currently available or this language may not be supported.
let v10 = Vector.CreateRandom(10, ArrayMutability.Immutable)
let v11 = Vector.CreateRandom(10, MersenneTwister(17))
A constant vector is a vector whose elements all have the same, constant value. Once created, none of the
elements 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 ConstantVectorT class and have only one overload with
two parameters. The first parameter specifies the length of the vector. The second argumentspecifies the constant
value. The following constructs a ConstantVector that represents the vector [2 2 2 2]:
Vector v1 = Vector.CreateConstant(4, 2.0);
Dim v1 As ConstantVector = Vector.CreateConstant(4, 2.0)
No code example is currently available or this language may not be supported.
No code example is currently available or this language may not be supported.
As mentioned earlier, constant vectors are also returned as diagonals of triangular matrices.
Band vectors are vectors whose elements are zero outside a contiguous
range of elements. The presence of the zero elements 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
BlockVectorT class.
The range of non-zero elements must be specified at the time of creation. Only elements within this range can
be modified. Any attempt to set an element outside this range results in a
ComponentReadOnlyException.
The simplest overload takes three integer parameters. The first argument is the length of the vector. The
second and third parameters specify the index of the first and last non-zero element of the vector. This example
creates a vector of the form [0 x x x 0 0]:
BandVector v1 = Vector.CreateBanded(6, 1, 3);
Dim v1 As BandVector = Vector.CreateBanded(6, 1, 3)
No code example is currently available or this language may not be supported.
No code example is currently available or this language may not be supported.
All elements are initially set to zero, but only the elements within the specified range can be modified.
A second overload takes a Double array as its fourth
parameter. This array specifies the non-zero elements of the band vector. The second argument is the length of the
new vector. The index of the first and last element are the 3rd and 4th parameters. The elements are
copied from the element array starting with the first element. The following constructs a band vector with value [0
1 2 3 0 0]:
double[] components = {1, 2, 3};
BandVector v2 = Vector.CreateBanded(6, 1, 3, components);
Dim components As Double() = {1, 2, 3, 4, 5};
Dim v2 As BandVector = Vector.CreateBanded(6, 1, 3, components)
No code example is currently available or this language may not be supported.
No code example is currently available or this language may not be supported.
Notice that the elements array does not contain any zero elements.
A third overload has an additional Boolean parameter, reuseComponentArray, that
specifies whether to use the element array directly for data storage (reuseComponentArray = true), or to copy the data to a private storage (reuseComponentArray = false). In the former case,
any changes to the original array cause the elements of the vector to change as well. The following
example illustrates what happens:
BandVector v3 = Vector.CreateBanded(6, 1, 3, components, true);
Console.WriteLine("v3 = {0}", v3);
components[1] = 1;
Console.WriteLine("v3 = {0}", v3);
Dim v3 As BandVector = Vector.CreateBanded(6, 1, 3, components, True)
Console.WriteLine("v3 = {0}", v3)
components(1) = 1
Console.WriteLine("v3 = {0}", v3)
No code example is currently available or this language may not be supported.
No code example is currently available or this language may not be supported.
Changing the value of the second element of Components changes the second non-zero element 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
triangular matrices.