Vector Basics
The Vector<T> class is an abstract base class that exposes the mathematical methods and properties of vectors. It also contains a default implementation for these methods and properties. A series of derived classes implement specific types of vectors. These are listed in the table below.
Class | Description |
|---|---|
A standard vector. The elements can take on any value. This is the most common type. | |
A vector whose elements are all equal to the same real number. | |
A vector that is made up of smaller vectors that may be of different types. | |
A vector that only stores the non-zero elements. |
In addition to these, several internal vector classes are used to represent rows, columns and diagonals of matrices.
The Vector<T> class does not specify how vector elements should be stored. This is left entirely up to the derived classes. The derived vector types provide their own optimized implementations for many of the base methods and properties. Wherever possible, use is made of optimized implementations of the Basic Linear Algebra Subroutines (BLAS).
Some vector types restrict assignment to individual coordinates. For example, a ConstantVector<T> represents one value repeated throughout the vector, so a coordinate cannot be changed independently. A supported whole-vector operation may still change its common value when the instance is mutable. Attempting an unsupported coordinate assignment throws ComponentReadOnlyException.
A DenseVector<T> can represent arbitrary values at every coordinate, but an instance may still be read-only. Storage representation and permission to write are separate concerns. Use IsElementWritable(Int32) to check a particular coordinate.
Creating vectors
Vectors can be created by calling one of the factory methods of the Vector class. Most of these methods are overloaded.
Method | Description |
|---|---|
Constructs a new dense vector with all elements initialized to zero. | |
Constructs a new constant vector. | |
Constructs a new sparse vector. | |
Constructs a new vector whose elements are restricted to a limited set of values (categories). | |
Constructs a new vector that is nonzero only over part of the vector. | |
Constructs a new linearly spaced vector. | |
Constructs a new logarithmically spaced vector. | |
Constructs a new vector that contains random values. |
Copying vectors
There are three ways to make a copy of a vector.
The ShallowCopy() method creates another object without copying its element storage. It is not an independent snapshot. Writes can be visible through both objects when storage is shared; copy-on-write instances may detach on mutation. Use a full clone when independence is required.
The Clone() method returns an independent copy. Its default cloning mode is NonzeroElementsWritable. The representation may change to make each logically nonzero coordinate independently writable. For example, cloning a nonzero constant vector can produce a dense vector. Changes to the clone do not change the original.
The overload Clone(CloningMethod) lets you choose the cloning contract:
Mode | Contract |
|---|---|
Preserves the representation and logical values without promising independent assignment at every coordinate. | |
Makes every logically nonzero source coordinate independently writable; this is the default. | |
Makes every logical coordinate independently writable, changing representation if necessary. | |
Creates an independent zero/default-valued vector with the same representation and structural layout, without promising positional writability. |
The CloneData() method detaches an instance from shared element storage when its representation supports this operation. Successful detachment also clears copy-on-write and shared-writable-storage state. It does not make structurally constrained coordinates independently writable. To obtain an independent object whose every coordinate is writable, use Clone(CloningMethod) with AllElementsWritable.
// Create a vector, a shallow copy, and a clone:
Vector v = Vector.Create(1, 2, 3, 4, 5);
Vector vShallowCopy = v.ShallowCopy();
Vector vClone = v.Clone();
// This prints '2':
Console.WriteLine("v[1] = {0}", v[1]);
// Now change the value of the 2nd component.
v[1] = -1;
// Component changed. This prints '-1':
Console.WriteLine("v[1] = {0}", v[1]);
// Shallow copy changed, also. This prints '-1':
Console.WriteLine("vShallowCopy[1] = {0}", v[1]);
// Clone is left unchanged. This prints '2':
Console.WriteLine("vClone[1] = {0}", v[1]);The third way to copy a vector is by calling the vector's ToDenseVector method. This method returns a stand-alone DenseVector<T> whose elements are copied from the original vector. If this method is called on a DenseVector<T>, a new vector is still created. In all cases, the elements of the new vector are guaranteed to be stored in a contiguous block of memory.
You can copy the elements of a vector to another, previously created vector using the CopyTo method. This method takes the second (destination) vector as its first and only argument. There is an overload that takes a second parameter, indicating the index in the destination where copying should begin. Care should be taken that you don't try to modify elements that are read-only. Any such attempt will result in a ComponentReadOnlyException.
Finally, the Swap<T> method swaps the elements between two vectors.