76void TCovSecondOrder::assembleCrossCovDirectional(
77 const GaussianProcessRegression& gpr,
const Eigen::MatrixXd& x_q,
78 const Eigen::VectorXd& q_ind,
const Eigen::VectorXd& dir,
79 Eigen::MatrixXd* K_first_out, Eigen::MatrixXd* K_second_out)
82 const Eigen::VectorXd& ind1 = gpr.getRIndices();
83 SexpatCF& cov = *gpr.sexpat_cov_func;
85 Eigen::VectorXd uDdim1, uDdim2;
86 gpr.extractUniqueIndices(ind1, uDdim1);
87 gpr.extractUniqueIndices(q_ind, uDdim2);
89 if (K_first_out) *K_first_out = EigenMatrix::Zero(x1.rows(), x_q.rows());
90 if (K_second_out) *K_second_out = EigenMatrix::Zero(x1.rows(), x_q.rows());
92 Eigen::Map<const Eigen::ArrayXd> ind1_arr(ind1.data(), ind1.size());
93 Eigen::Map<const Eigen::ArrayXd> ind2_arr(q_ind.data(), q_ind.size());
99 const Eigen::Index cnt1_0 = (ind1_arr == 0).count();
100 const Eigen::Index cnt2_0 = (ind2_arr == 0).count();
102 x1_loc_0.resize(cnt1_0, x1.cols() + 1);
104 x2_loc_0.resize(cnt2_0, x_q.cols() + 1);
107 gpr.extractCoordinatesByIndex(x1, ind1, 0, x1_loc_0);
108 gpr.extractCoordinatesByIndex(x_q, q_ind, 0, x2_loc_0);
111 if (cnt1_0 > 0 && cnt2_0 > 0) {
112 cov.calculateDirectionalCovarianceMatrix(x1_loc_0, x2_loc_0, dir,
113 block_first, block_second);
114 if (K_first_out && block_first.size() != 0) {
115 gpr.assignBlockToMatrix(ind1, q_ind, 0, 0, block_first,
118 if (K_second_out && block_second.size() != 0) {
119 gpr.assignBlockToMatrix(ind1, q_ind, 0, 0, block_second,
128 for (Eigen::Index i = 0; i < uDdim2.size(); ++i) {
129 dims2(0, 0) =
static_cast<Index_t>(uDdim2[i]);
130 const Eigen::Index cnt2_j = (ind2_arr == uDdim2[i]).count();
131 x2_loc_1.resize(cnt2_j, x_q.cols() + 1);
133 gpr.extractCoordinatesByIndex(
134 x_q, q_ind,
static_cast<Index_t>(uDdim2[i]), x2_loc_1);
135 cov.calculateDirectionalCovarianceMatrixWithDerivatives(
136 x2_loc_1, x1_loc_0, dims2, dir, block_first, block_second);
137 if (K_first_out && block_first.size() != 0) {
138 gpr.assignBlockToMatrix(ind1, q_ind, 0,
139 static_cast<Index_t>(uDdim2[i]),
140 block_first, *K_first_out,
143 if (K_second_out && block_second.size() != 0) {
144 gpr.assignBlockToMatrix(ind1, q_ind, 0,
145 static_cast<Index_t>(uDdim2[i]),
146 block_second, *K_second_out,
154 for (Eigen::Index i = 0; i < uDdim1.size(); ++i) {
155 dims1(0, 0) =
static_cast<Index_t>(uDdim1[i]);
156 const Eigen::Index cnt1_i = (ind1_arr == uDdim1[i]).count();
157 x1_loc_1.resize(cnt1_i, x1.cols() + 1);
159 gpr.extractCoordinatesByIndex(
160 x1, ind1,
static_cast<Index_t>(uDdim1[i]), x1_loc_1);
161 cov.calculateDirectionalCovarianceMatrixWithDerivatives(
162 x1_loc_1, x2_loc_0, dims1, dir, block_first, block_second);
163 if (K_first_out && block_first.size() != 0) {
164 gpr.assignBlockToMatrix(ind1, q_ind,
165 static_cast<Index_t>(uDdim1[i]), 0,
166 block_first, *K_first_out);
168 if (K_second_out && block_second.size() != 0) {
169 gpr.assignBlockToMatrix(ind1, q_ind,
170 static_cast<Index_t>(uDdim1[i]), 0,
171 block_second, *K_second_out);
177 for (Eigen::Index i = 0; i < uDdim1.size(); ++i) {
178 dims1(0, 0) =
static_cast<Index_t>(uDdim1[i]);
179 const Eigen::Index cnt1_i = (ind1_arr == uDdim1[i]).count();
180 x1_loc_1.resize(cnt1_i, x1.cols() + 1);
182 gpr.extractCoordinatesByIndex(x1, ind1,
static_cast<Index_t>(uDdim1[i]),
184 for (Eigen::Index j = 0; j < uDdim2.size(); ++j) {
185 dims2(0, 0) =
static_cast<Index_t>(uDdim2[j]);
186 const Eigen::Index cnt2_j = (ind2_arr == uDdim2[j]).count();
187 x2_loc_1.resize(cnt2_j, x_q.cols() + 1);
189 gpr.extractCoordinatesByIndex(
190 x_q, q_ind,
static_cast<Index_t>(uDdim2[j]), x2_loc_1);
191 cov.calculateDirectionalCovarianceMatrixWithDerivatives2(
192 x1_loc_1, x2_loc_1, dims1, dims2, dir, block_first,
194 if (K_first_out && block_first.size() != 0) {
195 gpr.assignBlockToMatrix(
196 ind1, q_ind,
static_cast<Index_t>(uDdim1[i]),
197 static_cast<Index_t>(uDdim2[j]), block_first, *K_first_out);
199 if (K_second_out && block_second.size() != 0) {
200 gpr.assignBlockToMatrix(ind1, q_ind,
201 static_cast<Index_t>(uDdim1[i]),
202 static_cast<Index_t>(uDdim2[j]),
203 block_second, *K_second_out);
210 const Eigen::MatrixXd& H_inv)
214 if (
gpr.sexpat_cov_func ==
nullptr)
return false;
215 if (
gpr.getRMatrix().size() == 0)
return false;
216 if (
gpr.getAlpha().size() == 0)
return false;
220 if (!
gpr.hasTrainingCholeskyFactor(
gpr.getRMatrix().rows()))
return false;
225 const auto& ls_ref =
gpr.sexpat_cov_func->getLengthScaleRef();
226 const Eigen::Index n_ls = ls_ref.size();
227 if (n_ls < 1)
return false;
234 if (n_ls <= 1)
return false;
236 n_ls_ =
static_cast<std::size_t
>(n_ls);
237 const int p =
static_cast<int>(H_inv.rows());
238 const int p_expected = 1 +
static_cast<int>(n_ls) + 1;
239 if (p != p_expected)
return false;
240 if (H_inv.cols() != p)
return false;
243 alpha_star_ =
gpr.getAlpha();
245 Eigen::VectorXd uDdim;
246 gpr.extractUniqueIndices(
gpr.getRIndices(), uDdim);
252 const Eigen::Index p_sex = 1 + n_ls;
254 K_d_.reserve(n_ls_ + 1);
255 std::vector<EigenMatrix> K_dd_diag;
256 K_dd_diag.reserve(n_ls_ + 1);
258 for (Eigen::Index d = 0; d <= n_ls; ++d) {
259 Eigen::VectorXd e_d = Eigen::VectorXd::Zero(p_sex);
263 gpr.assembleDirectionalCovarianceDerivatives(
264 gpr.getRMatrix(),
gpr.getRIndices(), uDdim, e_d, first, second);
265 if (first.size() == 0 || second.size() == 0) {
269 first =
gpr.symmetricMatrixInTrainingFactorLayout(first);
270 second =
gpr.symmetricMatrixInTrainingFactorLayout(second);
271 K_d_.push_back(std::move(first));
272 K_dd_diag.push_back(std::move(second));
282 const std::size_t p_eff = n_ls_ + 1;
284 K_ij_full_.resize(p_eff * (p_eff + 1) / 2);
287 for (std::size_t i = 0; i < p_eff; ++i) {
288 K_ij_full_[ijFlatIndex(i, i)] = std::move(K_dd_diag[i]);
292 for (Eigen::Index i = 0; i < static_cast<Eigen::Index>(p_eff); ++i) {
293 for (Eigen::Index j = i + 1; j < static_cast<Eigen::Index>(p_eff);
295 Eigen::VectorXd sum_dir = Eigen::VectorXd::Zero(p_sex);
300 gpr.assembleDirectionalCovarianceDerivatives(
301 gpr.getRMatrix(),
gpr.getRIndices(), uDdim, sum_dir, first,
303 if (second.size() == 0) {
307 second =
gpr.symmetricMatrixInTrainingFactorLayout(second);
311 K_ij_full_[ijFlatIndex(
static_cast<std::size_t
>(i),
312 static_cast<std::size_t
>(i))] -
313 K_ij_full_[ijFlatIndex(
static_cast<std::size_t
>(j),
314 static_cast<std::size_t
>(j))]);
315 K_ij_full_[ijFlatIndex(
static_cast<std::size_t
>(i),
316 static_cast<std::size_t
>(j))] =
324 for (std::size_t d = 0; d < p_eff; ++d) {
325 const Eigen::VectorXd Kd_alpha = K_d_[d] * alpha_star_;
326 z_.push_back(
gpr.solveTrainingCovarianceInFactorLayout(Kd_alpha));
332 beta_full_.resize(p_eff * (p_eff + 1) / 2);
333 for (std::size_t i = 0; i < p_eff; ++i) {
334 for (std::size_t j = i; j < p_eff; ++j) {
335 const auto& K_ij = K_ij_full_[ijFlatIndex(i, j)];
338 rhs = 2.0 * (K_d_[i] * z_[i]) - K_ij * alpha_star_;
340 rhs = K_d_[i] * z_[j] + K_d_[j] * z_[i] - K_ij * alpha_star_;
342 beta_full_[ijFlatIndex(i, j)] =
343 gpr.solveTrainingCovarianceInFactorLayout(rhs);
350 beta_.reserve(p_eff);
351 for (std::size_t d = 0; d < p_eff; ++d) {
352 beta_.push_back(beta_full_[ijFlatIndex(0, d)]);
363 if (
gpr.sexpat_cov_func ==
nullptr)
return;
364 if (
gpr.getRMatrix().size() == 0)
return;
368 if (N_im == 0)
return;
379 Eigen::VectorXd R_mod_ind;
383 const Eigen::Index n_ls =
static_cast<Eigen::Index
>(n_ls_);
384 const Eigen::Index p_sex = 1 + n_ls;
385 const Eigen::Index m_q = R_mod.rows();
391 gpr.evaluateCovarianceMatrix(
gpr.getRMatrix(), R_mod,
gpr.getRIndices(),
393 k_star =
gpr.rowsInTrainingFactorLayout(k_star);
401 const std::size_t p_eff = n_ls_ + 1;
402 std::vector<EigenMatrix> dk_cross(p_eff);
403 std::vector<EigenMatrix> dk2_cross(p_eff * (p_eff + 1) / 2);
404 for (Eigen::Index d = 0; d <= n_ls; ++d) {
405 Eigen::VectorXd e_d = Eigen::VectorXd::Zero(p_sex);
407 const std::size_t du =
static_cast<std::size_t
>(d);
409 TCovSecondOrder::assembleCrossCovDirectional(
410 gpr, R_mod, R_mod_ind, e_d, &dk_cross[du], &diag_second);
411 dk_cross[du] =
gpr.rowsInTrainingFactorLayout(dk_cross[du]);
412 diag_second =
gpr.rowsInTrainingFactorLayout(diag_second);
413 dk2_cross[ijFlatIndex(du, du)] = std::move(diag_second);
415 for (Eigen::Index i = 0; i <= n_ls; ++i) {
416 for (Eigen::Index j = i + 1; j <= n_ls; ++j) {
417 Eigen::VectorXd sum_dir = Eigen::VectorXd::Zero(p_sex);
421 TCovSecondOrder::assembleCrossCovDirectional(
422 gpr, R_mod, R_mod_ind, sum_dir,
nullptr, &sum_second);
423 sum_second =
gpr.rowsInTrainingFactorLayout(sum_second);
425 const auto& k_ii = dk2_cross[ijFlatIndex(
426 static_cast<std::size_t
>(i),
static_cast<std::size_t
>(i))];
427 const auto& k_jj = dk2_cross[ijFlatIndex(
428 static_cast<std::size_t
>(j),
static_cast<std::size_t
>(j))];
429 dk2_cross[ijFlatIndex(
static_cast<std::size_t
>(i),
430 static_cast<std::size_t
>(j))] =
431 0.5 * (sum_second - k_ii - k_jj);
440 Eigen::VectorXd corr = Eigen::VectorXd::Zero(m_q);
444 std::vector<std::vector<Eigen::RowVectorXd>> z_dot_k(p_eff);
445 for (std::size_t d = 0; d < p_eff; ++d) {
446 z_dot_k[d].resize(p_eff);
447 for (std::size_t e = 0; e < p_eff; ++e) {
448 z_dot_k[d][e] = z_[e].transpose() * dk_cross[d];
452 std::vector<Eigen::RowVectorXd> kstar_dot_beta(p_eff * (p_eff + 1) / 2);
453 for (std::size_t i = 0; i < p_eff; ++i) {
454 for (std::size_t j = i; j < p_eff; ++j) {
455 kstar_dot_beta[ijFlatIndex(i, j)] =
456 beta_full_[ijFlatIndex(i, j)].transpose() * k_star;
467 for (std::size_t i = 0; i < p_eff; ++i) {
468 for (std::size_t j = i; j < p_eff; ++j) {
469 const Eigen::Index ii =
static_cast<Eigen::Index
>(i);
470 const Eigen::Index jj =
static_cast<Eigen::Index
>(j);
471 Eigen::RowVectorXd kij_dot_a =
472 alpha_star_.transpose() * dk2_cross[ijFlatIndex(i, j)];
473 Eigen::RowVectorXd d2_ij = kij_dot_a - z_dot_k[i][j] -
475 kstar_dot_beta[ijFlatIndex(i, j)];
477 (i == j) ? 0.5 * H_inv_(ii, jj) : H_inv_(ii, jj);
478 corr += coeff * d2_ij.transpose();
492 auto cap = [](
double map_val,
double corr_val) {
495 if (!std::isfinite(map_val) || !std::isfinite(corr_val))
return 0.0;
497 if (std::fabs(corr_val) <= bound)
return corr_val;
498 return std::copysign(bound, corr_val);
504 for (
Index_t i = 0; i < N_im; ++i) {
505 if (!std::isfinite(image1.
E(0, i))) {
506 image1.
G.row(i).setZero();
509 image1.
E(0, i) += cap(image1.
E(0, i), corr(i));
511 for (
Index_t d = 0; d < D; ++d) {
512 for (
Index_t i = 0; i < N_im; ++i) {
513 if (!std::isfinite(image1.
E(0, i)))
continue;
514 const double component = image1.
G(i, d);
515 if (!std::isfinite(component)) {
516 image1.
G(i, d) = 0.0;
520 corr(
static_cast<Eigen::Index
>((d + 1) * N_im + i));
521 image1.
G(i, d) += cap(component, c);
Linear algebra abstraction layer for GPR hot-path operations.
Some auxiliary functions.
void assembleMatrixOfRepetitiveCoordinates(const gpr::Coord &coord, gpr::EigenMatrix &matrix, Eigen::VectorXd &ind)
Assemble Eigen matrix with repetitive rows based on provided set of coordinates R.
void invalidate() noexcept
Invalidate (drops all cached vectors).
bool compute(const GaussianProcessRegression &gpr, const Eigen::MatrixXd &H_inv)
static constexpr double kTrustTau
void applyCorrection(const GaussianProcessRegression &gpr, Observation &image1)
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Eigen::Matrix< Index_t, EigenDynamic, EigenDynamic, FieldMatrixStorage > FieldMatrixIdx
Eigen::Matrix< double, EigenDynamic, EigenDynamic, EigenMatrixStorage > EigenMatrix
Eigen::Matrix< double, EigenDynamic, EigenDynamic, FieldMatrixStorage > FieldMatrixd
Observation data: energy, gradient and positions.