35 double inheritedRoundoff = 0)
37 if (input.rows() == 0 || input.rows() != input.cols() ||
38 !input.allFinite() || !std::isfinite(inheritedRoundoff) ||
39 inheritedRoundoff < 0)
40 throw std::invalid_argument(
41 "Finite nonempty square Gaussian covariance required");
43 const double scale = input.cwiseAbs().maxCoeff();
46 128 * std::numeric_limits<double>::epsilon() * input.rows() * scale;
48 throw std::overflow_error(
49 "Gaussian covariance roundoff exceeds output range");
50 if ((input - input.transpose()).cwiseAbs().maxCoeff() > result.
tolerance)
51 throw std::invalid_argument(
"Symmetric Gaussian covariance required");
52 result.
matrix = .5 * input + .5 * input.transpose();
53 Eigen::SelfAdjointEigenSolver<Matrix> solver(result.
matrix,
54 Eigen::EigenvaluesOnly);
55 if (solver.info() != Eigen::Success ||
56 solver.eigenvalues().minCoeff() < -result.
tolerance)
57 throw std::invalid_argument(
58 "Positive semidefinite Gaussian covariance required");
81 const auto count =
static_cast<Eigen::Index
>(measurement.
channels.size());
82 if (count == 0 || measurement.
noise.rows() != count ||
83 measurement.
noise.cols() != count || !std::isfinite(measurement.
cost) ||
84 !(measurement.
cost > 0))
85 throw std::invalid_argument(
86 "Matching nonempty Gaussian measurement and positive cost "
89 Matrix marginal = noise.matrix;
91 std::vector<bool> seen(covariance.
matrix.rows(),
false);
92 for (Eigen::Index i = 0; i < count; ++i) {
93 const auto channel = measurement.
channels[i];
94 if (channel < 0 || channel >= covariance.
matrix.rows() || seen[channel])
95 throw std::invalid_argument(
96 "Distinct in-range Gaussian measurement channels required");
98 cross.row(i) = covariance.
matrix.row(channel);
100 for (Eigen::Index i = 0; i < count; ++i)
101 for (Eigen::Index j = 0; j < count; ++j)
104 if (!marginal.allFinite())
105 throw std::overflow_error(
106 "Gaussian measurement covariance exceeds output range");
107 std::vector<Eigen::Index> support;
108 for (Eigen::Index i = 0; i < count; ++i) {
109 if (marginal(i, i) > 0)
110 support.push_back(i);
111 else if (marginal(i, i) < -(covariance.
tolerance + noise.tolerance) ||
112 cross.row(i).cwiseAbs().maxCoeff() > covariance.
tolerance)
113 throw std::domain_error(
114 "Zero-variance Gaussian measurement has unresolved cross "
117 if (support.empty())
return Matrix(covariance.
matrix.rows(), 0);
118 const auto dimension =
static_cast<Eigen::Index
>(support.size());
119 Matrix correlation(dimension, dimension),
120 scaledCross(dimension, cross.cols());
121 for (Eigen::Index i = 0; i < dimension; ++i) {
122 const double scale = std::sqrt(marginal(support[i], support[i]));
123 scaledCross.row(i) = cross.row(support[i]) / scale;
124 for (Eigen::Index j = 0; j < dimension; ++j)
125 correlation(i, j) = (marginal(support[i], support[j]) / scale) /
126 std::sqrt(marginal(support[j], support[j]));
129 Eigen::LLT<Matrix> factor(observed.matrix);
130 if (factor.info() == Eigen::Success &&
136 .minCoeff() > observed.tolerance) {
137 const Matrix result = factor.matrixL().solve(scaledCross);
138 if (!result.allFinite())
139 throw std::overflow_error(
140 "Gaussian conditioning factor exceeds output range");
141 return result.transpose();
143 Eigen::SelfAdjointEigenSolver<Matrix> spectrum(observed.matrix);
144 if (spectrum.info() != Eigen::Success)
145 throw std::domain_error(
146 "Gaussian measurement support eigensolve failed");
148 (spectrum.eigenvalues().array() > observed.tolerance).count();
150 Eigen::Index column = 0;
151 for (Eigen::Index j = 0; j < dimension; ++j)
152 if (spectrum.eigenvalues()[j] > observed.tolerance)
153 result.col(column++) = scaledCross.transpose() *
154 spectrum.eigenvectors().col(j) /
155 std::sqrt(spectrum.eigenvalues()[j]);
156 if (!result.allFinite())
157 throw std::overflow_error(
158 "Gaussian conditioning factor exceeds output range");
162 const Matrix &factor,
double cost)
166 if (factor.rows() != weights.size() || !factor.allFinite() ||
167 !std::isfinite(cost) || !(cost > 0))
168 throw std::invalid_argument(
169 "Finite matching Gaussian conditioning factor and positive cost "
171 const auto projection =
172 (factor.cast<
long double>().transpose() * weights.cast<
long double>())
174 const long double reduction = projection.squaredNorm();
175 const double roundoff = covariance.
tolerance * weights.squaredNorm() +
176 128 * std::numeric_limits<double>::epsilon() *
178 if (!std::isfinite(reduction) || !std::isfinite(roundoff) ||
179 reduction >
static_cast<long double>(result.
priorVariance) + roundoff)
180 throw std::runtime_error(
181 "Gaussian measurement violates the functional Schur bound");
187 throw std::overflow_error(
188 "Gaussian measurement score exceeds output range");
192 ? std::numeric_limits<double>::infinity()
193 : -.5 * std::log1p(-fraction);
227 const std::vector<Measurement> &measurements, std::size_t budget)
229 if (budget > measurements.size())
230 throw std::invalid_argument(
231 "Measurement budget exceeds the candidate count");
235 std::vector<bool> selected(measurements.size(),
false);
236 const auto absoluteWeights = weights.cast<
long double>().cwiseAbs().eval();
237 long double functionalWork = absoluteWeights.dot(
238 current.matrix.cast<
long double>().cwiseAbs() * absoluteWeights);
239 Eigen::Index maximumFactorColumns = 0;
240 for (std::size_t step = 0; step < budget; ++step) {
241 const long double functionalRoundoff =
242 128 * std::numeric_limits<double>::epsilon() *
243 (current.matrix.rows() + maximumFactorColumns) * functionalWork;
245 std::size_t chosen = measurements.size();
248 for (std::size_t j = 0; j < measurements.size(); ++j)
253 scoreFactor(current, weights, factor, measurements[j].cost);
254 if (chosen == measurements.size() ||
258 chosenFactor = factor;
262 selected[chosen] =
true;
264 result.
scores.push_back(best);
265 const auto absoluteProjection =
266 (chosenFactor.cast<
long double>().cwiseAbs().transpose() *
269 functionalWork += absoluteProjection.squaredNorm();
270 maximumFactorColumns =
271 std::max(maximumFactorColumns, chosenFactor.cols());
272 const Matrix correction = chosenFactor * chosenFactor.transpose();
273 const Matrix absoluteProduct =
274 chosenFactor.cwiseAbs() * chosenFactor.cwiseAbs().transpose();
275 const double rounding =
276 128 * std::numeric_limits<double>::epsilon() *
277 current.matrix.rows() *
278 (current.matrix.cwiseAbs().maxCoeff() +
279 std::max<Eigen::Index>(1, chosenFactor.cols()) *
280 absoluteProduct.maxCoeff());
282 current.tolerance + rounding);