79 if (!(lambda >= 0) || !(volume > 0) || !(kT > 0))
80 throw std::invalid_argument(
"lambda >= 0, a > 0 and kT > 0 required");
81 const double z = volume * volume * lambda /
82 (4 * std::numbers::pi * std::numbers::pi * kT);
85 m.
harmonic = lambda > 0 ? std::sqrt(2 * std::numbers::pi * kT / lambda)
86 : std::numeric_limits<double>::infinity();
88 m.
harmonicRegime = kT < volume * volume * lambda / (2 * std::numbers::pi);
98 const std::vector<double>& excluded,
99 double alphaMin,
double alphaMax,
102 if (H.rows() != H.cols() || !(alphaMin > 0) || !(alphaMax > alphaMin) ||
104 throw std::invalid_argument(
105 "Square H, 0 < alphaMin < alphaMax and at least four shifts "
107 Sparse identity(H.rows(), H.cols());
108 identity.setIdentity();
109 Vector alpha(shifts), f(shifts);
110 for (
int k = 0; k < shifts; ++k) {
111 alpha(k) = alphaMin * std::pow(alphaMax / alphaMin,
112 static_cast<double>(k) / (shifts - 1));
113 Eigen::SimplicialLDLT<Sparse> ldlt(
Sparse(H + alpha(k) * identity));
114 if (ldlt.info() != Eigen::Success)
115 throw std::runtime_error(
"Shifted factorisation failed");
117 for (Eigen::Index i = 0; i < ldlt.vectorD().size(); ++i) {
118 const double d = ldlt.vectorD()(i);
120 throw std::domain_error(
"The shift must make H positive");
121 logdet += std::log(d);
123 for (
double l: excluded) {
124 if (!(l + alpha(k) > 0))
125 throw std::domain_error(
126 "An excluded eigenvalue is below minus the shift");
127 logdet -= std::log(l + alpha(k));
131 const auto fit = [&](
int order) {
132 Matrix V(shifts, order + 1);
133 for (
int k = 0; k < shifts; ++k)
134 for (
int p = 0; p <= order; ++p)
135 V(k, p) = std::pow(alpha(k) / alphaMax, p);
136 return V.colPivHouseholderQr().solve(f)(0);
138 const double one = fit(1), two = fit(2), three = fit(3);
139 return {two, std::max(std::abs(two - one), std::abs(three - two))};
147 const std::function<
Vector(
const Vector&)>& apply, Eigen::Index n,
148 int probes,
int steps,
unsigned seed = 1)
150 if (n < 1 || probes < 2 || steps < 1)
151 throw std::invalid_argument(
152 "Size, at least two probes and steps required");
153 std::mt19937_64 rng(seed);
154 std::bernoulli_distribution coin(.5);
155 std::vector<double> samples;
156 for (
int p = 0; p < probes; ++p) {
158 for (Eigen::Index i = 0; i < n; ++i)
159 v(i) = coin(rng) ? 1. : -1.;
160 const double norm2 = v.squaredNorm();
161 const int m =
static_cast<int>(std::min<Eigen::Index>(steps, n));
163 Vector a = Vector::Zero(m), b = Vector::Zero(m);
164 Q.col(0) = v / std::sqrt(norm2);
166 for (
int j = 0; j < m; ++j) {
167 Vector w = apply(Q.col(j));
168 a(j) = Q.col(j).dot(w);
169 w -= a(j) * Q.col(j);
170 if (j > 0) w -= b(j - 1) * Q.col(j - 1);
171 for (
int pass = 0; pass < 2; ++pass)
172 w -= Q.leftCols(j + 1) * (Q.leftCols(j + 1).transpose() * w);
173 if (j + 1 == m)
break;
175 if (!(b(j) > 1e-12 * std::abs(a(j)))) {
179 Q.col(j + 1) = w / b(j);
181 Matrix T = Matrix::Zero(used, used);
182 for (
int j = 0; j < used; ++j) {
184 if (j + 1 < used) T(j, j + 1) = T(j + 1, j) = b(j);
186 Eigen::SelfAdjointEigenSolver<Matrix> s(T);
187 if (s.info() != Eigen::Success || !(s.eigenvalues().minCoeff() > 0))
188 throw std::domain_error(
189 "Stochastic log-det needs a positive operator");
191 for (
int j = 0; j < used; ++j)
192 quad += s.eigenvectors()(0, j) * s.eigenvectors()(0, j) *
193 std::log(s.eigenvalues()(j));
194 samples.push_back(norm2 * quad);
197 for (
double x: samples)
201 for (
double x: samples)
202 var += (
x - mean) * (
x - mean);
204 return {mean, std::sqrt(var / probes)};