GQCP
Loading...
Searching...
No Matches
StorageArray.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
24
25#include <vector>
26
27
28namespace GQCP {
29
30
37template <typename _Element, typename _Vectorizer>
39public:
40 // The type of element that this array stores.
41 using Element = _Element;
42
43 // The type of the vectorizer that relates multiple tuple coordinates to a one-dimensional index.
44 using Vectorizer = _Vectorizer;
45
46 // The type of this;
48
49 // The number of axes for the underlying vectorizer.
50 static constexpr auto NumberOfAxes = Vectorizer::NumberOfAxes;
51
52
53private:
54 // The one-dimensional representation of the elements of the array.
55 std::vector<Element> m_elements;
56
57 // The vectorizer that relates multiple tuple coordinates to a one-dimensional index.
58 Vectorizer m_vectorizer;
59
60
61public:
62 /*
63 * MARK: Constructors
64 */
65
72 StorageArray(const std::vector<Element>& elements, const Vectorizer& vectorizer) :
73 m_elements {elements},
74 m_vectorizer {vectorizer} {}
75
76
83 StorageArray(const Element& element, const Vectorizer& vectorizer) :
84 StorageArray(std::vector<Element>(vectorizer.numberOfElements(), element), vectorizer) {}
85
86
93 StorageArray(std::vector<Element>(vectorizer.numberOfElements()), vectorizer) {}
94
95
104 template <size_t N>
105 StorageArray(const std::array<Element, N>& elements, const Vectorizer& vectorizer) :
106 StorageArray(std::vector<Element>(elements.begin(), elements.end()), vectorizer) {} // Convert the std::array into a std::vector.
107
108
109 /*
110 * MARK: Conversions
111 */
112
118 template <typename Z = Vectorizer, typename = typename std::enable_if<std::is_same<Z, ScalarVectorizer>::value>::type>
119 operator Element() const {
120 return (*this)(); // the call operator with no indices
121 }
122
123
124 /*
125 * MARK: Element access
126 */
127
133 template <typename... Indices>
134 const Element& operator()(const Indices&... indices) const {
135
136 static_assert(sizeof...(indices) == NumberOfAxes, "The number of indices must match the number of axes.");
137
138 // Convert the indices pack to a vector so we can easily traverse.
139 std::vector<size_t> indices_vector {static_cast<size_t>(indices)...};
140 std::array<size_t, NumberOfAxes> indices_array {};
141 std::copy(indices_vector.begin(), indices_vector.end(), indices_array.begin());
142
143 // Use the underlying vectorizer to produce the 1D offset, and access the 1D storage array accordingly.
144 const auto vector_index = this->vectorizer().offset(indices_array);
145 return this->elements()[vector_index];
146 }
147
148
154 template <typename... Indices>
155 Element& operator()(const Indices&... indices) { return const_cast<Element&>(const_cast<const Self*>(this)->operator()(indices...)); }
156
157
161 const std::vector<Element>& elements() const { return this->m_elements; }
162
163
167 std::vector<Element>& elements() { return this->m_elements; }
168
169
170 /*
171 * MARK: General information
172 */
173
177 const Vectorizer& vectorizer() const { return this->m_vectorizer; }
178
179
180 /*
181 * MARK: Conversions
182 */
183
189 template <typename Z = Vectorizer>
190 enable_if_t<std::is_same<Z, VectorVectorizer>::value, VectorX<Element>> asVector() { // Marking this method `const` causes compile-time errors related to Eigen.
191
192 return Eigen::Map<Eigen::Matrix<Element, Eigen::Dynamic, 1>> {this->elements().data(), static_cast<long>(this->vectorizer().dimension(0))}; // The dimension of the '0'-th axis is the number of rows, i.e. the dimension of the columns.
193 }
194};
195
196
197} // namespace GQCP
Definition: Matrix.hpp:47
Definition: StorageArray.hpp:38
StorageArray(const std::vector< Element > &elements, const Vectorizer &vectorizer)
Definition: StorageArray.hpp:72
std::vector< Element > & elements()
Definition: StorageArray.hpp:167
_Element Element
Definition: StorageArray.hpp:41
const std::vector< Element > & elements() const
Definition: StorageArray.hpp:161
Element & operator()(const Indices &... indices)
Definition: StorageArray.hpp:155
const Vectorizer & vectorizer() const
Definition: StorageArray.hpp:177
static constexpr auto NumberOfAxes
Definition: StorageArray.hpp:50
StorageArray(const Vectorizer &vectorizer)
Definition: StorageArray.hpp:92
StorageArray(const std::array< Element, N > &elements, const Vectorizer &vectorizer)
Definition: StorageArray.hpp:105
enable_if_t< std::is_same< Z, VectorVectorizer >::value, VectorX< Element > > asVector()
Definition: StorageArray.hpp:190
const Element & operator()(const Indices &... indices) const
Definition: StorageArray.hpp:134
_Vectorizer Vectorizer
Definition: StorageArray.hpp:44
StorageArray(const Element &element, const Vectorizer &vectorizer)
Definition: StorageArray.hpp:83
Definition: BaseOneElectronIntegralBuffer.hpp:25
typename std::enable_if< B, T >::type enable_if_t
Definition: type_traits.hpp:37