65 double alpha,
double shift)
67 const auto n = B.rows(), r = B.cols(), m = Q.cols();
68 if (n <= 0 || r <= 0 || m <= 0 || m > n || Q.rows() != n ||
69 AQ.rows() != n || AQ.cols() != m || !B.allFinite() || !Q.allFinite() ||
70 !AQ.allFinite() || !std::isfinite(alpha) || alpha < 0 ||
71 !std::isfinite(shift) || !(shift + alpha > 0))
72 throw std::invalid_argument(
73 "Finite compatible bath data and positive spectral shift required");
74 const double epsilon = 256 * std::numeric_limits<double>::epsilon() * n;
75 if ((Q.transpose() * Q - Matrix::Identity(m, m)).norm() > epsilon)
76 throw std::invalid_argument(
"Orthonormal bath response basis required");
77 const Matrix G = Q.transpose() * B;
78 const Matrix missing = B - Q * G;
79 for (Eigen::Index j = 0; j < r; ++j)
80 if (missing.col(j).stableNorm() > epsilon * B.col(j).stableNorm())
81 throw std::invalid_argument(
82 "Bath coupling lies outside the supplied basis");
83 Matrix T = Q.transpose() * AQ;
84 const double operatorScale = AQ.stableNorm();
85 if ((T - T.transpose()).norm() > epsilon * operatorScale)
86 throw std::invalid_argument(
87 "Self-adjoint bath operator actions required");
88 T = (.5 * T + .5 * T.transpose()).eval();
90 positive.diagonal().array() -= alpha;
91 Eigen::SelfAdjointEigenSolver<Matrix> spectrum(positive);
92 const double tolerance = epsilon * std::max(operatorScale, std::abs(alpha));
93 if (spectrum.info() != Eigen::Success ||
94 spectrum.eigenvalues().minCoeff() < -tolerance)
95 throw std::domain_error(
96 "Bath actions violate the declared spectral lower bound");
107 Matrix residual = AQ - Q * T;
108 for (
int pass = 0; pass < 2; ++pass)
109 residual.noalias() -= Q * (Q.transpose() * residual).eval();
110 Eigen::JacobiSVD<Matrix> factor(residual,
111 Eigen::ComputeThinU | Eigen::ComputeThinV);
112 if (factor.info() != Eigen::Success)
113 throw std::runtime_error(
"Bath residual factorization failed");
117 factor.singularValues().asDiagonal() * factor.matrixV().transpose();
118 Vector inverse = Vector::Zero(m);
119 for (Eigen::Index j = 0; j < m; ++j) {
120 if (spectrum.eigenvalues()[j] > tolerance)
121 inverse[j] = 1 / spectrum.eigenvalues()[j];
122 else if ((F * spectrum.eigenvectors().col(j)).stableNorm() >
123 epsilon * operatorScale)
124 throw std::domain_error(
125 "Bath residual leaves the supported stiffness range");
127 const Matrix pseudoinverse = spectrum.eigenvectors() *
128 inverse.asDiagonal() *
129 spectrum.eigenvectors().transpose();
132 extended.bottomLeftCorner(m, m) = F;
133 extended.topRightCorner(m, m) = F.transpose();
134 extended.bottomRightCorner(m, m) = F * pseudoinverse * F.transpose();
135 extended.bottomRightCorner(m, m).diagonal().array() += alpha;
136 Matrix coupling = Matrix::Zero(2 * m, r);
137 coupling.topRows(m) = G;