Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
polynomial1d.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_POLYNOMIAL1D_HPP_
24#define VULPES_POLYNOMIALS_POLYNOMIAL1D_HPP_
25
26#include <cstddef>
27#include <ostream>
28#include <vector>
29
32
33namespace vulpes {
34namespace polynomial {
35
43template <typename T>
44class Polynomial : public PolynomialBase<T, 1, Polynomial<T>> {
45 public:
49 explicit Polynomial() : m_coefficients{0} {}
50
54 explicit Polynomial(std::size_t size) : m_coefficients(size) {}
55
62 explicit Polynomial(const std::vector<T>& coeffs) : m_coefficients(coeffs) {
63 trim();
64 }
65
66 auto& data() { return m_coefficients; }
67 const auto& data() const { return m_coefficients; }
68
74 std::size_t degree() const { return m_coefficients.size() - 1; }
75
81 constexpr std::size_t size() const { return m_coefficients.size(); }
82
89 const T& operator[](std::size_t i) const { return m_coefficients[i]; }
90
97 const T& coef(std::size_t i) const { return m_coefficients[i]; }
98
105 T& coef(std::size_t i) { return m_coefficients[i]; }
106
114 Polynomial<T> der_poly = *this;
115 der_poly.derivative_();
116 return der_poly;
117 }
118
125 Polynomial<T> derivative(std::size_t component) const {
126 assert(component == 0);
127 return derivative();
128 }
129
136 Eigen::Vector<Polynomial<T>, 1> gradient() const {
137 return Eigen::Vector<Polynomial<T>, 1>{derivative()};
138 }
139
145 void derivative_() {
146 if (m_coefficients.size() == 1) {
147 m_coefficients[0] = T(0);
148 return;
149 }
150 for (std::size_t i = 1; i < m_coefficients.size(); ++i) {
151 m_coefficients[i - 1] = m_coefficients[i] * T(i);
152 }
153 m_coefficients.resize(m_coefficients.size() - 1);
154 }
155
163 T integrate(const T& left, const T& right) const {
164 const auto primitive_inst = primitive();
165 return primitive_inst.evaluate(right) - primitive_inst.evaluate(left);
166 }
167
175 Polynomial<T> primitive(const T& c = T(0)) const {
176 Polynomial<T> prim_poly = *this;
177 prim_poly.primitive_(c);
178 return prim_poly;
179 }
180
192 void primitive_(const T& ordinate = T(0), const T& target_value = T(0)) {
193 static_assert(
194 !std::is_integral_v<T>,
195 "Primitive is not well defined for integral polynomial types");
196 m_coefficients.push_back(m_coefficients.back() / m_coefficients.size());
197 for (std::size_t i = m_coefficients.size() - 2; i > 0; --i) {
198 m_coefficients[i] = m_coefficients[i - 1] / T(i);
199 }
200 const auto ordinate_value = ordinate == T(0) ? T(0) : evaluate(ordinate);
201 m_coefficients[0] = target_value - ordinate_value;
202 }
203
222 T evaluate(const T& x) const {
223 if (m_coefficients.size() == 1) {
224 return m_coefficients[0];
225 }
226 T result = m_coefficients.back();
227 for (std::size_t i = m_coefficients.size() - 2; i > 0; --i) {
228 result = result * x + m_coefficients[i];
229 }
230 result = result * x + m_coefficients[0];
231 return result;
232 }
233
234 T evaluate(const Eigen::Vector<T, 1>& x) const { return evaluate(x[0]); }
235 T evaluate(const T* x) const { return evaluate(x[0]); }
236
238 const Eigen::Map<Eigen::Vector<T, -1>>& monomial_integrals) const {
239 assert(m_coefficients.size() <= monomial_integrals.size());
240 T integral = 0.0;
241 for (std::size_t i = 0; i < m_coefficients.size(); ++i) {
242 integral += m_coefficients[i] * monomial_integrals[i];
243 }
244 return integral;
245 }
246
254 if (m_coefficients.size() == 1) {
255 return poly;
256 }
257 Polynomial<T> result({m_coefficients.back()});
258 for (std::size_t i = m_coefficients.size() - 2; i > 0; --i) {
259 result = result * poly + m_coefficients[i];
260 }
261 result = result * poly + m_coefficients[0];
262 return result;
263 }
264
272 template <typename U>
274 std::vector<U> new_coefficients;
275 for (const auto& curr_coef : m_coefficients) {
276 new_coefficients.push_back(vmath::to<U>(curr_coef));
277 }
278 return Polynomial<U>(new_coefficients);
279 }
280
288 if (other.m_coefficients.size() > m_coefficients.size()) {
289 m_coefficients.resize(other.m_coefficients.size(), T(0));
290 }
291
292 for (std::size_t i = 0; i < other.m_coefficients.size(); ++i) {
293 m_coefficients[i] += other.m_coefficients[i];
294 }
295
296 trim();
297 return *this;
298 }
299
307 if (other.m_coefficients.size() > m_coefficients.size()) {
308 m_coefficients.resize(other.m_coefficients.size(), T(0));
309 }
310
311 for (std::size_t i = 0; i < other.m_coefficients.size(); ++i) {
312 m_coefficients[i] -= other.m_coefficients[i];
313 }
314
315 trim();
316 return *this;
317 }
318
326 std::vector<T> result(
327 m_coefficients.size() + other.m_coefficients.size() - 1, T(0));
328
329 for (std::size_t i = 0; i < m_coefficients.size(); ++i) {
330 for (std::size_t j = 0; j < other.m_coefficients.size(); ++j) {
331 result[i + j] += m_coefficients[i] * other.m_coefficients[j];
332 }
333 }
334
335 m_coefficients = result;
336 trim();
337 return *this;
338 }
339
346 Polynomial<T>& operator*=(const T& scalar) {
347 for (std::size_t i = 0; i < m_coefficients.size(); ++i) {
348 m_coefficients[i] *= scalar;
349 }
350 return *this;
351 }
352
362 Polynomial<T>& operator/=(const T& scalar) {
363 static_assert(
364 !std::is_integral_v<T>,
365 "Division is not well defined for integral polynomial types");
366 for (std::size_t i = 0; i < m_coefficients.size(); ++i) {
367 m_coefficients[i] /= scalar;
368 }
369 return *this;
370 }
371
383 static void divide(
384 const Polynomial<T>& divisor,
385 Polynomial<T>& quotient,
386 Polynomial<T>& remainder) {
387 static_assert(
388 !std::is_integral_v<T>,
389 "Division is not well defined for integral polynomial types");
390 assert(divisor.m_coefficients.size() > 0);
391
392 while (remainder.degree() >= divisor.degree() &&
393 remainder.m_coefficients.size() > 0) {
394 const std::size_t degree_diff = remainder.degree() - divisor.degree();
395 T leading_coeff =
396 remainder.m_coefficients.back() / divisor.m_coefficients.back();
397
398 std::vector<T> term(degree_diff + 1, T(0));
399 term.back() = leading_coeff;
400
401 Polynomial<T> term_poly(term);
402 quotient += term_poly;
403 remainder -= (term_poly * divisor);
404 remainder.trim();
405 }
406 }
407
417 Polynomial<T> quotient;
418 Polynomial<T>::divide(divisor, quotient, *this);
419 return *this;
420 }
421
429 Polynomial<T> remainder = *this;
430 m_coefficients.resize(1);
431 m_coefficients[0] = T(0);
432 Polynomial<T>::divide(divisor, *this, remainder);
433 return *this;
434 }
435
446 lhs /= rhs;
447 return lhs;
448 }
449
460 lhs %= rhs;
461 return lhs;
462 }
463
471 friend bool operator==(const Polynomial<T>& lhs, const Polynomial<T>& rhs) {
472 return lhs.m_coefficients == rhs.m_coefficients;
473 }
474
482 friend std::ostream& operator<<(std::ostream& os, const Polynomial<T>& poly) {
483 for (std::size_t j = 0; j <= poly.degree(); ++j) {
484 const auto i = poly.degree() - j;
485 if (poly[i] != T(0)) {
486 if (i < poly.degree()) {
487 if (poly[i] > 0) {
488 os << " +";
489 } else {
490 os << " ";
491 }
492 }
493 if (i == 0 || poly[i] != T(1)) {
494 os << poly[i];
495 }
496 if (i > 0) {
497 os << "x";
498 }
499 if (i > 1) {
500 os << "^" << i;
501 }
502 }
503 }
504 return os;
505 }
506
507 protected:
511 std::vector<T> m_coefficients;
512
520 void trim() {
521 while (m_coefficients.size() > 1 && m_coefficients.back() == T(0)) {
522 m_coefficients.pop_back();
523 }
524 if (m_coefficients.size() == 0) {
525 m_coefficients.push_back(T(0));
526 }
527 }
528};
529
547template <typename T>
548std::vector<Polynomial<T>> buildLegendrePolynomials(std::size_t deg) {
549 std::vector<Polynomial<T>> legendre_polynomials;
550 const Polynomial<T> one(std::vector<T>{1});
551 legendre_polynomials.push_back(one);
552 if (deg == 0) {
553 return legendre_polynomials;
554 }
555 legendre_polynomials.push_back(Polynomial<T>({0, 1}));
556
557 const Polynomial<T> X({0, 1});
558 for (std::size_t n = 1; n < deg + 1; n++) {
559 const auto& Pn_1 = legendre_polynomials[n - 1];
560 const auto& Pn = legendre_polynomials[n];
561 T coef1 = 2 * n + 1;
562 T coef2 = n;
563 legendre_polynomials.push_back(coef1 * X * Pn - coef2 * Pn_1);
564 legendre_polynomials.back() /= T(n + 1);
565 }
566 return legendre_polynomials;
567}
568
569} // namespace polynomial
570} // namespace vulpes
571
572#endif // VULPES_POLYNOMIALS_POLYNOMIAL1D_HPP_
Base template class for a general polynomial.
Definition polynomial.hpp:46
Template class to represent and operate on polynomials.
Definition polynomial1d.hpp:44
friend Polynomial< T > operator%(Polynomial< T > lhs, const Polynomial< T > &rhs)
Friend function to compute the remainder of dividing one polynomial by another.
Definition polynomial1d.hpp:459
Polynomial< T > & operator-=(const Polynomial< T > &other)
Subtracts another polynomial from the current polynomial in place.
Definition polynomial1d.hpp:306
friend Polynomial< T > operator/(Polynomial< T > lhs, const Polynomial< T > &rhs)
Friend function to compute the remainder of dividing one polynomial by another.
Definition polynomial1d.hpp:445
T evaluate(const T *x) const
Definition polynomial1d.hpp:235
T evaluate(const T &x) const
Evaluates the polynomial at a given point using Horner's method.
Definition polynomial1d.hpp:222
Polynomial< T > & operator*=(const T &scalar)
Multiplies the polynomial by a scalar in place.
Definition polynomial1d.hpp:346
Polynomial< U > to() const
Converts the underlying type of the polynomial's coefficients to another type.
Definition polynomial1d.hpp:273
Eigen::Vector< Polynomial< T >, 1 > gradient() const
Computes the derivative of the polynomial.
Definition polynomial1d.hpp:136
Polynomial< T > & operator+=(const Polynomial< T > &other)
Adds another polynomial to the current polynomial in place.
Definition polynomial1d.hpp:287
Polynomial(const std::vector< T > &coeffs)
Constructs a polynomial with the given coefficients.
Definition polynomial1d.hpp:62
Polynomial< T > primitive(const T &c=T(0)) const
Computes the indefinite integral (primitive) of the polynomial.
Definition polynomial1d.hpp:175
T evaluate(const Eigen::Vector< T, 1 > &x) const
Definition polynomial1d.hpp:234
Polynomial< T > substitute(const Polynomial< T > &poly) const
Substitutes another polynomial into the current polynomial.
Definition polynomial1d.hpp:253
void trim()
Removes leading zeros from the coefficients vector.
Definition polynomial1d.hpp:520
T & coef(std::size_t i)
Accesses the coefficient of the x^i term.
Definition polynomial1d.hpp:105
friend bool operator==(const Polynomial< T > &lhs, const Polynomial< T > &rhs)
Compares two polynomials for equality.
Definition polynomial1d.hpp:471
void primitive_(const T &ordinate=T(0), const T &target_value=T(0))
Computes the indefinite integral (primitive) of the polynomial in place.
Definition polynomial1d.hpp:192
auto & data()
Definition polynomial1d.hpp:66
Polynomial< T > derivative(std::size_t component) const
Computes the derivative of the polynomial.
Definition polynomial1d.hpp:125
constexpr std::size_t size() const
Returns the number of the monomials.
Definition polynomial1d.hpp:81
friend std::ostream & operator<<(std::ostream &os, const Polynomial< T > &poly)
Friend function to output a polynomial to an output stream.
Definition polynomial1d.hpp:482
void derivative_()
Computes the derivative of the polynomial in place.
Definition polynomial1d.hpp:145
const T & coef(std::size_t i) const
Accesses the coefficient of the x^i term.
Definition polynomial1d.hpp:97
const T & operator[](std::size_t i) const
Accesses the coefficient of the x^i term.
Definition polynomial1d.hpp:89
std::vector< T > m_coefficients
Vector storing the coefficients of the polynomial.
Definition polynomial1d.hpp:511
static void divide(const Polynomial< T > &divisor, Polynomial< T > &quotient, Polynomial< T > &remainder)
Divides one polynomial by another, updating the quotient and remainder.
Definition polynomial1d.hpp:383
Polynomial< T > & operator/=(const T &scalar)
Divides the polynomial by a scalar in place.
Definition polynomial1d.hpp:362
Polynomial< T > derivative() const
Computes the derivative of the polynomial.
Definition polynomial1d.hpp:113
Polynomial(std::size_t size)
Constructor. Constructs a polynomial with a given size.
Definition polynomial1d.hpp:54
Polynomial< T > & operator/=(const Polynomial< T > &divisor)
Divides the current polynomial by another polynomial in place.
Definition polynomial1d.hpp:428
T integrate(const T &left, const T &right) const
Computes the definite integral of the polynomial between two bounds.
Definition polynomial1d.hpp:163
Polynomial()
Default constructor. Constructs a polynomial with a zero term.
Definition polynomial1d.hpp:49
T integrate(const Eigen::Map< Eigen::Vector< T, -1 > > &monomial_integrals) const
Definition polynomial1d.hpp:237
std::size_t degree() const
Returns the degree of the polynomial.
Definition polynomial1d.hpp:74
Polynomial< T > & operator*=(const Polynomial< T > &other)
Multiplies the current polynomial by another polynomial in place.
Definition polynomial1d.hpp:325
Polynomial< T > & operator%=(const Polynomial< T > &divisor)
Computes the remainder of dividing the current polynomial by another polynomial in place.
Definition polynomial1d.hpp:416
const auto & data() const
Definition polynomial1d.hpp:67
Definition monomial.hpp:39
std::vector< Polynomial< T > > buildLegendrePolynomials(std::size_t deg)
Builds a set of Legendre polynomials up to a given degree.
Definition polynomial1d.hpp:548
V to(const U &src)
Converts a value of one pseudo-arithmetic type to another.
Definition vmath.hpp:321
Definition cell_manager.hpp:38