# Sparse Vector

#### Description

The templated class `sparse_vector<T, A>` is the base container adaptor for sparse vectors. For a n-dimensional sparse vector and 0 <= i < n the non-zero elements vi are mapped to consecutive elements of the associative container, i.e. for elements k = vi1and k + 1 = vi2of the container holds i1 < i2.

#### Example

    #include <boost/numeric/ublas/vector_sparse.hpp>
    #include <boost/numeric/ublas/io.hpp>

    int main () {
        using namespace boost::numeric::ublas;
        sparse_vector<double> v (3, 3);
        for (unsigned i = 0; i < v.size (); ++ i)
            v (i) = i;
        std::cout << v << std::endl;
    }

#### Definition

Defined in the header vector_sparse.hpp.

#### Template parameters

Parameter | Description | Default
--- | --- | ---
`T` | The type of object stored in the sparse vector. |
`A` | The type of the adapted array. [1] | `map_std<std::size_t, T>`

#### Model of

Vector .

#### Type requirements

None, except for those imposed by the requirements of Vector .

#### Public base classes

`vector_expression<sparse_vector<T, A> >`

#### Members

Member | Description
--- | ---
`sparse_vector ()` | Allocates a `sparse_vector` that holds zero elements.
`sparse_vector (size_type size, size_type non_zeros = 0)` | Allocates a `sparse_vector` that holds at most `size` elements.
`sparse_vector (const sparse_vector &v)` | The copy constructor.
`template<class AE> sparse_vector (size_type non_zeros, const vector_expression<AE> &ae)` | The extended copy constructor.
`void resize (size_type size, bool preserve = true)` | Reallocates a `sparse_vector` to hold at most `size` elements. The existing elements of the `sparse_vector` are preseved when specified.
`size_type size () const` | Returns the size of the `sparse_vector`.
`const_reference operator () (size_type i) const` | Returns the value of the `i`-th element.
`reference operator () (size_type i)` | Returns a reference of the `i`-th element.
`const_reference operator [] (size_type i) const` | Returns the value of the `i`-th element.
`reference operator [] (size_type i)` | Returns a reference of the `i`-th element.
`sparse_vector &operator = (const sparse_vector &v)` | The assignment operator.
`sparse_vector &assign_temporary (sparse_vector &v)` | Assigns a temporary. May change the sparse vector `v` .
`template<class AE> sparse_vector &operator = (const vector_expression<AE> &ae)` | The extended assignment operator.
`template<class AE> sparse_vector &assign (const vector_expression<AE> &ae)` | Assigns a vector expression to the sparse vector. Left and right hand side of the assignment should be independent.
`template<class AE> sparse_vector &operator += (const vector_expression<AE> &ae)` | A computed assignment operator. Adds the vector expression to the sparse vector.
`template<class AE> sparse_vector &plus_assign (const vector_expression<AE> &ae)` | Adds a vector expression to the sparse vector. Left and right hand side of the assignment should be independent.
`template<class AE> sparse_vector &operator -= (const vector_expression<AE> &ae)` | A computed assignment operator. Subtracts the vector expression from the sparse vector.
`template<class AE> sparse_vector &minus_assign (const vector_expression<AE> &ae)` | Subtracts a vector expression from the sparse vector. Left and right hand side of the assignment should be independent.
`template<class AT> sparse_vector &operator *= (const AT &at)` | A computed assignment operator. Multiplies the sparse vector with a scalar.
`template<class AT> sparse_vector &operator /= (const AT &at)` | A computed assignment operator. Divides the sparse vector through a scalar.
`void swap (sparse_vector &v)` | Swaps the contents of the sparse vectors.
`void insert (size_type i, const_reference t)` | Inserts the value `t` at the `i`-th element.
`void erase (size_type i)` | Erases the value at the `i`-th element.
`void clear ()` | Clears the sparse vector.
`const_iterator begin () const` | Returns a `const_iterator` pointing to the beginning of the `sparse_vector`.
`const_iterator end () const` | Returns a `const_iterator` pointing to the end of the `sparse_vector`.
`iterator begin ()` | Returns a `iterator` pointing to the beginning of the `sparse_vector`.
`iterator end ()` | Returns a `iterator` pointing to the end of the `sparse_vector`.
`const_reverse_iterator rbegin () const` | Returns a `const_reverse_iterator` pointing to the beginning of the reversed `sparse_vector`.
`const_reverse_iterator rend () const` | Returns a `const_reverse_iterator` pointing to the end of the reversed `sparse_vector`.
`reverse_iterator rbegin ()` | Returns a `reverse_iterator` pointing to the beginning of the reversed `sparse_vector`.
`reverse_iterator rend ()` | Returns a `reverse_iterator` pointing to the end of the reversed `sparse_vector`.

#### Notes

[1] Supported parameters for the adapted array are `map_array<std::size_t, T>` and `map_std<std::size_t, T>`. The latter is equivalent to `std::map<std::size_t, T>`.

## Compressed Vector

#### Description

The templated class `compressed_vector<T, IB, IA, TA>` is the base container adaptor for compressed vectors. For a n-dimensional compressed vector and 0 <= i < n the non-zero elements vi are mapped to consecutive elements of the index and value container, i.e. for elements k = vi1and k + 1 = vi2of these containers holds i1 < i2.

#### Example

    #include <boost/numeric/ublas/vector_sparse.hpp>
    #include <boost/numeric/ublas/io.hpp>

    int main () {
        using namespace boost::numeric::ublas;
        compressed_vector<double> v (3, 3);
        for (unsigned i = 0; i < v.size (); ++ i)
            v (i) = i;
        std::cout << v << std::endl;
    }

#### Definition

Defined in the header vector_sparse.hpp.

#### Template parameters

Parameter | Description | Default
--- | --- | ---
`T` | The type of object stored in the compressed vector. |
`IB` | The index base of the compressed vector. [1] | `0`
`IA` | The type of the adapted array for indices. [2] | `unbounded_array<std::size_t>`
`TA` | The type of the adapted array for values. [2] | `unbounded_array<T>`

#### Model of

Vector .

#### Type requirements

None, except for those imposed by the requirements of Vector .

#### Public base classes

`vector_expression<compressed_vector<T, IB, IA, TA> >`

#### Members

Member | Description
--- | ---
`compressed_vector ()` | Allocates a `compressed_vector` that holds zero elements.
`compressed_vector (size_type size, size_type non_zeros)` | Allocates a `compressed_vector` that holds at most `size` elements.
`compressed_vector (const compressed_vector &v)` | The copy constructor.
`template<class AE> compressed_vector (size_type non_zeros, const vector_expression<AE> &ae)` | The extended copy constructor.
`void resize (size_type size, bool preserve = true)` | Reallocates a `compressed_vector` to hold at most `size` elements. The existing elements of the `compress_vector` are preseved when specified.
`size_type size () const` | Returns the size of the `compressed_vector`.
`const_reference operator () (size_type i) const` | Returns the value of the `i`-th element.
`reference operator () (size_type i)` | Returns a reference of the `i`-th element.
`const_reference operator [] (size_type i) const` | Returns the value of the `i`-th element.
`reference operator [] (size_type i)` | Returns a reference of the `i`-th element.
`compressed_vector &operator = (const compressed_vector &v)` | The assignment operator.
`compressed_vector &assign_temporary (compressed_vector &v)` | Assigns a temporary. May change the compressed vector `v`.
`template<class AE> compressed_vector &operator = (const vector_expression<AE> &ae)` | The extended assignment operator.
`template<class AE> compressed_vector &assign (const vector_expression<AE> &ae)` | Assigns a vector expression to the compressed vector. Left and right hand side of the assignment should be independent.
`template<class AE> compressed_vector &operator += (const vector_expression<AE> &ae)` | A computed assignment operator. Adds the vector expression to the compressed vector.
`template<class AE> compressed_vector &plus_assign (const vector_expression<AE> &ae)` | Adds a vector expression to the compressed vector. Left and right hand side of the assignment should be independent.
`template<class AE> compressed_vector &operator -= (const vector_expression<AE> &ae)` | A computed assignment operator. Subtracts the vector expression from the compressed vector.
`template<class AE> compressed_vector &minus_assign (const vector_expression<AE> &ae)` | Subtracts a vector expression from the compressed vector. Left and right hand side of the assignment should be independent.
`template<class AT> compressed_vector &operator *= (const AT &at)` | A computed assignment operator. Multiplies the compressed vector with a scalar.
`template<class AT> compressed_vector &operator /= (const AT &at)` | A computed assignment operator. Divides the compressed vector through a scalar.
`void swap (compressed_vector &v)` | Swaps the contents of the compressed vectors.
`void insert (size_type i, const_reference t)` | Inserts the value `t` at the `i`-th element.
`void erase (size_type i)` | Erases the value at the `i`-th element.
`void clear ()` | Clears the compressed vector.
`const_iterator begin () const` | Returns a `const_iterator` pointing to the beginning of the `compressed_vector`.
`const_iterator end () const` | Returns a `const_iterator` pointing to the end of the `compressed_vector`.
`iterator begin ()` | Returns a `iterator` pointing to the beginning of the `compressed_vector`.
`iterator end ()` | Returns a `iterator` pointing to the end of the `compressed_vector`.
`const_reverse_iterator rbegin () const` | Returns a `const_reverse_iterator` pointing to the beginning of the reversed `compressed_vector`.
`const_reverse_iterator rend () const` | Returns a `const_reverse_iterator` pointing to the end of the reversed `compressed_vector`.
`reverse_iterator rbegin ()` | Returns a `reverse_iterator` pointing to the beginning of the reversed `compressed_vector`.
`reverse_iterator rend ()` | Returns a `reverse_iterator` pointing to the end of the reversed `compressed_vector`.

#### Notes

[1] Supported parameters for the index base are `0` and `1` at least.

[2] Supported parameters for the adapted array are `unbounded_array<>` , `bounded_array<>` and `std::vector<>` .

## Coordinate Vector

#### Description

The templated class `coordinate_vector<T, IB, IA, TA>` is the base container adaptor for compressed vectors. For a n-dimensional sorted coordinate vector and 0 <= i < n the non-zero elements vi are mapped to consecutive elements of the index and value container, i.e. for elements k = vi1and k + 1 = vi2of these containers holds i1 < i2.

#### Example

    #include <boost/numeric/ublas/vector_sparse.hpp>
    #include <boost/numeric/ublas/io.hpp>

    int main () {
        using namespace boost::numeric::ublas;
        coordinate_vector<double> v (3, 3);
        for (unsigned i = 0; i < v.size (); ++ i)
            v (i) = i;
        std::cout << v << std::endl;
    }

#### Definition

Defined in the header vector_sparse.hpp.

#### Template parameters

Parameter | Description | Default
--- | --- | ---
`T` | The type of object stored in the coordinate vector. |
`IB` | The index base of the coordinate vector. [1] | `0`
`IA` | The type of the adapted array for indices. [2] | `unbounded_array<std::size_t>`
`TA` | The type of the adapted array for values. [2] | `unbounded_array<T>`

#### Model of

Vector .

#### Type requirements

None, except for those imposed by the requirements of Vector .

#### Public base classes

`vector_expression<coordinate_vector<T, IB, IA, TA> >`

#### Members

Member | Description
--- | ---
`coordinate_vector ()` | Allocates a `coordinate_vector` that holds zero elements.
`coordinate_vector (size_type size, size_type non_zeros)` | Allocates a `coordinate_vector` that holds at most `size` elements.
`coordinate_vector (const coordinate_vector &v)` | The copy constructor.
`template<class AE> coordinate_vector (size_type non_zeros, const vector_expression<AE> &ae)` | The extended copy constructor.
`void resize (size_type size, bool preserve = true)` | Reallocates a `coordinate_vector` to hold at most `size` elements. The existing elements of the `coordinate_vector` are preseved when specified.
`size_type size () const` | Returns the size of the `coordinate_vector`.
`const_reference operator () (size_type i) const` | Returns the value of the `i`-th element.
`reference operator () (size_type i)` | Returns a reference of the `i`-th element.
`const_reference operator [] (size_type i) const` | Returns the value of the `i`-th element.
`reference operator [] (size_type i)` | Returns a reference of the `i`-th element.
`coordinate_vector &operator = (const coordinate_vector &v)` | The assignment operator.
`coordinate_vector &assign_temporary (coordinate_vector &v)` | Assigns a temporary. May change the coordinate vector `v`.
`template<class AE> coordinate_vector &operator = (const vector_expression<AE> &ae)` | The extended assignment operator.
`template<class AE> coordinate_vector &assign (const vector_expression<AE> &ae)` | Assigns a vector expression to the coordinate vector. Left and right hand side of the assignment should be independent.
`template<class AE> coordinate_vector &operator += (const vector_expression<AE> &ae)` | A computed assignment operator. Adds the vector expression to the coordinate vector.
`template<class AE> coordinate_vector &plus_assign (const vector_expression<AE> &ae)` | Adds a vector expression to the coordinate vector. Left and right hand side of the assignment should be independent.
`template<class AE> coordinate_vector &operator -= (const vector_expression<AE> &ae)` | A computed assignment operator. Subtracts the vector expression from the coordinate vector.
`template<class AE> coordinate_vector &minus_assign (const vector_expression<AE> &ae)` | Subtracts a vector expression from the coordinate vector. Left and right hand side of the assignment should be independent.
`template<class AT> coordinate_vector &operator *= (const AT &at)` | A computed assignment operator. Multiplies the coordinate vector with a scalar.
`template<class AT> coordinate_vector &operator /= (const AT &at)` | A computed assignment operator. Divides the coordinate vector through a scalar.
`void swap (coordinate_vector &v)` | Swaps the contents of the coordinate vectors.
`void insert (size_type i, const_reference t)` | Inserts the value `t` at the `i`-th element.
`void erase (size_type i)` | Erases the value at the `i`-th element.
`void clear ()` | Clears the coordinate vector.
`const_iterator begin () const` | Returns a `const_iterator` pointing to the beginning of the `coordinate_vector`.
`const_iterator end () const` | Returns a `const_iterator` pointing to the end of the `coordinate_vector`.
`iterator begin ()` | Returns a `iterator` pointing to the beginning of the `coordinate_vector`.
`iterator end ()` | Returns a `iterator` pointing to the end of the `coordinate_vector`.
`const_reverse_iterator rbegin () const` | Returns a `const_reverse_iterator` pointing to the beginning of the reversed `coordinate_vector`.
`const_reverse_iterator rend () const` | Returns a `const_reverse_iterator` pointing to the end of the reversed `coordinate_vector`.
`reverse_iterator rbegin ()` | Returns a `reverse_iterator` pointing to the beginning of the reversed `coordinate_vector`.
`reverse_iterator rend ()` | Returns a `reverse_iterator` pointing to the end of the reversed `coordinate_vector`.

#### Notes

[1] Supported parameters for the index base are `0` and `1` at least.

[2] Supported parameters for the adapted array are `unbounded_array<>` , `bounded_array<>` and `std::vector<>` .

Copyright (©) 2000-2002 Joerg Walter, Mathias Koch
Permission to copy, use, modify, sell and distribute this document is granted provided this copyright notice appears in all copies. This document is provided ``as is'' without express or implied warranty, and with no claim as to its suitability for any purpose.

Last revised: 1/15/2003