Loading...
Searching...
No Matches
PermutationUtils.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
5#ifndef GPR_ML_PERMUTATIONUTILS_H_
6#define GPR_ML_PERMUTATIONUTILS_H_
7
8#include <algorithm>
9#include <cmath>
10#include <limits>
11#include <stdexcept>
12#include <vector>
13
15
16namespace gpr {
17
18namespace detail {
19
20struct ObservationChannelBlock {
21 Index_t channel;
22 Eigen::Index start;
23 Eigen::Index count;
24};
25
26inline bool validObservationChannel(double value)
27{
28 return std::isfinite(value) && value >= 0.0 &&
29 value <= std::numeric_limits<Index_t>::max() &&
30 value == std::floor(value);
31}
32
33inline Eigen::Index storedObservationChannelCount(
34 const Eigen::VectorXd& indices)
35{
36 if (indices.size() == 0) return 0;
37 std::vector<double> channels(indices.data(),
38 indices.data() + indices.size());
39 if (std::any_of(channels.begin(), channels.end(), [](double value) {
40 return !validObservationChannel(value);
41 }))
42 throw std::invalid_argument("observation channel index is invalid");
43 std::sort(channels.begin(), channels.end());
44 return std::unique(channels.begin(), channels.end()) - channels.begin();
45}
46
47class ObservationChannelLayout {
48public:
49 explicit ObservationChannelLayout(const Eigen::VectorXd& indices)
50 : rows_(indices.size())
51 {
52 for (Eigen::Index row = 0; row < indices.size(); ++row) {
53 const double value = indices(row);
54 if (!validObservationChannel(value) ||
55 (!blocks_.empty() && value < blocks_.back().channel)) {
56 valid_ = false;
57 blocks_.clear();
58 return;
59 }
60 const auto channel = static_cast<Index_t>(value);
61 if (blocks_.empty() || channel != blocks_.back().channel)
62 blocks_.push_back({channel, row, 1});
63 else
64 ++blocks_.back().count;
65 }
66 if (!blocks_.empty()) {
67 rows_per_channel_ = blocks_.front().count;
68 for (const auto& block: blocks_)
69 if (block.count != rows_per_channel_) {
70 rows_per_channel_ = 0;
71 break;
72 }
73 }
74 }
75
76 bool valid() const
77 {
78 return valid_;
79 }
80 Eigen::Index channelCount() const
81 {
82 return blocks_.size();
83 }
84 Eigen::Index rowsPerChannel() const
85 {
86 return rows_per_channel_;
87 }
88
89 ObservationChannelBlock block(Index_t channel) const
90 {
91 if (!valid_)
92 throw std::invalid_argument(
93 "observation channels must be grouped in order");
94 const auto found = find(channel);
95 return found != blocks_.end() && found->channel == channel
96 ? *found
97 : ObservationChannelBlock{channel, rows_, 0};
98 }
99
100 Eigen::Index ordinal(Index_t channel) const
101 {
102 const auto found = find(channel);
103 return found != blocks_.end() && found->channel == channel
104 ? found - blocks_.begin()
105 : -1;
106 }
107
108 bool completeCartesian(Eigen::Index dimensions) const
109 {
110 if (!valid_ || rows_per_channel_ <= 0 || dimensions < 0 ||
111 channelCount() != dimensions + 1)
112 return false;
113 for (Eigen::Index channel = 0; channel < channelCount(); ++channel)
114 if (blocks_[channel].channel != channel) return false;
115 return true;
116 }
117
118 bool repeatsCoordinates(const EigenMatrix& rows,
119 Eigen::Index dimensions) const
120 {
121 if (!valid_ || rows.rows() != rows_ || rows_per_channel_ <= 0 ||
122 dimensions <= 0 || rows.cols() < dimensions)
123 return false;
124 for (const auto& block: blocks_)
125 if (!(rows.block(block.start, 0, block.count, dimensions).array() ==
126 rows.block(0, 0, block.count, dimensions).array())
127 .all())
128 return false;
129 return true;
130 }
131
132private:
133 std::vector<ObservationChannelBlock>::const_iterator find(
134 Index_t channel) const
135 {
136 return std::lower_bound(blocks_.begin(), blocks_.end(), channel,
137 [](const auto& block, Index_t value) {
138 return block.channel < value;
139 });
140 }
141
142 Eigen::Index rows_;
143 std::vector<ObservationChannelBlock> blocks_;
144 Eigen::Index rows_per_channel_ = 0;
145 bool valid_ = true;
146};
147
148} // namespace detail
149
151 std::vector<Index_t> perm;
152 std::vector<Index_t> inv_perm;
153};
154
156 const Eigen::VectorXd& ind_Ddim)
157{
159 const Index_t n = static_cast<Index_t>(ind_Ddim.size());
160 p.perm.reserve(n);
161 p.inv_perm.resize(n, -1);
162
163 std::vector<Index_t> groups;
164 groups.reserve(n);
165 for (Index_t i = 0; i < n; ++i) {
166 const Index_t value = static_cast<Index_t>(ind_Ddim(i));
167 if (std::find(groups.begin(), groups.end(), value) == groups.end())
168 groups.push_back(value);
169 }
170 std::sort(groups.begin(), groups.end());
171
172 for (Index_t group: groups) {
173 for (Index_t i = 0; i < n; ++i) {
174 if (static_cast<Index_t>(ind_Ddim(i)) == group) p.perm.push_back(i);
175 }
176 }
177
178 for (Index_t i = 0; i < n; ++i)
179 p.inv_perm[p.perm[i]] = i;
180
181 return p;
182}
183
185 const Eigen::VectorXd& ind_Ddim)
186{
188 const Index_t n = static_cast<Index_t>(ind_Ddim.size());
189 p.perm.reserve(n);
190 p.inv_perm.resize(n, -1);
191
192 if (n == 0) return p;
193
194 const detail::ObservationChannelLayout channels(ind_Ddim);
195 const Index_t n_blocks = static_cast<Index_t>(channels.channelCount());
196 const Index_t block_rows = static_cast<Index_t>(channels.rowsPerChannel());
197
198 // A tile contains one row from each stored channel. Unequal channel
199 // counts have no rectangular observation-tiled representation.
200 if (!channels.valid() || block_rows == 0) {
201 p.perm.clear();
202 p.inv_perm.assign(static_cast<size_t>(n), -1);
203 return p;
204 }
205
206 for (Index_t row = 0; row < block_rows; ++row) {
207 for (Index_t block = 0; block < n_blocks; ++block) {
208 p.perm.push_back(block * block_rows + row);
209 }
210 }
211
212 for (Index_t i = 0; i < n; ++i)
213 p.inv_perm[p.perm[i]] = i;
214
215 return p;
216}
217
218inline Eigen::VectorXd permuteVector(const Eigen::VectorXd& input,
219 const StructuredPermutation& p)
220{
221 Eigen::VectorXd output(input.size());
222 for (Index_t i = 0; i < static_cast<Index_t>(p.perm.size()); ++i)
223 output(i) = input(p.perm[i]);
224 return output;
225}
226
227inline Eigen::VectorXd unpermuteVector(const Eigen::VectorXd& input,
228 const StructuredPermutation& p)
229{
230 Eigen::VectorXd output(input.size());
231 for (Index_t i = 0; i < static_cast<Index_t>(p.perm.size()); ++i)
232 output(p.perm[i]) = input(i);
233 return output;
234}
235
237 const StructuredPermutation& p)
238{
239 EigenMatrix output(input.rows(), input.cols());
240 for (Index_t i = 0; i < static_cast<Index_t>(p.perm.size()); ++i)
241 output.row(i) = input.row(p.perm[i]);
242 return output;
243}
244
246 const StructuredPermutation& p)
247{
248 EigenMatrix output(input.rows(), input.cols());
249 for (Index_t i = 0; i < static_cast<Index_t>(p.perm.size()); ++i)
250 output.row(p.perm[i]) = input.row(i);
251 return output;
252}
253
255 const StructuredPermutation& p)
256{
257 EigenMatrix output(input.rows(), input.cols());
258 for (Index_t i = 0; i < static_cast<Index_t>(p.perm.size()); ++i) {
259 for (Index_t j = 0; j < static_cast<Index_t>(p.perm.size()); ++j) {
260 output(i, j) = input(p.perm[i], p.perm[j]);
261 }
262 }
263 return output;
264}
265
267 const StructuredPermutation& p)
268{
269 EigenMatrix output(input.rows(), input.cols());
270 for (Index_t i = 0; i < static_cast<Index_t>(p.perm.size()); ++i) {
271 for (Index_t j = 0; j < static_cast<Index_t>(p.perm.size()); ++j) {
272 output(p.perm[i], p.perm[j]) = input(i, j);
273 }
274 }
275 return output;
276}
277
278} // namespace gpr
279
280#endif // GPR_ML_PERMUTATIONUTILS_H_
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
Eigen::VectorXd permuteVector(const Eigen::VectorXd &input, const StructuredPermutation &p)
Eigen::VectorXd unpermuteVector(const Eigen::VectorXd &input, const StructuredPermutation &p)
StructuredPermutation buildStructuredPermutation(const Eigen::VectorXd &ind_Ddim)
uint32_t Index_t
Definition Macros.h:17
EigenMatrix permuteMatrixRows(const EigenMatrix &input, const StructuredPermutation &p)
EigenMatrix permuteSymmetricMatrix(const EigenMatrix &input, const StructuredPermutation &p)
StructuredPermutation buildObservationTiledPermutation(const Eigen::VectorXd &ind_Ddim)
EigenMatrix unpermuteSymmetricMatrix(const EigenMatrix &input, const StructuredPermutation &p)
EigenMatrix unpermuteMatrixRows(const EigenMatrix &input, const StructuredPermutation &p)
Eigen::Matrix< double, EigenDynamic, EigenDynamic, EigenMatrixStorage > EigenMatrix
Definition Macros.h:20
std::vector< Index_t > inv_perm
std::vector< Index_t > perm