56template <Space Space_, RealScalar Scalar_ =
double>
70 internal::IndexVector<MomentsType::ValueDimensionAtCompileTime, MomentsType::MaxValueDimensionAtCompileTime>;
96 : m_space{std::move(
space)}
97 , m_corner{std::move(corner)}
98 , m_width{std::move(width)}
99 , m_counts{std::move(
counts)}
100 , m_covariance{std::move(covariance)}
103 isFinite(relativeWidth) && relativeWidth >
Scalar(0) &&
"the relative width must be positive and finite"
106 m_corner.size() == m_width.size() && m_corner.size() == m_counts.size()
107 && m_covariance.rows() == m_corner.size() && m_covariance.cols() == m_corner.size()
108 &&
"one corner, width, count, and covariance row per coordinate"
111 for (
Index i = 0; i < m_counts.size(); ++i)
113 EMRYS_FILTER_ASSERT(m_counts(i) >= 0 &&
"a count must not be negative");
117 m_size *= m_counts(i);
118 Scalar const deviation = relativeWidth * m_width(i) /
Scalar(m_counts(i));
119 m_covariance(i, i) = deviation * deviation;
122 EMRYS_FILTER_ASSERT(gridded > 0 &&
"the grid needs a gridded coordinate");
123 m_logWeight = -Eigen::numext::log(
Scalar(m_size));
137 return m_corner.size();
155 [[nodiscard]] Element
158 EMRYS_FILTER_ASSERT(k >= 0 && k < m_size &&
"component index out of range");
161 for (
Index i = 0; i < m_counts.size(); ++i)
163 Index const count = m_counts(i);
166 Index const j = rest % count;
203template <Space S, RealScalar Scalar,
typename CountOf>
204[[nodiscard]] UniformGrid<S, Scalar>
205uniformGrid(Uniform<S, Scalar>
const & uniform, CountOf
const & countOf, Scalar relativeWidth)
207 using Grid = UniformGrid<S, Scalar>;
208 Eigen::Index
const n = uniform.valueDimension();
209 typename Grid::CountsType counts(n);
210 for (Eigen::Index i = 0; i < n; ++i)
212 counts(i) = countOf(i);
213 EMRYS_FILTER_ASSERT(counts(i) >= 1 &&
"each coordinate needs at least one component");
220 Grid::MatrixType::Zero(n, n),
227inline constexpr bool isUniform =
false;
230template <Space S, RealScalar Scalar>
231inline constexpr bool isUniform<Uniform<S, Scalar>>{
true};
241template <
typename F,
typename BlockSpace>
242inline constexpr bool isGaussianFactor<EachRepetition<F, BlockSpace>>{
isGaussian<F>};
250template <Space S, RealScalar Scalar,
typename... Factors,
typename CountOf>
251[[nodiscard]] UniformGrid<S, Scalar>
253 Product<S, Scalar, Factors...>
const & product,
254 CountOf
const & countOf,
255 Eigen::Index givenCounts,
260 ((isUniform<Factors> || isGaussianFactor<Factors>) && ...),
261 "uniformComponents() of a Product takes Uniform factors placed with on<Path>() and GaussianParams factors"
263 static_assert((isUniform<Factors> || ...),
"uniformComponents() of a Product needs a Uniform factor to grid");
264 using Grid = UniformGrid<S, Scalar>;
265 using Index = Eigen::Index;
266 Index
const n = product.valueDimension();
267 auto corner =
typename Grid::VectorType(n);
268 typename Grid::MatrixType covariance = Grid::MatrixType::Zero(n, n);
269 typename Grid::VectorType width = Grid::VectorType::Zero(n);
270 typename Grid::CountsType counts = Grid::CountsType::Zero(n);
271 [&]<std::size_t... K>(std::index_sequence<K...>) {
274 auto const & factor = product.template factor<K>();
275 auto const & indices = product.template indices<K>();
276 if constexpr (isUniform<std::tuple_element_t<K, std::tuple<Factors...>>>)
278 corner(indices) = factor.lower();
279 width(indices) = factor.width();
280 counts(indices).setOnes();
284 auto const & moments = factor.moments();
285 using FactorMatrix = std::remove_cvref_t<
decltype(moments.covarianceLower())>;
286 corner(indices) = moments.mean();
287 covariance(indices, indices) =
288 FactorMatrix{moments.covarianceLower().template selfadjointView<Eigen::Lower>()};
293 }(std::index_sequence_for<Factors...>{});
296 for (Index i = 0; i < n; ++i)
300 counts(i) = countOf(gridded++);
301 EMRYS_FILTER_ASSERT(counts(i) >= 1 &&
"each gridded coordinate needs at least one component");
305 (givenCounts < 0 || givenCounts == gridded) &&
"give one count per coordinate of the Uniform factors"
312 std::move(covariance),
359template <Space S, RealScalar Scalar,
typename CountsDerived>
360 requires std::integral<typename CountsDerived::Scalar>
364 Eigen::DenseBase<CountsDerived>
const & counts,
365 std::type_identity_t<Scalar> relativeWidth = Scalar(0.6)
368 EMRYS_FILTER_ASSERT(counts.size() == uniform.
valueDimension() &&
"give one count per coordinate");
369 return internal::uniformGrid(
371 [&](Eigen::Index i) {
372 return Eigen::Index(counts(i));
381template <Space S, RealScalar Scalar>
382[[nodiscard]] UniformGrid<S, Scalar>
386 std::type_identity_t<Scalar> relativeWidth = Scalar(0.6)
389 return internal::uniformGrid(
417template <Space S, RealScalar Scalar,
typename... Factors,
typename CountsDerived>
418 requires std::integral<typename CountsDerived::Scalar>
419[[nodiscard]] UniformGrid<S, Scalar>
422 Eigen::DenseBase<CountsDerived>
const & counts,
423 std::type_identity_t<Scalar> relativeWidth = Scalar(0.6)
426 return internal::productGrid(
428 [&](Eigen::Index j) {
429 EMRYS_FILTER_ASSERT(j < counts.size() &&
"give one count per coordinate of the Uniform factors");
430 return Eigen::Index(counts(j));
441template <Space S, RealScalar Scalar,
typename... Factors>
442[[nodiscard]] UniformGrid<S, Scalar>
446 std::type_identity_t<Scalar> relativeWidth = Scalar(0.6)
449 return internal::productGrid(
What a container of several Gaussians needs of each one (decisions.md [component-first]): the Compone...
Execution policies (decisions.md [execution-policies]): execution::sequential, the ExecutionPolicy co...
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,...
The mixture: the parameters MixtureParams of weighted components of one type (decisions....
Moments: the mean and covariance that a Gaussian carries and that a mixture or a sample set is moment...
Product: the product of independent densities, each on its own block of a space, built with on<Path>(...
The Space concept and what every continuous space shares (decisions.md [sizes-and-dimensions]): the t...
First and second moments (mean and covariance) of a distribution.
Definition Moments.h:55
DifferenceMatrix< Space_, Space_, Scalar > MatrixType
Definition Moments.h:76
ValueVector< Space_, Scalar > VectorType
Definition Moments.h:74
The product of independent densities, each on its own coordinates of a space.
Definition Product.h:772
bool isFinite(T const &x)
Definition Math.h:305
constexpr bool isGaussian
Whether C is a Gaussian in covariance form: the component for which a container offers the convenienc...
Definition Component.h:111
UniformGrid< S, Scalar > uniformComponents(Uniform< S, Scalar > const &uniform, Eigen::DenseBase< CountsDerived > const &counts, std::type_identity_t< Scalar > relativeWidth=Scalar(0.6))
Definition UniformMixture.h:362