SparseVector or an alias object referring to a mutable sparse vector.
The real result type is a temporary object of type pm::SameElementVector. It contains only a single copy of an element, or just a reference to an element, but makes it to appear as a sequence of identical elements.
The real result type is a temporary alias object of type pm::VectorSlice. It inherits the const-ness from the original vector.
This assertion is checked only in the DIMENSION_CHECKS compilation mode.
Random-access vector object: Vector, concatenation of random-access vectors, or a slice of a random-access vector with a random-access index set sequence or series.
if no preceding random access operations were performed on the sparse vector
The concrete vector type hidden behind the GenericVector.
if there already were random access operations on the sparse vector
Either a functor class satisfying the extended requirements for binary operations, or an untyped template of such a class wrapped into BuildBinary or BuildBinaryIt (preferably expressed via the convenience typedef from namespace polymake::operations.
A GenericSet<...,int> or Complement<..., int> is allowed here.
Vector or FixedVector. In the latter case the dimension argument is ignored, as the vector dimension is hard encoded into the object type.
The real result type is a temporary alias object of type pm::VectorChain. It is mutable only if both operands are mutable too.
The real return type is a temporary object (expression template) implementing the lazy evaluation of the operation result. You should have it in mind when passing the result of a vector expression as a GenericVector argument to a template function where it has a chance to be evaluated multiple times. See also prevent_lazy class.
The vector and matrix operators defined in the Polymake Template Library do take care of this, so this remark applies more likely to your own functions.
Any persistent vector: Vector or SparseVector, or an alias object referring to a mutable (non-const) vector.
The real result type is a masquerade reference pm::SingleElementVector&.
This assertion is checked only in the INDEX_CHECKS compilation mode.
Any persistent class: Vector, SparseVector, or FixedVector. In the latter case the dimension argument is ignored, as the vector dimension is hard encoded into the object type.
Either a functor class satisfying the extended requirements for unary operations, or an untyped template of such a class wrapped into BuildUnary or BuildUnaryIt (preferably expressed via the convenience typedef from namespace polymake::operations.
The real result type is a temporary object of type pm::SameElementSparseVector. It contains only a single copy of an element, or just a reference to it, but makes it to appear as a sequence of elements.
The real return type is a temporary proxy object, forwarding the read/write access operations to the sparse vector. Prior to the destruction, which happens after the completion of the expression envolving the random access, it checks whether the element became zero and, if this holds, removes it from the vector.

### Prerequisits

    #include <Vector.h>
    #include <SparseVector.h>
    using namespace polymake;

### Introduction

The Vector class family implement vectors in the usual algebraic notion. It contains three persistent vector types with different data storage organization:

template <typename ElementType> class Vector;

holds the elements in a contiguous array.

template <typename ElementType> class SparseVector;

is an associative container with element indices (coordinates) as keys; elements equal to the default value (ElementType(), which is 0 for most numerical types) are not stored, but implicitly encoded by the gaps in the key set. It is based on a balanced binary search (AVL) tree.

template <typename ElementType, int size> class FixedVector;

is a built-in array of a fixed dimension decorated with the common vector interface.

The following brief analysis might help you when choosing the most efficient representation for a concrete case. n is the number of (non-zero in sparse case) elements in the vector.

Performance comparison
Operation | Vector | SparseVector
--- | --- | ---
sequential iteration | O(n) | O(n), but with greater overhead constant
random element access | O(1) | O(log(n))
append an element | O(n) + reallocation costs | O(1) or O(log(n))

Vector and SparseVector keep the data attached to a smart pointer with reference counting.

### Generic type

template <typename _Vector, typename ElementType=typename _Vector::element_type> class GenericVector;

The parent generic class for all vector classes. Defines the following types:

element_type | ElementType, the persistent element type
top_type | _Vector, the concrete vector object behind the generic reference
generic_type | GenericVector<_Vector> itself
persistent_type | Vector<ElementType> for dense vectors, SparseVector<ElementType> for sparse vectors.

Methods and friend functions applicable to GenericVector directly are scattered over the entire page. They are discernible on the prefix GenericVector:: or parameters of type GenericVector&. To use other methods via a generic reference (or pointer), you must first move to the concrete object using the top() method.

### Construction

Vector();
Create a vector of dimension 0.
explicit Vector (int n);
Create a vector of dimension n, initialize all elements with 0 (in SparseVector implicitly.)
Vector (int n, const ElementType& value);
Create a vector of dimension n, initialize all elements with value. Use of corresponding constructor for SparseVector would defeat all the advantages of the sparse structure, therefore it is not defined.
template <typename Iterator> Vector (int n, Iterator src);
Create a vector of dimension n, initialize the elements from a data sequence.
For SparseVector, Iterator can be either indexed, or supply index-value pairs, e.g. std::pair<int,ElementType> or a plain sequence of data items. In the latter case zero elements are filtered out.
template <typename OtherVector> Vector (const GenericVector<OtherVector, ElementType>&);
Create a vector as a copy of another vector of the same element type.
template <typename OtherVector, typename OtherElementType> explicit Vector (const GenericVector<OtherVector, OtherElementType>&);
Copying with element conversion is also possible, but requires an explicit constructor call.
template <int n> Vector (const ElementType (&a)[n]);
Create a vector of dimension n, copy element values from a built-in array.
const GenericVector convert_to<OtherElementType>(const GenericVector&);
Create a vector with another element type, using explicit type conversion.

### Modification

template <typename OtherVector> GenericVector::operator= (const GenericVector<OtherVector, ElementType>&); template <int n> GenericVector::operator= (const ElementType (&a)[n]);
Persistent vector objects have after the assignment the same dimension as the right hand side operand. Alias objects, such as vector slice or concatenated vector, cannot be resized, thus they must have the same dimension as the right hand side operand.
void std::swap(GenericVector&, GenericVector&);
Swap the contents of two vectors in a most efficient way. If at least one non-persistent object is involved, the operands must have equal dimensions.
void Vector::resize(int n);
Extend or truncate to the new dimension. In the case of extension new elements are initialized with 0 (in SparseVector implicitly.)
void Vector::clear();
Truncate to dimension 0.
template <typename Scalar> void GenericVector::fill(const Scalar& value);
Fill with given value without changing the dimension. value can be of arbitrary type assignable to ElementType.
void GenericVector::remove0s();
Physically remove all zero elements from a sparse vector that might have creeped in by some previous operation.

### Element access

int GenericVector::dim() const;
Tell the current vector dimension. For dense vectors it is always the same as size(). For sparse vectors, however, the latter tells the number of non-zero elements.

#### Sequential access

All vector classes implement the STL Forward Container interface or one of its refinements. This means, every vector object can be traversed using an iterator or const_iterator created by begin() method or entire() function, its size can be retrieved with size() method and so on.

For a vector object accessed via GenericVector reference or pointer, you need first get to the concrete class with top() method before you can use the standard container interface.

SparseVector inherits the data access methods from the binary tree. Note that dereferencing the SparseVector::iterator yields a reference to the element value, not a key-value pair as in the std::map class or other associative containers. The SparseVector::iterator::index() method tells the current element index (coordinate.)

#### Random access

ElementType& GenericVector::operator[] (int i); const ElementType& GenericVector::operator[] (int i) const;
The index must lie in the valid range.
ElementType& SparseVector::operator[] (int i); const ElementType& SparseVector::operator[] (int i) const;
The const operator returns a reference to a static zero object if the specified element is an implicit 0.
The index must lie in the valid range.

### Operations

const GenericVector operator- (const GenericVector&); const GenericVector operator+ (const GenericVector&, const GenericVector&); const GenericVector operator- (const GenericVector&, const GenericVector&); const GenericVector operator* (const Scalar&, const GenericVector&); const GenericVector operator* (const GenericVector&, const Scalar&); const GenericVector operator/ (const GenericVector&, const Scalar&); ElementType operator* (const GenericVector&, const GenericVector&);
Do exactly what the mnemonics suggest. Every type combination is allowed, including mixing of sparse and dense vectors. ElementType of vector operands and Scalar type can be different, provided the arithmetic operator with corresponding arguments exists.
The dimensions of the operands must match.
_Vector& GenericVector::negate(); _Vector& GenericVector::operator+= (const GenericVector&); _Vector& GenericVector::operator-= (const GenericVector&); _Vector& GenericVector::operator*= (const Scalar&); _Vector& GenericVector::operator/= (const Scalar&);
Corresponding assignment versions of the arithmetic operators.
ElementType sqr(const GenericVector& a);
Scalar product a∗a.
const GenericVector tensor_product(const GenericVector& a, const GenericVector& b);
Tensor product T=a ⊗ b: T[i∗b.dim() + j] = a[i] ∗ b[j].
bool operator== (const GenericVector&, const GenericVector&); bool operator!= (const GenericVector&, const GenericVector&); bool operator< (const GenericVector&, const GenericVector&); bool operator> (const GenericVector&, const GenericVector&); bool operator<= (const GenericVector&, const GenericVector&); bool operator>= (const GenericVector&, const GenericVector&);
Compare two vectors lexicographically.
GenericVector::operator bool() const; bool GenericVector::operator! () const;
Look for a non-zero element.
namespace std_ext { template <typename ElementType> struct hash< Vector<ElementType> >; template <typename ElementType> struct hash< SparseVector<ElementType> >; }
These specializations allow to use the vectors as search keys in the containers std_ext::hash_set and std_ext::hash_map.

### Other constructions

GenericVector GenericVector::slice(const IndexSet& indices); GenericVector GenericVector::slice(int start, int size=0);
Select a vector slice consisting of elements with given indices. The last variant selects a contiguous range of indices beginning with start. size==0 means up to the end of the vector. The const variants of these methods create immutable slice objects.
The indices must lie in the valid range.
GenericVector operator| (const GenericVector&, const GenericVector&); GenericVector operator| (const Scalar&, const GenericVector&); GenericVector operator| (const GenericVector&, const Scalar&);
Concatenate two vectors, yielding a longer vector. A scalar argument is treated as a vector of dimension 1. ElementType of vector operands must be identical, otherwise you will get a rather cryptic message from the compiler.
Vector& Vector::operator|= (const GenericVector&); Vector& Vector::operator|= (const Scalar&);
Append a vector or a scalar, increasing the vector dimension.
const GenericVector scalar2vector(const ElementType& x); const GenericVector scalar2sparse_vector(const ElementType& x);
Disguise x as a dense or sparse vector of dimension 1.
const GenericVector same_element_vector(const ElementType& x, int n); const GenericVector ones_vector<ElementType>(int n); const GenericVector zero_vector<ElementType>(int n);
Create a dense vector of dimension n, with all elements equal to x, 1, or 0, respectively. Note the obligatory explicit specification of the template parameter ElementType, where it cannot be deduced from the function arguments.
const GenericVector same_element_sparse_vector(const Set& indices, const ElementType& x, int n); const GenericVector same_element_sparse_vector<ElementType>(const Set& indices, int n);
Create a sparse vector of dimension n, with elements equal to x at given coordinates and 0 as the rest (vice versa by Complement).
If omitted, x is taken as 1; note the obligatory explicit template parameter specification in this case.
const GenericVector unit_vector(int n, int i, const ElementType& x); const GenericVector unit_vector<ElementType>(int n, int i);
Create a sparse vector of dimension n, whose only non-zero element has the coordinate i and value x.
If omitted, x is taken as 1; note the obligatory explicit template parameter specification in this case.