ergo
MatrixHierarchicBase.h
Go to the documentation of this file.
1/* Ergo, version 3.8.2, a program for linear scaling electronic structure
2 * calculations.
3 * Copyright (C) 2023 Elias Rudberg, Emanuel H. Rubensson, Pawel Salek,
4 * and Anastasia Kruchinina.
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Primary academic reference:
20 * Ergo: An open-source program for linear-scaling electronic structure
21 * calculations,
22 * Elias Rudberg, Emanuel H. Rubensson, Pawel Salek, and Anastasia
23 * Kruchinina,
24 * SoftwareX 7, 107 (2018),
25 * <http://dx.doi.org/10.1016/j.softx.2018.03.005>
26 *
27 * For further information about Ergo, see <http://www.ergoscf.org>.
28 */
29
40#ifndef MAT_MATRIXHIERARCHICBASE
41#define MAT_MATRIXHIERARCHICBASE
42#include "matInclude.h"
43#include "allocate.h"
44namespace mat{
51 template<class Treal, class Telement = Treal>
53 public:
54 /* No public constructors (!) */
55 inline bool operator==(int k) const {
56 if (k == 0)
57 return this->is_zero();
58 else
59 throw Failure("Matrix::operator== only implemented for k == 0");
60 }
61 /* Check if matrix is zero (k = 0) */
62#if 1
63 inline const int& nScalarsRows() const
64 {return rows.getNScalars();}
65 inline const int& nScalarsCols() const
66 {return cols.getNScalars();}
67#endif
68
69 inline const int& nrows() const /* Number of rows in Telement matrix */
70 {return rows.getNBlocks();}
71 inline const int& ncols() const /* Number of cols in Telement matrix */
72 {return cols.getNBlocks();}
73
74 inline Telement& operator() /* Returns the element A(row, col) */
75 (int row, int col) {
77 assert(row >= 0);
78 assert(col >= 0);
79 assert(row < nrows());
81 return elements[row + col * nrows()];
82 }
83 inline const Telement& operator() /*Write protected reference returned*/
84 (int row, int col) const {
86 assert(row >= 0);
87 assert(col >= 0);
88 assert(row < nrows());
90 return elements[row + col * nrows()];
91 }
92
93 inline Telement& operator[]
94 (int index) {
96 assert(index >= 0);
97 assert(index < nElements());
98 return elements[index];
99 }
100 inline Telement const & operator[]
101 (int index) const {
103 assert(index >= 0);
104 assert(index < nElements());
105 return elements[index];
106 }
107
108 inline bool is_zero() const {return !elements;}
109
110 inline int nElements() const {
111 return rows.getNBlocks() * cols.getNBlocks();
112 }
113
114 inline void resetRows(SizesAndBlocks const & newRows) {
116 elements = 0;
117 rows = newRows;
118 }
119 inline void resetCols(SizesAndBlocks const & newCols) {
121 elements = 0;
122 cols = newCols;
123 }
124
125 inline void getRows(SizesAndBlocks & rowsCopy) const {
126 rowsCopy = rows;
127 }
128 inline void getCols(SizesAndBlocks & colsCopy) const {
129 colsCopy = cols;
130 }
131
132
133 inline bool highestLevel() const {
134 return (rows.getNTotalScalars() == rows.getNScalars() &&
136 }
137
143 inline bool is_empty() const {
144 return rows.is_empty() || cols.is_empty();
145 }
146 protected:
147
148
152 SizesAndBlocks const & colsInp)
153 : rows(rowsInp), cols(colsInp), elements(0) {}
155
156
159
162
166 Telement* elements; /* Length is nRows * nCols unless 0 */
167
168
169
170#if 0
171 inline void assert_alloc() {
172 if (this->cap < this->nel) {
173 freeElements(this->elements);
174 this->cap = this->nel;
175 this->elements = allocateElements<Telement>(this->cap);
176 for (int ind = 0; ind < this->cap; ind++)
177 this->elements[ind] = 0;
178 }
179 }
180#endif
181 private:
182
183 }; /* end class */
184
185
186
187 template<class Treal, class Telement> /* Copy constructor */
190 : rows(mat.rows), cols(mat.cols), elements(0) {
191 if (!mat.is_zero()) {
193 for (int i = 0; i < nElements(); i++)
194 elements[i] = mat.elements[i];
195 }
196 }
197
198
199 template<class Treal, class Telement> /*Assignment operator*/
203 if (mat.is_zero()) { /* Condition also matches empty matrices. */
204 rows = mat.rows;
205 cols = mat.cols;
206 freeElements(elements);
207 elements = 0;
208 return *this;
209 }
210 if (is_zero() || (nElements() != mat.nElements())) {
211 freeElements(elements);
212 elements = allocateElements<Telement>(mat.nElements());
213 }
214 rows = mat.rows;
215 cols = mat.cols;
216 for (int i = 0; i < nElements(); i++)
217 elements[i] = mat.elements[i];
218 return *this;
219 }
220
221 template<class Treal, class Telement>
225 assert(A.rows == B.rows && A.cols == B.cols);
226 Telement* elementsTmp = A.elements;
227 A.elements = B.elements;
228 B.elements = elementsTmp;
229 }
230
231
232 template<class Treal, class Telement>
237
238
239} /* end namespace mat */
240
241#endif
Code for memory allocation/deallocation routines used by matrix library.
Definition Failure.h:57
Base class for Matrix and Matrix specialization.
Definition MatrixHierarchicBase.h:52
bool highestLevel() const
Definition MatrixHierarchicBase.h:133
MatrixHierarchicBase(const MatrixHierarchicBase< Treal, Telement > &mat)
Definition MatrixHierarchicBase.h:189
const int & ncols() const
Definition MatrixHierarchicBase.h:71
void getRows(SizesAndBlocks &rowsCopy) const
Definition MatrixHierarchicBase.h:125
static void swap(MatrixHierarchicBase< Treal, Telement > &A, MatrixHierarchicBase< Treal, Telement > &B)
Definition MatrixHierarchicBase.h:223
const int & nrows() const
Definition MatrixHierarchicBase.h:69
void getCols(SizesAndBlocks &colsCopy) const
Definition MatrixHierarchicBase.h:128
SizesAndBlocks cols
Definition MatrixHierarchicBase.h:165
SizesAndBlocks rows
Definition MatrixHierarchicBase.h:164
Telement * elements
Definition MatrixHierarchicBase.h:166
bool operator==(int k) const
Definition MatrixHierarchicBase.h:55
virtual ~MatrixHierarchicBase()
Definition MatrixHierarchicBase.h:233
void resetRows(SizesAndBlocks const &newRows)
Definition MatrixHierarchicBase.h:114
const int & nScalarsCols() const
Definition MatrixHierarchicBase.h:65
MatrixHierarchicBase()
Definition MatrixHierarchicBase.h:149
Telement int col
Definition MatrixHierarchicBase.h:75
const int & nScalarsRows() const
Definition MatrixHierarchicBase.h:63
return elements[row+col *nrows()]
Definition MatrixHierarchicBase.h:81
int nElements() const
Definition MatrixHierarchicBase.h:110
bool is_empty() const
Check if matrix is empty Empty is different from zero, a zero matrix contains information about block...
Definition MatrixHierarchicBase.h:143
MatrixHierarchicBase(SizesAndBlocks const &rowsInp, SizesAndBlocks const &colsInp)
Definition MatrixHierarchicBase.h:151
const Telement int col const
Definition MatrixHierarchicBase.h:84
MatrixHierarchicBase< Treal, Telement > & operator=(const MatrixHierarchicBase< Treal, Telement > &mat)
Definition MatrixHierarchicBase.h:202
bool is_zero() const
Definition MatrixHierarchicBase.h:108
Telement & operator()(int row
const Telement & operator()(int row
void resetCols(SizesAndBlocks const &newCols)
Definition MatrixHierarchicBase.h:119
Describes dimensions of matrix and its blocks on all levels.
Definition SizesAndBlocks.h:45
int const & getNScalars() const
Definition SizesAndBlocks.h:73
int const & getNBlocks() const
Definition SizesAndBlocks.h:72
bool is_empty() const
Definition SizesAndBlocks.h:71
int getNTotalScalars() const
Definition SizesAndBlocks.h:84
mat::SizesAndBlocks rows
Definition test.cc:51
mat::SizesAndBlocks cols
Definition test.cc:52
#define B
#define A
Copyright(c) Emanuel Rubensson 2006.
Definition allocate.cc:39
void freeElements(float *ptr)
Definition allocate.cc:49
T * allocateElements(int n)
Definition allocate.h:42