Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
polynomial2d.hpp
Go to the documentation of this file.
1// ----------------------------------------------------------------------
2//
3// Copyright (C) 2024 - 2025 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
23#ifndef VULPES_POLYNOMIALS_POLYNOMIAL2D_HPP_
24#define VULPES_POLYNOMIALS_POLYNOMIAL2D_HPP_
25
26#include <cstddef>
27#include <ostream>
28#include <utility>
29#include <vector>
30
34
35namespace vulpes {
36namespace polynomial {
37
52template <typename T, bool IsSpaceP = true>
53class Polynomial2D : public PolynomialBase<T, 2, Polynomial2D<T, IsSpaceP>> {
54 public:
56 explicit Polynomial2D(std::size_t size) : m_coefficients(size) {}
57
58 constexpr std::size_t dim() const { return m_coefficients.dim(); }
59 constexpr std::size_t order() const { return m_coefficients.dim() - 1; }
60
61 const T& coef(std::size_t i, std::size_t j) const {
62 return m_coefficients(i, j);
63 }
64 T& coef(std::size_t i, std::size_t j) { return m_coefficients(i, j); }
65
66 auto& data() { return m_coefficients; }
67 const auto& data() const { return m_coefficients; }
68
75 Polynomial2D<T, IsSpaceP> derivative(std::size_t component) const {
76 assert(component < 2);
78 if (component == 0) {
79 for (std::size_t i = 1; i < dim(); ++i) {
80 const auto jmax = dim() - IsSpaceP * i;
81 for (std::size_t j = 0; j < jmax; ++j) {
82 result.coef(i - 1, j) = coef(i, j) * T(i);
83 }
84 }
85 } else {
86 for (std::size_t i = 0; i < dim(); ++i) {
87 const auto jmax = dim() - IsSpaceP * i;
88 for (std::size_t j = 1; j < jmax; ++j) {
89 result.coef(i, j - 1) = coef(i, j) * T(j);
90 }
91 }
92 }
93 return result;
94 }
95
102 Eigen::Vector<Polynomial2D<T, IsSpaceP>, 2> gradient() const {
103 return Eigen::Vector<Polynomial2D<T, IsSpaceP>, 2>{
104 derivative(0), derivative(1)};
105 }
106
110 T evaluate(const Eigen::Vector<T, 2>& x) const { return evaluate(x.data()); }
111
116 T evaluate(const T* x) const {
117 // WARNING: this is not thread safe
118 static std::vector<T> partial_eval;
119 if (partial_eval.size() < dim()) {
120 partial_eval.resize(dim());
121 }
122
123 // Step 1: Compute Q_i(y0) for each i using Horner's method in y
124 for (std::size_t i = 0; i < dim(); ++i) {
125 T Q_i = T(0);
126 const auto jmax = dim() - IsSpaceP * i;
127 for (std::size_t j = jmax - 1; j > 0; --j) {
128 Q_i = Q_i * x[1] + m_coefficients(i, j);
129 }
130 partial_eval[i] = Q_i * x[1] + m_coefficients(i, 0);
131 }
132
133 // Step 2: Compute P(x0, y0) using Horner's method in x
134 T eval = T(0);
135 for (std::size_t i = dim() - 1; i > 0; --i) {
136 eval = eval * x[0] + partial_eval[i];
137 }
138
139 return eval * x[0] + partial_eval[0];
140 }
141
143 const Eigen::Map<Eigen::Vector<T, -1>>& monomial_integrals) const {
144 assert(m_coefficients.size() <= monomial_integrals.size());
145 T integral = 0.0;
146 for (std::size_t i = 0; i < m_coefficients.size(); ++i) {
147 integral += m_coefficients.data()[i] * monomial_integrals[i];
148 }
149 return integral;
150 }
151
159 template <typename U>
162 std::transform(
163 m_coefficients.begin(), m_coefficients.end(), result.data().begin(),
165 return result;
166 }
167
175 const Polynomial2D<T, IsSpaceP>& other) {
176 return add_<true>(other);
177 }
178
186 const Polynomial2D<T, IsSpaceP>& other) {
187 return add_<false>(other);
188 }
189
197 const Polynomial2D<T, IsSpaceP>& other) {
198 std::size_t new_dim = dim() + other.dim();
199 decltype(m_coefficients) new_coeffs(new_dim);
200
201 for (std::size_t i = 0; i < dim(); ++i) {
202 const auto jmax = dim() - IsSpaceP * i;
203 for (std::size_t j = 0; j < jmax; ++j) {
204 for (std::size_t k = 0; k < other.dim(); ++k) {
205 const auto lmax = other.dim() - IsSpaceP * k;
206 for (std::size_t l = 0; l < lmax; ++l) {
207 new_coeffs(i + k, j + l) += coef(i, j) * other.coef(k, l);
208 }
209 }
210 }
211 }
212
213 m_coefficients = std::move(new_coeffs);
214 return *this;
215 }
216
224 std::transform(
225 m_coefficients.begin(), m_coefficients.end(), m_coefficients.begin(),
226 [&](const auto& val) { return val * scalar; });
227 return *this;
228 }
229
237 friend std::ostream& operator<<(std::ostream& os, const Polynomial2D& poly) {
238 for (std::size_t i = 0; i < poly.dim(); ++i) {
239 const auto jmax = poly.dim() - IsSpaceP * i;
240 for (std::size_t j = 0; j < jmax; ++j) {
241 if (poly.m_coefficients(i, j) != T(0)) {
242 if (i > 0 || j > 0) {
243 os << " + ";
244 }
245 os << poly.m_coefficients(i, j);
246 if (i > 0) {
247 os << "x^" << i;
248 }
249 if (j > 0) {
250 os << "y^" << j;
251 }
252 }
253 }
254 }
255 return os;
256 }
257
258 protected:
260
264 template <bool Add>
266 if (dim() < other.dim()) {
267 auto result_coef = other.m_coefficients;
268 if constexpr (!Add) {
269 // Negate coefficients for subtraction
270 std::transform(
271 result_coef.begin(), result_coef.end(), result_coef.begin(),
272 [](const auto& val) { return -val; });
273 }
274 for (std::size_t i = 0; i < dim(); ++i) {
275 const auto jmax = dim() - IsSpaceP * i;
276 for (std::size_t j = 0; j < jmax; ++j) {
277 result_coef(i, j) += coef(i, j);
278 }
279 }
280 m_coefficients = std::move(result_coef);
281 } else {
282 for (std::size_t i = 0; i < other.dim(); ++i) {
283 const auto jmax = other.dim() - IsSpaceP * i;
284 for (std::size_t j = 0; j < jmax; ++j) {
285 if constexpr (Add) {
286 m_coefficients(i, j) += other.coef(i, j);
287 } else {
288 m_coefficients(i, j) -= other.coef(i, j);
289 }
290 }
291 }
292 }
293 return *this;
294 }
295};
296
297} // namespace polynomial
298} // namespace vulpes
299
300#endif // VULPES_POLYNOMIALS_POLYNOMIAL2D_HPP_
A class that represents a triangular or square matrix, with optional support for tensor-like multi-di...
Definition tensor.hpp:60
Polynomial2D< T, IsSpaceP > & operator*=(const Polynomial2D< T, IsSpaceP > &other)
Multiplies the current polynomial by another polynomial in place.
Definition polynomial2d.hpp:196
Polynomial2D< T, IsSpaceP > & operator*=(const T &scalar)
Multiplies the polynomial by a scalar in place.
Definition polynomial2d.hpp:223
constexpr std::size_t order() const
Definition polynomial2d.hpp:59
T evaluate(const T *x) const
Evaluation of the polynomial with Horner's rule in a point passed as a raw buffer.
Definition polynomial2d.hpp:116
datastructure::TriMatrix< IsSpaceP, false, T, std::size_t > m_coefficients
Definition polynomial2d.hpp:259
Polynomial2D< T, IsSpaceP > & add_(const Polynomial2D< T, IsSpaceP > &other)
add or subtract two polynomials depending of template parameter
Definition polynomial2d.hpp:265
T evaluate(const Eigen::Vector< T, 2 > &x) const
Evaluation of the polynomial with Horner's rule.
Definition polynomial2d.hpp:110
friend std::ostream & operator<<(std::ostream &os, const Polynomial2D &poly)
Friend function to output a polynomial to an output stream.
Definition polynomial2d.hpp:237
auto & data()
Definition polynomial2d.hpp:66
T & coef(std::size_t i, std::size_t j)
Definition polynomial2d.hpp:64
Eigen::Vector< Polynomial2D< T, IsSpaceP >, 2 > gradient() const
Computes the gradient of the polynomial.
Definition polynomial2d.hpp:102
Polynomial2D< T, IsSpaceP > derivative(std::size_t component) const
Computes the derivative of the polynomial.
Definition polynomial2d.hpp:75
Polynomial2D< T, IsSpaceP > & operator-=(const Polynomial2D< T, IsSpaceP > &other)
Subtracts another polynomial from the current polynomial in place.
Definition polynomial2d.hpp:185
constexpr std::size_t dim() const
Definition polynomial2d.hpp:58
Polynomial2D(std::size_t size)
Definition polynomial2d.hpp:56
T integrate(const Eigen::Map< Eigen::Vector< T, -1 > > &monomial_integrals) const
Definition polynomial2d.hpp:142
const auto & data() const
Definition polynomial2d.hpp:67
Polynomial2D()
Definition polynomial2d.hpp:55
Polynomial2D< U, IsSpaceP > to() const
Converts the underlying type of the polynomial's coefficients to another type.
Definition polynomial2d.hpp:160
Polynomial2D< T, IsSpaceP > & operator+=(const Polynomial2D< T, IsSpaceP > &other)
Adds another polynomial to the current polynomial in place.
Definition polynomial2d.hpp:174
const T & coef(std::size_t i, std::size_t j) const
Definition polynomial2d.hpp:61
Base template class for a general polynomial.
Definition polynomial.hpp:46
Definition monomial.hpp:39
V to(const U &src)
Converts a value of one pseudo-arithmetic type to another.
Definition vmath.hpp:321
Definition cell_manager.hpp:38