24#ifndef VULPES_POLYNOMIALS_MONOMIAL_HPP_
25#define VULPES_POLYNOMIALS_MONOMIAL_HPP_
40template <std::
size_t Dim,
typename IndexType,
typename CoefType,
bool IsScaled>
47template <
typename T,
typename = std::enable_if_t<std::is_
integral_v<T>>>
53 constexpr real_t one = 1;
54 constexpr real_t two = 2;
55 constexpr real_t eight = 8;
57 const real_t tria_lvl_f =
58 (std::sqrt(one + eight *
static_cast<real_t
>(n)) - one) / two;
61 const std::size_t tria_lvl =
static_cast<std::size_t
>(tria_lvl_f);
62 const std::size_t before =
63 tria_lvl * (tria_lvl + std::size_t(1)) / std::size_t(2);
64 assert(
static_cast<std::size_t
>(before) <= n);
65 assert(n <
static_cast<std::size_t
>((tria_lvl + 1) * (tria_lvl + 2) / 2));
66 const std::size_t remain = n - before;
67 return {{
static_cast<T
>(tria_lvl - remain),
static_cast<T
>(remain)}};
70template <
typename T,
typename = std::enable_if_t<std::is_
integral_v<T>>>
77 constexpr real_t one = 1;
78 constexpr real_t two = 2;
79 constexpr real_t three = 3;
80 constexpr real_t six = 6;
81 constexpr real_t cbrt6 = 1.817120592832139658891211756327;
82 const real_t n_f =
static_cast<real_t
>(n);
85 const real_t x = cbrt6 * std::cbrt(n_f) - one;
87 const real_t pyramid_lvl_f =
88 (six * n_f + (two * x + three) * x * x) / (three * x * (x + two) + two);
90 const std::size_t pyramid_lvl =
static_cast<std::size_t
>(pyramid_lvl_f);
91 const std::size_t before =
92 pyramid_lvl * (pyramid_lvl + 1) * (pyramid_lvl + 2) / 6;
93 assert(
static_cast<std::size_t
>(before) <= n);
95 n <
static_cast<std::size_t
>(
96 (pyramid_lvl + 1) * (pyramid_lvl + 2) * (pyramid_lvl + 3) / 6));
97 const std::size_t lvl_id = n - before;
100 {
static_cast<T
>(pyramid_lvl - ij[0] - ij[1]),
static_cast<T
>(ij[0]),
101 static_cast<T
>(ij[1])}};
131template <
typename T,
typename = std::enable_if_t<std::is_
integral_v<T>>>
133 static constexpr std::size_t max_deg = 10;
134 static constexpr std::size_t max_cache = (max_deg + 1) * (max_deg + 2) / 2;
135 static constexpr std::array<std::array<T, 2>, max_cache> cache = []() {
136 std::array<std::array<T, 2>, max_cache> cache;
137 for (std::size_t i = 0; i < max_cache; ++i) {
176template <
typename T,
typename = std::enable_if_t<std::is_
integral_v<T>>>
178 static constexpr std::size_t max_deg = 10;
179 static constexpr std::size_t max_cache =
180 (max_deg + 1) * (max_deg + 2) * (max_deg + 3) / 6;
181 static constexpr std::array<std::array<T, 3>, max_cache> cache = []() {
182 std::array<std::array<T, 3>, max_cache> cache;
183 for (std::size_t i = 0; i < max_cache; ++i) {
198template <std::
size_t Dim,
typename T>
200 if constexpr (Dim == 1) {
202 }
else if constexpr (Dim == 2) {
204 }
else if constexpr (Dim == 3) {
207 static_assert(Dim * 0 &&
"Not implemented for dimension > 3");
217template <std::
size_t Dim,
typename T>
219 if constexpr (Dim == 1) {
221 }
else if constexpr (Dim == 2) {
223 return ij * (ij + 1) / 2 +
monomial[1];
224 }
else if constexpr (Dim == 3) {
227 return i_j_k * (i_j_k + 1) * (i_j_k + 2) / 6 + j_k * (j_k + 1) / 2 +
230 static_assert(Dim * 0 &&
"Not implemented for dimension > 3");
237template <std::
size_t Dim,
typename IndexType,
typename CoefType>
252template <std::
size_t Dim,
typename IndexType,
typename CoefType,
bool IsScaled>
255 internal::ScaledMonomial<Dim, IndexType, CoefType>,
256 internal::NonScaledMonomial> {
263 if constexpr (IsScaled) {
265 this->m_translation.fill(0);
274 if constexpr (IsScaled) {
276 this->m_translation.fill(0);
284 const std::array<IndexType, Dim>& exponent,
const CoefType& coefficient)
286 if constexpr (IsScaled) {
288 this->m_translation.fill(0);
296 const std::array<IndexType, Dim>& exponent,
297 const CoefType& coefficient,
298 const CoefType& scaling,
299 const std::array<IndexType, Dim>& translation)
301 if constexpr (IsScaled) {
302 this->m_scaling = scaling;
303 this->m_translation = translation;
310 constexpr CoefType
evaluate(
const Eigen::Vector<CoefType, Dim>& point)
const {
317 constexpr CoefType
evaluate(
const CoefType* point)
const {
319 for (std::size_t i = 0; i < Dim; ++i) {
320 CoefType x = point[i];
321 if constexpr (IsScaled) {
322 x = (x - this->m_translation[i]) * this->m_scaling;
336 assert(component < Dim);
342 std::array<IndexType, Dim> new_exponent =
m_exponent;
343 new_exponent[component] -= 1;
347 if constexpr (IsScaled) {
349 new_exponent, new_coefficient, this->m_scaling, this->m_translation);
351 return Monomial(new_exponent, new_coefficient);
363 constexpr Eigen::Vector<Monomial, Dim>
gradient()
const {
364 Eigen::Vector<Monomial, Dim> grad;
365 for (std::size_t d = 0; d < Dim; ++d) {
374 template <
typename U>
376 if constexpr (IsScaled) {
386 template <std::
size_t PreDim, std::
size_t PostDim>
389 constexpr auto OutDim = Dim + PreDim + PostDim;
390 std::array<IndexType, OutDim> exponent = {0};
391 std::copy(
exp().cbegin(),
exp().cend(), exponent.begin() + PreDim);
392 return {exponent,
coef()};
400 os <<
"Coefficient: " <<
monomial.m_coefficient <<
", ";
402 os <<
"Exponents: [";
403 for (std::size_t i = 0; i < Dim; ++i) {
404 os << static_cast<std::size_t>(
monomial.m_exponent[i]);
411 if constexpr (IsScaled) {
412 os <<
", Scaling: " <<
monomial.m_scaling <<
", Translation: [";
413 for (std::size_t i = 0; i < Dim; ++i) {
430 if constexpr (IsScaled) {
433 lhs.m_scaling == rhs.m_scaling &&
434 lhs.m_translation == rhs.m_translation;
458 if constexpr (IsScaled) {
460 if (lhs.m_scaling != rhs.m_scaling) {
461 return lhs.m_scaling < rhs.m_scaling;
465 return lhs.m_translation < rhs.m_translation;
Class representing a (possibly scaled) monomial.
Definition monomial.hpp:256
const auto & coef() const
Definition monomial.hpp:471
constexpr Monomial(const std::array< IndexType, Dim > &exponent, const CoefType &coefficient)
Constructor for a general non-scaled monomial.
Definition monomial.hpp:283
constexpr Monomial derivative(std::size_t component) const
Compute the derivative.
Definition monomial.hpp:335
Monomial< Dim+PreDim+PostDim, IndexType, CoefType, IsScaled > liftDimension() const
Definition monomial.hpp:388
friend constexpr bool operator==(const Monomial &lhs, const Monomial &rhs)
Equality operator.
Definition monomial.hpp:429
constexpr Monomial(const CoefType &coeff)
Constructor for the constant monomial.
Definition monomial.hpp:272
const auto & exp() const
Definition monomial.hpp:472
constexpr CoefType evaluate(const Eigen::Vector< CoefType, Dim > &point) const
Evaluate the monomial in a point.
Definition monomial.hpp:310
constexpr CoefType evaluate(const CoefType *point) const
Evaluate the monomial in a point passed as a raw buffer.
Definition monomial.hpp:317
constexpr bool empty() const
Check if it is the zero monomial.
Definition monomial.hpp:358
constexpr Monomial(const std::array< IndexType, Dim > &exponent, const CoefType &coefficient, const CoefType &scaling, const std::array< IndexType, Dim > &translation)
Constructor for a general scaled monomial.
Definition monomial.hpp:295
std::array< IndexType, Dim > m_exponent
Definition monomial.hpp:475
Monomial< Dim, IndexType, U, IsScaled > to() const
Cast the monomial to a different underlying type.
Definition monomial.hpp:375
constexpr Eigen::Vector< Monomial, Dim > gradient() const
Compute the gradient of the monomial.
Definition monomial.hpp:363
CoefType m_coefficient
Definition monomial.hpp:476
constexpr Monomial()
Constructor for the zero monomial.
Definition monomial.hpp:261
friend std::ostream & operator<<(std::ostream &os, const Monomial &monomial)
Output operator.
Definition monomial.hpp:398
friend constexpr bool operator<(const Monomial &lhs, const Monomial &rhs)
Less operator.
Definition monomial.hpp:447
Definition monomial.hpp:243
Definition monomial.hpp:238
std::array< IndexType, Dim > m_translation
Definition monomial.hpp:241
CoefType m_scaling
Definition monomial.hpp:240
Template class to represent and operate on polynomials in nD.
Definition sparse_polynomial.hpp:63
#define VULPES_LIKELY(x)
Branch hint: x is expected to evaluate to true.
Definition macros.hpp:36
Definition monomial.hpp:46
constexpr std::array< T, 2 > getMonomial2D_Impl(std::size_t n)
Definition monomial.hpp:48
constexpr std::array< T, 3 > getMonomial3D_Impl(std::size_t n)
Definition monomial.hpp:71
Definition monomial.hpp:44
std::array< T, 2 > getMonomial2D(std::size_t n)
Computes the 2D monomial exponents for a given linear index.
Definition monomial.hpp:132
std::array< T, Dim > getMonomial(std::size_t n)
Computes the Dim-D monomial exponents for a given linear index.
Definition monomial.hpp:199
std::array< T, 3 > getMonomial3D(std::size_t n)
Computes the 3D monomial exponents for a given linear index.
Definition monomial.hpp:177
T getLinearIndex(const std::array< T, Dim > &monomial)
Computes the linear index given the exponents of the monomial.
Definition monomial.hpp:218
Definition monomial.hpp:39
V to(const U &src)
Converts a value of one pseudo-arithmetic type to another.
Definition vmath.hpp:321
constexpr U pow(U base, V exp, U result=U(1))
Computes the power of a number.
Definition vmath.hpp:171
Definition cell_manager.hpp:38