GammaLib 2.1.0.dev
Loading...
Searching...
No Matches
GVectorSparse Class Reference

Sparse vector class. More...

#include <GVectorSparse.hpp>

Inheritance diagram for GVectorSparse:
GBase

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.
 
GVectorSparseoperator= (const GVectorSparse &vector)
 Assignment operator.
 
void clear (void)
 Clear sparse vector.
 
GVectorSparseclone (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
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ GVectorSparse() [1/4]

GVectorSparse::GVectorSparse ( void )

Void sparse vector constructor.

Definition at line 50 of file GVectorSparse.cpp.

References init_members().

Referenced by clone().

◆ GVectorSparse() [2/4]

GVectorSparse::GVectorSparse ( const int & num,
const int & alloc = 0 )

Sparse vector constructor.

Parameters
[in]numVector size.
[in]allocNumber 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() [3/4]

GVectorSparse::GVectorSparse ( const GVector & vector)

Vector conversion constructor.

Parameters
[in]vectorVector.

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() [4/4]

GVectorSparse::GVectorSparse ( const GVectorSparse & vector)

Copy constructor.

Parameters
[in]vectorSparse vector.

Definition at line 126 of file GVectorSparse.cpp.

References copy_members(), and init_members().

◆ ~GVectorSparse()

GVectorSparse::~GVectorSparse ( void )
virtual

Destructor.

Definition at line 142 of file GVectorSparse.cpp.

References free_members().

Member Function Documentation

◆ alloc_members()

void GVectorSparse::alloc_members ( const int & alloc)
private

Allocate memory for elements.

Parameters
[in]allocNumber 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().

◆ classname()

std::string GVectorSparse::classname ( void ) const
inlinevirtual

Return class name.

Returns
String containing the class name ("GVectorSparse").

Implements GBase.

Definition at line 112 of file GVectorSparse.hpp.

◆ clear()

void GVectorSparse::clear ( void )
virtual

Clear sparse vector.

Implements GBase.

Definition at line 243 of file GVectorSparse.cpp.

References free_members(), and init_members().

◆ clone()

GVectorSparse * GVectorSparse::clone ( void ) const
virtual

Clone sparse vector.

Returns
Pointer to deep copy of sparse vector.

Implements GBase.

Definition at line 261 of file GVectorSparse.cpp.

References GVectorSparse().

◆ copy_members()

void GVectorSparse::copy_members ( const GVectorSparse & vector)
private

Copy class members.

Parameters
[in]vectorSparse 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=().

◆ data()

const double & GVectorSparse::data ( const int & index) const
inline

Return value for sparse vector element.

Parameters
[in]indexSparse vector element index.
Returns
Value of sparse vector element.

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().

◆ elements()

const int & GVectorSparse::elements ( void ) const
inline

Return number of elements in sparse vector.

Returns
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-=().

◆ free_members()

void GVectorSparse::free_members ( void )
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().

◆ index2inx()

int GVectorSparse::index2inx ( const int & index) const
private

Return inx for element index.

Parameters
[in]indexElement index [0,...,size()[.
Returns
Sparse index (-1 if not found).

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[]().

◆ init_members()

void GVectorSparse::init_members ( void )
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=().

◆ insert()

void GVectorSparse::insert ( const int & index,
const int & inx,
const double & data )
private

Insert one element into sparse vector.

Parameters
[in]indexElement index [0,...,elements()+1[.
[in]inxVector index [0,...,size()[.
[in]dataVector 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[]().

◆ inx()

const int & GVectorSparse::inx ( const int & index) const
inline

Return inx for sparse vector element.

Parameters
[in]indexSparse vector element index.
Returns
Index of element in uncompressed vector.

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[]().

◆ operator!=()

bool GVectorSparse::operator!= ( const GVectorSparse & vector) const

Non-equality operator.

Parameters
[in]vectorSparse vector.
Returns
True if both sparse vectors are different.

Definition at line 224 of file GVectorSparse.cpp.

References operator==().

◆ operator=()

GVectorSparse & GVectorSparse::operator= ( const GVectorSparse & vector)

Assignment operator.

Parameters
[in]vectorSparse vector.
Returns
Sparse vector.

Definition at line 164 of file GVectorSparse.cpp.

References copy_members(), free_members(), and init_members().

◆ operator==()

bool GVectorSparse::operator== ( const GVectorSparse & vector) const

Equality operator.

Parameters
[in]vectorSparse vector.
Returns
True if sparse vectors are identical.

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!=().

◆ operator[]() [1/2]

double & GVectorSparse::operator[] ( const int & index)

Sparse vector element access with range checking.

Parameters
[in]indexElement index [0,...,size()[.
Returns
Reference to vector element.
Exceptions
GException::out_of_rangeElement 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().

◆ operator[]() [2/2]

const double & GVectorSparse::operator[] ( const int & index) const

Sparse vector element access with range checking.

Parameters
[in]indexElement index [0,...,size()[.
Returns
Reference to vector element.
Exceptions
GException::out_of_rangeElement 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()

std::string GVectorSparse::print ( const GChatter & chatter = NORMAL) const
virtual

Print sparse vector information.

Parameters
[in]chatterChattiness.
Returns
String containing vector information.

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().

◆ size()

const int & GVectorSparse::size ( void ) const
inline

Return full size of sparse vector.

Returns
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[]().

Friends And Related Symbol Documentation

◆ GMatrixSparse

friend class GMatrixSparse
friend

Definition at line 58 of file GVectorSparse.hpp.

Member Data Documentation

◆ m_alloc

int GVectorSparse::m_alloc
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().

◆ m_colinx

int GVectorSparse::m_colinx
private

Column index.

Definition at line 100 of file GVectorSparse.hpp.

Referenced by GMatrixSparse::column_to_vector(), copy_members(), init_members(), and print().

◆ m_data

◆ m_elements

◆ m_inx

◆ m_num

int GVectorSparse::m_num
private

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