80{
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 "
87 "required");
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");
97 seen[channel] = true;
98 cross.row(i) = covariance.
matrix.row(channel);
99 }
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 "
115 "covariance");
116 }
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]));
127 }
129 Eigen::LLT<Matrix> factor(observed.matrix);
130 if (factor.info() == Eigen::Success &&
131 factor.matrixL()
132 .toDenseMatrix()
133 .diagonal()
134 .array()
135 .square()
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();
142 }
143 Eigen::SelfAdjointEigenSolver<Matrix> spectrum(observed.matrix);
144 if (spectrum.info() != Eigen::Success)
145 throw std::domain_error(
146 "Gaussian measurement support eigensolve failed");
147 const auto rank =
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");
159 return result;
160}
std::vector< Eigen::Index > channels