GQCP
Loading...
Searching...
No Matches
MatrixVectorProductCalculation.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
23
24#include <type_traits>
25
26
27namespace GQCP {
28
29
34 public Step<EigenproblemEnvironment<double>> {
35
36public:
37 /*
38 * PUBLIC OVERRIDDEN METHODS
39 */
40
44 std::string description() const override {
45 return "Calculate the matrix-vector products for all the (new) guess vectors and add them to the environment.";
46 }
47
48
54 void execute(EigenproblemEnvironment<double>& environment) override {
55
56 const auto& V = environment.V; // the subspace of guess vectors
57
58 assert((V.transpose() * V).isApprox(MatrixX<double>::Identity(V.cols(), V.cols()), 1.0e-08)); // make sure that the subspace vectors are orthonormal
59
60
61 auto& VA = environment.VA; // VA = A * V (implicitly calculated through the matrix-vector product)
62 const auto& matvec = environment.matrix_vector_product_function;
63
64 // Check how many vectors there currently are in V and in VA: only calculate the expensive matrix-vector product for 'new' vectors.
65 // If there is no difference, no matrix-vector products should be calculated.
66 // If V is larger than VA:
67 // - VA should be expanded
68 // - we should calculate the matrix-vector products for the new vectors and place them into the columns of the new VA
69 // If V is smaller than VA, which can only happen after a subspace collapse:
70 // - VA should shrink
71 // - we should calculate the matrix-vector products for all the vectors in the collapsed subspace
72 const auto vectors_in_V = V.cols();
73 const auto vectors_in_VA = VA.cols();
74 const auto difference = vectors_in_V - vectors_in_VA;
75
76 if (difference != 0) {
77 VA.conservativeResize(Eigen::NoChange, VA.cols() + difference); // accounts for both expansion and shrinking
78
79 // Calculate the only the necessary matrix-vector products; find the start_index that accounts for both expansion and shrinking
80 size_t start_index = 0;
81 if ((difference > 0) && (vectors_in_VA > 0)) {
82 start_index = vectors_in_VA - 1; // -1 because of computers
83 }
84
85 for (size_t column_index = start_index; column_index < vectors_in_V; column_index++) {
86 VA.col(column_index) = matvec(V.col(column_index));
87 }
88 }
89 }
90};
91
92
93} // namespace GQCP
Definition: EigenproblemEnvironment.hpp:35
MatrixX< Scalar > V
Definition: EigenproblemEnvironment.hpp:73
VectorFunction< Scalar > matrix_vector_product_function
Definition: EigenproblemEnvironment.hpp:43
MatrixX< Scalar > VA
Definition: EigenproblemEnvironment.hpp:76
Definition: Matrix.hpp:47
Definition: MatrixVectorProductCalculation.hpp:34
void execute(EigenproblemEnvironment< double > &environment) override
Definition: MatrixVectorProductCalculation.hpp:54
std::string description() const override
Definition: MatrixVectorProductCalculation.hpp:44
Definition: Step.hpp:37
Definition: BaseOneElectronIntegralBuffer.hpp:25