Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
monomial.hpp
Go to the documentation of this file.
1// ----------------------------------------------------------------------
2//
3// Copyright (C) 2024 - 2026 by the Vulpes authors.
4// This file is part of Vulpes.
5//
6// Vulpes is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// Vulpes is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14// General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with Vulpes. If not, see <http://www.gnu.org/licenses/>.
18//
19// ----------------------------------------------------------------------
20
21// Author: Matteo Caldana <matteo.caldana@polimi.it>
22// Author: Pedro Peixoto <ppeixoto@usp.br>
23
24#ifndef VULPES_POLYNOMIALS_MONOMIAL_HPP_
25#define VULPES_POLYNOMIALS_MONOMIAL_HPP_
26
27#include <algorithm>
28#include <array>
29#include <cassert>
30#include <cmath>
31#include <cstddef>
32#include <type_traits>
33
36
37namespace vulpes {
38
39namespace polynomial {
40template <std::size_t Dim, typename IndexType, typename CoefType, bool IsScaled>
42}
43
44namespace monomial {
45
46namespace internal {
47template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
48inline constexpr std::array<T, 2> getMonomial2D_Impl(std::size_t n) {
49 // float32 max safe int should be 16777215, ok for all reasonable polynomials
50 // degrees
51 using real_t = float;
52 // for ease of changing `real_t`
53 constexpr real_t one = 1;
54 constexpr real_t two = 2;
55 constexpr real_t eight = 8;
56 // solve the quadratic
57 const real_t tria_lvl_f =
58 (std::sqrt(one + eight * static_cast<real_t>(n)) - one) / two;
59 // compiler by default should trunc (we need trunc and nothing else!)
60 // possible packing in avx2: https://stackoverflow.com/q/41144668
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)}};
68}
69
70template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
71inline constexpr std::array<T, 3> getMonomial3D_Impl(std::size_t n) {
72 if (n == 0) {
73 return {0, 0, 0};
74 }
75 using real_t = float;
76 // for ease of changing `real_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);
83 // the cubic: pyramid_lvl(pyramid_lvl+1)(pyramid_lvl+2)/6 = n
84 // solve the cubic: initial guess
85 const real_t x = cbrt6 * std::cbrt(n_f) - one;
86 // solve the cubic: newton iter
87 const real_t pyramid_lvl_f =
88 (six * n_f + (two * x + three) * x * x) / (three * x * (x + two) + two);
89 // supposing compiler trunc the float
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);
94 assert(
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;
98 const auto ij = getMonomial2D_Impl<std::size_t>(lvl_id);
99 return {
100 {static_cast<T>(pyramid_lvl - ij[0] - ij[1]), static_cast<T>(ij[0]),
101 static_cast<T>(ij[1])}};
102}
103} // namespace internal
104
131template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
132inline std::array<T, 2> getMonomial2D(std::size_t n) {
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) {
139 }
140 return cache;
141 }();
142 if (VULPES_LIKELY(n < max_cache)) {
143 return cache[n];
144 } else {
146 }
147}
148
176template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
177inline std::array<T, 3> getMonomial3D(std::size_t n) {
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) {
185 }
186 return cache;
187 }();
188 if (VULPES_LIKELY(n < max_cache)) {
189 return cache[n];
190 } else {
192 }
193}
194
198template <std::size_t Dim, typename T>
199inline std::array<T, Dim> getMonomial(std::size_t n) {
200 if constexpr (Dim == 1) {
201 return {n};
202 } else if constexpr (Dim == 2) {
203 return getMonomial2D<T>(n);
204 } else if constexpr (Dim == 3) {
205 return getMonomial3D<T>(n);
206 } else {
207 static_assert(Dim * 0 && "Not implemented for dimension > 3");
208 }
209}
210
217template <std::size_t Dim, typename T>
218inline T getLinearIndex(const std::array<T, Dim>& monomial) {
219 if constexpr (Dim == 1) {
220 return monomial[0];
221 } else if constexpr (Dim == 2) {
222 const T ij = monomial[0] + monomial[1];
223 return ij * (ij + 1) / 2 + monomial[1];
224 } else if constexpr (Dim == 3) {
225 const T j_k = monomial[1] + monomial[2];
226 const T i_j_k = monomial[0] + j_k;
227 return i_j_k * (i_j_k + 1) * (i_j_k + 2) / 6 + j_k * (j_k + 1) / 2 +
228 monomial[2];
229 } else {
230 static_assert(Dim * 0 && "Not implemented for dimension > 3");
231 }
232}
233
235
236namespace internal {
237template <std::size_t Dim, typename IndexType, typename CoefType>
239 protected:
240 CoefType m_scaling;
241 std::array<IndexType, Dim> m_translation;
242};
244} // namespace internal
245
252template <std::size_t Dim, typename IndexType, typename CoefType, bool IsScaled>
253class Monomial : public std::conditional_t<
254 IsScaled,
255 internal::ScaledMonomial<Dim, IndexType, CoefType>,
256 internal::NonScaledMonomial> {
257 public:
261 constexpr Monomial() : m_coefficient(0) {
262 m_exponent.fill(0);
263 if constexpr (IsScaled) {
264 this->m_scaling = 0;
265 this->m_translation.fill(0);
266 }
267 }
268
272 explicit constexpr Monomial(const CoefType& coeff) : m_coefficient(coeff) {
273 m_exponent.fill(0);
274 if constexpr (IsScaled) {
275 this->m_scaling = 1;
276 this->m_translation.fill(0);
277 }
278 }
279
283 constexpr Monomial(
284 const std::array<IndexType, Dim>& exponent, const CoefType& coefficient)
285 : m_exponent(exponent), m_coefficient(coefficient) {
286 if constexpr (IsScaled) {
287 this->m_scaling = 1; // Default scaling to 1
288 this->m_translation.fill(0); // Default translation to 0
289 }
290 }
291
295 constexpr Monomial(
296 const std::array<IndexType, Dim>& exponent,
297 const CoefType& coefficient,
298 const CoefType& scaling,
299 const std::array<IndexType, Dim>& translation)
300 : m_exponent(exponent), m_coefficient(coefficient) {
301 if constexpr (IsScaled) {
302 this->m_scaling = scaling;
303 this->m_translation = translation;
304 }
305 }
306
310 constexpr CoefType evaluate(const Eigen::Vector<CoefType, Dim>& point) const {
311 return evaluate(point.data());
312 }
313
317 constexpr CoefType evaluate(const CoefType* point) const {
318 CoefType value = m_coefficient;
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;
323 }
324 value *= vmath::pow(x, m_exponent[i]);
325 }
326 return value;
327 }
328
335 constexpr Monomial derivative(std::size_t component) const {
336 assert(component < Dim);
337
338 if (m_exponent[component] == 0) {
339 return Monomial{};
340 }
341
342 std::array<IndexType, Dim> new_exponent = m_exponent;
343 new_exponent[component] -= 1;
344
345 CoefType new_coefficient = m_coefficient * m_exponent[component];
346
347 if constexpr (IsScaled) {
348 return Monomial(
349 new_exponent, new_coefficient, this->m_scaling, this->m_translation);
350 } else {
351 return Monomial(new_exponent, new_coefficient);
352 }
353 }
354
358 constexpr bool empty() const { return m_coefficient == CoefType(0); }
359
363 constexpr Eigen::Vector<Monomial, Dim> gradient() const {
364 Eigen::Vector<Monomial, Dim> grad;
365 for (std::size_t d = 0; d < Dim; ++d) {
366 grad[d] = this->derivative(d);
367 }
368 return grad;
369 }
370
374 template <typename U>
376 if constexpr (IsScaled) {
379 vmath::to<U>(this->m_scaling), this->m_translation);
380 } else {
383 }
384 }
385
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()};
393 }
394
398 friend std::ostream& operator<<(std::ostream& os, const Monomial& monomial) {
399 os << "Monomial(";
400 os << "Coefficient: " << monomial.m_coefficient << ", ";
401
402 os << "Exponents: [";
403 for (std::size_t i = 0; i < Dim; ++i) {
404 os << static_cast<std::size_t>(monomial.m_exponent[i]);
405 if (i < Dim - 1) {
406 os << ", ";
407 }
408 }
409 os << "]";
410
411 if constexpr (IsScaled) {
412 os << ", Scaling: " << monomial.m_scaling << ", Translation: [";
413 for (std::size_t i = 0; i < Dim; ++i) {
414 os << monomial.m_translation[i];
415 if (i < Dim - 1) {
416 os << ", ";
417 }
418 }
419 os << "]";
420 }
421
422 os << ")";
423 return os;
424 }
425
429 friend constexpr bool operator==(const Monomial& lhs, const Monomial& rhs) {
430 if constexpr (IsScaled) {
431 return lhs.m_coefficient == rhs.m_coefficient &&
432 lhs.m_exponent == rhs.m_exponent &&
433 lhs.m_scaling == rhs.m_scaling &&
434 lhs.m_translation == rhs.m_translation;
435 } else {
436 return lhs.m_coefficient == rhs.m_coefficient &&
437 lhs.m_exponent == rhs.m_exponent;
438 }
439 }
440
447 friend constexpr bool operator<(const Monomial& lhs, const Monomial& rhs) {
448 // Compare exponents lexicographically
449 if (lhs.m_exponent != rhs.m_exponent) {
450 return lhs.m_exponent < rhs.m_exponent;
451 }
452
453 // Compare coefficients
454 if (lhs.m_coefficient != rhs.m_coefficient) {
455 return lhs.m_coefficient < rhs.m_coefficient;
456 }
457
458 if constexpr (IsScaled) {
459 // Compare scaling factors
460 if (lhs.m_scaling != rhs.m_scaling) {
461 return lhs.m_scaling < rhs.m_scaling;
462 }
463
464 // Compare translation factors lexicographically
465 return lhs.m_translation < rhs.m_translation;
466 }
467
468 return false;
469 }
470
471 const auto& coef() const { return m_coefficient; }
472 const auto& exp() const { return m_exponent; }
473
474 protected:
475 std::array<IndexType, Dim> m_exponent;
477
478 friend class polynomial::SparsePolynomial<Dim, IndexType, CoefType, IsScaled>;
479};
480
481} // namespace monomial
482} // namespace vulpes
483
484#endif // VULPES_POLYNOMIALS_MONOMIAL_HPP_
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
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