GammaLib 2.1.0.dev
Loading...
Searching...
No Matches
GVectorSparse.hpp
Go to the documentation of this file.
1/***************************************************************************
2 * GVectorSparse.hpp - Sparse vector class *
3 * ----------------------------------------------------------------------- *
4 * copyright (C) 2024 by Juergen Knoedlseder *
5 * ----------------------------------------------------------------------- *
6 * *
7 * This program is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation, either version 3 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19 * *
20 ***************************************************************************/
21/**
22 * @file GVectorSparse.hpp
23 * @brief Sparce vector class interface definition
24 * @author Juergen Knoedlseder
25 */
26
27#ifndef GSPARSEVECTOR_HPP
28#define GSPARSEVECTOR_HPP
29
30/* __ Includes ___________________________________________________________ */
31#include <string>
32#include "GBase.hpp"
33#include "GException.hpp"
34
35/* __ Definitions ________________________________________________________ */
36#define G_SPARSE_VECTOR_DEFAULT_MEM_BLOCK 512 // Default Memory block size
37
38/* __ Forward declarations _______________________________________________ */
39class GVector;
40
41
42/***********************************************************************//**
43 * @class GVectorSparse
44 *
45 * @brief Sparse vector class
46 *
47 * This class implements a sparse double precision floating point vector
48 * that is mainly used as an interface class to the sparse matrix class.
49 * In that way, sparse matrix columns can be extracted and filled without
50 * carying unnecessary zero elements.
51 *
52 * Note that GMatrixSparse is a friend class of GVectorSparse so that it
53 * can directly manipulate the class members, avoiding any overhead.
54 ***************************************************************************/
55class GVectorSparse : public GBase {
56
57 // Friend classes
58 friend class GMatrixSparse;
59
60public:
61 // Constructors and destructors
62 GVectorSparse(void);
63 GVectorSparse(const int& num, const int& alloc = 0);
64 GVectorSparse(const GVector& vector);
65 GVectorSparse(const GVectorSparse& vector);
66 virtual ~GVectorSparse(void);
67
68 // Sparse vector element access operators
69 double& operator[](const int& index);
70 const double& operator[](const int& index) const;
71
72 // Vector operators
73 bool operator==(const GVectorSparse& vector) const;
74 bool operator!=(const GVectorSparse& vector) const;
76
77 // Sparse vector methods
78 void clear(void);
79 GVectorSparse* clone(void) const;
80 std::string classname(void) const;
81 const int& size(void) const;
82 const int& elements(void) const;
83 const int& inx(const int& index) const;
84 const double& data(const int& index) const;
85 std::string print(const GChatter& chatter = NORMAL) const;
86
87private:
88 // Private methods
89 void init_members(void);
90 void copy_members(const GVectorSparse& vector);
91 void free_members(void);
92 void alloc_members(const int& alloc);
93 int index2inx(const int& index) const;
94 void insert(const int& index, const int& inx, const double& data);
95
96 // Private data area
97 int m_num; //!< Vector size
98 int m_elements; //!< Number of elements in vector
99 int m_alloc; //!< Number of allocated elements
100 int m_colinx; //!< Column index
101 int* m_inx; //!< Index array
102 double* m_data; //!< Data array
103};
104
105
106/***********************************************************************//**
107 * @brief Return class name
108 *
109 * @return String containing the class name ("GVectorSparse").
110 ***************************************************************************/
111inline
112std::string GVectorSparse::classname(void) const
113{
114 return ("GVectorSparse");
115}
116
117
118/***********************************************************************//**
119 * @brief Return full size of sparse vector
120 *
121 * @return Full size of sparse vector
122 *
123 * Returns the full size of the vector, including all zero elements. To
124 * get the number of non-zero vector elements, use the elements() method.
125 ***************************************************************************/
126inline
127const int& GVectorSparse::size() const
128{
129 return (m_num);
130}
131
132
133/***********************************************************************//**
134 * @brief Return number of elements in sparse vector
135 *
136 * @return Number of elements in sparse vector
137 *
138 * Returns the number of non-zero vector elements.
139 ***************************************************************************/
140inline
141const int& GVectorSparse::elements() const
142{
143 return (m_elements);
144}
145
146
147/***********************************************************************//**
148 * @brief Return inx for sparse vector element
149 *
150 * @param[in] index Sparse vector element index.
151 * @return Index of element in uncompressed vector.
152 *
153 * Returns the index of element @p index in uncompressed vector.
154 *
155 * This method does not perform any check on the validity of @p index, hence
156 * the client needs to make sure that @p index is valid.
157 ***************************************************************************/
158inline
159const int& GVectorSparse::inx(const int& index) const
160{
161 return (m_inx[index]);
162}
163
164
165/***********************************************************************//**
166 * @brief Return value for sparse vector element
167 *
168 * @param[in] index Sparse vector element index.
169 * @return Value of sparse vector element.
170 *
171 * Returns the value of element @p index.
172 *
173 * This method does not perform any check on the validity of @p index, hence
174 * the client needs to make sure that @p index is valid.
175 ***************************************************************************/
176inline
177const double& GVectorSparse::data(const int& index) const
178{
179 return (m_data[index]);
180}
181
182#endif /* GSPARSEVECTOR_HPP */
Definition of interface for all GammaLib classes.
Exception handler interface definition.
GChatter
Definition GTypemaps.hpp:33
@ NORMAL
Definition GTypemaps.hpp:36
Interface class for all GammaLib classes.
Definition GBase.hpp:52
Sparse matrix class interface definition.
Sparse vector class.
int m_elements
Number of elements in vector.
int * m_inx
Index array.
std::string classname(void) const
Return class name.
void alloc_members(const int &alloc)
Allocate memory for elements.
void copy_members(const GVectorSparse &vector)
Copy class members.
GVectorSparse & operator=(const GVectorSparse &vector)
Assignment operator.
int m_alloc
Number of allocated elements.
void free_members(void)
Delete class members.
int index2inx(const int &index) const
Return inx for element index.
GVectorSparse(void)
Void sparse vector constructor.
double & operator[](const int &index)
Sparse vector element access with range checking.
void clear(void)
Clear sparse vector.
bool operator==(const GVectorSparse &vector) const
Equality operator.
const int & size(void) const
Return full size of sparse vector.
const int & inx(const int &index) const
Return inx for sparse vector element.
GVectorSparse * clone(void) const
Clone sparse vector.
const double & data(const int &index) const
Return value for sparse vector element.
int m_num
Vector size.
bool operator!=(const GVectorSparse &vector) const
Non-equality operator.
virtual ~GVectorSparse(void)
Destructor.
const int & elements(void) const
Return number of elements in sparse vector.
void insert(const int &index, const int &inx, const double &data)
Insert one element into sparse vector.
void init_members(void)
Initialise class members.
double * m_data
Data array.
int m_colinx
Column index.
std::string print(const GChatter &chatter=NORMAL) const
Print sparse vector information.
Vector class.
Definition GVector.hpp:46