GammaLib 2.1.0.dev
|
Sparse vector class. More...
#include <GVectorSparse.hpp>
Public Member Functions | |
GVectorSparse (void) | |
Void sparse vector constructor. | |
GVectorSparse (const int &num, const int &alloc=0) | |
Sparse vector constructor. | |
GVectorSparse (const GVector &vector) | |
Vector conversion constructor. | |
GVectorSparse (const GVectorSparse &vector) | |
Copy constructor. | |
virtual | ~GVectorSparse (void) |
Destructor. | |
double & | operator[] (const int &index) |
Sparse vector element access with range checking. | |
const double & | operator[] (const int &index) const |
Sparse vector element access with range checking. | |
bool | operator== (const GVectorSparse &vector) const |
Equality operator. | |
bool | operator!= (const GVectorSparse &vector) const |
Non-equality operator. | |
GVectorSparse & | operator= (const GVectorSparse &vector) |
Assignment operator. | |
void | clear (void) |
Clear sparse vector. | |
GVectorSparse * | clone (void) const |
Clone sparse vector. | |
std::string | classname (void) const |
Return class name. | |
const int & | size (void) const |
Return full size of sparse vector. | |
const int & | elements (void) const |
Return number of elements in sparse vector. | |
const int & | inx (const int &index) const |
Return inx for sparse vector element. | |
const double & | data (const int &index) const |
Return value for sparse vector element. | |
std::string | print (const GChatter &chatter=NORMAL) const |
Print sparse vector information. | |
Public Member Functions inherited from GBase | |
virtual | ~GBase (void) |
Destructor. | |
Private Member Functions | |
void | init_members (void) |
Initialise class members. | |
void | copy_members (const GVectorSparse &vector) |
Copy class members. | |
void | free_members (void) |
Delete class members. | |
void | alloc_members (const int &alloc) |
Allocate memory for elements. | |
int | index2inx (const int &index) const |
Return inx for element index. | |
void | insert (const int &index, const int &inx, const double &data) |
Insert one element into sparse vector. | |
Private Attributes | |
int | m_num |
Vector size. | |
int | m_elements |
Number of elements in vector. | |
int | m_alloc |
Number of allocated elements. | |
int | m_colinx |
Column index. | |
int * | m_inx |
Index array. | |
double * | m_data |
Data array. | |
Friends | |
class | GMatrixSparse |
Sparse vector class.
This class implements a sparse double precision floating point vector that is mainly used as an interface class to the sparse matrix class. In that way, sparse matrix columns can be extracted and filled without carying unnecessary zero elements.
Note that GMatrixSparse is a friend class of GVectorSparse so that it can directly manipulate the class members, avoiding any overhead.
Definition at line 55 of file GVectorSparse.hpp.
GVectorSparse::GVectorSparse | ( | void | ) |
Void sparse vector constructor.
Definition at line 50 of file GVectorSparse.cpp.
References init_members().
Referenced by clone().
GVectorSparse::GVectorSparse | ( | const int & | num, |
const int & | alloc = 0 ) |
Sparse vector constructor.
[in] | num | Vector size. |
[in] | alloc | Number of elements for allocation. |
Constructs a sparse vector, specifying the size of the expanded vector num
and the number of elements alloc
for which memory should be allocated. Using pre-allocated memory reduces the dynamic reallocation of memory.
Definition at line 71 of file GVectorSparse.cpp.
References alloc_members(), init_members(), and m_num.
GVectorSparse::GVectorSparse | ( | const GVector & | vector | ) |
Vector conversion constructor.
[in] | vector | Vector. |
Converts a regular vector into a sparse vector by stripping all zero elements.
Definition at line 95 of file GVectorSparse.cpp.
References alloc_members(), init_members(), m_data, m_elements, m_inx, m_num, GVector::non_zeros(), and GVector::size().
GVectorSparse::GVectorSparse | ( | const GVectorSparse & | vector | ) |
Copy constructor.
[in] | vector | Sparse vector. |
Definition at line 126 of file GVectorSparse.cpp.
References copy_members(), and init_members().
|
virtual |
|
private |
Allocate memory for elements.
[in] | alloc | Number of elements for allocation. |
Allocates memory for alloc
elements. Memory for existing elements will be deleted.
Definition at line 502 of file GVectorSparse.cpp.
References m_alloc, m_data, and m_inx.
Referenced by GMatrixSparse::column_to_vector(), GVectorSparse(), and GVectorSparse().
|
inlinevirtual |
Return class name.
Implements GBase.
Definition at line 112 of file GVectorSparse.hpp.
|
virtual |
Clear sparse vector.
Implements GBase.
Definition at line 243 of file GVectorSparse.cpp.
References free_members(), and init_members().
|
virtual |
Clone sparse vector.
Implements GBase.
Definition at line 261 of file GVectorSparse.cpp.
References GVectorSparse().
|
private |
Copy class members.
[in] | vector | Sparse vector. |
Definition at line 444 of file GVectorSparse.cpp.
References m_alloc, m_colinx, m_data, m_elements, m_inx, and m_num.
Referenced by GVectorSparse(), and operator=().
|
inline |
Return value for sparse vector element.
[in] | index | Sparse vector element index. |
Returns the value of element index
.
This method does not perform any check on the validity of index
, hence the client needs to make sure that index
is valid.
Definition at line 177 of file GVectorSparse.hpp.
References m_data.
Referenced by insert(), GObservation::likelihood_gaussian_binned(), and GObservation::likelihood_poisson_binned().
|
inline |
Return number of elements in sparse vector.
Returns the number of non-zero vector elements.
Definition at line 141 of file GVectorSparse.hpp.
References m_elements.
Referenced by GMatrixSparse::colfill_set_column(), GObservation::likelihood_gaussian_binned(), GObservation::likelihood_poisson_binned(), GMatrixSparse::operator+=(), and GMatrixSparse::operator-=().
|
private |
Delete class members.
Definition at line 476 of file GVectorSparse.cpp.
References m_alloc, m_data, and m_inx.
Referenced by clear(), operator=(), and ~GVectorSparse().
|
private |
Return inx for element index.
[in] | index | Element index [0,...,size()[. |
Returns sparse index for a given element index. If no sparse index is found the method returns -1.
Definition at line 531 of file GVectorSparse.cpp.
References inx(), m_elements, and m_inx.
Referenced by operator[]().
|
private |
Initialise class members.
Definition at line 424 of file GVectorSparse.cpp.
References m_alloc, m_colinx, m_data, m_elements, m_inx, and m_num.
Referenced by clear(), GVectorSparse(), GVectorSparse(), GVectorSparse(), GVectorSparse(), and operator=().
|
private |
Insert one element into sparse vector.
[in] | index | Element index [0,...,elements()+1[. |
[in] | inx | Vector index [0,...,size()[. |
[in] | data | Vector data. |
Insert one element into the sparse vector. An element consists of an index inx
and a value data
. The element is inserted before the element with index
. If index
is equal to elements() the element is appended.
The method does only allocate fresh memory in case that the existing memory is exhausted. Fresh memory is allocated in blocks of 512 elements until the total vector size is filled.
Definition at line 565 of file GVectorSparse.cpp.
References data(), G_SPARSE_VECTOR_DEFAULT_MEM_BLOCK, inx(), m_alloc, m_data, m_elements, m_inx, and m_num.
Referenced by operator[]().
|
inline |
Return inx for sparse vector element.
[in] | index | Sparse vector element index. |
Returns the index of element index
in uncompressed vector.
This method does not perform any check on the validity of index
, hence the client needs to make sure that index
is valid.
Definition at line 159 of file GVectorSparse.hpp.
References m_inx.
Referenced by index2inx(), insert(), GObservation::likelihood_gaussian_binned(), GObservation::likelihood_poisson_binned(), GMatrixSparse::operator+=(), GMatrixSparse::operator-=(), and operator[]().
bool GVectorSparse::operator!= | ( | const GVectorSparse & | vector | ) | const |
Non-equality operator.
[in] | vector | Sparse vector. |
Definition at line 224 of file GVectorSparse.cpp.
References operator==().
GVectorSparse & GVectorSparse::operator= | ( | const GVectorSparse & | vector | ) |
Assignment operator.
[in] | vector | Sparse vector. |
Definition at line 164 of file GVectorSparse.cpp.
References copy_members(), free_members(), and init_members().
bool GVectorSparse::operator== | ( | const GVectorSparse & | vector | ) | const |
Equality operator.
[in] | vector | Sparse vector. |
Returns true if both sparse vectors are identical. Sparse vectors are considered identical if they have the same size and if all their elements are identical.
Definition at line 195 of file GVectorSparse.cpp.
References m_data, m_elements, m_inx, and m_num.
Referenced by operator!=().
double & GVectorSparse::operator[] | ( | const int & | index | ) |
Sparse vector element access with range checking.
[in] | index | Element index [0,...,size()[. |
GException::out_of_range | Element index is out of range. |
Returns the sparse vector element on the basis of the index
of the full vector. If no element exists yet for this index
, a element with value of zero will be added to the vector. Note that using the method will be very slow, and if no values are set, the sparse vector will be populated with zero values.
Definition at line 283 of file GVectorSparse.cpp.
References G_OPERATOR, insert(), m_data, m_elements, m_inx, and size().
const double & GVectorSparse::operator[] | ( | const int & | index | ) | const |
Sparse vector element access with range checking.
[in] | index | Element index [0,...,size()[. |
GException::out_of_range | Element index is out of range. |
Returns the sparse vector element on the basis of the index
of the full vector. If no element exists for this index
, a static zero value is returned.
Definition at line 355 of file GVectorSparse.cpp.
References G_OPERATOR, index2inx(), inx(), m_data, and size().
Print sparse vector information.
[in] | chatter | Chattiness. |
Implements GBase.
Definition at line 385 of file GVectorSparse.cpp.
References m_alloc, m_colinx, m_elements, m_num, gammalib::parformat(), SILENT, and gammalib::str().
|
inline |
Return full size of sparse vector.
Returns the full size of the vector, including all zero elements. To get the number of non-zero vector elements, use the elements() method.
Definition at line 127 of file GVectorSparse.hpp.
References m_num.
Referenced by GMatrixSparse::colfill_set_column(), operator[](), and operator[]().
|
friend |
Definition at line 58 of file GVectorSparse.hpp.
|
private |
Number of allocated elements.
Definition at line 99 of file GVectorSparse.hpp.
Referenced by alloc_members(), GMatrixSparse::column_to_vector(), copy_members(), free_members(), init_members(), insert(), and print().
|
private |
Column index.
Definition at line 100 of file GVectorSparse.hpp.
Referenced by GMatrixSparse::column_to_vector(), copy_members(), init_members(), and print().
|
private |
Data array.
Definition at line 102 of file GVectorSparse.hpp.
Referenced by GMatrixSparse::add_to_column(), alloc_members(), GMatrixSparse::colfill_set_column(), GMatrixSparse::column_to_vector(), copy_members(), data(), free_members(), GVectorSparse(), init_members(), insert(), GMatrixSparse::operator+=(), GMatrixSparse::operator-=(), operator==(), operator[](), and operator[]().
|
private |
Number of elements in vector.
Definition at line 98 of file GVectorSparse.hpp.
Referenced by GMatrixSparse::add_to_column(), GMatrixSparse::column_to_vector(), copy_members(), elements(), GVectorSparse(), index2inx(), init_members(), insert(), GMatrixSparse::operator+=(), GMatrixSparse::operator-=(), operator==(), operator[](), and print().
|
private |
Index array.
Definition at line 101 of file GVectorSparse.hpp.
Referenced by GMatrixSparse::add_to_column(), alloc_members(), GMatrixSparse::colfill_set_column(), GMatrixSparse::column_to_vector(), copy_members(), free_members(), GVectorSparse(), index2inx(), init_members(), insert(), inx(), GMatrixSparse::operator+=(), GMatrixSparse::operator-=(), operator==(), and operator[]().
|
private |
Vector size.
Definition at line 97 of file GVectorSparse.hpp.
Referenced by GMatrixSparse::column_to_vector(), copy_members(), GVectorSparse(), GVectorSparse(), init_members(), insert(), operator==(), print(), and size().