GQCP
Loading...
Searching...
No Matches
SQOperatorStorage.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 <algorithm>
25
26
27namespace GQCP {
28
29
30/*
31 * MARK: Implementing SQOperatorStorage
32 */
33
41template <typename _MatrixRepresentation, typename _Vectorizer, typename _DerivedOperator>
43 public SQOperatorStorageBase<_MatrixRepresentation, _Vectorizer, _DerivedOperator>,
44 public VectorSpaceArithmetic<typename OperatorTraits<_DerivedOperator>::DerivedOperator, typename OperatorTraits<_DerivedOperator>::Scalar> {
45public:
46 // The type used to represent the set of parameters/matrix elements/integrals for one component of a second-quantized operator.
47 using MatrixRepresentation = _MatrixRepresentation;
48
49 // The type of the operator that derives from this class, enabling CRTP and compile-time polymorphism.
50 using DerivedOperator = _DerivedOperator;
51
52 // The scalar type used for a single parameter/matrix element/integral: real or complex.
54
55 // The type of the vectorizer that relates a one-dimensional storage of matrix representations to the tensor structure of the second-quantized operators. This allows for a distinction between scalar operators (such as the kinetic energy or Coulomb operator), vector operators (such as the spin operator) and matrix/tensor operators (such as quadrupole and multipole operators).
56 using Vectorizer = _Vectorizer;
57
58 // The type of the final derived operator, enabling CRTP and compile-time polymorphism. VectorSpaceArithmetic (and other functionality) should be implemented on the **final** deriving class, not on intermediate classes. In the current design of the SQOperator classes, there is just one intermediate class, so finding out the final derived class is easy.
60
61 // The type that corresponds to the scalar version of the final second-quantized operator operator type.
63
64
65public:
66 /*
67 * MARK: Constructors
68 */
69
70 // Inherit `SQOperatorStorageBase`'s constructors.
72
73
74 /*
75 * MARK: Conforming to `VectorSpaceArithmetic`
76 */
77
81 FinalOperator& operator+=(const FinalOperator& rhs) override {
82
83 // Use the STL to implement element-wise addition.
84 std::transform(this->array.elements().begin(), this->array.elements().end(),
85 rhs.array.elements().begin(), this->array.elements().begin(),
86 [](const MatrixRepresentation& M_lhs, const MatrixRepresentation& M_rhs) {
87 return M_lhs.Eigen() + M_rhs.Eigen();
88 });
89
90 return static_cast<FinalOperator&>(*this);
91 }
92
93
97 FinalOperator& operator*=(const Scalar& a) override {
98
99 // Use the STL to implement element-wise scalar multiplication.
100 std::transform(this->array.elements().begin(), this->array.elements().end(),
101 this->array.elements().begin(), [a](const MatrixRepresentation& M) { return M * a; });
102
103 return static_cast<FinalOperator&>(*this);
104 }
105
106
107 /*
108 * MARK: Other arithmetic
109 */
110
118 template <typename Z = Vectorizer>
120
121 if (this->array.vectorizer().numberOfElements() != v.size()) {
122 throw std::invalid_argument("SimpleSQOneElectronOperator(const StorageArray<Scalar, Vectorizer>&): The dimension of the given vector is incompatible with this vector operator.)");
123 }
124
125 const auto dimension = this->numberOfOrbitals();
126 ScalarFinalOperator result = ScalarFinalOperator::Zero(dimension); // Initializes a scalar-like operator with parameters that are zero.
127
128 // Calculate the dot/inner product of two vectors.
129 for (size_t i = 0; i < this->numberOfComponents(); i++) {
130 result += v(i) * (*this)(i);
131 }
132
133 return result;
134 }
135};
136
137
138} // namespace GQCP
Definition: Matrix.hpp:47
Definition: SQOperatorStorageBase.hpp:45
SQOperatorStorageBase()
Definition: SQOperatorStorageBase.hpp:130
size_t numberOfOrbitals() const
Definition: SQOperatorStorageBase.hpp:277
StorageArray< MatrixRepresentation, Vectorizer > array
Definition: SQOperatorStorageBase.hpp:68
size_t numberOfComponents() const
Definition: SQOperatorStorageBase.hpp:272
Definition: SQOperatorStorage.hpp:44
typename OperatorTraits< DerivedOperator >::DerivedOperator FinalOperator
Definition: SQOperatorStorage.hpp:59
typename OperatorTraits< FinalOperator >::ScalarOperator ScalarFinalOperator
Definition: SQOperatorStorage.hpp:62
typename OperatorTraits< DerivedOperator >::Scalar Scalar
Definition: SQOperatorStorage.hpp:53
_MatrixRepresentation MatrixRepresentation
Definition: SQOperatorStorage.hpp:47
_DerivedOperator DerivedOperator
Definition: SQOperatorStorage.hpp:50
FinalOperator & operator+=(const FinalOperator &rhs) override
Definition: SQOperatorStorage.hpp:81
FinalOperator & operator*=(const Scalar &a) override
Definition: SQOperatorStorage.hpp:97
_Vectorizer Vectorizer
Definition: SQOperatorStorage.hpp:56
ScalarFinalOperator dot(const VectorX< Scalar > &v) const
Definition: SQOperatorStorage.hpp:119
const std::vector< Element > & elements() const
Definition: StorageArray.hpp:161
const Vectorizer & vectorizer() const
Definition: StorageArray.hpp:177
Definition: VectorSpaceArithmetic.hpp:35
Definition: BaseOneElectronIntegralBuffer.hpp:25
Definition: OperatorTraits.hpp:28