6#ifndef THEORETICA_POLYNOMIAL_H
7#define THEORETICA_POLYNOMIAL_H
9#ifndef THEORETICA_NO_PRINT
14#include "../core/real_analysis.h"
15#include "../algebra/vec.h"
16#include "../complex/complex.h"
17#include "../complex/complex_analysis.h"
27 template<
typename Type = real>
47 template<
typename Vector, enable_vector<Vector> = true>
50 coeff.resize(c.size());
52 for (
size_t i = 0; i < c.size(); ++i)
63 template<
typename ...Args>
68 inline const Type&
at(
unsigned int i)
const {
92 template<
typename InputType = Type>
98 for (
unsigned int i = 0; i < coeff.size(); ++i)
99 sum = coeff[coeff.size() - i - 1] +
sum * x;
107 template<
typename InputType = Type>
117 for (
int i = coeff.size() - 1; i >= 0; --i) {
130 for (
size_t i = coeff.size() - 1; i > 0; --i) {
140 coeff[0] =
Type(0.0);
177 r.coeff.resize(
max(coeff.size(),
p.size()));
181 for (; i <
min(coeff.size(),
p.size()); ++i)
182 r[i] = coeff[i] +
p[i];
184 if (coeff.size() >
p.size())
185 for (; i < coeff.size(); ++i)
188 for (; i <
p.size(); ++i)
199 r.coeff.resize(
max(coeff.size(),
p.size()));
203 for (; i <
min(
r.size(),
p.size()); ++i)
204 r[i] = coeff[i] -
p[i];
206 if (coeff.size() >
p.size())
207 for (; i < coeff.size(); ++i)
210 for (; i <
p.size(); ++i)
221 r.coeff.resize(this->
size() + p.
size() - 1);
223 for (
unsigned int i = 0; i <
size(); ++i) {
224 for (
unsigned int j = 0;
j <
p.size(); ++
j) {
225 r[i +
j] += coeff[i] *
p[
j];
239 if(
d_order == 0 && d[0] == 0) {
254 const unsigned int r_order =
r.degree();
285 template<
typename ScalarType, disable_vector<ScalarType> = true>
292 template<
typename ScalarType, disable_vector<ScalarType> = true>
301 for (
unsigned int i = 0; i <
size(); ++i)
312 if(coeff.size() <
p.size())
313 coeff.resize(
p.size(),
Type(0));
315 for (
unsigned int i = 0; i <
min(
size(),
p.size()); ++i)
326 if(coeff.size() <
p.size())
327 coeff.resize(
p.size(),
Type(0));
329 for (
unsigned int i = 0; i <
min(coeff.size(),
p.size()); ++i)
342 for (
unsigned int i = 0; i < coeff.size(); ++i)
343 for (
unsigned int j = 0;
j <
p.size(); ++
j)
344 r[i +
j] += coeff[i] *
p[
j];
352 template<
typename ScalarType, disable_vector<ScalarType> = true>
355 for (
unsigned int i = 0; i < coeff.size(); ++i)
364 return (*
this = (*
this / a));
369 template<
typename ScalarType, disable_vector<ScalarType> = true>
377 for (
unsigned int i = 0; i < coeff.size(); ++i)
407 return coeff.begin();
421 return coeff.cbegin();
444 const Type a = coeff[2];
445 const Type b = coeff[1];
446 const Type c = coeff[0];
475 template<
typename Vector, enable_vector<Vector> = true>
482 for (
size_t i = 0; i <
roots.size(); i++)
514 template<
typename ScalarType, disable_vector<ScalarType> = true>
520#ifndef THEORETICA_NO_PRINT
524 const std::string&
unknown =
"x",
527 std::stringstream
res;
528 const int sz = coeff.size();
535 res << coeff[
sz - 1];
540 for (
int i =
sz - 2; i >= 0; --i) {
545 res << (coeff[i] >= 0 ?
"+ " :
"- ");
559 inline operator std::string() {
567 return out <<
obj.to_string();
Complex number in algebraic form .
Definition complex.h:26
A polynomial of arbitrary order with coefficients of a specified type.
Definition polynomial.h:28
polynomial & operator/=(const polynomial &a)
Divide a polynomial by a polynomial.
Definition polynomial.h:363
polynomial()=default
Default constructor.
polynomial & operator+=(const polynomial &p)
Sum a polynomial to this one.
Definition polynomial.h:309
auto begin()
Get an iterator for the first coefficient of the polynomial.
Definition polynomial.h:406
polynomial operator/(ScalarType a) const
Divide a polynomial by a scalar, provided it has coefficients supporting division.
Definition polynomial.h:293
polynomial(std::initializer_list< Type > l)
Initialize from an std::initializer_list.
Definition polynomial.h:58
void trim(real tolerance=MACH_EPSILON)
Remove trailing zero coefficients.
Definition polynomial.h:127
polynomial operator*(ScalarType a) const
Multiply a polynomial by a scalar.
Definition polynomial.h:286
polynomial(const Vector &c)
Initialize from a vector of coefficients where the n-th order coefficient is given by the n-th elemen...
Definition polynomial.h:48
polynomial operator/(const polynomial &d) const
Polynomial division.
Definition polynomial.h:234
void resize(size_t sz)
Change the number of coefficients of the polynomial.
Definition polynomial.h:168
vec< Type > & coeffs()
Get the coefficients of the polynomial as reference.
Definition polynomial.h:156
polynomial & operator*=(const polynomial &p)
Multiply two polynomials.
Definition polynomial.h:337
const vec< Type > & coeffs() const
Get the coefficients of the polynomial as const reference.
Definition polynomial.h:150
Type & at(unsigned int i)
Access i-th coefficient by reference, with bound checking.
Definition polynomial.h:73
unsigned int degree(real tolerance=MACH_EPSILON) const
Find the true order of the polynomial (ignoring trailing null coefficients)
Definition polynomial.h:115
polynomial(Type a)
Initialize as a constant.
Definition polynomial.h:41
auto end()
Get an iterator for the one plus last coefficient of the polynomial.
Definition polynomial.h:413
vec< complex<>, 2 > quadratic_roots() const
Compute the roots of a quadratic polynomial.
Definition polynomial.h:433
polynomial & operator*=(ScalarType a)
Multiply a polynomial by a scalar value.
Definition polynomial.h:353
friend std::ostream & operator<<(std::ostream &out, const polynomial &obj)
Stream the polynomial in string representation to an output stream (std::ostream)
Definition polynomial.h:566
polynomial & operator-=(const polynomial &p)
Subtract a polynomial from this one.
Definition polynomial.h:323
const Type & operator[](unsigned int i) const
Get the n-th order coefficient by constant reference.
Definition polynomial.h:79
static polynomial< Type > monomial(Type c, unsigned int order)
Returns a monomial of the given degree and coefficient.
Definition polynomial.h:490
bool operator==(const polynomial &other) const
Check whether two polynomials are equal.
Definition polynomial.h:385
polynomial operator*(const polynomial &p) const
Multiply two polynomials.
Definition polynomial.h:218
const Type & at(unsigned int i) const
Get i-th coefficient by constant reference, with bound checking.
Definition polynomial.h:68
Type & operator[](unsigned int i)
Get the n-th order coefficient by reference.
Definition polynomial.h:85
Type eval(InputType x) const
Evaluate the polynomial for a given value.
Definition polynomial.h:93
polynomial & operator/=(ScalarType a)
Divide a polynomial by a scalar value, provided it has coefficients supporting division.
Definition polynomial.h:370
polynomial(Args... args)
Construct a polynomial from its coefficients, where the n-th order coefficient is given by the n-th e...
Definition polynomial.h:64
size_t size() const
Get the number of coefficients.
Definition polynomial.h:162
auto cend() const
Get a const iterator for the one plus last coefficient of the polynomial.
Definition polynomial.h:427
static polynomial< Type > from_roots(const Vector &roots)
Construct a polynomial from its roots Uses numerically stable pairwise multiplication with sorted roo...
Definition polynomial.h:476
polynomial operator+(const polynomial &p) const
Sum two polynomials.
Definition polynomial.h:174
polynomial operator-(const polynomial &p) const
Subtract a polynomial from another.
Definition polynomial.h:196
std::string to_string(const std::string &unknown="x", const std::string &exponentiation="^") const
Convert the polynomial to string representation.
Definition polynomial.h:523
auto cbegin() const
Get a const iterator for the first coefficient of the polynomial.
Definition polynomial.h:420
Type operator()(InputType x) const
Evaluate the polynomial for a given value.
Definition polynomial.h:108
A statically allocated N-dimensional vector with elements of the given type.
Definition vec.h:92
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compilation op...
Definition error.h:219
Main namespace of the library which contains all functions and objects.
Definition algebra.h:27
double real
A real number, defined as a floating point type.
Definition constants.h:207
auto min(const Vector &X)
Finds the minimum value inside a dataset.
Definition dataset.h:347
dual2 sqrt(dual2 x)
Compute the square root of a second order dual number.
Definition dual2_functions.h:54
dual2 abs(dual2 x)
Compute the absolute value of a second order dual number.
Definition dual2_functions.h:242
Vector make_error()
Create a vector representing an error state, with all NaN values.
Definition algebra.h:103
auto sum(const Vector &X)
Compute the sum of a vector of real values using pairwise summation to reduce round-off error.
Definition dataset.h:215
auto max(const Vector &X)
Finds the maximum value inside a dataset.
Definition dataset.h:326
Vector roots(RealFunction f, real a, real b, real tol=OPTIMIZATION_TOL, real steps=10)
Find the roots of a univariate real function inside a given interval, by first searching for candidat...
Definition roots.h:649
TH_CONSTEXPR real nan()
Return a quiet NaN number in floating point representation.
Definition error.h:74
@ ImpossibleOperation
Mathematically impossible operation.
@ NoConvergence
Algorithm did not converge.
@ DivByZero
Division by zero.
constexpr real MACH_EPSILON
Machine epsilon for the real type.
Definition constants.h:216