Constructing Vectors | Extreme Optimization Numerical Libraries for .NET Professional |

The Vector

A DenseVector

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:

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.

The third 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). The default is 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); // v3 -> [ 1.0, 2.0, 3.0, 4.0, 5.0 ] var v4 = Vector.Create(elements, true); elements[3] = 99.0; // v4 -> [ 1.0, 2.0, 3.0, 99.0, 5.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); // v5 -> [ 1.0, 2.0, 3.0 ]

A fifth 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:

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); // v7 -> [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ] var v8 = Vector.CreateRange(5.0, 10.0); // v8 -> [ 5.0, 6.0, 7.0, 8.0, 9.0 ] var v9 = Vector.CreateRange(5.0, 10.0, 5); // v9 -> [ 5.0, 6.25, 7.5, 8.75, 10.0 ]

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); // v10 -> [ <10 random values between 0 and 1> ] var v11 = Vector.CreateRandom(10, new MersenneTwister(17)); // v11 -> [ ]

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 ConstantVector 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]:

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
BlockVector

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]:

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);

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);

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.

Copyright Â© 2004-20116,
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 *Optimized for Visual Studio* logo

are
registered trademarks of Microsoft Corporation.