summaryrefslogtreecommitdiffstats
path: root/include/astra/SparseMatrix.h
blob: acad42ee64504b7a2c20f323a707a9d55b234157 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
-----------------------------------------------------------------------
Copyright: 2010-2021, imec Vision Lab, University of Antwerp
           2014-2021, CWI, Amsterdam

Contact: astra@astra-toolbox.com
Website: http://www.astra-toolbox.com/

This file is part of the ASTRA Toolbox.


The ASTRA Toolbox is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

The ASTRA Toolbox is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>.

-----------------------------------------------------------------------
*/

#ifndef _INC_ASTRA_SPARSEMATRIX
#define _INC_ASTRA_SPARSEMATRIX

namespace astra
{


/** This class implements a sparse matrix. It is stored as three arrays.
 *  The values are stored row-by-row.
 *  m_pfValues     contains the values
 *  m_piColIndices contains the col indices of the values
 *  m_plRowStarts  contains the start offsets of the rows
 */


class _AstraExport CSparseMatrix {
public:
	CSparseMatrix();

	// TODO: are ints large enough for width/height?
	CSparseMatrix(unsigned int _iHeight, unsigned int _iWidth,
	              unsigned long _lSize);

	/** Initialize the matrix structure.
	 *  It does not initialize any values.
	 *
	 * @param _iHeight number of rows
	 * @param _iWidth number of columns
	 * @param _lSize maximum number of non-zero entries
	 * @return initialization successful?
	 */

	bool initialize(unsigned int _iHeight, unsigned int _iWidth,
	                unsigned long _lSize);

	/** Destructor.
	 */
	~CSparseMatrix();

	/** Has the matrix structure been initialized?
	 *
	 * @return initialized successfully
	 */
	bool isInitialized() const { return m_bInitialized; }

	/** get a description of the class
	 *
	 * @return description string
	 */
	std::string description() const;

	/** get the data for a single row. Entries are stored from left to right.
	 *
	 * @param _iRow the row
	 * @param _iSize the returned number of elements in the row
	 * @param _pfValues the values of the non-zero entries in the row
	 * @param _piColIndices the column indices of the non-zero entries
	 */
	void getRowData(unsigned int _iRow, unsigned int& _iSize,
	                   const float32*& _pfValues, const unsigned int*& _piColIndices) const
	{
		assert(_iRow < m_iHeight);
		unsigned long lStart = m_plRowStarts[_iRow];
		_iSize = m_plRowStarts[_iRow+1] - lStart;
		_pfValues = &m_pfValues[lStart];
		_piColIndices = &m_piColIndices[lStart];
	}

	/** get the number of elements in a row
	 *
	 * @param _iRow the row
	 * @return number of stored entries in the row
	 */
	unsigned int getRowSize(unsigned int _iRow) const
	{
		assert(_iRow < m_iHeight);
		return m_plRowStarts[_iRow+1] - m_plRowStarts[_iRow];
	}


	/** Matrix width
	 */
	unsigned int m_iHeight;
	
	/** Matrix height
	 */
	unsigned int m_iWidth;
	
	/** Maximum number of non-zero entries
	 */
	unsigned long m_lSize;

	/** Contains the numeric values of all non-zero elements
	 */
	float32* m_pfValues;
	
	/** Contains the colon index of all non-zero elements
	 */
	unsigned int* m_piColIndices;
	
	/** The indices in this array point to the first element of each row in the m_pfValues array
	 */
	unsigned long* m_plRowStarts;

protected:
	
	/** Is the class initialized?
	 */
	bool m_bInitialized;
};


}


#endif