The VectorT
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 |
---|
DenseVectorT | A standard vector. The elements can take on any value. This is the most common type. |
ConstantVectorT | A vector whose elements are all equal to the same real number. |
BlockVectorT | A vector that is made up of smaller vectors that may be of different types. |
SparseVectorT | 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 VectorT
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 put restrictions on the elements that can be changed.
For example, you can't modify the elements of a
ConstantVectorT
at all. If you try to assign a value to a read-only element,
an exception of type ComponentReadonlyException is thrown.
DenseVectorT
can be used to represent any vector. It is the only vector type whose elements
are all guaranteed to be writable.
Vectors can be created by calling one of the factory methods
of the Vector
class. Most of these methods are overloaded.
There are three ways to make a copy of a vector.
The ShallowCopy method creates a derived
vector that is a member-wise clone of the original. The new vector has the same type and length as the
original vector. When you change the value of an element of this vector, the value of the corresponding
element in the original vector changes as well, and vice versa.
The Clone method creates a
stand-alone vector that is a complete copy of the original. This method creates a vector of the same type
as the original, and makes its own copy of the element storage. When you change the value of an element
of this vector, the value of the corresponding element in the original vector does not change. Likewise, if you
change the value of an element of the original vector, the value of the corresponding element in the copied
vector does not change.
The Clone method takes an optional parameter of type CloningMethod. This parameter specifies
if you want to preserve the read-only elements of the vector. The possible values for this parameter are listed in
the following table:
ValueDescriptionMakes an exact clone of the object.Makes a copy of the object such that all non-zero elements are writable.Makes a copy of the object such that all elements are writable.
You can convert a shallow copy to a full copy by calling the CloneData method. This method ensures that an instance
has its own copy of the data. After a call to this method, any changes to the elements of this vector will only
affect this instance.
// 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]);
' 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))
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.
The third way to copy a vector is by calling the vector's
ToDenseVector method.
This method returns a stand-alone DenseVectorT
whose elements are copied from the original vector.
If this method is called on a DenseVectorT, 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 SwapT method swaps the elements
between two vectors.