c++boost.gif Sparse Matrix

Sparse Matrix

Description

The templated class sparse_matrix<T, F, A> is the base container adaptor for sparse matrices. For a (m x n)-dimensional sparse matrix and 0 <= i < m, 0 <= j < n the non-zero elements mi, j are mapped via (i x n + j) for row major orientation or via (i + j x m) for column major orientation to consecutive elements of the associative container, i.e. for elements k = mi1 ,j1 and k + 1 = mi2 ,j2 of the container holds i1 < i2 or (i1 = i2 and j1 < j2) with row major orientation or j1 < j 2 or (j1 = j2 and i1 < i2 ) with column major orientation.

Example

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

int main () {
using namespace boost::numeric::ublas;
sparse_matrix<double> m (3, 3, 3 * 3);
for (unsigned i = 0; i < m.size1 (); ++ i)
for (unsigned j = 0; j < m.size2 (); ++ j)
m (i, j) = 3 * i + j;
std::cout << m << std::endl;
}

Definition

Defined in the header matrix_sparse.hpp.

Template parameters

Parameter Description Default
T The type of object stored in the sparse matrix.  
F Functor describing the storage organization. [1] row_major
A The type of the adapted array. [2] map_array<std::size_t, T>

Model of

Matrix .

Type requirements

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

Public base classes

matrix_expression<sparse_matrix<T, F, A> >

Members

Member Description
sparse_matrix () Allocates a sparse_matrix that holds at most zero rows of zero elements.
sparse_matrix (size_type size1, size_type2, size_type non_zeros) Allocates a sparse_matrix that holds at most size1 rows of size2 elements.
sparse_matrix (const sparse_matrix &m) The copy constructor.
template<class AE>
sparse_matrix (size_type non_zeros, const matrix_expression<AE> &ae)
The extended copy constructor.
void resize (size_type size1, size_type size2, size_type non_zeros) Reallocates a sparse_matrix to hold at most size1 rows of size2 elements. The content of the sparse_matrix is preserved.
size_type size1 () const Returns the number of rows.
size_type size2 () const Returns the number of columns.
const_reference operator () (size_type i, size_type j) const Returns the value of the j-th element in the i-th row.
reference operator () (size_type i, size_type j) Returns a reference of the j-th element in the i-th row.
sparse_matrix &operator = (const sparse_matrix &m) The assignment operator.
sparse_matrix &assign_temporary (sparse_matrix &m) Assigns a temporary. May change the sparse matrix m .
template<class AE>
sparse_matrix &operator = (const matrix_expression<AE> &ae)
The extended assignment operator.
template<class AE>
sparse_matrix &assign (const matrix_expression<AE> &ae)
Assigns a matrix expression to the sparse matrix. Left and right hand side of the assignment should be independent.
template<class AE>
sparse_matrix &operator += (const matrix_expression<AE> &ae)
A computed assignment operator. Adds the matrix expression to the sparse matrix.
template<class AE>
sparse_matrix &plus_assign (const matrix_expression<AE> &ae)
Adds a matrix expression to the sparse matrix. Left and right hand side of the assignment should be independent.
template<class AE>
sparse_matrix &operator -= (const matrix_expression<AE> &ae)
A computed assignment operator. Subtracts the matrix expression from the sparse matrix.
template<class AE>
sparse_matrix &minus_assign (const matrix_expression<AE> &ae)
Subtracts a matrix expression from the sparse matrix. Left and right hand side of the assignment should be independent.
template<class AT>
sparse_matrix &operator *= (const AT &at)
A computed assignment operator. Multiplies the sparse matrix with a scalar.
template<class AT>
sparse_matrix &operator /= (const AT &at)
A computed assignment operator. Divides the sparse matrix through a scalar.
void swap (sparse_matrix &m) Swaps the contents of the sparse matrices.
void insert (size_type i, size_type j, const_reference t) Inserts the value t at the j-th element of the i-th row.
void erase (size_type i, size_type j) Erases the value at the j-th element of the i-th row.
void clear () Clears the sparse matrix.
const_iterator1 begin1 () const Returns a const_iterator1 pointing to the beginning of the sparse_matrix.
const_iterator1 end1 () const Returns a const_iterator1 pointing to the end of the sparse_matrix.
iterator1 begin1 () Returns a iterator1 pointing to the beginning of the sparse_matrix.
iterator1 end1 () Returns a iterator1 pointing to the end of the sparse_matrix.
const_iterator2 begin2 () const Returns a const_iterator2 pointing to the beginning of the sparse_matrix.
const_iterator2 end2 () const Returns a const_iterator2 pointing to the end of the sparse_matrix.
iterator2 begin2 () Returns a iterator2 pointing to the beginning of the sparse_matrix.
iterator2 end2 () Returns a iterator2 pointing to the end of the sparse_matrix.
const_reverse_iterator1 rbegin1 () const Returns a const_reverse_iterator1 pointing to the beginning of the reversed sparse_matrix.
const_reverse_iterator1 rend1 () const Returns a const_reverse_iterator1 pointing to the end of the reversed sparse_matrix.
reverse_iterator1 rbegin1 () Returns a reverse_iterator1 pointing to the beginning of the reversed sparse_matrix.
reverse_iterator1 rend1 () Returns a reverse_iterator1 pointing to the end of the reversed sparse_matrix.
const_reverse_iterator2 rbegin2 () const Returns a const_reverse_iterator2 pointing to the beginning of the reversed sparse_matrix.
const_reverse_iterator2 rend2 () const Returns a const_reverse_iterator2 pointing to the end of the reversed sparse_matrix.
reverse_iterator2 rbegin2 () Returns a reverse_iterator2 pointing to the beginning of the reversed sparse_matrix.
reverse_iterator2 rend2 () Returns a reverse_iterator2 pointing to the end of the reversed sparse_matrix.

Notes

[1] Supported parameters for the storage organization are row_major and column_major.

[2] Supported parameters for the adapted array are map_array<std::size_t, T> and std::map<std::size_t, T>.

Interface

    // Array based sparse matrix class 
template<class T, class F, class A>
class sparse_matrix:
public matrix_expression<sparse_matrix<T, F, 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 sparse_matrix<T, F, A> const_self_type;
typedef sparse_matrix<T, F, A> self_type;
typedef const matrix_const_reference<const_self_type> const_closure_type;
typedef matrix_reference<self_type> closure_type;
typedef typename A::const_iterator const_iterator_type;
typedef typename A::iterator iterator_type;
typedef sparse_tag storage_category;
typedef typename F::orientation_category orientation_category;

// Construction and destruction
sparse_matrix ();
sparse_matrix (size_type size1, size_type size2, size_type non_zeros = 0);
sparse_matrix (const sparse_matrix &m);
template<class AE>
sparse_matrix (const matrix_expression<AE> &ae, size_type non_zeros = 0);

// Accessors
size_type size1 () const;
size_type size2 () const;
size_type non_zeros () const;
const_array_type &data () const;
array_type &data ();

// Resizing
void resize (size_type size1, size_type size2, size_type non_zeros = 0);

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

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

// Swapping
void swap (sparse_matrix &m);
friend void swap (sparse_matrix &m1, sparse_matrix &m2);

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

class const_iterator1;
class iterator1;
class const_iterator2;
class iterator2;
typedef reverse_iterator_base1<const_iterator1> const_reverse_iterator1;
typedef reverse_iterator_base1<iterator1> reverse_iterator1;
typedef reverse_iterator_base2<const_iterator2> const_reverse_iterator2;
typedef reverse_iterator_base2<iterator2> reverse_iterator2;

// Element lookup
const_iterator1 find1 (int rank, size_type i, size_type j) const;
iterator1 find1 (int rank, size_type i, size_type j);
const_iterator2 find2 (int rank, size_type i, size_type j) const;
iterator2 find2 (int rank, size_type i, size_type j);
const_iterator1 find_first1 (int rank, size_type i, size_type j) const;
iterator1 find_first1 (int rank, size_type i, size_type j);
const_iterator1 find_last1 (int rank, size_type i, size_type j) const;
iterator1 find_last1 (int rank, size_type i, size_type j);
const_iterator2 find_first2 (int rank, size_type i, size_type j) const;
iterator2 find_first2 (int rank, size_type i, size_type j);
const_iterator2 find_last2 (int rank, size_type i, size_type j) const;
iterator2 find_last2 (int rank, size_type i, size_type j);

// Iterators simply are pointers.

class const_iterator1:
public container_const_reference<sparse_matrix>,
public bidirectional_iterator_base<const_iterator1, value_type> {
public:
typedef sparse_bidirectional_iterator_tag iterator_category;
typedef typename sparse_matrix::difference_type difference_type;
typedef typename sparse_matrix::value_type value_type;
typedef typename sparse_matrix::const_reference reference;
typedef typename sparse_matrix::const_pointer pointer;
typedef const_iterator2 dual_iterator_type;
typedef const_reverse_iterator2 dual_reverse_iterator_type;
typedef typename functor_type::functor1_type functor1_type;

// Construction and destruction
const_iterator1 ();
const_iterator1 (const sparse_matrix &m, int rank, size_type i, size_type j, const const_iterator_type &it);
const_iterator1 (const iterator1 &it);

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

// Dereference
reference operator * () const;

const_iterator2 begin () const;
const_iterator2 end () const;
const_reverse_iterator2 rbegin () const;
const_reverse_iterator2 rend () const;

// Indices
size_type index1 () const;
size_type index2 () const;

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

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

const_iterator1 begin1 () const;
const_iterator1 end1 () const;

class iterator1:
public container_reference<sparse_matrix>,
public bidirectional_iterator_base<iterator1, value_type> {
public:
typedef sparse_bidirectional_iterator_tag iterator_category;
typedef typename sparse_matrix::difference_type difference_type;
typedef typename sparse_matrix::value_type value_type;
typedef typename sparse_matrix::reference reference;
typedef typename sparse_matrix::pointer pointer;
typedef iterator2 dual_iterator_type;
typedef reverse_iterator2 dual_reverse_iterator_type;
typedef typename functor_type::functor1_type functor1_type;

// Construction and destruction
iterator1 ();
iterator1 (sparse_matrix &m, int rank, size_type i, size_type j, const iterator_type &it);

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

// Dereference
reference operator * () const;

iterator2 begin () const;
iterator2 end () const;
reverse_iterator2 rbegin () const;
reverse_iterator2 rend () const;

// Indices
size_type index1 () const;
size_type index2 () const;

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

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

iterator1 begin1 ();
iterator1 end1 ();

class const_iterator2:
public container_const_reference<sparse_matrix>,
public bidirectional_iterator_base<const_iterator2, value_type> {
public:
typedef sparse_bidirectional_iterator_tag iterator_category;
typedef typename sparse_matrix::difference_type difference_type;
typedef typename sparse_matrix::value_type value_type;
typedef typename sparse_matrix::const_reference reference;
typedef typename sparse_matrix::const_pointer pointer;
typedef const_iterator1 dual_iterator_type;
typedef const_reverse_iterator1 dual_reverse_iterator_type;
typedef typename functor_type::functor2_type functor2_type;

// Construction and destruction
const_iterator2 ();
const_iterator2 (const sparse_matrix &m, int rank, size_type i, size_type j, const const_iterator_type &it);
const_iterator2 (const iterator2 &it);

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

// Dereference
reference operator * () const;

const_iterator1 begin () const;
const_iterator1 end () const;
const_reverse_iterator1 rbegin () const;
const_reverse_iterator1 rend () const;

// Indices
size_type index1 () const;
size_type index2 () const;

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

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

const_iterator2 begin2 () const;
const_iterator2 end2 () const;

class iterator2:
public container_reference<sparse_matrix>,
public bidirectional_iterator_base<iterator2, value_type> {
public:
typedef sparse_bidirectional_iterator_tag iterator_category;
typedef typename sparse_matrix::difference_type difference_type;
typedef typename sparse_matrix::value_type value_type;
typedef typename sparse_matrix::reference reference;
typedef typename sparse_matrix::pointer pointer;
typedef iterator1 dual_iterator_type;
typedef reverse_iterator1 dual_reverse_iterator_type;
typedef typename functor_type::functor2_type functor2_type;

// Construction and destruction
iterator2 ();
iterator2 (sparse_matrix &m, int rank, size_type i, size_type j, const iterator_type &it);

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

// Dereference
reference operator * () const;

iterator1 begin () const;
iterator1 end () const;
reverse_iterator1 rbegin () const;
reverse_iterator1 rend () const;

// Indices
size_type index1 () const;
size_type index2 () const;

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

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

iterator2 begin2 ();
iterator2 end2 ();

// Reverse iterators

const_reverse_iterator1 rbegin1 () const;
const_reverse_iterator1 rend1 () const;

reverse_iterator1 rbegin1 ();
reverse_iterator1 rend1 ();

const_reverse_iterator2 rbegin2 () const;
const_reverse_iterator2 rend2 () const;

reverse_iterator2 rbegin2 ();
reverse_iterator2 rend2 ();
};

Compressed Matrix

Description

The templated class compressed_matrix<T, F, IB, IA, TA> is the base container adaptor for compressed matrices. For a (m x n )-dimensional compressed matrix and 0 <= i < m, 0 <= j < n the non-zero elements mi, j are mapped via (i x n + j) for row major orientation or via (i + j x m) for column major orientation to consecutive elements of the index and value containers, i.e. for elements k = mi 1,j1 and k + 1 = mi2 ,j2 of the container holds i1 < i2 or (i1 = i2 and j1 < j2) with row major orientation or j1 < j 2 or (j1 = j2 and i1 < i2 ) with column major orientation.

Example

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

int main () {
using namespace boost::numeric::ublas;
compressed_matrix<double> m (3, 3, 3 * 3);
for (unsigned i = 0; i < m.size1 (); ++ i)
for (unsigned j = 0; j < m.size2 (); ++ j)
m (i, j) = 3 * i + j;
std::cout << m << std::endl;
}

Definition

Defined in the header matrix_sparse.hpp.

Template parameters

Parameter Description Default
T The type of object stored in the compressed matrix.  
F Functor describing the storage organization. [1] row_major
IB The index base of the compressed vector. [2] 0
IA The type of the adapted array for indices. [3] unbounded_array<std::size_t>
TA The type of the adapted array for values. [3] unbounded_array<T>

Model of

Matrix .

Type requirements

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

Public base classes

matrix_expression<compressed_matrix<T, F, IB, IA, TA> >

Members

Member Description
compressed_matrix () Allocates a compressed_matrix that holds at most zero rows of zero elements.
compressed_matrix (size_type size1, size_type2, size_type non_zeros) Allocates a compressed_matrix that holds at most size1 rows of size2 elements.
compressed_matrix (const compressed_matrix &m) The copy constructor.
template<class AE>
compressed_matrix (size_type non_zeros, const matrix_expression<AE> &ae)
The extended copy constructor.
void resize (size_type size1, size_type size2, size_type non_zeros) Reallocates a compressed_matrix to hold at most size1 rows of size2 elements. The content of the compressed_matrix is preserved.
size_type size1 () const Returns the number of rows.
size_type size2 () const Returns the number of columns.
const_reference operator () (size_type i, size_type j) const Returns the value of the j-th element in the i-th row.
reference operator () (size_type i, size_type j) Returns a reference of the j-th element in the i-th row.
compressed_matrix &operator = (const compressed_matrix &m) The assignment operator.
compressed_matrix &assign_temporary (compressed_matrix &m) Assigns a temporary. May change the compressed matrix m.
template<class AE>
compressed_matrix &operator = (const matrix_expression<AE> &ae)
The extended assignment operator.
template<class AE>
compressed_matrix &assign (const matrix_expression<AE> &ae)
Assigns a matrix expression to the compressed matrix. Left and right hand side of the assignment should be independent.
template<class AE>
compressed_matrix &operator += (const matrix_expression<AE> &ae)
A computed assignment operator. Adds the matrix expression to the compressed matrix.
template<class AE>
compressed_matrix &plus_assign (const matrix_expression<AE> &ae)
Adds a matrix expression to the compressed matrix. Left and right hand side of the assignment should be independent.
template<class AE>
compressed_matrix &operator -= (const matrix_expression<AE> &ae)
A computed assignment operator. Subtracts the matrix expression from the compressed matrix.
template<class AE>
compressed_matrix &minus_assign (const matrix_expression<AE> &ae)
Subtracts a matrix expression from the compressed matrix. Left and right hand side of the assignment should be independent.
template<class AT>
compressed_matrix &operator *= (const AT &at)
A computed assignment operator. Multiplies the compressed matrix with a scalar.
template<class AT>
compressed_matrix &operator /= (const AT &at)
A computed assignment operator. Divides the compressed matrix through a scalar.
void swap (compressed_matrix &m) Swaps the contents of the compressed matrices.
void insert (size_type i, size_type j, const_reference t) Inserts the value t at the j-th element of the i-th row.
void erase (size_type i, size_type j) Erases the value at the j-th element of the i-th row.
void clear () Clears the compressed matrix.
const_iterator1 begin1 () const Returns a const_iterator1 pointing to the beginning of the compressed_matrix.
const_iterator1 end1 () const Returns a const_iterator1 pointing to the end of the compressed_matrix.
iterator1 begin1 () Returns a iterator1 pointing to the beginning of the compressed_matrix.
iterator1 end1 () Returns a iterator1 pointing to the end of the compressed_matrix.
const_iterator2 begin2 () const Returns a const_iterator2 pointing to the beginning of the compressed_matrix.
const_iterator2 end2 () const Returns a const_iterator2 pointing to the end of the compressed_matrix.
iterator2 begin2 () Returns a iterator2 pointing to the beginning of the compressed_matrix.
iterator2 end2 () Returns a iterator2 pointing to the end of the compressed_matrix.
const_reverse_iterator1 rbegin1 () const Returns a const_reverse_iterator1 pointing to the beginning of the reversed compressed_matrix.
const_reverse_iterator1 rend1 () const Returns a const_reverse_iterator1 pointing to the end of the reversed compressed_matrix.
reverse_iterator1 rbegin1 () Returns a reverse_iterator1 pointing to the beginning of the reversed compressed_matrix.
reverse_iterator1 rend1 () Returns a reverse_iterator1 pointing to the end of the reversed compressed_matrix.
const_reverse_iterator2 rbegin2 () const Returns a const_reverse_iterator2 pointing to the beginning of the reversed compressed_matrix.
const_reverse_iterator2 rend2 () const Returns a const_reverse_iterator2 pointing to the end of the reversed compressed_matrix.
reverse_iterator2 rbegin2 () Returns a reverse_iterator2 pointing to the beginning of the reversed compressed_matrix.
reverse_iterator2 rend2 () Returns a reverse_iterator2 pointing to the end of the reversed compressed_matrix.

Notes

[1] Supported parameters for the storage organization are row_major and column_major.

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

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

Interface

    // Array based sparse matrix class 
template<class T, class F, std::size_t IB, class IA, class TA>
class compressed_matrix:
public matrix_expression<compressed_matrix<T, F, IB, IA, TA> > {
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 IA index_array_type;
typedef TA value_array_type;
typedef const compressed_matrix<T, F, IB, IA, TA> const_self_type;
typedef compressed_matrix<T, F, IB, IA, TA> self_type;
typedef const matrix_const_reference<const_self_type> const_closure_type;
typedef matrix_reference<self_type> closure_type;
typedef typename A::const_iterator const_iterator_type;
typedef typename A::iterator iterator_type;
typedef sparse_tag storage_category;
typedef typename F::orientation_category orientation_category;

// Construction and destruction
compressed_matrix ();
compressed_matrix (size_type size1, size_type size2, size_type non_zeros = 0);
compressed_matrix (const compressed_matrix &m);
template<class AE>
compressed_matrix (const matrix_expression<AE> &ae, size_type non_zeros = 0);

// Accessors
size_type size1 () const;
size_type size2 () const;
size_type non_zeros () const;
static size_type index_base ();
const index_array_type &index1_data () const;
index_array_type &index1_data ();
const index_array_type &index2_data () const;
index_array_type &index2_data ();
const value_array_type &value_data () const;
value_array_type &value_data ();

// Resizing
void resize (size_type size1, size_type size2, size_type non_zeros = 0);

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

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

// Swapping
void swap (compressed_matrix &m);
friend void swap (compressed_matrix &m1, compressed_matrix &m2);

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

class const_iterator1;
class iterator1;
class const_iterator2;
class iterator2;
typedef reverse_iterator_base1<const_iterator1> const_reverse_iterator1;
typedef reverse_iterator_base1<iterator1> reverse_iterator1;
typedef reverse_iterator_base2<const_iterator2> const_reverse_iterator2;
typedef reverse_iterator_base2<iterator2> reverse_iterator2;

// Element lookup
const_iterator1 find1 (int rank, size_type i, size_type j) const;
iterator1 find1 (int rank, size_type i, size_type j);
const_iterator2 find2 (int rank, size_type i, size_type j) const;
iterator2 find2 (int rank, size_type i, size_type j);
const_iterator1 find_first1 (int rank, size_type i, size_type j) const;
iterator1 find_first1 (int rank, size_type i, size_type j);
const_iterator1 find_last1 (int rank, size_type i, size_type j) const;
iterator1 find_last1 (int rank, size_type i, size_type j);
const_iterator2 find_first2 (int rank, size_type i, size_type j) const;
iterator2 find_first2 (int rank, size_type i, size_type j);
const_iterator2 find_last2 (int rank, size_type i, size_type j) const;
iterator2 find_last2 (int rank, size_type i, size_type j);

// Iterators simply are pointers.

class const_iterator1:
public container_const_reference<compressed_matrix>,
public bidirectional_iterator_base<const_iterator1, value_type> {
public:
typedef sparse_bidirectional_iterator_tag iterator_category;
typedef typename compressed_matrix::difference_type difference_type;
typedef typename compressed_matrix::value_type value_type;
typedef typename compressed_matrix::const_reference reference;
typedef typename compressed_matrix::const_pointer pointer;
typedef const_iterator2 dual_iterator_type;
typedef const_reverse_iterator2 dual_reverse_iterator_type;
typedef typename functor_type::functor1_type functor1_type;

// Construction and destruction
const_iterator1 ();
const_iterator1 (const compressed_matrix &m, int rank, size_type i, size_type j, const const_iterator_type &it);
const_iterator1 (const iterator1 &it);

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

// Dereference
reference operator * () const;

const_iterator2 begin () const;
const_iterator2 end () const;
const_reverse_iterator2 rbegin () const;
const_reverse_iterator2 rend () const;

// Indices
size_type index1 () const;
size_type index2 () const;

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

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

const_iterator1 begin1 () const;
const_iterator1 end1 () const;

class iterator1:
public container_reference<compressed_matrix>,
public bidirectional_iterator_base<iterator1, value_type> {
public:
typedef sparse_bidirectional_iterator_tag iterator_category;
typedef typename compressed_matrix::difference_type difference_type;
typedef typename compressed_matrix::value_type value_type;
typedef typename compressed_matrix::reference reference;
typedef typename compressed_matrix::pointer pointer;
typedef iterator2 dual_iterator_type;
typedef reverse_iterator2 dual_reverse_iterator_type;
typedef typename functor_type::functor1_type functor1_type;

// Construction and destruction
iterator1 ();
iterator1 (compressed_matrix &m, int rank, size_type i, size_type j, const iterator_type &it);

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

// Dereference
reference operator * () const;

iterator2 begin () const;
iterator2 end () const;
reverse_iterator2 rbegin () const;
reverse_iterator2 rend () const;

// Indices
size_type index1 () const;
size_type index2 () const;

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

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

iterator1 begin1 ();
iterator1 end1 ();

class const_iterator2:
public container_const_reference<compressed_matrix>,
public bidirectional_iterator_base<const_iterator2, value_type> {
public:
typedef sparse_bidirectional_iterator_tag iterator_category;
typedef typename compressed_matrix::difference_type difference_type;
typedef typename compressed_matrix::value_type value_type;
typedef typename compressed_matrix::const_reference reference;
typedef typename compressed_matrix::const_pointer pointer;
typedef const_iterator1 dual_iterator_type;
typedef const_reverse_iterator1 dual_reverse_iterator_type;
typedef typename functor_type::functor2_type functor2_type;

// Construction and destruction
const_iterator2 ();
const_iterator2 (const compressed_matrix &m, int rank, size_type i, size_type j, const const_iterator_type &it);
const_iterator2 (const iterator2 &it);

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

// Dereference
reference operator * () const;

const_iterator1 begin () const;
const_iterator1 end () const;
const_reverse_iterator1 rbegin () const;
const_reverse_iterator1 rend () const;

// Indices
size_type index1 () const;
size_type index2 () const;

// Assignment
const_iterator2 &operato r = (const const_iterator2 &it);

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

const_iterator2 begin2 () const;
const_iterator2 end2 () const;

class iterator2:
public container_reference<compressed_matrix>,
public bidirectional_iterator_base<iterator2, value_type> {
public:
typedef sparse_bidirectional_iterator_tag iterator_category;
typedef typename compressed_matrix::difference_type difference_type;
typedef typename compressed_matrix::value_type value_type;
typedef typename compressed_matrix::reference reference;
typedef typename compressed_matrix::pointer pointer;
typedef iterator1 dual_iterator_type;
typedef reverse_iterator1 dual_reverse_iterator_type;
typedef typename functor_type::functor2_type functor2_type;

// Construction and destruction
iterator2 ();
iterator2 (compressed_matrix &m, int rank, size_type i, size_type j, const iterator_type &it);

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

// Dereference
reference operator * () const;

iterator1 begin () const;
iterator1 end () const;
reverse_iterator1 rbegin () const;
reverse_iterator1 rend () const;

// Indices
size_type index1 () const;
size_type index2 () const;

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

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

iterator2 begin2 ();
iterator2 end2 ();

// Reverse iterators

const_reverse_iterator1 rbegin1 () const;
const_reverse_iterator1 rend1 () const;

reverse_iterator1 rbegin1 ();
reverse_iterator1 rend1 ();

const_reverse_iterator2 rbegin2 () const;
const_reverse_iterator2 rend2 () const;

reverse_iterator2 rbegin2 ();
reverse_iterator2 rend2 ();
};

Coordinate Matrix

Description

The templated class coordinate_matrix<T, F, IB, IA, TA> is the base container adaptor for compressed matrices. For a (m x n )-dimensional sorted coordinate matrix and 0 <= i < m, 0 <= j < n the non-zero elements mi, j are mapped via (i x n + j) for row major orientation or via (i + j x m) for column major orientation to consecutive elements of the index and value containers, i.e. for elements k = m i1,j 1 and k + 1 = mi 2,j2 of the container holds i1 < i2 or (i1 = i 2 and j1 < j 2) with row major orientation or j1 < j2 or (j1 = j2 and i1 < i2) with column major orientation.

Example

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

int main () {
using namespace boost::numeric::ublas;
coordinate_matrix<double> m (3, 3, 3 * 3);
for (unsigned i = 0; i < m.size1 (); ++ i)
for (unsigned j = 0; j < m.size2 (); ++ j)
m (i, j) = 3 * i + j;
std::cout << m << std::endl;
}

Definition

Defined in the header matrix_sparse.hpp.

Template parameters

Parameter Description Default
T The type of object stored in the coordinate matrix.  
F Functor describing the storage organization. [1] row_major
IB The index base of the coordinate vector. [2] 0
IA The type of the adapted array for indices. [3] unbounded_array<std::size_t>
TA The type of the adapted array for values. [3] unbounded_array<T>

Model of

Matrix .

Type requirements

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

Public base classes

matrix_expression<coordinate_matrix<T, F, IB, IA, TA> >

Members

Member Description
coordinate_matrix () Allocates a coordinate_matrix that holds at most zero rows of zero elements.
coordinate_matrix (size_type size1, size_type2, size_type non_zeros) Allocates a coordinate_matrix that holds at most size1 rows of size2 elements.
coordinate_matrix (const coordinate_matrix &m) The copy constructor.
template<class AE>
coordinate_matrix (size_type non_zeros, const matrix_expression<AE> &ae)
The extended copy constructor.
void resize (size_type size1, size_type size2, size_type non_zeros) Reallocates a coordinate_matrix to hold at most size1 rows of size2 elements. The content of the coordinate_matrix is preserved.
size_type size1 () const Returns the number of rows.
size_type size2 () const Returns the number of columns.
const_reference operator () (size_type i, size_type j) const Returns the value of the j-th element in the i-th row.
reference operator () (size_type i, size_type j) Returns a reference of the j-th element in the i-th row.
coordinate_matrix &operator = (const coordinate_matrix &m) The assignment operator.
coordinate_matrix &assign_temporary (coordinate_matrix &m) Assigns a temporary. May change the coordinate matrix m.
template<class AE>
coordinate_matrix &operator = (const matrix_expression<AE> &ae)
The extended assignment operator.
template<class AE>
coordinate_matrix &assign (const matrix_expression<AE> &ae)
Assigns a matrix expression to the coordinate matrix. Left and right hand side of the assignment should be independent.
template<class AE>
coordinate_matrix &operator += (const matrix_expression<AE> &ae)
A computed assignment operator. Adds the matrix expression to the coordinate matrix.
template<class AE>
coordinate_matrix &plus_assign (const matrix_expression<AE> &ae)
Adds a matrix expression to the coordinate matrix. Left and right hand side of the assignment should be independent.
template<class AE>
coordinate_matrix &operator -= (const matrix_expression<AE> &ae)
A computed assignment operator. Subtracts the matrix expression from the coordinate matrix.
template<class AE>
coordinate_matrix &minus_assign (const matrix_expression<AE> &ae)
Subtracts a matrix expression from the coordinate matrix. Left and right hand side of the assignment should be independent.
template<class AT>
coordinate_matrix &operator *= (const AT &at)
A computed assignment operator. Multiplies the coordinate matrix with a scalar.
template<class AT>
coordinate_matrix &operator /= (const AT &at)
A computed assignment operator. Divides the coordinate matrix through a scalar.
void swap (coordinate_matrix &m) Swaps the contents of the coordinate matrices.
void insert (size_type i, size_type j, const_reference t) Inserts the value t at the j-th element of the i-th row.
void erase (size_type i, size_type j) Erases the value at the j-th element of the i-th row.
void clear () Clears the coordinate matrix.
const_iterator1 begin1 () const Returns a const_iterator1 pointing to the beginning of the coordinate_matrix.
const_iterator1 end1 () const Returns a const_iterator1 pointing to the end of the coordinate_matrix.
iterator1 begin1 () Returns a iterator1 pointing to the beginning of the coordinate_matrix.
iterator1 end1 () Returns a iterator1 pointing to the end of the coordinate_matrix.
const_iterator2 begin2 () const Returns a const_iterator2 pointing to the beginning of the coordinate_matrix.
const_iterator2 end2 () const Returns a const_iterator2 pointing to the end of the coordinate_matrix.
iterator2 begin2 () Returns a iterator2 pointing to the beginning of the coordinate_matrix.
iterator2 end2 () Returns a iterator2 pointing to the end of the coordinate_matrix.
const_reverse_iterator1 rbegin1 () const Returns a const_reverse_iterator1 pointing to the beginning of the reversed coordinate_matrix.
const_reverse_iterator1 rend1 () const Returns a const_reverse_iterator1 pointing to the end of the reversed coordinate_matrix.
reverse_iterator1 rbegin1 () Returns a reverse_iterator1 pointing to the beginning of the reversed coordinate_matrix.
reverse_iterator1 rend1 () Returns a reverse_iterator1 pointing to the end of the reversed coordinate_matrix.
const_reverse_iterator2 rbegin2 () const Returns a const_reverse_iterator2 pointing to the beginning of the reversed coordinate_matrix.
const_reverse_iterator2 rend2 () const Returns a const_reverse_iterator2 pointing to the end of the reversed coordinate_matrix.
reverse_iterator2 rbegin2 () Returns a reverse_iterator2 pointing to the beginning of the reversed coordinate_matrix.
reverse_iterator2 rend2 () Returns a reverse_iterator2 pointing to the end of the reversed coordinate_matrix.

Notes

[1] Supported parameters for the storage organization are row_major and column_major.

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

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

Interface

    // Array based sparse matrix class 
template<class T, class F, std::size_t IB, class IA, class TA>
class coordinate_matrix:
public matrix_expression<coordinate_matrix<T, F, IB, IA, TA> > {
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 IA index_array_type;
typedef TA value_array_type;
typedef const coordinate_matrix<T, F, IB, IA, TA> const_self_type;
typedef coordinate_matrix<T, F, IB, IA, TA> self_type;
typedef const matrix_const_reference<const_self_type> const_closure_type;
typedef matrix_reference<self_type> closure_type;
typedef typename A::const_iterator const_iterator_type;
typedef typename A::iterator iterator_type;
typedef sparse_tag storage_category;
typedef typename F::orientation_category orientation_category;

// Construction and destruction
coordinate_matrix ();
coordinate_matrix (size_type size1, size_type size2, size_type non_zeros = 0);
coordinate_matrix (const coordinate_matrix &m);
template<class AE>
coordinate_matrix (const matrix_expression<AE> &ae, size_type non_zeros = 0);

// Accessors
size_type size1 () const;
size_type size2 () const;
size_type non_zeros () const;
static size_type index_base ();
const index_array_type &index1_data () const;
index_array_type &index1_data ();
const index_array_type &index2_data () const;
index_array_type &index2_data ();
const value_array_type &value_data () const;
value_array_type &value_data ();

// Resizing
void resize (size_type size1, size_type size2, size_type non_zeros = 0);

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

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

// Swapping
void swap (coordinate_matrix &m);
friend void swap (coordinate_matrix &m1, coordinate_matrix &m2);

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

class const_iterator1;
class iterator1;
class const_iterator2;
class iterator2;
typedef reverse_iterator_base1<const_iterator1> const_reverse_iterator1;
typedef reverse_iterator_base1<iterator1> reverse_iterator1;
typedef reverse_iterator_base2<const_iterator2> const_reverse_iterator2;
typedef reverse_iterator_base2<iterator2> reverse_iterator2;

// Element lookup
const_iterator1 find1 (int rank, size_type i, size_type j) const;
iterator1 find1 (int rank, size_type i, size_type j);
const_iterator2 find2 (int rank, size_type i, size_type j) const;
iterator2 find2 (int rank, size_type i, size_type j);
const_iterator1 find_first1 (int rank, size_type i, size_type j) const;
iterator1 find_first1 (int rank, size_type i, size_type j);
const_iterator1 find_last1 (int rank, size_type i, size_type j) const;
iterator1 find_last1 (int rank, size_type i, size_type j);
const_iterator2 find_first2 (int rank, size_type i, size_type j) const;
iterator2 find_first2 (int rank, size_type i, size_type j);
const_iterator2 find_last2 (int rank, size_type i, size_type j) const;
iterator2 find_last2 (int rank, size_type i, size_type j);

// Iterators simply are pointers.

class const_iterator1:
public container_const_reference<coordinate_matrix>,
public bidirectional_iterator_base<const_iterator1, value_type> {
public:
typedef sparse_bidirectional_iterator_tag iterator_category;
typedef typename coordinate_matrix::difference_type difference_type;
typedef typename coordinate_matrix::value_type value_type;
typedef typename coordinate_matrix::const_reference reference;
typedef typename coordinate_matrix::const_pointer pointer;
typedef const_iterator2 dual_iterator_type;
typedef const_reverse_iterator2 dual_reverse_iterator_type;
typedef typename functor_type::functor1_type functor1_type;

// Construction and destruction
const_iterator1 ();
const_iterator1 (const coordinate_matrix &m, int rank, size_type i, size_type j, const const_iterator_type &it);
const_iterator1 (const iterator1 &it);

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

// Dereference
reference operator * () const;

const_iterator2 begin () const;
const_iterator2 end () const;
const_reverse_iterator2 rbegin () const;
const_reverse_iterator2 rend () const;

// Indices
size_type index1 () const;
size_type index2 () const;

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

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

const_iterator1 begin1 () const;
const_iterator1 end1 () const;

class iterator1:
public container_reference<coordinate_matrix>,
public bidirectional_iterator_base<iterator1, value_type> {
public:
typedef sparse_bidirectional_iterator_tag iterator_category;
typedef typename coordinate_matrix::difference_type difference_type;
typedef typename coordinate_matrix::value_type value_type;
typedef typename coordinate_matrix::reference reference;
typedef typename coordinate_matrix::pointer pointer;
typedef iterator2 dual_iterator_type;
typedef reverse_iterator2 dual_reverse_iterator_type;
typedef typename functor_type::functor1_type functor1_type;

// Construction and destruction
iterator1 ();
iterator1 (coordinate_matrix &m, int rank, size_type i, size_type j, const iterator_type &it);

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

// Dereference
reference operator * () const;

iterator2 begin () const;
iterator2 end () const;
reverse_iterator2 rbegin () const;
reverse_iterator2 rend () const;

// Indices
size_type index1 () const;
size_type index2 () const;

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

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

iterator1 begin1 ();< br> iterator1 end1 ();

class const_iterator2:
public container_const_reference<coordinate_matrix>,
public bidirectional_iterator_base<const_iterator2, value_type> {
public:
typedef sparse_bidirectional_iterator_tag iterator_category;
typedef typename coordinate_matrix::difference_type difference_type;
typedef typename coordinate_matrix::value_type value_type;
typedef typename coordinate_matrix::const_reference reference;
typedef typename coordinate_matrix::const_pointer pointer;
typedef const_iterator1 dual_iterator_type;
typedef const_reverse_iterator1 dual_reverse_iterator_type;
typedef typename functor_type::functor2_type functor2_type;

// Construction and destruction
const_iterator2 ();
const_iterator2 (const coordinate_matrix &m, int rank, size_type i, size_type j, const const_iterator_type &it);
const_iterator2 (const iterator2 &it);

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

// Dereference
reference operator * () const;

const_iterator1 begin () const;
const_iterator1 end () const;
const_reverse_iterator1 rbegin () const;
const_reverse_iterator1 rend () const;

// Indices
size_type index1 () const;
size_type index2 () const;

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

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

const_iterator2 begin2 () const;
const_iterator2 end2 () const;

class iterator2:
public container_reference<coordinate_matrix>,
public bidirectional_iterator_base<iterator2, value_type> {
public:
typedef sparse_bidirectional_iterator_tag iterator_category;
typedef typename coordinate_matrix::difference_type difference_type;
typedef typename coordinate_matrix::value_type value_type;
typedef typename coordinate_matrix::reference reference;
typedef typename coordinate_matrix::pointer pointer;
typedef iterator1 dual_iterator_type;
typedef reverse_iterator1 dual_reverse_iterator_type;
typedef typename functor_type::functor2_type functor2_type;

// Construction and destruction
iterator2 ();
iterator2 (coordinate_matrix &m, int rank, size_type i, size_type j, const iterator_type &it);

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

// Dereference
reference operator * () const;

iterator1 begin () const;
iterator1 end () const;
reverse_iterator1 rbegin () const;
reverse_iterator1 rend () const;

// Indices
size_type index1 () const;
size_type index2 () const;

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

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

iterator2 begin2 ();
iterator2 end2 ();

// Reverse iterators

const_reverse_iterator1 rbegin1 () const;
const_reverse_iterator1 rend1 () const;

reverse_iterator1 rbegin1 ();
reverse_iterator1 rend1 ();

const_reverse_iterator2 rbegin2 () const;
const_reverse_iterator2 rend2 () const;

reverse_iterator2 rbegin2 ();
reverse_iterator2 rend2 ();
};

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