Loading...
Searching...
No Matches
GaussianInfluenceFunctional.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
6#pragma once
7#include <Eigen/Cholesky>
8#include <cmath>
9#include <stdexcept>
11struct Result {
12 double action;
13 Eigen::VectorXd mean;
14 Eigen::MatrixXd response;
15};
16inline Result evaluate(const Eigen::MatrixXd& G, const Eigen::MatrixXd& C,
17 const Eigen::VectorXd& f, double hbar)
18{
19 if (!(hbar > 0.) || !std::isfinite(hbar) || G.rows() == 0 ||
20 G.rows() != G.cols() || C.rows() != G.rows() || C.cols() != G.cols() ||
21 f.size() != G.rows() || !G.allFinite() || !C.allFinite() ||
22 !f.allFinite())
23 throw std::invalid_argument("Finite matching influence data required");
24 if (!G.isApprox(G.transpose(), 1e-12) || !C.isApprox(C.transpose(), 1e-12))
25 throw std::invalid_argument("Symmetric influence operators required");
26 Eigen::LLT<Eigen::MatrixXd> base(G);
27 if (base.info() != Eigen::Success)
28 throw std::domain_error("Nonpositive reference");
29 const Eigen::MatrixXd L = base.matrixL();
30 const Eigen::MatrixXd A =
31 Eigen::MatrixXd::Identity(G.rows(), G.cols()) + L.transpose() * C * L;
32 Eigen::LLT<Eigen::MatrixXd> update(A);
33 if (update.info() != Eigen::Success)
34 throw std::domain_error("Nonpositive influence");
35 const Eigen::MatrixXd Q = L * update.solve(L.transpose());
36 const Eigen::VectorXd diagonal =
37 Eigen::MatrixXd(update.matrixL()).diagonal();
38 return {-.5 * f.dot(Q * f) + hbar * diagonal.array().log().sum(), -Q * f,
39 Q};
40}
41// Coordinate derivatives keep the reference response and hbar fixed.
42inline double coordinateGradient(const Result& r, const Eigen::MatrixXd& Ci,
43 const Eigen::VectorXd& fi, double hbar)
44{
45 const Eigen::MatrixXd moment =
46 r.mean * r.mean.transpose() + hbar * r.response;
47 return r.mean.dot(fi) + .5 * moment.cwiseProduct(Ci.transpose()).sum();
48}
49inline double coordinateHessian(const Result& r, const Eigen::MatrixXd& Ci,
50 const Eigen::VectorXd& fi,
51 const Eigen::MatrixXd& Cj,
52 const Eigen::VectorXd& fj,
53 const Eigen::MatrixXd& Cij,
54 const Eigen::VectorXd& fij, double hbar)
55{
56 const Eigen::MatrixXd moment =
57 r.mean * r.mean.transpose() + hbar * r.response;
58 const Eigen::VectorXd ai = fi + Ci * r.mean, aj = fj + Cj * r.mean;
59 return r.mean.dot(fij) + .5 * moment.cwiseProduct(Cij.transpose()).sum() -
60 ai.dot(r.response * aj) -
61 .5 * hbar * (r.response * Ci * r.response * Cj).trace();
62}
63} // namespace gaussianinfluence
Result evaluate(const Eigen::MatrixXd &G, const Eigen::MatrixXd &C, const Eigen::VectorXd &f, double hbar)
double coordinateGradient(const Result &r, const Eigen::MatrixXd &Ci, const Eigen::VectorXd &fi, double hbar)
double coordinateHessian(const Result &r, const Eigen::MatrixXd &Ci, const Eigen::VectorXd &fi, const Eigen::MatrixXd &Cj, const Eigen::VectorXd &fj, const Eigen::MatrixXd &Cij, const Eigen::VectorXd &fij, double hbar)