81 const Eigen::MatrixXd& columns,
const Eigen::VectorXd& positions,
82 const Eigen::VectorXd& masses,
const Eigen::MatrixXd* measured =
nullptr)
85 const Eigen::Index dof = positions.size();
86 if (columns.rows() != dof || columns.cols() != dof ||
87 masses.size() * 3 != dof || !columns.allFinite())
89 Eigen::VectorXd inverse_root(dof);
90 for (Eigen::Index k = 0; k < dof; ++k)
91 inverse_root(k) = 1.0 / std::sqrt(masses(k / 3));
92 const Eigen::MatrixXd weight = inverse_root * inverse_root.transpose();
93 const Eigen::MatrixXd antisymmetric =
94 (0.5 * (columns - columns.transpose())).cwiseProduct(weight);
95 out.hessian = (0.5 * (columns + columns.transpose())).cwiseProduct(weight);
96 out.resolution = antisymmetric.size() > 0
97 ? Eigen::JacobiSVD<Eigen::MatrixXd>(antisymmetric)
100 const bool reduced = measured !=
nullptr && measured->rows() == dof &&
101 measured->cols() > 0 && measured->cols() < dof &&
102 measured->allFinite();
103 Eigen::MatrixXd vibrational;
105 Eigen::VectorXd root(dof);
106 for (Eigen::Index k = 0; k < dof; ++k)
107 root(k) = std::sqrt(masses(k / 3));
108 const Eigen::HouseholderQR<Eigen::MatrixXd> qr(root.asDiagonal() *
110 const Eigen::MatrixXd image =
112 Eigen::MatrixXd::Identity(dof, measured->cols());
113 const Eigen::MatrixXd block = image.transpose() * antisymmetric * image;
115 Eigen::JacobiSVD<Eigen::MatrixXd>(block).singularValues()(0);
121 const double outside =
122 (vibrational - image * (image.transpose() * vibrational)).norm();
123 if (vibrational.cols() == 0 || !(outside <= 1e-8)) vibrational = image;
127 const Eigen::MatrixXd projected =
128 vibrational.transpose() *
out.hessian * vibrational;
134 if (projected.rows() == 0 || projected.cols() == 0 ||
135 !projected.allFinite())
139 const Eigen::MatrixXd symmetric = 0.5 * (projected + projected.transpose());
140 const Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> spectrum(symmetric);
141 if (spectrum.info() != Eigen::Success)
return out;
142 out.curvatures = spectrum.eigenvalues();
143 out.modes = vibrational * spectrum.eigenvectors();
144 const double scale =
out.curvatures.cwiseAbs().maxCoeff();
146 std::max(
out.resolution, std::numeric_limits<double>::epsilon() *
147 static_cast<double>(std::max<Eigen::Index>(
148 1,
out.curvatures.size())) *
201 const Eigen::VectorXd& masses,
202 const Eigen::MatrixXd& weak,
double step,
203 const Gradients& gradients,
204 double noise_multiplier = 0.0)
207 const Eigen::Index dof = positions.size();
208 const Eigen::Index k = weak.cols();
209 if (k == 0 || weak.rows() != dof)
return out;
210 Eigen::VectorXd root(dof);
211 for (Eigen::Index i = 0; i < dof; ++i)
212 root(i) = std::sqrt(masses(i / 3));
213 Eigen::MatrixXd directions(dof, k);
214 Eigen::VectorXd norms(k);
215 for (Eigen::Index j = 0; j < k; ++j) {
216 const Eigen::VectorXd cartesian = weak.col(j).cwiseQuotient(root);
217 norms(j) = cartesian.norm();
218 if (!(norms(j) > 0.0))
return out;
219 directions.col(j) = cartesian / norms(j);
221 Eigen::MatrixXd points(dof, 4 * k);
222 const double offsets[4] = {step, -step, 0.5 * step, -0.5 * step};
223 for (Eigen::Index j = 0; j < k; ++j)
224 for (
int o = 0; o < 4; ++o)
225 points.col(4 * j + o) = positions + offsets[o] * directions.col(j);
226 const Eigen::MatrixXd g = gradients(points);
227 if (g.rows() != 4 * k || g.cols() != dof || !g.allFinite())
return out;
228 Eigen::MatrixXd action(dof, k), truncation(dof, k), even(dof, k);
229 for (Eigen::Index j = 0; j < k; ++j) {
230 const Eigen::VectorXd coarse =
231 (g.row(4 * j) - g.row(4 * j + 1)).transpose() / (2.0 * step);
232 const Eigen::VectorXd fine =
233 (g.row(4 * j + 2) - g.row(4 * j + 3)).transpose() / step;
236 ((4.0 * fine - coarse) / 3.0).cwiseQuotient(root) * norms(j);
238 ((coarse - fine) / 3.0).cwiseQuotient(root) * norms(j);
239 even.col(j) = 0.5 * (g.row(4 * j) + g.row(4 * j + 1)).transpose();
241 const Eigen::MatrixXd projected = weak.transpose() * action;
242 const Eigen::MatrixXd symmetric = 0.5 * (projected + projected.transpose());
243 const Eigen::MatrixXd projected_error = weak.transpose() * truncation;
244 if (noise_multiplier > 0.0 && std::isfinite(noise_multiplier)) {
246 const double spread =
247 3.0 * std::sqrt(projected_error.diagonal().squaredNorm() /
248 static_cast<double>(k));
249 out.noise_floor = noise_multiplier * spread;
251 Eigen::MatrixXd envelope =
252 0.5 * (projected_error.cwiseAbs() +
253 projected_error.transpose().cwiseAbs()) +
254 (0.5 * (projected - projected.transpose())).cwiseAbs();
255 envelope.array() +=
out.noise_floor;
256 const Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> spectrum(symmetric);
257 if (spectrum.info() != Eigen::Success)
return out;
258 out.curvatures = spectrum.eigenvalues();
259 out.modes = weak * spectrum.eigenvectors();
260 out.error = Eigen::JacobiSVD<Eigen::MatrixXd>(envelope).singularValues()(0);
262 out.truncation = truncation;
263 out.directions = directions;
264 out.inverse_mass = norms.array().square();
342 const Eigen::MatrixXd& block,
const Eigen::MatrixXd& errors,
343 const Eigen::MatrixXd& coupling,
const Eigen::VectorXd& tail,
346 const double unresolved = -std::numeric_limits<double>::infinity();
347 const Eigen::Index weak = block.rows();
348 const Eigen::Index dimension = weak + tail.size();
349 if (weak < 2)
return unresolved;
350 const Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> spectrum(block);
351 if (spectrum.info() != Eigen::Success)
return unresolved;
352 const Eigen::MatrixXd rotation = spectrum.eigenvectors();
353 const Eigen::MatrixXd magnitude = rotation.cwiseAbs();
354 const Eigen::MatrixXd transformed = rotation.transpose() * block * rotation;
355 const Eigen::MatrixXd symmetric =
356 0.5 * (transformed + transformed.transpose());
357 const Eigen::MatrixXd envelope = magnitude.transpose() * errors * magnitude;
358 const Eigen::MatrixXd cross = coupling * magnitude;
359 Eigen::MatrixXd comparison = Eigen::MatrixXd::Zero(dimension, dimension);
360 comparison.topLeftCorner(weak, weak) = -symmetric.cwiseAbs() - envelope;
361 comparison.diagonal().head(weak) =
362 symmetric.diagonal() - envelope.diagonal();
363 comparison.bottomLeftCorner(tail.size(), weak) = -cross;
364 comparison.topRightCorner(weak, tail.size()) = -cross.transpose();
365 comparison.diagonal().tail(tail.size()) = tail;
366 const double roundoff =
367 64.0 * std::numeric_limits<double>::epsilon() * dimension;
368 const double gram_defect = (rotation.transpose() * rotation -
369 Eigen::MatrixXd::Identity(weak, weak))
371 roundoff * magnitude.squaredNorm();
372 if (!(gram_defect < 1.0) || !comparison.allFinite())
return unresolved;
374 (magnitude.transpose() * (block.cwiseAbs() + errors) * magnitude)
376 cross.norm() + comparison.norm();
378 const double rotated_allowance =
379 (1.0 + gram_defect) * allowance + roundoff * scale;
380 const Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> lower(
381 comparison, Eigen::EigenvaluesOnly);
382 if (lower.info() != Eigen::Success)
return unresolved;
383 const double bound = lower.eigenvalues()(0) - rotated_allowance;
384 return bound / (bound >= 0.0 ? 1.0 + gram_defect : 1.0 - gram_defect);
392 const StencilIndex& index,
const Eigen::VectorXd& positions,
393 const Eigen::VectorXd& masses,
double step,
const Gradients& gradients,
394 Eigen::Index minimum_weak = 0,
double noise_multiplier = 0.0)
397 const Eigen::Index dimension = index.
curvatures.size();
398 if (!index.
valid || dimension == 0)
return out;
402 out.higher_order =
true;
407 const bool refine_lowest =
409 Eigen::Index weak = 0;
410 while (weak + 1 < dimension &&
413 weak = std::max(weak, std::min(minimum_weak, dimension - 1));
414 out.refined_positive_modes = weak;
416 out.positive_lower = dimension > 1
418 : std::numeric_limits<double>::infinity();
419 if (!refine_lowest) {
420 out.first_order =
out.negative_upper < 0.0;
426 const Eigen::Index begin = refine_lowest ? 0 : 1;
427 const Eigen::Index count = weak + (refine_lowest ? 1 : 0);
428 const Eigen::MatrixXd basis = index.
modes.middleCols(begin, count);
430 gradients, noise_multiplier);
431 if (!refined.valid)
return out;
439 double noise = refined.noise_floor;
440 if (noise_multiplier > 0.0 && std::isfinite(noise_multiplier))
441 noise = std::max(noise, noise_multiplier * std::sqrt(5.0) *
443 std::sqrt(
static_cast<double>(dimension)));
445 const Eigen::MatrixXd projected = index.
modes.transpose() * refined.action;
446 Eigen::MatrixXd nominal = index.
curvatures.asDiagonal();
447 nominal.middleCols(begin, count) = projected;
448 nominal.middleRows(begin, count) = projected.transpose();
449 const Eigen::MatrixXd refined_block = projected.middleRows(begin, count);
450 nominal.block(begin, begin, count, count) =
451 0.5 * (refined_block + refined_block.transpose());
452 Eigen::MatrixXd correction = nominal;
454 out.hessian += index.
modes * correction * index.
modes.transpose();
457 out.weak_direction = refined.directions.col(1 - begin);
458 out.weak_even = refined.even.col(1 - begin);
459 out.weak_step = refined.step;
460 out.weak_inverse_mass = refined.inverse_mass(1 - begin);
462 const auto positive_modes = index.
modes.middleCols(1, weak);
463 const auto action = refined.action.rightCols(weak);
464 const auto truncation = refined.truncation.rightCols(weak);
465 const Eigen::MatrixXd block = positive_modes.transpose() * action;
466 const Eigen::MatrixXd symmetric = 0.5 * (block + block.transpose());
467 const Eigen::MatrixXd block_error = positive_modes.transpose() * truncation;
468 Eigen::MatrixXd errors =
469 0.5 * (block_error.cwiseAbs() + block_error.transpose().cwiseAbs()) +
470 (0.5 * (block - block.transpose())).cwiseAbs();
471 errors.array() += noise;
472 Eigen::MatrixXd comparison =
473 Eigen::MatrixXd::Zero(dimension - 1, dimension - 1);
474 comparison.topLeftCorner(weak, weak) = -symmetric.cwiseAbs() - errors;
475 comparison.diagonal().head(weak) = symmetric.diagonal() - errors.diagonal();
476 const Eigen::Index tail = dimension - weak - 1;
477 Eigen::MatrixXd coupling(tail, weak);
479 const auto tail_modes = index.
modes.rightCols(tail);
480 coupling = (tail_modes.transpose() * action).cwiseAbs() +
481 (tail_modes.transpose() * truncation).cwiseAbs();
482 coupling.array() += noise;
483 comparison.bottomLeftCorner(tail, weak) = -coupling;
484 comparison.topRightCorner(weak, tail) = -coupling.transpose();
485 comparison.diagonal().tail(tail) =
488 const double orthogonality =
490 Eigen::MatrixXd::Identity(dimension, dimension))
492 const double allowance =
494 64.0 * std::numeric_limits<double>::epsilon() * dimension) *
495 (comparison.norm() + refined.action.norm() + refined.truncation.norm() +
498 const Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> lower(
499 comparison, Eigen::EigenvaluesOnly);
500 if (lower.info() != Eigen::Success)
return out;
501 out.positive_lower = lower.eigenvalues()(0) - allowance;
502 if (weak > 1 &&
out.positive_lower <= 0.0)
504 std::max(
out.positive_lower,
506 symmetric, errors, coupling,
507 comparison.diagonal().tail(tail), allowance));
511 index.
modes.col(0).dot(refined.action.col(0)) +
512 std::abs(index.
modes.col(0).dot(refined.truncation.col(0))) +
514 out.first_order =
out.negative_upper < 0.0 &&
out.positive_lower > 0.0;
516 if (weak > 0 &&
out.negative_upper < 0.0) {
517 const Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> spectrum(
519 if (spectrum.info() != Eigen::Success)
return out;
520 const Eigen::VectorXd mode = spectrum.eigenvectors().col(0);
521 const Eigen::VectorXd magnitude = mode.cwiseAbs();
522 out.weak_error = magnitude.dot(errors * magnitude) + allowance;
523 out.weak_lower = spectrum.eigenvalues()(0) -
out.weak_error;
524 out.weak_block_error = errors.norm() + allowance;
525 if (weak == dimension - 1) {
526 out.weak_block_lower =
out.positive_lower;
528 const Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> lower(
529 comparison.topLeftCorner(weak, weak), Eigen::EigenvaluesOnly);
530 if (lower.info() != Eigen::Success)
return out;
531 out.weak_block_lower = lower.eigenvalues()(0) - allowance;
532 if (weak > 1 &&
out.weak_block_lower <= 0.0)
533 out.weak_block_lower =
534 std::max(
out.weak_block_lower,
536 symmetric, errors, Eigen::MatrixXd(0, weak),
537 Eigen::VectorXd(0), allowance));
542 const double upper = spectrum.eigenvalues()(0) +
543 magnitude.dot(errors * magnitude) + allowance;
544 Eigen::RowVectorXd cross_error =
545 (index.
modes.col(0).transpose() * truncation).cwiseAbs();
546 cross_error.array() += noise;
547 const double coupling =
548 std::abs(index.
modes.col(0).dot(action * mode)) +
549 (cross_error * magnitude)(0) + allowance;
550 Eigen::Matrix2d majorant;
551 majorant <<
out.negative_upper, coupling, coupling, upper;
552 const Eigen::SelfAdjointEigenSolver<Eigen::Matrix2d> pair(
553 majorant, Eigen::EigenvaluesOnly);
554 if (pair.info() == Eigen::Success) {
555 out.negative_pair_upper = pair.eigenvalues()(1) + allowance;
556 out.higher_order =
out.negative_pair_upper < 0.0;
557 out.weak_higher_order =
out.higher_order;
558 if (
out.higher_order)
out.escape = positive_modes * mode;
684 const Eigen::MatrixXd& previous_cartesian_hessian,
685 const Eigen::VectorXd& positions,
const Eigen::VectorXd& masses,
686 const Eigen::VectorXd& centre_gradient,
double step,
double step_bound,
687 const Gradients& gradients)
690 if (positions.size() < 6 || masses.size() * 3 != positions.size() ||
691 centre_gradient.size() != positions.size() || !positions.allFinite() ||
692 !masses.allFinite() || (masses.array() <= 0.0).any() ||
693 !centre_gradient.allFinite() || !(step > 0.0) || !std::isfinite(step))
697 const auto proposal =
699 if (!proposal.valid || proposal.modes.cols() < 2)
return out;
700 const Eigen::MatrixXd basis = proposal.modes.leftCols(2);
703 if (!refined.valid)
return out;
704 const Eigen::Matrix2d block = basis.transpose() * refined.action;
705 const Eigen::Matrix2d symmetric = 0.5 * (block + block.transpose());
706 const Eigen::Matrix2d projected_error =
707 basis.cwiseAbs().transpose() * refined.truncation.cwiseAbs();
708 const Eigen::Matrix2d error =
709 0.5 * (projected_error + projected_error.transpose()) +
710 (0.5 * (block - block.transpose())).cwiseAbs();
711 const double gram_defect =
712 (basis.transpose() * basis - Eigen::Matrix2d::Identity()).norm();
713 const double allowance =
714 (gram_defect + 64.0 * std::numeric_limits<double>::epsilon() *
715 static_cast<double>(positions.size())) *
716 (symmetric.norm() + refined.action.norm() + refined.truncation.norm());
717 if (!(gram_defect < 1.0) || !error.allFinite() || !std::isfinite(allowance))
719 Eigen::Matrix2d majorant = symmetric.cwiseAbs() + error;
720 majorant.diagonal() = symmetric.diagonal() + error.diagonal();
721 const Eigen::SelfAdjointEigenSolver<Eigen::Matrix2d> spectrum(majorant);
722 if (spectrum.info() != Eigen::Success)
return out;
726 out.upper = spectrum.eigenvalues()(1) + allowance;
727 out.higher_order =
out.upper < 0.0;
728 if (!
out.higher_order)
return out;
729 const Eigen::VectorXd direction = refined.directions.col(1);
730 const double third = 2.0 * refined.inverse_mass(1) *
731 direction.dot(refined.even.col(1) - centre_gradient) /
734 direction, third, step, symmetric(1, 1) - error(1, 1) - allowance,
735 error(1, 1) + allowance, step_bound);
805 const StencilIndex& index,
const Eigen::VectorXd& positions,
806 const Eigen::VectorXd& masses,
double step,
const Gradients& gradients,
807 double noise_multiplier = 0.0)
809 Eigen::MatrixXd retained_points(positions.size(), 0);
810 Eigen::MatrixXd retained_gradients(0, positions.size());
811 const auto measure = [&](
const Eigen::MatrixXd& points) -> Eigen::MatrixXd {
812 std::vector<Eigen::Index> slots(points.cols());
813 std::vector<Eigen::Index> missing;
814 const Eigen::Index held = retained_points.cols();
815 for (Eigen::Index j = 0; j < points.cols(); ++j) {
816 Eigen::Index found = 0;
817 while (found < held && !(retained_points.col(found).array() ==
818 points.col(j).array())
823 std::find_if(missing.begin(), missing.end(),
824 [&](Eigen::Index candidate) {
825 return (points.col(candidate).array() ==
826 points.col(j).array())
829 slots[j] = held + std::distance(missing.begin(), pending);
830 if (pending == missing.end()) missing.push_back(j);
835 if (!missing.empty()) {
836 Eigen::MatrixXd requests(points.rows(), missing.size());
837 for (Eigen::Index j = 0; j < requests.cols(); ++j)
838 requests.col(j) = points.col(missing[j]);
839 const Eigen::MatrixXd measured = gradients(requests);
840 if (measured.rows() != requests.cols() ||
841 measured.cols() != requests.rows() || !measured.allFinite())
842 return Eigen::MatrixXd::Constant(
843 points.cols(), points.rows(),
844 std::numeric_limits<double>::quiet_NaN());
845 retained_points.conservativeResize(Eigen::NoChange,
846 held + requests.cols());
847 retained_gradients.conservativeResize(held + requests.cols(),
849 retained_points.rightCols(requests.cols()) = requests;
850 retained_gradients.bottomRows(requests.cols()) = measured;
852 Eigen::MatrixXd result(points.cols(), points.rows());
853 for (Eigen::Index j = 0; j < points.cols(); ++j)
854 result.row(j) = retained_gradients.row(slots[j]);
858 cert.refinement_points = retained_points;
859 cert.refinement_gradients = retained_gradients;
864 int step_reductions = 0;
865 while (!
out.first_order && !
out.higher_order &&
out.negative_upper < 0.0 &&
866 std::isfinite(
out.weak_block_lower) &&
867 std::isfinite(
out.weak_block_error)) {
868 Eigen::Index weak =
out.refined_positive_modes;
869 const bool smaller_step =
out.weak_block_lower <= 0.0;
872 const double next = 0.5 * step;
873 if (!(next > 0.0) || next == step)
break;
877 if (weak + 1 >= index.
curvatures.size())
break;
881 measure, weak, noise_multiplier);
883 if (refined.first_order || refined.higher_order)
return refined;
884 if (smaller_step && !(refined.weak_block_error <
out.weak_block_error))
886 out = std::move(refined);
912 const Eigen::VectorXd& masses,
913 const Eigen::VectorXd& u,
const Eigen::VectorXd& v,
914 double step,
const Gradients& gradients)
917 const Eigen::Index dof = center.size();
918 if (masses.size() * 3 != dof || u.size() != dof || v.size() != dof ||
921 Eigen::VectorXd root(dof);
922 for (Eigen::Index k = 0; k < dof; ++k)
923 root(k) = std::sqrt(masses(k / 3));
924 Eigen::VectorXd yu = root.cwiseProduct(u);
925 if (!(yu.norm() > 0.0))
return out;
927 Eigen::VectorXd yv = root.cwiseProduct(v);
928 yv -= yu.dot(yv) * yu;
929 if (!(yv.norm() > 1e-8))
return out;
931 out.points.resize(dof, 4);
932 const Eigen::VectorXd xu = yu.cwiseQuotient(root);
933 const Eigen::VectorXd xv = yv.cwiseQuotient(root);
934 out.points.col(0) = center + step * xu;
935 out.points.col(1) = center - step * xu;
936 out.points.col(2) = center + step * xv;
937 out.points.col(3) = center - step * xv;
938 out.gradients = gradients(
out.points);
939 if (
out.gradients.rows() != 4 ||
out.gradients.cols() != dof ||
940 !
out.gradients.allFinite())
943 const Eigen::VectorXd hu =
944 ((
out.gradients.row(0) -
out.gradients.row(1)).transpose() /
946 .cwiseQuotient(root);
947 const Eigen::VectorXd hv =
948 ((
out.gradients.row(2) -
out.gradients.row(3)).transpose() /
950 .cwiseQuotient(root);
951 const double a = yu.dot(hu);
952 const double d = yv.dot(hv);
953 const double b_uv = yu.dot(hv);
954 const double b_vu = yv.dot(hu);
955 const double b = 0.5 * (b_uv + b_vu);
956 const double radius = std::hypot(0.5 * (a - d), b);
957 out.lower = 0.5 * (a + d) - radius;
958 out.upper = 0.5 * (a + d) + radius;
959 out.asymmetry = 0.5 * std::abs(b_uv - b_vu);
960 out.measured = std::isfinite(
out.lower) && std::isfinite(
out.upper);
990 const Eigen::VectorXd& masses,
991 const Eigen::MatrixXd& candidates,
992 double step,
const Gradients& gradients)
995 const Eigen::Index dof = center.size();
996 if (masses.size() * 3 != dof || candidates.rows() != dof ||
997 candidates.cols() == 0 || !(step > 0.0) || !candidates.allFinite())
999 Eigen::VectorXd root(dof);
1000 for (Eigen::Index k = 0; k < dof; ++k)
1001 root(k) = std::sqrt(masses(k / 3));
1002 std::vector<Eigen::VectorXd> kept;
1003 for (Eigen::Index c = 0; c < candidates.cols(); ++c) {
1004 Eigen::VectorXd
y = root.cwiseProduct(candidates.col(c));
1005 if (!(
y.norm() > 0.0))
continue;
1007 for (
int pass = 0; pass < 2; ++pass)
1008 for (
const auto& k: kept)
1010 if (!(
y.norm() > 1e-8))
continue;
1014 const Eigen::Index m =
static_cast<Eigen::Index
>(kept.size());
1015 if (m == 0)
return out;
1016 out.basis.resize(dof, m);
1017 out.points.resize(dof, 2 * m);
1018 for (Eigen::Index j = 0; j < m; ++j) {
1019 out.basis.col(j) = kept[
static_cast<std::size_t
>(j)];
1020 const Eigen::VectorXd
x =
out.basis.col(j).cwiseQuotient(root);
1021 out.points.col(2 * j) = center + step *
x;
1022 out.points.col(2 * j + 1) = center - step *
x;
1024 out.gradients = gradients(
out.points);
1025 if (
out.gradients.rows() != 2 * m ||
out.gradients.cols() != dof ||
1026 !
out.gradients.allFinite())
1029 Eigen::MatrixXd actions(dof, m);
1030 for (Eigen::Index j = 0; j < m; ++j)
1032 ((
out.gradients.row(2 * j) -
out.gradients.row(2 * j + 1))
1035 .cwiseQuotient(root);
1036 const Eigen::MatrixXd block =
out.basis.transpose() * actions;
1037 const Eigen::MatrixXd symmetric = 0.5 * (block + block.transpose());
1038 out.asymmetry = 0.5 * (block - block.transpose()).cwiseAbs().maxCoeff();
1039 const Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> spectrum(symmetric);
1040 if (spectrum.info() != Eigen::Success)
return out;
1041 out.values = spectrum.eigenvalues();
1042 out.directions.resize(dof, m);
1043 for (Eigen::Index j = 0; j < m; ++j) {
1044 Eigen::VectorXd direction =
1045 (
out.basis * spectrum.eigenvectors().col(j)).cwiseQuotient(root);
1046 const double norm = direction.norm();
1047 out.directions.col(j) = norm > 0.0 ? direction / norm : direction;
1049 out.measured =
out.values.allFinite() &&
out.directions.allFinite() &&
1050 std::isfinite(
out.asymmetry);
1264 const Eigen::MatrixXd& columns,
const Eigen::VectorXd& positions,
1265 const Eigen::VectorXd& masses, Eigen::Index count,
1266 const Eigen::MatrixXd* measured =
nullptr)
1270 if (!index.
valid || index.
modes.cols() == 0 || count <= 0)
return {};
1271 const Eigen::Index n = std::min(count, index.
modes.cols());
1272 Eigen::VectorXd root(positions.size());
1273 for (Eigen::Index k = 0; k < positions.size(); ++k)
1274 root(k) = std::sqrt(masses(k / 3));
1275 Eigen::MatrixXd cartesian(positions.size(), n);
1276 for (Eigen::Index j = 0; j < n; ++j) {
1277 Eigen::VectorXd direction = index.
modes.col(j).cwiseQuotient(root);
1278 const double norm = direction.norm();
1279 cartesian.col(j) = norm > 0.0 ? direction / norm : direction;