42 {
43 if (q.size() != frequencies_.size() || !q.allFinite() ||
44 !(t > 0 && hbar > 0) || !std::isfinite(t) || !std::isfinite(hbar))
45 throw std::invalid_argument(
46 "Invalid harmonic tail geometry or scales");
47 const double logTwo = std::log(2.), pi = std::acos(-1.);
48 const Eigen::VectorXd modal = modes_.transpose() * q;
49 Eigen::VectorXd momentum(q.size()), turn(q.size()), logTerms(q.size()),
50 slopes(q.size());
51 HarmonicTail result;
52 result.logEnergy = -std::numeric_limits<double>::infinity();
53 for (Eigen::Index i = 0; i < q.size(); ++i) {
54 const double omega = frequencies_[i],
x = omega * t;
55 if (!(
x > 0) || !std::isfinite(2 *
x))
56 throw std::invalid_argument(
57 "Unresolved harmonic tail duration");
58 const double tangent = std::tanh(
x),
59 logSech = logTwo -
x - std::log1p(std::exp(-2 *
x));
60 momentum[i] = omega * tangent * modal[i];
61 turn[i] = std::exp(logSech) * modal[i];
62 result.action += modal[i] * momentum[i];
63 const double squared =
x < .01 ?
x *
x : 0;
64 const double abbreviated =
67 (2. / 3 +
68 squared * (-8. / 15 +
69 squared * (34. / 105 +
70 squared * (-496. / 2835 +
71 squared * 2764. /
72 31185))))
73 : tangent - std::
exp(std::
log(
x) + 2 * logSech);
74 result.abbreviatedAction +=
75 omega * modal[i] * modal[i] * abbreviated;
76 const double logSinh =
77 2 *
x + std::log(-std::expm1(-4 *
x)) - logTwo;
78 result.logDiagonalKernel +=
79 .5 * (std::log(omega) - logTwo - std::log(pi) - std::log(hbar) -
80 logSinh);
81 logTerms[i] = modal[i] == 0
82 ? -std::numeric_limits<double>::infinity()
83 : -logTwo + 2 * std::
log(omega) +
84 2 * std::
log(std::
abs(modal[i])) +
85 2 * logSech;
86 slopes[i] = -2 * omega * tangent;
87 result.logEnergy = std::max(result.logEnergy, logTerms[i]);
88 }
89 if (std::isfinite(result.logEnergy)) {
90 double sum = 0, derivative = 0;
91 for (Eigen::Index i = 0; i < q.size(); ++i) {
92 const double weight = std::exp(logTerms[i] - result.logEnergy);
93 sum += weight;
94 derivative += weight * slopes[i];
95 }
96 result.logEnergy += std::log(sum);
97 result.logEnergyDerivative = derivative / sum;
98 result.energy = std::exp(result.logEnergy);
99 }
100 result.logDiagonalKernel -= result.action / hbar;
101 result.momentum = modes_ * momentum;
102 result.turningPoint = modes_ * turn;
103 if (!std::isfinite(result.action) ||
104 !std::isfinite(result.abbreviatedAction) ||
105 !std::isfinite(result.energy) ||
106 !std::isfinite(result.logDiagonalKernel) ||
107 !result.momentum.allFinite() || !result.turningPoint.allFinite())
108 throw std::invalid_argument("Nonfinite harmonic tail result");
109 return result;
110 }
DoubleDouble abs(DoubleDouble a)
double log(double x) noexcept
double exp(double x) noexcept