GQCP
Loading...
Searching...
No Matches
SquareRankFourTensor.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 <iostream>
25
26
27namespace GQCP {
28
29
35template <typename _Scalar>
36class SquareRankFourTensor: public Tensor<_Scalar, 4> {
37public:
38 using Scalar = _Scalar;
39
42
43
44public:
45 /*
46 * CONSTRUCTORS
47 */
48
53 Base() {}
54
55
61 SquareRankFourTensor(const size_t dim) :
62 Base(dim, dim, dim, dim) {}
63
64
70 SquareRankFourTensor(const Base& tensor) :
71 Base(tensor) {
72
73 // Check if the given tensor is 'square'.
74 auto dims = this->dimensions();
75 if ((dims[0] != dims[1]) || (dims[1] != dims[2]) || (dims[2] != dims[3])) {
76 throw std::invalid_argument("SquareRankFourTensor(Eigen::TensorBase<OtherDerived, AccessLevel>): The given tensor should have equal dimensions in every rank.");
77 }
78 }
79
80
89 template <typename ExpDerived, int AccessLevel>
90 SquareRankFourTensor(const Eigen::TensorBase<ExpDerived, AccessLevel>& exp) :
91 Self(Base(exp)) {} // the Base constructor returns the required type for the square-checking constructor
92
93
94 /*
95 * NAMED CONSTRUCTORS
96 */
97
104 static Self FromFile(const std::string& filename, size_t dim) {
105
106 Self result {dim};
107 result.setZero(); // make sure that the tensor is initialized to zero values before reading in
108
109 std::ifstream file {filename};
110 if (file.is_open()) {
111 std::string line;
112 while (std::getline(file, line)) {
113 std::vector<std::string> splitted_line; // create a container for the line to be split in
114
115 // Split the line on any whitespace or tabs.
116 boost::split(splitted_line, line, boost::is_any_of(" \t"), boost::token_compress_on);
117
118 if (splitted_line.size() != 5) {
119 throw std::runtime_error("SquareRankFourTensor::FromFile(std::string, size_t): Found a line that doesn't contain exactly 5 fields delimited by whitespace.");
120 }
121
122 auto i = std::stoi(splitted_line[0]);
123 auto j = std::stoi(splitted_line[1]);
124 auto k = std::stoi(splitted_line[2]);
125 auto l = std::stoi(splitted_line[3]);
126 auto value = std::stod(splitted_line[4]);
127
128 result(i, j, k, l) = value;
129 }
130
131 file.close();
132 } else {
133 throw std::runtime_error("SquareRankFourTensor::FromFile(std::string, size_t): Cannot open the given file. Maybe you specified a wrong path?");
134 }
135
136 return result;
137 }
138
139
147 static Self Zero(const size_t dim) {
148
149 SquareRankFourTensor<Scalar> T {dim}; // 'Random' initialization happens here.
150 T.setZero();
151 return T;
152 }
153
154
162 static Self Random(const size_t dim) {
163
165 T.setRandom(); // Uniformly distributed between [0, 1].
166
167 // Move the distribution from [0, 1] -> [-1, 1].
168 for (size_t i = 0; i < dim; i++) {
169 for (size_t j = 0; j < dim; j++) {
170 for (size_t k = 0; k < dim; k++) {
171 for (size_t l = 0; l < dim; l++) {
172 T(i, j, k, l) = 2 * T(i, j, k, l) - 1; // Scale from [0, 1] -> [0, 2] -> [-1, 1].
173 }
174 }
175 }
176 }
177 return T;
178 }
179
180
181 /*
182 * OPERATORS
183 */
184
193 template <typename ExpDerived, int AccessLevel>
194 Self& operator=(const Eigen::TensorBase<ExpDerived, AccessLevel>& exp) {
195 this->Base::operator=(exp);
196 return (*this);
197 }
198
199
200 /*
201 * PUBLIC METHODS
202 */
203
204 using Base::dimension;
205
209 size_t dimension() const { return this->dimension(0); } // all tensor dimensions are equal because of the constructor
210
219
220 // Initialize the resulting matrix
221 const auto K = this->dimension();
223
224 // Calculate the compound indices and bring the elements from the tensor over into the matrix
225 size_t row_index = 0;
226 for (size_t j = 0; j < K; j++) { // "column major" ordering for row_index<-i,j so we do j first, then i
227 for (size_t i = j + 1; i < K; i++) { // in column major indices, columns are contiguous, so the first of two indices changes more rapidly
228 // require i > j for "lower triangle"
229
230 size_t column_index = 0;
231 for (size_t l = 0; l < K; l++) { // "column major" ordering for column_index<-k,l so we do l first, then k
232 for (size_t k = l + 1; k < K; k++) { // in column major indices, columns are contiguous, so the first of two indices changes more rapidly
233 // require l > k for "lower triangle"
234
235 M(row_index, column_index) = this->operator()(i, j, k, l);
236
237 column_index++;
238 }
239 }
240
241 row_index++;
242 }
243 }
244
245 return M;
246 }
247};
248
249} // namespace GQCP
Definition: SquareMatrix.hpp:39
static Self Zero(const size_t dim)
Definition: SquareMatrix.hpp:289
Definition: SquareRankFourTensor.hpp:36
SquareMatrix< double > pairWiseStrictReduced() const
Definition: SquareRankFourTensor.hpp:218
static Self FromFile(const std::string &filename, size_t dim)
Definition: SquareRankFourTensor.hpp:104
Self & operator=(const Eigen::TensorBase< ExpDerived, AccessLevel > &exp)
Definition: SquareRankFourTensor.hpp:194
static Self Random(const size_t dim)
Definition: SquareRankFourTensor.hpp:162
SquareRankFourTensor()
Definition: SquareRankFourTensor.hpp:52
SquareRankFourTensor(const Eigen::TensorBase< ExpDerived, AccessLevel > &exp)
Definition: SquareRankFourTensor.hpp:90
size_t dimension() const
Definition: SquareRankFourTensor.hpp:209
SquareRankFourTensor(const size_t dim)
Definition: SquareRankFourTensor.hpp:61
static Self Zero(const size_t dim)
Definition: SquareRankFourTensor.hpp:147
_Scalar Scalar
Definition: SquareRankFourTensor.hpp:38
SquareRankFourTensor(const Base &tensor)
Definition: SquareRankFourTensor.hpp:70
Definition: Tensor.hpp:46
Definition: BaseOneElectronIntegralBuffer.hpp:25