GQCP
Loading...
Searching...
No Matches
MatrixRepresentationEvaluationContainer.hpp
Go to the documentation of this file.
1// This file is part of GQCG-GQCP.
2//
3// Copyright (C) 2017-2020 the GQCG developers
4//
5// GQCG-GQCP is free software: you can redistribute it and/or modify
6// it under the terms of the GNU Lesser General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// GQCG-GQCP is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU Lesser General Public License for more details.
14//
15// You should have received a copy of the GNU Lesser General Public License
16// along with GQCG-GQCP. If not, see <http://www.gnu.org/licenses/>.
17
18#pragma once
19
20
22
23#include <Eigen/Sparse>
24
25
26namespace GQCP {
27
28
34template <typename _Matrix>
36public:
37 // The type that is used as a matrix representation, i.e. a container that stores the matrix representation.
38 using Matrix = _Matrix;
39
40 // The scalar type of one of the matrix elements: real or complex.
41 using Scalar = typename Matrix::Scalar;
42
43
44public:
45 // The current index of the iterator, which is an address in a particular ONV basis.
46 size_t index = 0;
47
48 // The expected last position of the iterator, i.e. the dimension of the particular ONV basis.
49 size_t end;
50
51 // The matrix representation.
53
54
55public:
56 /*
57 * MARK: Constructors
58 */
59
66 matrix {Matrix::Zero(dimension)},
67 end {dimension} {}
68
69
70 /*
71 * MARK: Additions and evaluations
72 */
73
79 void addDiagonally(const Scalar value) {
80 this->matrix(this->index, this->index) += value;
81 }
82
89 void addColumnwise(const size_t column, const Scalar value) {
90 this->matrix(this->index, column) += value;
91 }
92
93
100 void addRowwise(const size_t row, const Scalar value) {
101 this->matrix(row, this->index) += value;
102 }
103
104
108 const Matrix& evaluation() const { return this->matrix; }
109
110
111 /*
112 * MARK: Iterator
113 */
114
118 void increment() { this->index++; }
119
125 bool isFinished() {
126
127 if (this->index == this->end) {
128 this->index = 0;
129 return true;
130 } else {
131 return false;
132 }
133 }
134};
135
136
143template <typename _Scalar>
144class MatrixRepresentationEvaluationContainer<Eigen::SparseMatrix<_Scalar>> {
145public:
146 // The scalar type of one of the matrix elements: real or complex.
147 using Scalar = _Scalar;
148
149
150public:
151 size_t index = 0; // Current position of the iterator in the dimension of the ONV basis.
152 size_t end; // The total dimension.
153
154 Eigen::SparseMatrix<Scalar> matrix; // Matrix containing the evaluations.
155 std::vector<Eigen::Triplet<Scalar>> triplet_vector; // Vector which temporarily contains the added values.
156
157
158public:
159 /*
160 * CONSTRUCTORS
161 */
162
167 matrix {Eigen::SparseMatrix<Scalar>(dimension, dimension)},
168 end(dimension) {}
169
170
171 /*
172 * PUBLIC METHODS
173 */
174
183 void addColumnwise(const size_t column, const Scalar value) { this->triplet_vector.emplace_back(this->index, column, value); }
184
193 void addRowwise(const size_t row, const Scalar value) { this->triplet_vector.emplace_back(row, this->index, value); }
194
201 void addToMatrix() {
202 this->matrix.setFromTriplets(this->triplet_vector.begin(), this->triplet_vector.end());
203 this->triplet_vector = {};
204 }
205
211 const Eigen::SparseMatrix<Scalar>& evaluation() const { return this->matrix; }
212
216 void increment() { this->index++; }
217
223 bool isFinished() {
224 if (this->index == this->end) {
225 this->index = 0;
226 return true;
227 } else {
228 return false;
229 }
230 }
231
237 void reserve(const size_t n) {
238 this->triplet_vector.reserve(n);
239 this->matrix.reserve(n);
240 }
241
245 const std::vector<Eigen::Triplet<Scalar>>& triplets() const { return triplet_vector; }
246};
247
248
254template <typename _Scalar>
256public:
257 // The scalar type of one of the matrix elements: real or complex.
258 using Scalar = _Scalar;
259
260public:
261 size_t index = 0; // Current position of the iterator in the dimension of the ONV basis.
262 size_t end; // The total dimension.
263
264 VectorX<Scalar> matvec; // Matvec containing the evaluations.
265 const VectorX<Scalar>& coefficient_vector; // Vector with which is multiplied.
266 double sequential_double = 0; // Double which temporarily contains the sum of added values, it corresponds to the value in the matvec of the current index and allows a for a single write access each iteration instead of multiple.
267 double nonsequential_double = 0; // Double gathered from the coefficient for non-sequential matvec additions, this corresponds to the value of the coefficient vector of the current index, it allows for a single read operation each iteration.
268
269
270public:
271 /*
272 * CONSTRUCTORS
273 */
274
278 MatrixRepresentationEvaluationContainer(const VectorX<Scalar>& coefficient_vector, const VectorX<Scalar>& diagonal) :
279 end {static_cast<size_t>(coefficient_vector.rows())},
280 coefficient_vector {coefficient_vector},
281 matvec {diagonal.cwiseProduct(coefficient_vector)} {}
282
284 end {static_cast<size_t>(coefficient_vector.rows())},
285 coefficient_vector {coefficient_vector},
286 matvec {VectorX<Scalar>::Zero(coefficient_vector.rows())} {}
287
288
289 /*
290 * PUBLIC METHODS
291 */
292
299 void addColumnwise(const size_t column, const Scalar value) { sequential_double += value * coefficient_vector(column); }
300
307 void addRowwise(const size_t row, const Scalar value) { this->matvec(row) += value * this->nonsequential_double; }
308
312 const VectorX<Scalar>& evaluation() const { return this->matvec; }
313
317 void increment() {
318 this->matvec(this->index) += this->sequential_double;
319 this->sequential_double = 0;
320 this->index++;
321 }
322
329 bool isFinished() {
330 if (this->index == this->end) {
331 this->index = 0;
332 return true;
333 } else {
334 this->nonsequential_double = this->coefficient_vector(this->index);
335 return false;
336 }
337 }
338};
339
340} // namespace GQCP
Definition: Matrix.hpp:47
_Scalar Scalar
Definition: Matrix.hpp:50
size_t end
Definition: MatrixRepresentationEvaluationContainer.hpp:152
const std::vector< Eigen::Triplet< Scalar > > & triplets() const
Definition: MatrixRepresentationEvaluationContainer.hpp:245
const Eigen::SparseMatrix< Scalar > & evaluation() const
Definition: MatrixRepresentationEvaluationContainer.hpp:211
bool isFinished()
Definition: MatrixRepresentationEvaluationContainer.hpp:223
void increment()
Definition: MatrixRepresentationEvaluationContainer.hpp:216
void addColumnwise(const size_t column, const Scalar value)
Definition: MatrixRepresentationEvaluationContainer.hpp:183
MatrixRepresentationEvaluationContainer(const size_t dimension)
Definition: MatrixRepresentationEvaluationContainer.hpp:166
void reserve(const size_t n)
Definition: MatrixRepresentationEvaluationContainer.hpp:237
void addToMatrix()
Definition: MatrixRepresentationEvaluationContainer.hpp:201
std::vector< Eigen::Triplet< Scalar > > triplet_vector
Definition: MatrixRepresentationEvaluationContainer.hpp:155
void addRowwise(const size_t row, const Scalar value)
Definition: MatrixRepresentationEvaluationContainer.hpp:193
_Scalar Scalar
Definition: MatrixRepresentationEvaluationContainer.hpp:147
Eigen::SparseMatrix< Scalar > matrix
Definition: MatrixRepresentationEvaluationContainer.hpp:154
MatrixRepresentationEvaluationContainer(const VectorX< Scalar > &coefficient_vector, const VectorX< Scalar > &diagonal)
Definition: MatrixRepresentationEvaluationContainer.hpp:278
VectorX< Scalar > matvec
Definition: MatrixRepresentationEvaluationContainer.hpp:264
void addColumnwise(const size_t column, const Scalar value)
Definition: MatrixRepresentationEvaluationContainer.hpp:299
const VectorX< Scalar > & coefficient_vector
Definition: MatrixRepresentationEvaluationContainer.hpp:265
size_t end
Definition: MatrixRepresentationEvaluationContainer.hpp:262
_Scalar Scalar
Definition: MatrixRepresentationEvaluationContainer.hpp:258
bool isFinished()
Definition: MatrixRepresentationEvaluationContainer.hpp:329
MatrixRepresentationEvaluationContainer(const VectorX< Scalar > &coefficient_vector)
Definition: MatrixRepresentationEvaluationContainer.hpp:283
const VectorX< Scalar > & evaluation() const
Definition: MatrixRepresentationEvaluationContainer.hpp:312
void increment()
Definition: MatrixRepresentationEvaluationContainer.hpp:317
void addRowwise(const size_t row, const Scalar value)
Definition: MatrixRepresentationEvaluationContainer.hpp:307
Definition: MatrixRepresentationEvaluationContainer.hpp:35
void addColumnwise(const size_t column, const Scalar value)
Definition: MatrixRepresentationEvaluationContainer.hpp:89
size_t end
Definition: MatrixRepresentationEvaluationContainer.hpp:49
_Matrix Matrix
Definition: MatrixRepresentationEvaluationContainer.hpp:38
void addDiagonally(const Scalar value)
Definition: MatrixRepresentationEvaluationContainer.hpp:79
typename Matrix::Scalar Scalar
Definition: MatrixRepresentationEvaluationContainer.hpp:41
bool isFinished()
Definition: MatrixRepresentationEvaluationContainer.hpp:125
MatrixRepresentationEvaluationContainer(const size_t dimension)
Definition: MatrixRepresentationEvaluationContainer.hpp:65
Matrix matrix
Definition: MatrixRepresentationEvaluationContainer.hpp:52
size_t index
Definition: MatrixRepresentationEvaluationContainer.hpp:46
const Matrix & evaluation() const
Definition: MatrixRepresentationEvaluationContainer.hpp:108
void addRowwise(const size_t row, const Scalar value)
Definition: MatrixRepresentationEvaluationContainer.hpp:100
void increment()
Definition: MatrixRepresentationEvaluationContainer.hpp:118
Definition: EvaluableLinearCombination.hpp:330
Definition: BaseOneElectronIntegralBuffer.hpp:25