5#ifndef GPR_ML_PERMUTATIONUTILS_H_
6#define GPR_ML_PERMUTATIONUTILS_H_
20struct ObservationChannelBlock {
26inline bool validObservationChannel(
double value)
28 return std::isfinite(value) && value >= 0.0 &&
29 value <= std::numeric_limits<Index_t>::max() &&
30 value == std::floor(value);
33inline Eigen::Index storedObservationChannelCount(
34 const Eigen::VectorXd& indices)
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);
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();
47class ObservationChannelLayout {
49 explicit ObservationChannelLayout(
const Eigen::VectorXd& indices)
50 : rows_(indices.size())
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)) {
60 const auto channel =
static_cast<Index_t>(value);
61 if (blocks_.empty() || channel != blocks_.back().channel)
62 blocks_.push_back({channel, row, 1});
64 ++blocks_.back().count;
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;
80 Eigen::Index channelCount()
const
82 return blocks_.size();
84 Eigen::Index rowsPerChannel()
const
86 return rows_per_channel_;
89 ObservationChannelBlock block(
Index_t channel)
const
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
97 : ObservationChannelBlock{channel, rows_, 0};
100 Eigen::Index ordinal(
Index_t channel)
const
102 const auto found = find(channel);
103 return found != blocks_.end() && found->channel == channel
104 ? found - blocks_.begin()
108 bool completeCartesian(Eigen::Index dimensions)
const
110 if (!valid_ || rows_per_channel_ <= 0 || dimensions < 0 ||
111 channelCount() != dimensions + 1)
113 for (Eigen::Index channel = 0; channel < channelCount(); ++channel)
114 if (blocks_[channel].channel != channel)
return false;
119 Eigen::Index dimensions)
const
121 if (!valid_ || rows.rows() != rows_ || rows_per_channel_ <= 0 ||
122 dimensions <= 0 || rows.cols() < dimensions)
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())
133 std::vector<ObservationChannelBlock>::const_iterator find(
136 return std::lower_bound(blocks_.begin(), blocks_.end(), channel,
137 [](
const auto& block,
Index_t value) {
138 return block.channel < value;
143 std::vector<ObservationChannelBlock> blocks_;
144 Eigen::Index rows_per_channel_ = 0;
156 const Eigen::VectorXd& ind_Ddim)
163 std::vector<Index_t> groups;
165 for (
Index_t i = 0; i < n; ++i) {
167 if (std::find(groups.begin(), groups.end(), value) == groups.end())
168 groups.push_back(value);
170 std::sort(groups.begin(), groups.end());
173 for (
Index_t i = 0; i < n; ++i) {
174 if (
static_cast<Index_t>(ind_Ddim(i)) == group) p.
perm.push_back(i);
178 for (
Index_t i = 0; i < n; ++i)
185 const Eigen::VectorXd& ind_Ddim)
192 if (n == 0)
return p;
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());
200 if (!channels.valid() || block_rows == 0) {
202 p.
inv_perm.assign(
static_cast<size_t>(n), -1);
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);
212 for (
Index_t i = 0; i < n; ++i)
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]);
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);
240 for (
Index_t i = 0; i < static_cast<Index_t>(p.
perm.size()); ++i)
241 output.row(i) = input.row(p.
perm[i]);
249 for (
Index_t i = 0; i < static_cast<Index_t>(p.
perm.size()); ++i)
250 output.row(p.
perm[i]) = input.row(i);
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]);
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);
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
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)
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
std::vector< Index_t > inv_perm
std::vector< Index_t > perm