Extreme Optimization >
Mathematics Library for .NET >
User's Guide >
Current Page >
Vector Basics
Extreme Optimization Mathematics Library for .NET
Vector Basics
The Vector
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 |
GeneralVector |
A standard vector. The components can take on any value. This is the most
common type. |
BandVector |
A vector that has one or more zero components at the beginning and/or at the
end. |
ConstantVector |
A vector whose elements are all equal to the same real number. |
VectorView |
A vector that represents a range or slice of another vector. |
RowVector |
A vector that represents (part of) a row in a matrix. |
ColumnVector |
A vector that represents (part of) a column in a matrix. |
DiagonalVector |
A vector that represents (part of) a diagonal in a matrix. |
The Vector
class does not specify how vector components 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 components that can be changed. For
example, you can only modify the non-zero elements of a BandVector.
If you try to assign a value to a read-only component, an exception
of type ComponentReadonlyException is thrown.
GeneralVector, as the name suggests, can be used to represent any
vector. It is the only vector type whose components are all guaranteed to be
writable. For this reason, it is used by methods that overwrite their vector
argument
Stand-alone and derived vectors
Vectors come in two guises: stand-alone vectors and derived vectors.
Stand-alone vectors 'own' their components. Changing the value of a
component does not have any side-effects. If there are no vectors derived from
it, no operation unrelated to the stand-alone vector can change any of its
components.
Derived vectors take their components from another vector or a matrix. Changing
the value of a component also changes the value of the corresponding
component in the vector or matrix from which it is derived. Changes to this
underlying vector or matrix are reflected in the derived vector.
Many methods return vectors, so how are we to know whether the return value is a
stand-alone vector or a derived vector? The rule is simple:
If a method returns a new vector whose components consists of the unmodified
components of another vector or matrix, then this return value is a derived
vector.
If a method returns a new vector whose components are the result
of a calculation involving the components of one or more
vectors or matrices, then the return value is a stand-alone vector.
Some methods return a reference to the instance the method was called
on. In this case, the return value is not a new vector and the above
rule does not apply. A method call on a vector has no effect on
whether the instance is a stand-alone or a derived vector. There is just one
exception: the
CloneData method. This method converts a derived vector to a
stand-alone vector.
Copying vectors
There are three ways to make a copy of a vector.
The
ShallowCopy method creates a derived vector that is a
memberwise clone of the original. The new vector has the same type
and length as the original vector. When you change the value of a
component of this vector, the value of the corresponding component 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 component storage. When you change the
value of a component of this vector, the value of the corresponding component
in the original vector does not change. Likewise, if you change the value of a
component of the original vector, the value of the corresponding component 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 components of the
vector. The possible values for this parameter are listed in the following
table:
| Value |
Description |
Complete |
Makes an exact clone of the object. |
NonZeroComponentsWriteable |
Makes a copy of the object such that all non-zero
components are writable. |
AllComponentsWriteable |
Makes a copy of the object such that all components 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 = new GeneralVector(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 = new GeneralVector(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))
|
| C# | VB.NET | |
The third way to copy a vector is by calling the vector's ToGeneralVector
method. This method returns a stand-alone GeneralVector whose
components are copied from the original vector. If this method is called on a
GeneralVector, a new vector is still created. In all cases, the
components of the new vector are guaranteed to be stored in a contiguous block
of memory.
You can copy the components of a vector to another, previously created vector
using the
CopyTo method. The
first variant of this method takes the second (destination) vector as
its first and only argument. There is
another variant 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 method swaps the components between two vectors.
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