IT++ Logo
Public Member Functions | Friends | Related Functions | List of all members
itpp::Sparse_Vec< T > Class Template Reference

Templated sparse vector class. More...

#include <itpp/base/svec.h>

Public Member Functions

 Sparse_Vec ()
 Default constructor. More...
 
 Sparse_Vec (int sz, int data_init=200)
 Initiate an empty sparse vector. More...
 
 Sparse_Vec (const Sparse_Vec< T > &v)
 Initiate a new sparse vector. More...
 
 Sparse_Vec (const Vec< T > &v)
 Initiate a new sparse vector from a dense vector. More...
 
 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. More...
 
 ~Sparse_Vec ()
 Destructor. More...
 
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. More...
 
int size () const
 Returns the size of the sparse vector. More...
 
int nnz ()
 Number of non-zero elements in the sparse vector. More...
 
double density ()
 Returns the density of the sparse vector: (number of non-zero elements)/(size of the vector) More...
 
void set_small_element (const T &epsilon)
 Set that all elements smaller than epsilon should be set to zero. More...
 
void remove_small_elements ()
 
void resize_data (int new_size)
 Set the maximum number of non-zero elements to new_size. More...
 
void compact ()
 Set the maximum number of non-zero elements equal to the actual number of non-zero elements. More...
 
void full (Vec< T > &v) const
 Returns a full, dense vector in v. More...
 
Vec< T > full () const
 Returns a full, dense vector. More...
 
operator() (int i) const
 Returns the element with index i. More...
 
void set (int i, T v)
 Set element i equal to v. More...
 
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. More...
 
void set_new (int i, T v)
 Set a new element with index i equal to v. More...
 
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) More...
 
void add_elem (const int i, const T v)
 Add element i with v. More...
 
void add (const ivec &index_vec, const Vec< T > &v)
 Add v to the elements specified by index_vec with v. More...
 
void zeros ()
 Set the sparse vector to the all zero vector (removes all non-zero elements) More...
 
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) More...
 
void clear ()
 Clear all non-zero elements of the sparse vector. More...
 
void clear_elem (const int i)
 Clear the i-th element (if it contains a non-zero value) More...
 
void get_nz_data (int p, T &data_out)
 Extract the reference to the p-th non-zero data element. More...
 
get_nz_data (int p)
 Returns the p-th non-zero data element. More...
 
int get_nz_index (int p)
 Returns the vector index of the p-th non-zero element. More...
 
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. More...
 
ivec get_nz_indices ()
 Return the indices of non-zero values. More...
 
Sparse_Vec< T > get_subvector (int i1, int i2) const
 Return sparse subvector from index i1 to index i2. More...
 
sqr () const
 Returns the sum of all values squared. More...
 
void operator= (const Sparse_Vec< T > &v)
 Assign sparse vector the value and length of the sparse vector v. More...
 
void operator= (const Vec< T > &v)
 Assign sparse vector the value and length of the dense vector v. More...
 
Sparse_Vec< T > operator- () const
 Returns the sign inverse of all elements in the sparse vector. More...
 
bool operator== (const Sparse_Vec< T > &v)
 Compare two sparse vectors. False if wrong sizes or different values. More...
 
void operator+= (const Sparse_Vec< T > &v)
 Add sparse vector v to all non-zero elements of the sparse vector. More...
 
void operator+= (const Vec< T > &v)
 Add vector v to all non-zero elements of the sparse vector. More...
 
void operator-= (const Sparse_Vec< T > &v)
 Subtract sparse vector v from all non-zero elements of the sparse vector. More...
 
void operator-= (const Vec< T > &v)
 Subtract vector v from all non-zero elements of the sparse vector. More...
 
void operator*= (const T &v)
 Multiply the scalar v to all non-zero elements of the sparse vector. More...
 
void operator/= (const T &v)
 Divide all non-zero elements of the sparse vector with the scalar v. More...
 

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. More...
 
operator* (const Sparse_Vec< T > &v1, const Sparse_Vec< T > &v2)
 Scalar product v1*v2 where v1 and v2 are sparse vectors. More...
 
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. More...
 
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. More...
 
Sparse_Vec< T > elem_mult (const Sparse_Vec< T > &v1, const Sparse_Vec< T > &v2)
 Element wise multiplication of two sparse vectors. More...
 
Vec< T > elem_mult (const Sparse_Vec< T > &v1, const Vec< T > &v2)
 Element wise multiplication of a sparse vector and a dense vector. More...
 
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. More...
 
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. More...
 
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. More...
 

Related Functions

(Note that these are not member functions.)

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

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. The elements are stored in random order, i.e. they are not sorted.

Definition at line 107 of file svec.h.

Constructor & Destructor Documentation

◆ Sparse_Vec() [1/5]

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

Default constructor.

Definition at line 388 of file svec.h.

◆ Sparse_Vec() [2/5]

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

Initiate an empty sparse vector.

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

Definition at line 394 of file svec.h.

◆ Sparse_Vec() [3/5]

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

Initiate a new sparse vector.

Parameters
vThe elements of v are copied into the new sparse vector

Definition at line 404 of file svec.h.

References itpp::eps.

◆ Sparse_Vec() [4/5]

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

Initiate a new sparse vector from a dense vector.

Parameters
vThe elements of v are copied into the new sparse vector

Definition at line 421 of file svec.h.

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

◆ Sparse_Vec() [5/5]

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

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 442 of file svec.h.

References std::abs(), itpp::eps, and itpp::Vec< Num_T >::size().

◆ ~Sparse_Vec()

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

Destructor.

Definition at line 464 of file svec.h.

Member Function Documentation

◆ set_size()

template<class T >
void itpp::Sparse_Vec< T >::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.

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

Definition at line 470 of file svec.h.

◆ size()

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

Returns the size of the sparse vector.

Definition at line 155 of file svec.h.

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

◆ nnz()

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

Number of non-zero elements in the sparse vector.

Definition at line 158 of file svec.h.

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

Referenced by itpp::LDPC_Parity_Unstructured::cycle_removal_MGW().

◆ density()

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

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

Definition at line 482 of file svec.h.

◆ set_small_element()

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

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

Definition at line 492 of file svec.h.

References std::abs(), and itpp::eps.

◆ remove_small_elements()

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

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 499 of file svec.h.

References std::abs(), and itpp::eps.

Referenced by itpp::Sparse_Vec< T >::get_nz(), itpp::Sparse_Vec< T >::get_nz_data(), itpp::Sparse_Vec< T >::get_nz_index(), and itpp::Sparse_Vec< T >::nnz().

◆ resize_data()

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

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

Parameters
new_sizeThe new maximum number of non-zero elements.

Definition at line 524 of file svec.h.

References it_assert.

Referenced by itpp::Sparse_Vec< T >::get_subvector().

◆ compact()

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

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

Definition at line 547 of file svec.h.

Referenced by itpp::Sparse_Vec< T >::get_subvector().

◆ full() [1/2]

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

Returns a full, dense vector in v.

Definition at line 556 of file svec.h.

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

Referenced by itpp::full().

◆ full() [2/2]

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

Returns a full, dense vector.

Definition at line 566 of file svec.h.

References itpp::full().

◆ operator()()

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

Returns the element with index i.

Definition at line 575 of file svec.h.

References it_assert_debug.

◆ set() [1/2]

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

Set element i equal to v.

Definition at line 591 of file svec.h.

References std::abs(), itpp::eps, and it_assert_debug.

◆ set() [2/2]

template<class T >
void itpp::Sparse_Vec< T >::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.

Definition at line 759 of file svec.h.

References it_assert_debug, and itpp::max().

◆ set_new() [1/2]

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

Set a new element with index i equal to v.

Definition at line 626 of file svec.h.

References std::abs(), itpp::eps, and it_assert_debug.

◆ set_new() [2/2]

template<class T >
void itpp::Sparse_Vec< T >::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)

Definition at line 771 of file svec.h.

References std::abs(), itpp::eps, it_assert_debug, itpp::max(), and itpp::Vec< Num_T >::size().

◆ add_elem()

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

Add element i with v.

Definition at line 641 of file svec.h.

References it_assert_debug.

◆ add()

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

Add v to the elements specified by index_vec with v.

Definition at line 669 of file svec.h.

References it_assert_debug, itpp::max(), and itpp::Vec< Num_T >::size().

◆ zeros()

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

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

Definition at line 703 of file svec.h.

◆ zero_elem()

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

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

Definition at line 710 of file svec.h.

References it_assert_debug.

◆ clear()

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

Clear all non-zero elements of the sparse vector.

Definition at line 731 of file svec.h.

◆ clear_elem()

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

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

Definition at line 738 of file svec.h.

References it_assert_debug.

◆ get_nz_data() [1/2]

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 230 of file svec.h.

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

◆ get_nz_data() [2/2]

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 238 of file svec.h.

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

◆ get_nz_index()

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 246 of file svec.h.

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

Referenced by itpp::LDPC_Parity_Unstructured::cycle_removal_MGW().

◆ get_nz()

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 254 of file svec.h.

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

◆ get_nz_indices()

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

Return the indices of non-zero values.

Definition at line 793 of file svec.h.

Referenced by itpp::LDPC_Parity::check_connectivity(), itpp::LDPC_Parity::check_for_cycles(), and itpp::LDPC_Code::decoder_parameterization().

◆ get_subvector()

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

Return sparse subvector from index i1 to index i2.

Definition at line 804 of file svec.h.

References itpp::Sparse_Vec< T >::compact(), itpp::eps, it_assert_debug, and itpp::Sparse_Vec< T >::resize_data().

◆ sqr()

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

Returns the sum of all values squared.

Definition at line 827 of file svec.h.

References itpp::sum().

◆ operator=() [1/2]

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

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

Definition at line 837 of file svec.h.

References itpp::eps.

◆ operator=() [2/2]

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

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

Definition at line 854 of file svec.h.

References std::abs(), itpp::eps, and itpp::Vec< Num_T >::size().

◆ operator-()

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

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

Definition at line 877 of file svec.h.

◆ operator==()

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

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

Definition at line 891 of file svec.h.

References std::abs().

◆ operator+=() [1/2]

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

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

Definition at line 949 of file svec.h.

References it_assert_debug, and itpp::Sparse_Vec< T >::size().

◆ operator+=() [2/2]

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

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

Definition at line 968 of file svec.h.

References it_assert_debug, and itpp::Vec< Num_T >::size().

◆ operator-=() [1/2]

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

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

Definition at line 983 of file svec.h.

References it_assert_debug, and itpp::Sparse_Vec< T >::size().

◆ operator-=() [2/2]

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

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

Definition at line 1002 of file svec.h.

References it_assert_debug, and itpp::Vec< Num_T >::size().

◆ operator*=()

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

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

Definition at line 1016 of file svec.h.

◆ operator/=()

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

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

Definition at line 1028 of file svec.h.

References itpp::eps.

Friends And Related Function Documentation

◆ sparse_ivec

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

Type definition of an integer sparse vector.

Definition at line 341 of file svec.h.

◆ sparse_vec

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

Type definition of a double sparse vector.

Definition at line 347 of file svec.h.

◆ sparse_cvec

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

Type definition of a complex<double> sparse vector.

Definition at line 353 of file svec.h.

◆ operator+

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.

Definition at line 1171 of file svec.h.

◆ operator* [1/3]

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.

Definition at line 1041 of file svec.h.

◆ operator* [2/3]

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.

Definition at line 1057 of file svec.h.

◆ operator* [3/3]

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.

Definition at line 1069 of file svec.h.

◆ elem_mult [1/3]

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.

Definition at line 1081 of file svec.h.

◆ elem_mult [2/3]

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.

Definition at line 1105 of file svec.h.

◆ elem_mult_s [1/2]

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.

Definition at line 1118 of file svec.h.

◆ elem_mult [3/3]

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.

Definition at line 1138 of file svec.h.

◆ elem_mult_s [2/2]

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.

Definition at line 1151 of file svec.h.


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

Generated on Tue Jan 24 2023 00:00:00 for IT++ by Doxygen 1.9.5