39template <
typename Source,
typename Selection,
typename SubSpace>
40class SqrtComponentMarginal;
79template <Space Space_, RealScalar Scalar_ =
double>
116 : m_space{std::move(
space)}
125 template <MatrixExpression Mean, MatrixExpression Root>
126 requires internal::SizedByLength<Space_>
128 : m_mean{std::forward<Mean>(
mean)}
129 , m_root{std::forward<Root>(
root)}
137 template <MatrixExpression Mean, MatrixExpression Root>
139 : m_mean{std::forward<Mean>(
mean)}
140 , m_root{std::forward<Root>(
root)}
141 , m_space{std::move(
space)}
161 [[nodiscard]] Space_
const &
193 template <MatrixExpression Mean>
197 m_stamp.touchValues();
199 m_mean = std::forward<Mean>(
mean);
206 template <MatrixExpression Root>
210 m_stamp.touchValues();
212 m_root = std::forward<Root>(
root);
213 m_root.template triangularView<Eigen::StrictlyUpper>().setZero();
217 [[nodiscard]] std::expected<MatrixType, GaussianError>
220 MatrixType result = internal::multiply(internal::asTriangular<Eigen::Lower>(m_root), m_root.transpose());
227 [[nodiscard]] std::expected<MatrixType, GaussianError>
233 return std::unexpected{inverse.error()};
235 MatrixType result = internal::multiply(inverse->transpose(), *inverse);
240 [[nodiscard]] std::expected<MatrixType, GaussianError>
249 [[nodiscard]] std::expected<MatrixType, GaussianError>
252 if (
auto checked = checkRoot(); !checked)
254 return std::unexpected{checked.error()};
257 internal::solveInPlace(m_root.template triangularView<Eigen::Lower>(), result);
268 template <
typename Path>
272 return internal::marginalOverPath<Path>(m_space, [&](
auto const & kept,
auto const & keptSpace) {
273 return marginalAt(kept, keptSpace);
280 template <
typename Path>
284 return internal::marginalOverElement<Path>(m_space, element, [&](
auto const & kept,
auto const & keptSpace) {
285 return marginalAt(kept, keptSpace);
296 [[nodiscard]] std::expected<void, GaussianError>
299 if (
auto checked = checkRoot(); !checked)
301 return std::unexpected{checked.error()};
304 Scalar const logDeterminant =
Scalar(2) * m_root.diagonal().array().abs().log().sum();
305 if (m_logDeterminant != logDeterminant)
307 m_logDeterminant = logDeterminant;
315 template <
typename R>
316 [[nodiscard]] std::expected<void, GaussianError>
321 "a square-root form's root is positive semidefinite by construction and has no repair; repairing a "
322 "covariance is the covariance form's (decisions.md [gaussian-forms])"
333 [[nodiscard]] std::expected<DensityType, GaussianError>
336 if (
auto factorized =
factorize(); !factorized)
338 return std::unexpected{factorized.error()};
355 template <typename, typename, typename>
356 friend class internal::SqrtComponentMarginal;
360 friend class internal::ValuesCheck;
372 if ((m_root.diagonal().array() ==
Scalar(0)).any())
383 template <
typename Kept,
typename KeptSpace>
385 marginalAt(Kept
const & kept, KeptSpace
const & keptSpace)
const
388 using Rows = internal::MatrixOf<
390 Result::DifferenceDimensionAtCompileTime,
392 Result::MaxDifferenceDimensionAtCompileTime,
394 typename Result::VectorType
mean = m_mean(kept.values);
395 Rows
const rows = m_root(kept.differences, Eigen::placeholders::all);
396 return Result{keptSpace, std::move(
mean), internal::lowerTriangularRoot(rows)};
400 template <
typename Kept,
typename KeptSpace>
402 densityMarginalAt(Kept
const & kept, KeptSpace
const & keptSpace)
const
404 return marginalAt(kept, keptSpace);
409 densityMoments()
const
425 m_root.template triangularView<Eigen::Lower>().solveInPlace(residual);
430 colorInto(
auto & colored,
auto const & normals)
const
432 colored.noalias() = internal::multiply(internal::asTriangular<Eigen::Lower>(m_root), normals);
441 internal::fitValueDimension(m_space, m_mean.size());
444 &&
"the root must have a row and a column per difference coordinate of the space"
447 m_root.template triangularView<Eigen::StrictlyUpper>().setZero();
453 mutable Scalar m_logDeterminant{};
454 internal::Stamp m_stamp;
466template <
typename Source,
typename Selection,
typename SubSpace>
467class SqrtComponentMarginal :
public SqrtGaussianParams<SubSpace, typename Source::Scalar>
474 SqrtComponentMarginal(Source
const & source, Selection shared)
475 : ParamsType{source.marginalAt(
477 decltype(
shared.resolved->indices) const &,
478 decltype(
shared.resolved->differenceIndices) const &>{
480 shared.resolved->differenceIndices
487 [[nodiscard]] ParamsType
504template <Space Space_, RealScalar Scalar_ =
double>
520template <GaussianForm G>
521[[nodiscard]] std::expected<SqrtGaussianParams<typename G::SpaceType, typename G::Scalar>,
GaussianError>
525 std::expected<typename G::VectorType, GaussianError> mean = form.mean();
528 return std::unexpected{mean.error()};
530 auto root = form.covarianceRoot();
533 return std::unexpected{root.error()};
537 typename Result::MatrixType lower = internal::lowerTriangularRoot(*root);
540 auto const step =
Emrys::minus(form.space(), *mean, form.reference());
541 auto const g =
resetJacobian(form.space(), form.reference(), step);
542 lower = internal::lowerTriangularRoot(
typename Result::MatrixType{internal::multiply(g, lower)});
544 return Result{form.space(), std::move(*mean), std::move(lower)};
548 return Result{form.space(), std::move(*mean), std::move(*root)};
554#ifndef EMRYS_FILTER_NO_IO
563template <
typename Space_,
typename Scalar_>
565describe(std::ostream & stream, SqrtGaussianParams<Space_, Scalar_>
const & gaussian, Indent indent)
567 stream << indent <<
"square-root Gaussian";
568 printDimension(stream, gaussian.valueDimension(), gaussian.differenceDimension());
569 printMoments(stream, indent.deeper(), gaussian.moments());
Spaces built from other spaces (decisions.md [sizes-and-dimensions], [several-resizable-blocks]): Com...
How a function that stores a matrix argument takes it (decisions.md [forwarded-matrices]): the concep...
The Gaussian: its parameters GaussianParams, the Gaussian that evaluates the density,...
What marginals and conditionals are built from: resolving Eigen index sets against a space; the share...
The macros every header includes first.
Mathematical constants and scalar functions that work in every scalar type the library accepts,...
Moments: the mean and covariance that a Gaussian carries and that a mixture or a sample set is moment...
Products of fixed-size matrices by Eigen's fixed-size product at every size: multiply() is by Eigen'...
Printing: operator<< and std::formatter for every distribution, every filter (which prints its name a...
The covariance reset (decisions.md [covariance-reset]): hasIdentityReset<>, resetJacobian(),...
Solving by a factorization of a small matrix: transposedSolve() gives , the shape of a Kalman gain an...
The Space concept and what every continuous space shares (decisions.md [sizes-and-dimensions]): the t...
What parameters and their views share (decisions.md [view-stamp], [params-and-density]): Stamp,...
Triangularization: the triangular factor of a stacked matrix by Householder reflections,...
First and second moments (mean and covariance) of a distribution.
Definition Moments.h:55
Scalar_ Scalar
Definition Moments.h:58
Eigen::Index Index
Definition Moments.h:59
DifferenceMatrix< Space_, Space_, Scalar > MatrixType
Definition Moments.h:76
DifferenceVector< Space_, Scalar > DifferenceVectorType
Definition Moments.h:75
ValueVector< Space_, Scalar > VectorType
Definition Moments.h:74
static constexpr int MaxDifferenceDimensionAtCompileTime
Definition Moments.h:72
static constexpr int DifferenceDimensionAtCompileTime
Definition Moments.h:69
static constexpr int MaxValueDimensionAtCompileTime
Definition Moments.h:66
static constexpr int ValueDimensionAtCompileTime
Definition Moments.h:63
A Gaussian kept as a mean and a lower-triangular root of its covariance.
Definition SqrtGaussian.h:81
std::expected< MatrixType, GaussianError > precision() const
Definition SqrtGaussian.h:228
typename MomentsType::VectorType VectorType
Definition SqrtGaussian.h:87
static constexpr int MaxValueDimensionAtCompileTime
Definition SqrtGaussian.h:97
Index differenceDimension() const
Definition SqrtGaussian.h:155
std::expected< void, GaussianError > factorize(R const &) const
Refused: a root needs no repair, being positive semidefinite by construction, so a mixture of square-...
Definition SqrtGaussian.h:317
static constexpr bool IsInfoForm
Definition SqrtGaussian.h:106
MomentsType moments() const
Definition SqrtGaussian.h:185
typename MomentsType::Index Index
Definition SqrtGaussian.h:86
static constexpr int DifferenceDimensionAtCompileTime
Definition SqrtGaussian.h:100
static constexpr bool IsSqrtForm
Definition SqrtGaussian.h:107
std::expected< MatrixType, GaussianError > covarianceRoot() const
Definition SqrtGaussian.h:241
SqrtGaussianParams(Space_ space)
Constructs a SqrtGaussianParams in space, with a zero mean and root.
Definition SqrtGaussian.h:115
std::expected< MatrixType, GaussianError > covariance() const
Definition SqrtGaussian.h:218
Index valueDimension() const
Definition SqrtGaussian.h:148
std::expected< void, GaussianError > factorize() const
Checks the root and computes , writing it to a cache when it has changed, so that a container of comp...
Definition SqrtGaussian.h:297
Space_ const & space() const
Definition SqrtGaussian.h:162
std::expected< DensityType, GaussianError > toDensity() const &
factorize(), so that the cache it writes is not safe on one object from two threads at once while the...
Definition SqrtGaussian.h:334
Moments< Space_, Scalar_ > MomentsType
Definition SqrtGaussian.h:84
VectorType const & mean() const
Definition SqrtGaussian.h:169
auto marginal() const
Definition SqrtGaussian.h:270
static constexpr int ValueDimensionAtCompileTime
Definition SqrtGaussian.h:94
typename MomentsType::DifferenceVectorType DifferenceVectorType
Definition SqrtGaussian.h:88
FormGaussian< SqrtGaussianParams > DensityType
Definition SqrtGaussian.h:91
SqrtGaussianParams(Mean &&mean, Root &&root)
Constructs a SqrtGaussianParams from mean and the lower triangle of root, moving either one passed as...
Definition SqrtGaussian.h:127
Space_ SpaceType
Definition SqrtGaussian.h:90
typename MomentsType::Scalar Scalar
Definition SqrtGaussian.h:85
static constexpr bool IsParams
Definition SqrtGaussian.h:105
SqrtGaussianParams(Space_ space, Mean &&mean, Root &&root)
SqrtGaussianParams(mean, root) in space, which is resized to fit them if it has one resizable block.
Definition SqrtGaussian.h:138
auto marginal(Index element) const
marginal<Path>() of repetition element of the repeated block Path starts at (see GaussianParams::marg...
Definition SqrtGaussian.h:282
void setMean(Mean &&mean)
Sets the mean, normalized by the space, moving mean if it is an rvalue of the stored type.
Definition SqrtGaussian.h:195
static constexpr int MaxDifferenceDimensionAtCompileTime
Definition SqrtGaussian.h:103
void toDensity() const &&=delete
A density of a temporary would outlive it.
typename MomentsType::MatrixType MatrixType
Definition SqrtGaussian.h:89
MatrixType const & root() const
Definition SqrtGaussian.h:178
SqrtGaussianParams()
Constructs a SqrtGaussianParams in a default space, with a zero mean and root.
Definition SqrtGaussian.h:110
std::expected< MatrixType, GaussianError > precisionRoot() const
Definition SqrtGaussian.h:250
void setRoot(Root &&root)
Sets the root to the lower triangle of root, moving it if it is an rvalue of the stored type.
Definition SqrtGaussian.h:208
bool allFinite(Eigen::DenseBase< Derived > const &x)
Definition Math.h:354
#define EMRYS_FILTER_NO_UNIQUE_ADDRESS
Assertion used for programmer errors (misuse of the API).
Definition Macros.h:38
@ NotPositiveDefinite
the covariance is not positive definite, so it has no Cholesky factor or inverse
Definition NumericError.h:31
@ NonFinite
a matrix, a factor, or a log-weight holds NaN or infinity (+inf, for a log-weight)
Definition NumericError.h:33
std::expected< SqrtGaussianParams< typename G::SpaceType, typename G::Scalar >, GaussianError > toSqrt(G const &form)
Definition SqrtGaussian.h:522
NumericError GaussianError
Reasons a Gaussian, in any of its forms (decisions.md [gaussian-forms]), can fail to give a density,...
Definition NumericError.h:45
constexpr bool isInfoForm
Whether the GaussianForm G holds the precision, as InfoGaussianParams and SqrtInfoGaussianParams do,...
Definition GaussianForm.h:69
FormGaussian< SqrtGaussianParams< Space_, Scalar_ > > SqrtGaussian
The Gaussian in square-root form, which evaluates its density: what SqrtGaussianParams::toDensity() r...
Definition SqrtGaussian.h:505
constexpr Shared shared()
Definition Batched.h:63
Eigen::Index valueDimension(S const &space)
Definition Space.h:272
auto minus(S const &space, Eigen::MatrixBase< ADerived > const &a, Eigen::MatrixBase< BDerived > const &b)
Definition Space.h:715
auto resetJacobian(S const &space, Eigen::MatrixBase< XDerived > const &x, Eigen::MatrixBase< DDerived > const &d)
Definition Reset.h:377
constexpr bool hasIdentityReset
Whether the reset Jacobian of S is the identity, so that a covariance needs no change when its mean m...
Definition Reset.h:100
Eigen::Index differenceDimension(S const &space)
Definition Space.h:284
void normalizeValues(S const &space, Eigen::MatrixBase< Derived > const &block)
Brings the values in block (rows are space's coordinates, columns samples) into canonical form,...
Definition Space.h:331
What a container does with a component beyond the component's own members (decisions....
Definition Component.h:104