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"
24 template<
typename Type = real>
27 std::vector<Type> coeff;
46 inline Type&
at(
int i) {
52 inline Type
get(
int i)
const {
70 template<
typename EvalType = Type>
71 inline EvalType
eval(EvalType x)
const {
79 for (
unsigned int i = 0; i < coeff.size(); ++i)
80 sum = coeff[coeff.size() - i - 1] + x *
sum;
90 template<
typename EvalType = Type>
100 for (
int i = coeff.size() - 1; i >= 0; --i) {
112 for (
unsigned int i = coeff.size() - 1; i >= 0; --i) {
131 r.coeff.resize(
max(coeff.size(), p.size()));
135 for (; i <
min(coeff.size(), p.size()); ++i)
136 r[i] = coeff[i] + p[i];
138 if (coeff.size() > p.size())
139 for (; i < coeff.size(); ++i)
142 for (; i < p.size(); ++i)
153 r.coeff.resize(
max(coeff.size(), p.size()));
157 for (; i <
min(r.
size(), p.size()); ++i)
158 r[i] = coeff[i] - p.get(i);
160 if (coeff.size() > p.size())
161 for (; i < coeff.size(); ++i)
164 for (; i < p.size(); ++i)
175 r.coeff.resize(this->
size() + p.
size() - 1);
177 for (
unsigned int i = 0; i <
size(); ++i) {
178 for (
unsigned int j = 0; j < p.size(); ++j) {
179 r[i + j] += coeff[i] * p.get(j);
193 if(d_order == 0 && d.
get(0) == 0) {
205 while(i < this_order) {
217 r.
get(r_order) / d.
get(d_order),
230 if(i == this_order) {
231 TH_MATH_ERROR(
"polynomial::operator/", i, NO_ALGO_CONVERGENCE);
244 for (
unsigned int i = 0; i <
size(); ++i)
260 for (
unsigned int i = 0; i <
size(); ++i)
271 if(coeff.size() < p.size())
272 coeff.resize(p.size(), Type(0));
274 for (
unsigned int i = 0; i <
min(
size(), p.size()); ++i)
275 coeff[i] += p.get(i);
285 if(coeff.size() < p.size())
286 coeff.resize(p.size(), Type(0));
288 for (
unsigned int i = 0; i <
min(coeff.size(), p.size()); ++i)
289 coeff[i] -= p.get(i);
299 r.coeff.resize(this->
size() + p.
size() - 1, Type(0));
301 for (
unsigned int i = 0; i < coeff.size(); ++i)
302 for (
unsigned int j = 0; j < p.size(); ++j)
303 r[i + j] += coeff[i] * p.get(j);
313 for (
unsigned int i = 0; i < coeff.size(); ++i)
322 return (*
this = (*
this / a));
334 for (
unsigned int i = 0; i < coeff.size(); ++i)
344 const unsigned int n =
min(other.
size(), this->size());
345 for (
unsigned int i = 0; i < n; ++i) {
351 for (
unsigned int i = 0; i <
size(); ++i) {
356 for (
unsigned int i = 0; i < other.
size(); ++i) {
369 return coeff.begin();
383 return coeff.cbegin();
401 TH_MATH_ERROR(
"quadratic_roots", order, IMPOSSIBLE_OPERATION);
405 const Type p = coeff[1] / coeff[2];
406 const Type q = coeff[0] / coeff[2];
417 z1 = -
sgn(p) * (
abs(p) / 2.0
434 const std::vector<Type>&
roots) {
439 for (
unsigned int i = 0; i <
roots.size(); ++i)
450 m.coeff = std::vector<Type>(order + 1, Type(0));
465 inline friend polynomial<Type>
operator-(
466 Type r,
const polynomial<Type>& z) {
471 inline friend polynomial<Type>
operator*(
472 Type r,
const polynomial<Type>& z) {
478 #ifndef THEORETICA_NO_PRINT
485 std::stringstream res;
487 const int sz = coeff.size();
489 for (
int i = sz - 1; i >= 0; --i) {
494 res << (coeff[i] >= 0 ?
"+ " :
"- ");
495 res <<
abs(coeff[i]);
498 res <<
"*" << unknown << exponentiation << i;
Complex number in algebraic form .
Definition: complex.h:26
A polynomial of arbitrary order.
Definition: polynomial.h:25
polynomial & operator-=(const polynomial &p)
Subtract a polynomial from this one.
Definition: polynomial.h:282
~polynomial()
Default destructor.
Definition: polynomial.h:42
polynomial & operator*=(Type a)
Multiply a polynomial by a scalar value.
Definition: polynomial.h:311
auto begin()
Get an iterator for the first coefficient of the polynomial.
Definition: polynomial.h:368
polynomial & operator+=(const polynomial &p)
Sum a polynomial to this one.
Definition: polynomial.h:268
polynomial(std::initializer_list< Type > l)
Initialize from an std::initializer_list.
Definition: polynomial.h:39
polynomial & operator/=(const polynomial &a)
Multiply a polynomial by a scalar value.
Definition: polynomial.h:321
polynomial(const std::vector< Type > &c)
Initialize from an std::vector.
Definition: polynomial.h:36
auto cbegin()
Get a const iterator for the first coefficient of the polynomial.
Definition: polynomial.h:382
const Type & operator[](unsigned int i) const
Return the nth order coefficient.
Definition: polynomial.h:58
static polynomial< Type > monomial(Type c, unsigned int order)
Returns a monomial of the given degree and coefficient.
Definition: polynomial.h:447
EvalType eval(EvalType x) const
Evaluate the polynomial using x as variable.
Definition: polynomial.h:71
polynomial operator/(const polynomial &d) const
Polynomial division.
Definition: polynomial.h:188
void trim()
Remove trailing zero coefficients.
Definition: polynomial.h:110
Type get(int i) const
Get the i-th by value.
Definition: polynomial.h:52
EvalType operator()(EvalType x) const
Evaluate the polynomial using x as variable.
Definition: polynomial.h:91
Type & operator[](unsigned int i)
Return the nth order coefficient.
Definition: polynomial.h:64
polynomial & operator/=(Type a)
Divide a polynomial by a scalar value.
Definition: polynomial.h:327
polynomial(Type a)
Initialize as a constant.
Definition: polynomial.h:33
auto end()
Get an iterator for the one plus last coefficient of the polynomial.
Definition: polynomial.h:375
bool operator==(const polynomial &other) const
Check whether two polynomials are equal.
Definition: polynomial.h:342
polynomial operator*(const polynomial &p) const
Multiply two polynomials.
Definition: polynomial.h:172
Type & at(int i)
Access i-th coefficient.
Definition: polynomial.h:46
polynomial()
Initialize as an empty polynomial.
Definition: polynomial.h:30
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:520
unsigned int find_order() const
Find the true order of the polynomial (ignoring trailing null coefficients)
Definition: polynomial.h:98
vec< complex<>, 2 > quadratic_roots() const
Compute the roots of a quadratic polynomial.
Definition: polynomial.h:395
auto cend()
Get a const iterator for the one plus last coefficient of the polynomial.
Definition: polynomial.h:389
size_t size() const
Get the number of coefficients.
Definition: polynomial.h:122
static polynomial< Type > from_roots(const std::vector< Type > &roots)
Construct a polynomial from its roots.
Definition: polynomial.h:433
polynomial operator+(const polynomial &p) const
Sum two polynomials.
Definition: polynomial.h:128
polynomial operator-(const polynomial &p) const
Subtract a polynomial from another.
Definition: polynomial.h:150
polynomial operator/(Type a) const
Divide a polynomial by a scalar.
Definition: polynomial.h:252
std::string to_string(const std::string &unknown="x", const std::string &exponentiation="^") const
Convert the polynomial to string representation.
Definition: polynomial.h:481
polynomial & operator*=(const polynomial &p)
Multiply two polynomials.
Definition: polynomial.h:296
polynomial operator*(Type a) const
Multiply a polynomial by a scalar.
Definition: polynomial.h:240
A statically allocated N-dimensional vector with elements of the given type.
Definition: vec.h:88
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compiling opti...
Definition: error.h:219
std::string string(size_t length)
Generate a random string made of human-readable ASCII characters.
Definition: random.h:102
Main namespace of the library which contains all functions and objects.
Definition: algebra.h:27
auto min(const Vector &X)
Finds the minimum value inside a dataset.
Definition: dataset.h:351
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:183
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:219
auto max(const Vector &X)
Finds the maximum value inside a dataset.
Definition: dataset.h:330
std::vector< real > roots(RealFunction f, real a, real b, real tolerance=OPTIMIZATION_TOL, real steps=10)
Find the roots of a function inside a given interval.
Definition: roots.h:462
constexpr real MACH_EPSILON
Machine epsilon for the real type.
Definition: constants.h:197
int sgn(real x)
Return the sign of x (1 if positive, -1 if negative, 0 if null)
Definition: real_analysis.h:259
dual2 square(dual2 x)
Return the square of a second order dual number.
Definition: dual2_functions.h:23
real nan()
Return a quiet NaN number in floating point representation.
Definition: error.h:54