1309{
1310 impl_->validateQueries(positions);
1311 const Eigen::Index n = positions.rows(), d = impl_->dimensions();
1312 if (weights.size() != n * (d + 1) || !weights.allFinite())
1313 throw std::invalid_argument("invalid atomic functional weights");
1314 std::vector<Impl::Environment> environments;
1315 environments.reserve(static_cast<std::size_t>(n));
1316 for (Eigen::Index row = 0; row < n; ++row)
1317 environments.push_back(impl_->query(positions.row(row).transpose()));
1318
1319 [[maybe_unused]] const SerialEigenProducts serial_products;
1320 std::vector<std::vector<std::vector<PriorSum>>> projected;
1321 Vector inducing = Vector::Zero(impl_->basis.values.rows());
1322 for (Eigen::Index row = 0; row < n; ++row) {
1324 for (Eigen::Index channel = 0; channel <= d; ++channel)
1325 local(channel) = weights(channel * n + row);
1327 impl_->crossRows(environments[row], impl_->fitted_parameters,
1328 false)[0],
1329 impl_->inducing_lower);
1330 inducing.noalias() += cross.transpose() * local;
1331 projected.emplace_back();
1332 const auto& environment = environments[row];
1333 for (std::size_t atom = 0; atom < environment.jacobians.size();
1334 ++atom) {
1335 const auto& support = environment.support[atom];
1336 const auto& jacobian = environment.jacobians[atom];
1337 std::vector<PriorSum> features(jacobian.cols());
1338 for (std::size_t index = 0; index < support.size(); ++index)
1339 for (Eigen::Index feature = 0; feature < jacobian.cols();
1340 ++feature)
1341 features[feature].addProduct(jacobian(index, feature),
1342 local(1 + support[index]));
1343 projected.back().push_back(std::move(features));
1344 }
1345 }
1346 PriorSum prior;
1347 for (Eigen::Index i = 0; i < n; ++i)
1348 for (Eigen::Index j = 0; j < n; ++j)
1349 for (Eigen::Index a = 0; a < environments[i].values.rows(); ++a)
1350 for (Eigen::Index b = 0; b < environments[j].values.rows();
1351 ++b) {
1352 const int element = environments[i].atomic_numbers[a];
1353 if (element != environments[j].atomic_numbers[b]) continue;
1354 const double length =
1355 impl_->fitted_parameters.lengths.at(element);
1356 const double inverse = 1.0 / (length * length);
1357 const Vector delta = (environments[i].values.row(a) -
1358 environments[j].values.row(b))
1359 .transpose();
1360 const double kernel =
1361 impl_->fitted_parameters.magnitude *
1362 std::exp(-0.5 * inverse * delta.squaredNorm());
1363 const auto& pi = projected[i][a];
1364 const auto& pj = projected[j][b];
1365 const PriorSum left = compensatedProjection(pi, delta);
1366 const PriorSum right = compensatedProjection(pj, delta);
1367 const double direct_weight = kernel * inverse;
1368 addTripleProduct(prior, kernel, weights(i), weights(j));
1369 addScaledProduct(prior, direct_weight,
1370 PriorSum{weights(i), 0.0}, right);
1371 addScaledProduct(prior, -direct_weight,
1372 PriorSum{weights(j), 0.0}, left);
1373 for (std::size_t feature = 0; feature < pi.size();
1374 ++feature)
1375 addScaledProduct(prior, direct_weight, pi[feature],
1376 pj[feature]);
1377 addScaledProduct(prior, -direct_weight * inverse, left,
1378 right);
1379 }
1380 const double prior_value = prior.value();
1381 const double value =
1382 prior_value +
1383 inducing.dot(impl_->posterior_factor->solve(inducing) - inducing);
1384 return nonnegativeVariance(value, prior_value);
1385}