IT++ Logo Newcom Logo

itpp::Sparse_Vec< T > Class Template Reference

Templated sparse vector class. More...

#include <itpp/base/svec.h>

List of all members.

Public Member Functions

 Sparse_Vec ()
 Default constructor.
 Sparse_Vec (int sz, int data_init=200)
 Initiate an empty sparse vector.
 Sparse_Vec (const Sparse_Vec< T > &v)
 Initiate a new sparse vector.
 Sparse_Vec (const Vec< T > &v)
 Initiate a new sparse vector from a dense vector.
 Sparse_Vec (const Vec< T > &v, T epsilon)
 Initiate a new sparse vector from a dense vector. Elements of v larger than epsilon are copied into the new sparse vector.
 ~Sparse_Vec ()
 Destructor.
void set_size (int sz, int data_init=-1)
 Set the size sz of the sparse vector. Default value data_init=-1 => allocated size for the data is not changed.
int size () const
 Returns the size of the sparse vector.
int nnz ()
 Number of non-zero elements in the sparse vector.
double density ()
 Returns the density of the sparse vector: (number of non-zero elements)/(size of the vector).
void set_small_element (const T &epsilon)
 Set that all elements smaller than epsilon should be set to zero.
void remove_small_elements ()
void resize_data (int new_size)
 Set the maximum number of non-zero elements to new_size.
void compact ()
 Set the maximum number of non-zero elements equal to the actual number of non-zero elements.
void full (Vec< T > &v) const
 Returns a full, dense vector in v.
Vec< T > full () const
 Returns a full, dense vector.
operator() (int i) const
 Returns the element with index i.
void set (int i, T v)
 Set element i equal to v.
void set (const ivec &index_vec, const Vec< T > &v)
 Set the elements of the sparse vector with indices index_vec to the values in v.
void set_new (int i, T v)
 Set a new element with index i equal to v.
void set_new (const ivec &index_vec, const Vec< T > &v)
 Set new elements with indices index_vec equal to the values in v (no check whether the same index is used several times).
void add_elem (const int i, const T v)
 Add element i with v.
void add (const ivec &index_vec, const Vec< T > &v)
 Add v to the elements specified by index_vec with v.
void zeros ()
 Set the sparse vector to the all zero vector (removes all non-zero elements).
void zero_elem (const int i)
 Set the i-th element to zero (i.e. clear that element if it contains a non-zero value).
void clear ()
 Clear all non-zero elements of the sparse vector.
void clear_elem (const int i)
 Clear the i-th element (if it contains a non-zero value).
void get_nz_data (int p, T &data_out)
 Extract the reference to the p-th non-zero data element.
get_nz_data (int p)
 Returns the p-th non-zero data element.
int get_nz_index (int p)
 Returns the vector index of the p-th non-zero element.
void get_nz (int p, int &idx, T &dat)
 Returns the p-th non-zero value in dat and the corresponding vector index in idx.
Sparse_Vec< T > get_subvector (int i1, int i2) const
 Return sparse subvector from index i1 to index i2.
sqr () const
 Returns the sum of all values squared.
void operator= (const Sparse_Vec< T > &v)
 Assign sparse vector the value and length of the sparse vector v.
void operator= (const Vec< T > &v)
 Assign sparse vector the value and length of the dense vector v.
Sparse_Vec< T > operator- () const
 Returns the sign inverse of all elements in the sparse vector.
bool operator== (const Sparse_Vec< T > &v)
 Compare two sparse vectors. False if wrong sizes or different values.
void operator+= (const Sparse_Vec< T > &v)
 Add sparse vector v to all non-zero elements of the sparse vector.
void operator+= (const Vec< T > &v)
 Add vector v to all non-zero elements of the sparse vector.
void operator-= (const Sparse_Vec< T > &v)
 Subtract sparse vector v from all non-zero elements of the sparse vector.
void operator-= (const Vec< T > &v)
 Subtract vector v from all non-zero elements of the sparse vector.
void operator *= (const T &v)
 Multiply the scalar v to all non-zero elements of the sparse vector.
void operator/= (const T &v)
 Divide all non-zero elements of the sparse vector with the scalar v.

Friends

Sparse_Vec< T > operator+ (const Sparse_Vec< T > &v1, const Sparse_Vec< T > &v2)
 Addition v1+v2 where v1 and v2 are sparse vector.
operator * (const Sparse_Vec< T > &v1, const Sparse_Vec< T > &v2)
 Scalar product v1*v2 where v1 and v2 are sparse vectors.
operator * (const Sparse_Vec< T > &v1, const Vec< T > &v2)
 Scalar product v1*v2 where v1 is a sparse vector and v2 is a dense vector.
operator * (const Vec< T > &v1, const Sparse_Vec< T > &v2)
 Scalar product v1*v2 where v1 is a dense vector and v2 is a sparse vector.
Sparse_Vec< T > elem_mult (const Sparse_Vec< T > &v1, const Sparse_Vec< T > &v2)
 Element wise multiplication of two sparse vectors.
Vec< T > elem_mult (const Sparse_Vec< T > &v1, const Vec< T > &v2)
 Element wise multiplication of a sparse vector and a dense vector.
Sparse_Vec< T > elem_mult_s (const Sparse_Vec< T > &v1, const Vec< T > &v2)
 Element wise multiplication of a sparse vector and a dense vector returning a sparse vector.
Vec< T > elem_mult (const Vec< T > &v1, const Sparse_Vec< T > &v2)
 Element wise multiplication of a a dense vector and a sparse vector.
Sparse_Vec< T > elem_mult_s (const Vec< T > &v1, const Sparse_Vec< T > &v2)
 Element wise multiplication of a a dense vector and a sparse vector returning a sparse vector.

Related Functions

(Note that these are not member functions.)

typedef Sparse_Vec< int > sparse_ivec
 Type definition of an integer sparse vector.
typedef Sparse_Vec
< double > 
sparse_vec
 Type definition of a double sparse vector.
typedef Sparse_Vec
< std::complex
< double > > 
sparse_cvec
 Type definition of a complex<double> sparse vector.


Detailed Description

template<class T>
class itpp::Sparse_Vec< T >

Templated sparse vector class.

Author:
Tony Ottosson and Tobias Ringstrom
A sparse vector is a vector where most elements are zero. The maximum number of none-zero elements is a parameter to the constructor.

Definition at line 95 of file svec.h.


Constructor & Destructor Documentation

template<class T>
itpp::Sparse_Vec< T >::Sparse_Vec (  )  [inline]

Default constructor.

Definition at line 376 of file svec.h.

template<class T>
itpp::Sparse_Vec< T >::Sparse_Vec ( int  sz,
int  data_init = 200 
) [inline]

Initiate an empty sparse vector.

Parameters:
sz Size of the sparse vector (i.e. maximum index is (sz - 1))
data_init Maximum number of non-zero elements in the sparse vector (default value 200)

Definition at line 382 of file svec.h.

template<class T>
itpp::Sparse_Vec< T >::Sparse_Vec ( const Sparse_Vec< T > &  v  )  [inline]

Initiate a new sparse vector.

Parameters:
v The elements of v are copied into the new sparse vector

Definition at line 392 of file svec.h.

References itpp::Sparse_Vec< T >::check_small_elems_flag, itpp::Sparse_Vec< T >::data, itpp::Sparse_Vec< T >::data_size, itpp::Sparse_Vec< T >::eps, itpp::Sparse_Vec< T >::index, itpp::Sparse_Vec< T >::used_size, and itpp::Sparse_Vec< T >::v_size.

template<class T>
itpp::Sparse_Vec< T >::Sparse_Vec ( const Vec< T > &  v  )  [inline]

Initiate a new sparse vector from a dense vector.

Parameters:
v The elements of v are copied into the new sparse vector

Definition at line 409 of file svec.h.

References itpp::Sparse_Vec< T >::compact(), min, itpp::Sparse_Vec< T >::resize_data(), and itpp::Vec< Num_T >::size().

template<class T>
itpp::Sparse_Vec< T >::Sparse_Vec ( const Vec< T > &  v,
epsilon 
) [inline]

Initiate a new sparse vector from a dense vector. Elements of v larger than epsilon are copied into the new sparse vector.

Note:
If the type T is complex<double>, then the elements of v larger than abs(epsilon) are copied into the new sparse vector.

Definition at line 430 of file svec.h.

References itpp::abs(), itpp::Sparse_Vec< T >::compact(), min, itpp::Sparse_Vec< T >::resize_data(), and itpp::Vec< Num_T >::size().

template<class T>
itpp::Sparse_Vec< T >::~Sparse_Vec (  )  [inline]

Destructor.

Definition at line 453 of file svec.h.


Member Function Documentation

template<class T>
void itpp::Sparse_Vec< T >::set_size ( int  sz,
int  data_init = -1 
) [inline]

Set the size sz of the sparse vector. Default value data_init=-1 => allocated size for the data is not changed.

Parameters:
sz Size of the sparse vector (i.e. maximum index is (sz - 1))
data_init Maximum number of non-zero elements in the sparse vector (default value -1 => allocated size for the data is not changed)

Definition at line 459 of file svec.h.

template<class T>
int itpp::Sparse_Vec< T >::size (  )  const [inline]

Returns the size of the sparse vector.

Definition at line 142 of file svec.h.

Referenced by itpp::operator *(), itpp::Sparse_Vec< T >::operator+=(), and itpp::Sparse_Vec< T >::operator-=().

template<class T>
int itpp::Sparse_Vec< T >::nnz (  )  [inline]

Number of non-zero elements in the sparse vector.

Definition at line 145 of file svec.h.

References itpp::Sparse_Vec< T >::remove_small_elements().

Referenced by itpp::operator *().

template<class T>
double itpp::Sparse_Vec< T >::density (  )  [inline]

Returns the density of the sparse vector: (number of non-zero elements)/(size of the vector).

Definition at line 471 of file svec.h.

References itpp::Sparse_Vec< T >::remove_small_elements().

template<class T>
void itpp::Sparse_Vec< T >::set_small_element ( const T &  epsilon  )  [inline]

Set that all elements smaller than epsilon should be set to zero.

Definition at line 481 of file svec.h.

References itpp::Sparse_Vec< T >::remove_small_elements().

template<class T>
void itpp::Sparse_Vec< T >::remove_small_elements (  )  [inline]

Removes all elements that are smaller than epsilon from the non-zero elements.

Note:
The small element epsilon can be set by the member function set_small_element. If no small value is set, the default value is always epsilon=0.

Definition at line 488 of file svec.h.

References itpp::abs().

Referenced by itpp::Sparse_Vec< T >::compact(), itpp::Sparse_Vec< T >::density(), itpp::Sparse_Vec< T >::get_nz(), itpp::Sparse_Vec< T >::get_nz_data(), itpp::Sparse_Vec< T >::get_nz_index(), itpp::Sparse_Vec< T >::nnz(), itpp::Sparse_Vec< T >::operator==(), itpp::Sparse_Vec< T >::set(), and itpp::Sparse_Vec< T >::set_small_element().

template<class T>
void itpp::Sparse_Vec< T >::resize_data ( int  new_size  )  [inline]

Set the maximum number of non-zero elements to new_size.

Parameters:
new_size The new maximum number of non-zero elements.

Definition at line 515 of file svec.h.

References it_assert.

Referenced by itpp::Sparse_Vec< T >::add(), itpp::Sparse_Vec< T >::add_elem(), itpp::Sparse_Vec< T >::compact(), itpp::elem_mult(), itpp::Sparse_Vec< T >::get_subvector(), itpp::operator+(), itpp::Sparse_Vec< T >::operator=(), itpp::Sparse_Vec< T >::set(), itpp::Sparse_Vec< T >::set_new(), and itpp::Sparse_Vec< T >::Sparse_Vec().

template<class T>
void itpp::Sparse_Vec< T >::compact (  )  [inline]

Set the maximum number of non-zero elements equal to the actual number of non-zero elements.

Definition at line 538 of file svec.h.

References itpp::Sparse_Vec< T >::remove_small_elements(), and itpp::Sparse_Vec< T >::resize_data().

Referenced by itpp::elem_mult(), itpp::Sparse_Vec< T >::get_subvector(), itpp::operator+(), itpp::Sparse_Vec< T >::operator=(), and itpp::Sparse_Vec< T >::Sparse_Vec().

template<class T>
void itpp::Sparse_Vec< T >::full ( Vec< T > &  v  )  const [inline]

Returns a full, dense vector in v.

Definition at line 547 of file svec.h.

References itpp::Vec< Num_T >::set_size().

Referenced by itpp::full(), and itpp::operator *().

template<class T>
Vec< T > itpp::Sparse_Vec< T >::full (  )  const [inline]

Returns a full, dense vector.

Definition at line 557 of file svec.h.

template<class T>
T itpp::Sparse_Vec< T >::operator() ( int  i  )  const [inline]

Returns the element with index i.

Definition at line 566 of file svec.h.

References it_assert0.

template<class T>
void itpp::Sparse_Vec< T >::set ( int  i,
v 
) [inline]

Set element i equal to v.

Definition at line 582 of file svec.h.

References itpp::abs(), it_assert0, itpp::Sparse_Vec< T >::remove_small_elements(), and itpp::Sparse_Vec< T >::resize_data().

template<class T>
void itpp::Sparse_Vec< T >::set ( const ivec &  index_vec,
const Vec< T > &  v 
) [inline]

Set the elements of the sparse vector with indices index_vec to the values in v.

Definition at line 750 of file svec.h.

References itpp::Sparse_Vec< T >::clear(), it_assert0, and itpp::max().

template<class T>
void itpp::Sparse_Vec< T >::set_new ( int  i,
v 
) [inline]

Set a new element with index i equal to v.

Definition at line 617 of file svec.h.

References itpp::abs(), it_assert0, and itpp::Sparse_Vec< T >::resize_data().

template<class T>
void itpp::Sparse_Vec< T >::set_new ( const ivec &  index_vec,
const Vec< T > &  v 
) [inline]

Set new elements with indices index_vec equal to the values in v (no check whether the same index is used several times).

Definition at line 762 of file svec.h.

References itpp::abs(), itpp::Sparse_Vec< T >::clear(), it_assert0, itpp::max(), itpp::Sparse_Vec< T >::resize_data(), and itpp::Vec< Num_T >::size().

template<class T>
void itpp::Sparse_Vec< T >::add_elem ( const int  i,
const T  v 
) [inline]

Add element i with v.

Definition at line 632 of file svec.h.

References it_assert0, and itpp::Sparse_Vec< T >::resize_data().

Referenced by itpp::Sparse_Vec< T >::operator+=(), and itpp::Sparse_Vec< T >::operator-=().

template<class T>
void itpp::Sparse_Vec< T >::add ( const ivec &  index_vec,
const Vec< T > &  v 
) [inline]

Add v to the elements specified by index_vec with v.

Definition at line 660 of file svec.h.

References it_assert0, itpp::max(), itpp::Sparse_Vec< T >::resize_data(), and itpp::Vec< Num_T >::size().

template<class T>
void itpp::Sparse_Vec< T >::zeros (  )  [inline]

Set the sparse vector to the all zero vector (removes all non-zero elements).

Definition at line 694 of file svec.h.

template<class T>
void itpp::Sparse_Vec< T >::zero_elem ( const int  i  )  [inline]

Set the i-th element to zero (i.e. clear that element if it contains a non-zero value).

Definition at line 701 of file svec.h.

References it_assert0.

template<class T>
void itpp::Sparse_Vec< T >::clear (  )  [inline]

Clear all non-zero elements of the sparse vector.

Definition at line 722 of file svec.h.

Referenced by itpp::Sparse_Vec< T >::set(), and itpp::Sparse_Vec< T >::set_new().

template<class T>
void itpp::Sparse_Vec< T >::clear_elem ( const int  i  )  [inline]

Clear the i-th element (if it contains a non-zero value).

Definition at line 729 of file svec.h.

References it_assert0.

template<class T>
void itpp::Sparse_Vec< T >::get_nz_data ( int  p,
T &  data_out 
) [inline]

Extract the reference to the p-th non-zero data element.

Definition at line 218 of file svec.h.

References itpp::Sparse_Vec< T >::remove_small_elements().

Referenced by itpp::operator *().

template<class T>
T itpp::Sparse_Vec< T >::get_nz_data ( int  p  )  [inline]

Returns the p-th non-zero data element.

Definition at line 227 of file svec.h.

References itpp::Sparse_Vec< T >::remove_small_elements().

template<class T>
int itpp::Sparse_Vec< T >::get_nz_index ( int  p  )  [inline]

Returns the vector index of the p-th non-zero element.

Definition at line 236 of file svec.h.

References itpp::Sparse_Vec< T >::remove_small_elements().

Referenced by itpp::operator *().

template<class T>
void itpp::Sparse_Vec< T >::get_nz ( int  p,
int &  idx,
T &  dat 
) [inline]

Returns the p-th non-zero value in dat and the corresponding vector index in idx.

Definition at line 245 of file svec.h.

References itpp::Sparse_Vec< T >::remove_small_elements().

template<class T>
Sparse_Vec< T > itpp::Sparse_Vec< T >::get_subvector ( int  i1,
int  i2 
) const [inline]

Return sparse subvector from index i1 to index i2.

Definition at line 784 of file svec.h.

References itpp::Sparse_Vec< T >::check_small_elems_flag, itpp::Sparse_Vec< T >::compact(), itpp::Sparse_Vec< T >::data, itpp::Sparse_Vec< T >::data_size, itpp::Sparse_Vec< T >::eps, itpp::Sparse_Vec< T >::index, it_assert0, itpp::Sparse_Vec< T >::resize_data(), and itpp::Sparse_Vec< T >::used_size.

template<class T>
T itpp::Sparse_Vec< T >::sqr (  )  const [inline]

Returns the sum of all values squared.

Definition at line 807 of file svec.h.

References itpp::sum().

template<class T>
void itpp::Sparse_Vec< T >::operator= ( const Sparse_Vec< T > &  v  )  [inline]

Assign sparse vector the value and length of the sparse vector v.

Definition at line 817 of file svec.h.

References itpp::Sparse_Vec< T >::check_small_elems_flag, itpp::Sparse_Vec< T >::data, itpp::Sparse_Vec< T >::data_size, itpp::Sparse_Vec< T >::eps, itpp::Sparse_Vec< T >::index, itpp::Sparse_Vec< T >::used_size, and itpp::Sparse_Vec< T >::v_size.

template<class T>
void itpp::Sparse_Vec< T >::operator= ( const Vec< T > &  v  )  [inline]

Assign sparse vector the value and length of the dense vector v.

Definition at line 834 of file svec.h.

References itpp::Sparse_Vec< T >::compact(), min, itpp::Sparse_Vec< T >::resize_data(), and itpp::Vec< Num_T >::size().

template<class T>
Sparse_Vec< T > itpp::Sparse_Vec< T >::operator- (  )  const [inline]

Returns the sign inverse of all elements in the sparse vector.

Definition at line 857 of file svec.h.

References itpp::Sparse_Vec< T >::data, itpp::Sparse_Vec< T >::index, and itpp::Sparse_Vec< T >::used_size.

template<class T>
bool itpp::Sparse_Vec< T >::operator== ( const Sparse_Vec< T > &  v  )  [inline]

Compare two sparse vectors. False if wrong sizes or different values.

Definition at line 871 of file svec.h.

References itpp::abs(), itpp::Sparse_Vec< T >::data, itpp::Sparse_Vec< T >::eps, itpp::Sparse_Vec< T >::index, itpp::Sparse_Vec< T >::remove_small_elements(), itpp::Sparse_Vec< T >::used_size, and itpp::Sparse_Vec< T >::v_size.

template<class T>
void itpp::Sparse_Vec< T >::operator+= ( const Sparse_Vec< T > &  v  )  [inline]

Add sparse vector v to all non-zero elements of the sparse vector.

Definition at line 928 of file svec.h.

References itpp::Sparse_Vec< T >::add_elem(), itpp::Sparse_Vec< T >::data, itpp::Sparse_Vec< T >::index, it_assert0, itpp::Sparse_Vec< T >::size(), and itpp::Sparse_Vec< T >::used_size.

template<class T>
void itpp::Sparse_Vec< T >::operator+= ( const Vec< T > &  v  )  [inline]

Add vector v to all non-zero elements of the sparse vector.

Definition at line 947 of file svec.h.

References itpp::Sparse_Vec< T >::add_elem(), it_assert0, and itpp::Vec< Num_T >::size().

template<class T>
void itpp::Sparse_Vec< T >::operator-= ( const Sparse_Vec< T > &  v  )  [inline]

Subtract sparse vector v from all non-zero elements of the sparse vector.

Definition at line 962 of file svec.h.

References itpp::Sparse_Vec< T >::add_elem(), itpp::Sparse_Vec< T >::data, itpp::Sparse_Vec< T >::index, it_assert0, itpp::Sparse_Vec< T >::size(), and itpp::Sparse_Vec< T >::used_size.

template<class T>
void itpp::Sparse_Vec< T >::operator-= ( const Vec< T > &  v  )  [inline]

Subtract vector v from all non-zero elements of the sparse vector.

Definition at line 981 of file svec.h.

References itpp::Sparse_Vec< T >::add_elem(), it_assert0, and itpp::Vec< Num_T >::size().

template<class T>
void itpp::Sparse_Vec< T >::operator *= ( const T &  v  )  [inline]

Multiply the scalar v to all non-zero elements of the sparse vector.

Definition at line 995 of file svec.h.

template<class T>
void itpp::Sparse_Vec< T >::operator/= ( const T &  v  )  [inline]

Divide all non-zero elements of the sparse vector with the scalar v.

Definition at line 1006 of file svec.h.

References itpp::abs().


Friends And Related Function Documentation

template<class T>
typedef Sparse_Vec<int> sparse_ivec [related]

Type definition of an integer sparse vector.

Definition at line 329 of file svec.h.

template<class T>
typedef Sparse_Vec<double> sparse_vec [related]

Type definition of a double sparse vector.

Definition at line 335 of file svec.h.

template<class T>
typedef Sparse_Vec<std::complex<double> > sparse_cvec [related]

Type definition of a complex<double> sparse vector.

Definition at line 341 of file svec.h.

template<class T>
Sparse_Vec<T> operator+ ( const Sparse_Vec< T > &  v1,
const Sparse_Vec< T > &  v2 
) [friend]

Addition v1+v2 where v1 and v2 are sparse vector.

template<class T>
T operator * ( const Sparse_Vec< T > &  v1,
const Sparse_Vec< T > &  v2 
) [friend]

Scalar product v1*v2 where v1 and v2 are sparse vectors.

template<class T>
T operator * ( const Sparse_Vec< T > &  v1,
const Vec< T > &  v2 
) [friend]

Scalar product v1*v2 where v1 is a sparse vector and v2 is a dense vector.

template<class T>
T operator * ( const Vec< T > &  v1,
const Sparse_Vec< T > &  v2 
) [friend]

Scalar product v1*v2 where v1 is a dense vector and v2 is a sparse vector.

template<class T>
Sparse_Vec<T> elem_mult ( const Sparse_Vec< T > &  v1,
const Sparse_Vec< T > &  v2 
) [friend]

Element wise multiplication of two sparse vectors.

template<class T>
Vec<T> elem_mult ( const Sparse_Vec< T > &  v1,
const Vec< T > &  v2 
) [friend]

Element wise multiplication of a sparse vector and a dense vector.

template<class T>
Sparse_Vec<T> elem_mult_s ( const Sparse_Vec< T > &  v1,
const Vec< T > &  v2 
) [friend]

Element wise multiplication of a sparse vector and a dense vector returning a sparse vector.

template<class T>
Vec<T> elem_mult ( const Vec< T > &  v1,
const Sparse_Vec< T > &  v2 
) [friend]

Element wise multiplication of a a dense vector and a sparse vector.

template<class T>
Sparse_Vec<T> elem_mult_s ( const Vec< T > &  v1,
const Sparse_Vec< T > &  v2 
) [friend]

Element wise multiplication of a a dense vector and a sparse vector returning a sparse vector.


The documentation for this class was generated from the following file:
SourceForge Logo

Generated on Thu Aug 30 02:47:24 2007 for IT++ by Doxygen 1.5.3