# Vector

#### Description

The templated class `vector<T, A>` is the base container adaptor for dense vectors. For a n-dimensional vector and 0 <= i < n every element vi is mapped to the i-th element of the container.

#### Example

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

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

#### Definition

Defined in the header vector.hpp.

#### Template parameters

Parameter | Description | Default
--- | --- | ---
`T` | The type of object stored in the vector. |
`A` | The type of the adapted array. [1] | `unbounded_array<T>`

#### Model of

Vector .

#### Type requirements

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

#### Public base classes

`vector_expression<vector<T, A> >`

#### Members

Member | Description
--- | ---
`vector ()` | Allocates an uninitialized `vector` that holds zero elements.
`vector (size_type size)` | Allocates an uninitialized `vector` that holds `size` elements.
`vector (const vector &v)` | The copy constructor.
`template<class AE> vector (const vector_expression<AE> &ae)` | The extended copy constructor.
`void resize (size_type size)` | Reallocates a `vector` to hold `size` elements. The content of the `vector` is not preserved.
`size_type size () const` | Returns the size of the `vector`.
`const_reference operator () (size_type i) const` | Returns a `const` reference 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 a `const` reference of the `i` -th element.
`reference operator [] (size_type i)` | Returns a reference of the `i`-th element.
`vector &operator = (const vector &v)` | The assignment operator.
`vector &assign_temporary (vector &v)` | Assigns a temporary. May change the vector `v`.
`template<class AE> vector &operator = (const vector_expression<AE> &ae)` | The extended assignment operator.
`template<class AE> vector &assign (const vector_expression<AE> &ae)` | Assigns a vector expression to the vector. Left and right hand side of the assignment should be independent.
`template<class AE> vector &operator += (const vector_expression<AE> &ae)` | A computed assignment operator. Adds the vector expression to the vector.
`template<class AE> vector &plus_assign (const vector_expression<AE> &ae)` | Adds a vector expression to the vector. Left and right hand side of the assignment should be independent.
`template<class AE> vector &operator -= (const vector_expression<AE> &ae)` | A computed assignment operator. Subtracts the vector expression from the vector.
`template<class AE> vector &minus_assign (const vector_expression<AE> &ae)` | Subtracts a vector expression from the vector. Left and right hand side of the assignment should be independent.
`template<class AT> vector &operator *= (const AT &at)` | A computed assignment operator. Multiplies the vector with a scalar.
`template<class AT> vector &operator /= (const AT &at)` | A computed assignment operator. Divides the vector through a scalar.
`void swap (vector &v)` | Swaps the contents of the 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 vector.
`const_iterator begin () const` | Returns a `const_iterator` pointing to the beginning of the `vector`.
`const_iterator end () const` | Returns a `const_iterator` pointing to the end of the `vector`.
`iterator begin ()` | Returns a `iterator` pointing to the beginning of the `vector`.
`iterator end ()` | Returns a `iterator` pointing to the end of the `vector`.
`const_reverse_iterator rbegin () const` | Returns a `const_reverse_iterator` pointing to the beginning of the reversed `vector`.
`const_reverse_iterator rend () const` | Returns a `const_reverse_iterator` pointing to the end of the reversed `vector`.
`reverse_iterator rbegin ()` | Returns a `reverse_iterator` pointing to the beginning of the reversed `vector`.
`reverse_iterator rend ()` | Returns a `reverse_iterator` pointing to the end of the reversed `vector`.

#### Notes

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

#### Interface

        // Array based vector class
        template<class T, class A>
        class vector:
            public vector_expression<vector<T, A> > {
        public:
            typedef std::size_t size_type;
            typedef std::ptrdiff_t difference_type;
            typedef T value_type;
            typedef const T &const_reference;
            typedef T &reference;
            typedef const T *const_pointer;
            typedef T *pointer;
            typedef F functor_type;
            typedef A array_type;
            typedef const A const_array_type;
            typedef const vector<T, A> const_self_type;
            typedef vector<T, A> self_type;
            typedef const vector_const_reference<const_self_type> const_closure_type;
            typedef vector_reference<self_type> closure_type;
            typedef typename A::const_iterator const_iterator_type;
            typedef typename A::iterator iterator_type;
            typedef dense_tag storage_category;

            // Construction and destruction
            vector ();
            vector (size_type size);
            vector (const vector &v);
            template<class AE>
            vector (const vector_expression<AE> &ae);

            // Accessors
            size_type size () const;
            const_array_type &data () const;
            array_type &data ();

            // Resizing
            void resize (size_type size);

            // Element access
            const_reference operator () (size_type i) const;
            reference operator () (size_type i);

            const_reference operator [] (size_type i) const;
            reference operator [] (size_type i);

            // Assignment
            vector &operator = (const vector &v);
            vector &assign_temporary (vector &v);
            template<class AE>
            vector &operator = (const vector_expression<AE> &ae);
            template<class AE>
            vector &reset (const vector_expression<AE> &ae);
            template<class AE>
            vector &assign (const vector_expression<AE> &ae);
            template<class AE>
            vector &operator += (const vector_expression<AE> &ae);
            template<class AE>
            vector &plus_assign (const vector_expression<AE> &ae);
            template<class AE>
            vector &operator -= (const vector_expression<AE> &ae);
            template<class AE>
            vector &minus_assign (const vector_expression<AE> &ae);
            template<class AT>
            vector &operator *= (const AT &at);
            template<class AT>
            vector &operator /= (const AT &at);

            // Swapping
            void swap (vector &v);
            friend void swap (vector &v1, vector &v2);

            // Element insertion and erasure
            void insert (size_type i, const_reference t);
            void erase (size_type i);
            void clear ();

            class const_iterator;
            class iterator;

            // Element lookup
            const_iterator find (size_type i) const;
            iterator find (size_type i);
            const_iterator find_first (size_type i) const;
            iterator find_first (size_type i);
            const_iterator find_last (size_type i) const;
            iterator find_last (size_type i);

            // Iterators simply are pointers.

            class const_iterator:
                public container_const_reference<vector>,
                public random_access_iterator_base<const_iterator, value_type> {
            public:
                typedef dense_random_access_iterator_tag iterator_category;
                typedef typename vector::difference_type difference_type;
                typedef typename vector::value_type value_type;
                typedef typename vector::const_reference reference;
                typedef typename vector::const_pointer pointer;

                // Construction and destruction
                const_iterator ();
                const_iterator (const vector &v, const const_iterator_type &it);
                const_iterator (const iterator &it):

                // Arithmetic
                const_iterator &operator ++ ();
                const_iterator &operator -- ();
                const_iterator &operator += (difference_type n);
                const_iterator &operator -= (difference_type n);
                difference_type operator - (const const_iterator &it) const;

                // Dereference
                reference operator * () const;

                // Index
                size_type index () const;

                // Assignment
                const_iterator &operator = (const const_iterator &it);

                // Comparison
                bool operator == (const const_iterator &it) const;
                bool operator < (const const_iterator &it) const;
            };

            const_iterator begin () const;
            const_iterator end () const;

            class iterator:
                public container_reference<vector>,
                public random_access_iterator_base<iterator, value_type> {
            public:
                typedef dense_random_access_iterator_tag iterator_category;
                typedef typename vector::difference_type difference_type;
                typedef typename vector::value_type value_type;
                typedef typename vector::reference reference;
                typedef typename vector::pointer pointer;

                // Construction and destruction
                iterator ();
                iterator (vector &v, const iterator_type &it);

                // Arithmetic
                iterator &operator ++ ();
                iterator &operator -- ();
                iterator &operator += (difference_type n);
                iterator &operator -= (difference_type n);
                difference_type operator - (const iterator &it) const;

                // Dereference
                reference operator * () const;

                // Index
                size_type index () const;

                // Assignment
                iterator &operator = (const iterator &it);

                // Comparison
                bool operator == (const iterator &it) const;
                bool operator < (const const_iterator &it) const;
            };

            iterator begin ();
            iterator end ();

            // Reverse iterator

            typedef reverse_iterator_base<const_iterator> const_reverse_iterator;

            const_reverse_iterator rbegin () const;
            const_reverse_iterator rend () const;

            typedef reverse_iterator_base<iterator> reverse_iterator;

            reverse_iterator rbegin ();
            reverse_iterator rend ();
        };

## Unit Vector

#### Description

The templated class `unit_vector<T>` represents canonical unit vectors. For the k-th n-dimensional canonical unit vector and 0 <= i < n holds uk i = 0, if i <> k, and u ki = 1.

#### Example

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

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

#### Definition

Defined in the header vector.hpp.

#### Template parameters

Parameter | Description | Default
--- | --- | ---
`T` | The type of object stored in the vector. |

#### Model of

Vector Expression .

#### Type requirements

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

#### Public base classes

`vector_expression<unit_vector<T> >`

#### Members

Member | Description
--- | ---
`unit_vector ()` | Constructs an `unit_vector` that holds zero elements.
`unit_vector (size_type size, size_type index)` | Constructs the `index`-th `unit_vector` that holds `size` elements.
`unit_vector (const unit_vector &v)` | The copy constructor.
`void resize (size_type size)` | Resizes a `unit_vector` to hold `size` elements.
`size_type size () const` | Returns the size of the `unit_vector`.
`size_type index () const` | Returns the index of the `unit_vector`.
`const_reference operator () (size_type i) const` | Returns the value of the `i`-th element.
`const_reference operator [] (size_type i) const` | Returns the value of the `i`-th element.
`unit_vector &operator = (const unit_vector &v)` | The assignment operator.
`unit_vector &assign_temporary (unit_vector &v)` | Assigns a temporary. May change the unit vector `v` .
`void swap (unit_vector &v)` | Swaps the contents of the unit vectors.
`const_iterator begin () const` | Returns a `const_iterator` pointing to the beginning of the `unit_vector`.
`const_iterator end () const` | Returns a `const_iterator` pointing to the end of the `unit_vector`.
`const_reverse_iterator rbegin () const` | Returns a `const_reverse_iterator` pointing to the beginning of the reversed `unit_vector`.
`const_reverse_iterator rend () const` | Returns a `const_reverse_iterator` pointing to the end of the reversed `unit_vector`.

#### Interface

        // Unit vector class
        template<class T>
        class unit_vector:
            public vector_expression<unit_vector<T> > {
        public:
            typedef std::size_t size_type;
            typedef std::ptrdiff_t difference_type;
            typedef T value_type;
            typedef const T &const_reference;
            typedef T &reference;
            typedef const T *const_pointer;
            typedef T *pointer;
            typedef const unit_vector<T> const_self_type;
            typedef unit_vector<T> self_type;
            typedef const vector_const_reference<const_self_type> const_closure_type;
            typedef size_type const_iterator_type;
            typedef packed_tag storage_category;

            // Construction and destruction
            unit_vector ();
            unit_vector (size_type size, size_type index);
            unit_vector (const unit_vector &v);

            // Accessors
            size_type size () const;
            size_type index () const;

            // Resizing
            void resize (size_type size);

            // Element access
            const_reference operator () (size_type i) const;

            const_reference operator [] (size_type i) const;

            // Assignment
            unit_vector &operator = (const unit_vector &v);
            unit_vector &assign_temporary (unit_vector &v);

            // Swapping
            void swap (unit_vector &v);
            friend void swap (unit_vector &v1, unit_vector &v2);

            class const_iterator;

            // Element lookup
            const_iterator find_first (size_type i) const;
            const_iterator find_last (size_type i) const;

            // Iterator simply is an index.

            class const_iterator:
                public container_const_reference<unit_vector>,
                public random_access_iterator_base<const_iterator, value_type> {
            public:
                typedef packed_random_access_iterator_tag iterator_category;
                typedef typename unit_vector::difference_type difference_type;
                typedef typename unit_vector::value_type value_type;
                typedef typename unit_vector::const_reference reference;
                typedef typename unit_vector::const_pointer pointer;

                // Construction and destruction
                const_iterator ();
                const_iterator (const unit_vector &v, const const_iterator_type &it);

                // Arithmetic
                const_iterator &operator ++ ();
                const_iterator &operator -- ();
                const_iterator &operator += (difference_type n);
                const_iterator &operator -= (difference_type n);
                difference_type operator - (const const_iterator &it) const;

                // Dereference
                reference operator * () const;

                // Index
                size_type index () const;

                // Assignment
                const_iterator &operator = (const const_iterator &it);

                // Comparison
                bool operator == (const const_iterator &it) const;
                bool operator < (const const_iterator &it) const;
            };

            typedef const_iterator iterator;

            const_iterator begin () const;
            const_iterator end () const;

            // Reverse iterator

            typedef reverse_iterator_base<const_iterator> const_reverse_iterator;

            const_reverse_iterator rbegin () const;
            const_reverse_iterator rend () const;
        };

## Zero Vector

#### Description

The templated class `zero_vector<T>` represents zero vectors. For a n-dimensional zero vector and 0 <= i < n holds zi = 0.

#### Example

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

    int main () {
        using namespace boost::numeric::ublas;
        zero_vector<double> v (3);
        std::cout << v << std::endl;
    }

#### Definition

Defined in the header vector.hpp.

#### Template parameters

Parameter | Description | Default
--- | --- | ---
`T` | The type of object stored in the vector. |

#### Model of

Vector Expression .

#### Type requirements

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

#### Public base classes

`vector_expression<zero_vector<T> >`

#### Members

Member | Description
--- | ---
`zero_vector ()` | Constructs a `zero_vector` that holds zero elements.
`zero_vector (size_type size)` | Constructs a `zero_vector` that holds `size` elements.
`zero_vector (const zero_vector &v)` | The copy constructor.
`void resize (size_type size)` | Resizes a `zero_vector` to hold `size` elements.
`size_type size () const` | Returns the size of the `zero_vector`.
`const_reference operator () (size_type i) const` | Returns the value of the `i`-th element.
`const_reference operator [] (size_type i) const` | Returns the value of the `i`-th element.
`zero_vector &operator = (const zero_vector &v)` | The assignment operator.
`zero_vector &assign_temporary (zero_vector &v)` | Assigns a temporary. May change the zero vector `v` .
`void swap (zero_vector &v)` | Swaps the contents of the zero vectors.
`const_iterator begin () const` | Returns a `const_iterator` pointing to the beginning of the `zero_vector`.
`const_iterator end () const` | Returns a `const_iterator` pointing to the end of the `zero_vector`.
`const_reverse_iterator rbegin () const` | Returns a `const_reverse_iterator` pointing to the beginning of the reversed `zero_vector`.
`const_reverse_iterator rend () const` | Returns a `const_reverse_iterator` pointing to the end of the reversed `zero_vector`.

#### Interface

        // Zero vector class
        template<class T>
        class zero_vector:
            public vector_expression<zero_vector<T> > {
        public:
            typedef std::size_t size_type;
            typedef std::ptrdiff_t difference_type;
            typedef T value_type;
            typedef const T &const_reference;
            typedef T &reference;
            typedef const T *const_pointer;
            typedef T *pointer;
            typedef const zero_vector<T> const_self_type;
            typedef zero_vector<T> self_type;
            typedef const vector_const_reference<const_self_type> const_closure_type;
            typedef size_type const_iterator_type;
            typedef sparse_tag storage_category;

            // Construction and destruction
            zero_vector ();
            zero_vector (size_type size);
            zero_vector (const zero_vector &v);

            // Accessors
            size_type size () const;
            size_type index () const;

            // Resizing
            void resize (size_type size);

            // Element access
            const_reference operator () (size_type i) const;

            const_reference operator [] (size_type i) const;

            // Assignment
            zero_vector &operator = (const zero_vector &v);
            zero_vector &assign_temporary (zero_vector &v);

            // Swapping
            void swap (zero_vector &v);
            friend void swap (zero_vector &v1, zero_vector &v2);

            class const_iterator;

            // Element lookup
            const_iterator find_first (size_type i) const;
            const_iterator find_last (size_type i) const;

            // Iterator simply is an index.

            class const_iterator:
                public container_const_reference<zero_vector>,
                public bidirectional_iterator_base<const_iterator, value_type> {
            public:
                typedef sparse_bidirectional_iterator_tag iterator_category;
                typedef typename zero_vector::difference_type difference_type;
                typedef typename zero_vector::value_type value_type;
                typedef typename zero_vector::const_reference reference;
                typedef typename zero_vector::const_pointer pointer;

                // Construction and destruction
                const_iterator ();
                const_iterator (const zero_vector &v, const const_iterator_type &it);

                // Arithmetic
                const_iterator &operator ++ ();
                const_iterator &operator -- ();

                // Dereference
                reference operator * () const;

                // Index
                size_type index () const;

                // Assignment
                const_iterator &operator = (const const_iterator &it);

                // Comparison
                bool operator == (const const_iterator &it) const;
            };

            typedef const_iterator iterator;

            const_iterator begin () const;
            const_iterator end () const;

            // Reverse iterator

            typedef reverse_iterator_base<const_iterator> const_reverse_iterator;

            const_reverse_iterator rbegin () const;
            const_reverse_iterator rend () const;
        };

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