# 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 put restrictions on the elements that can be changed. For example, you can't modify the elements of a ConstantVector<T> at all. If you try to assign a value to a read-only element, an exception of type ComponentReadonlyException is thrown.

DenseVector<T> can be used to represent any vector. It is the only vector type whose elements are all guaranteed to be writable.

## 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. | |

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

Value | Description |
---|---|

Complete | Makes an exact clone of the object. |

NonZeroComponentsWriteable | Makes a copy of the object such that all non-zero elements are writable. |

AllComponentsWriteable | 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]);
```

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.