#include <Polynomial.h>
Inheritance diagram for Polynomial< T >:
Class Polynomial is based on the class std::vector
, and thus requires the Standard C++ Library (STL).
Coeficients are stored with lowest degree coeficent first, i.e.: the polynomial will be stored in the vector as .
The coeficient of degree 0 is always populated. That means P
[0] will always be valid for a polynomial P
.
Evaluations are made using Horner's recurence, or synthetic division.
Valid types for template parameter T are any floating point, or complex types which define operators +, -, *, /, and functions abs().
Several constructors are available, the following example shows them and their usage.
// constructor.cpp : show usage of Polynomial class constructors. #include <vector> #include <polynomial.h> int main() { // Initialize p(x) = 5x^4 + 3x^2 + x + 10 Polynomial<double> p(4, 10., 1., 3., 0., 5.); // Initialize q(x) = x^3 + x^2 - 5x + 2 Polynomial<double> q(3, 2., -5., 1., 1.); // Initialize r(x) = x^20 - 1 Polynomial<double> r; r.resize(21); r[20] = 1; r[0] = -1; // initialize u(x) = 1 Polynomial<double> u(1.); std::vector<float> v; Polynomial<float> t; // initialize v //... // create a polynomial from vector v t = v; return 0; }
Definition at line 86 of file Polynomial.h.
Public Member Functions | |
Construction | |
Polynomial (const T &a=T(0)) | |
Polynomial (const std::vector< T > &p) | |
Polynomial (unsigned n, T a0,...) | |
Methods | |
void | compact () |
int | degree () const |
int | degree () |
void | shift (int n) |
void | makeMonic () |
bool | isMonic () const |
void | getDerivative (Polynomial< T > &pp) const |
Evaluation | |
T | eval (const T &x) const |
T | evalAndDeflate (const T &a, Polynomial< T > &q) const |
T | evalAndDerive (const T &x, T &ppx) const |
Operators | |
const Polynomial< T > & | operator= (const std::vector< T > &q) |
const Polynomial< T > & | operator+= (const Polynomial< T > &q) |
const Polynomial< T > & | operator-= (const Polynomial< T > &q) |
const Polynomial< T > | operator *= (const Polynomial< T > &p) |
const Polynomial< T > & | operator *= (const T &f) |
Polynomial< T > & | operator/= (const T &s) |
Friends | |
Non-member operators | |
Polynomial< T > | operator+ (const Polynomial< T > &p, const Polynomial< T > &q) |
Polynomial< T > | operator- (const Polynomial< T > &p, const Polynomial< T > &q) |
Polynomial< T > | operator * (const Polynomial< T > &p, const Polynomial< T > &q) |
Polynomial< T > | operator * (const T &s, const Polynomial< T > &p) |
Polynomial< T > | operator/ (const Polynomial< T > &p, const T &s) |
|
default constructor. Creates an empty polynomial of degree zero, you can optionally specify a scalar value.
Definition at line 100 of file Polynomial.h. |
|
copy constructor. Creates an exact copy of Polynomial
Definition at line 113 of file Polynomial.h. |
|
initialization constructor. Initializes a polynomial of arbitrary size and coeficients. Care must be taken to properly cast the variable arguments to the same type as template type T.
Definition at line 127 of file Polynomial.h. |
|
compacts array. Compacts array size to match actual polynomial degree by enforcing the rule p[p.size() - 1] != 0. This function is called by other member functions to enforce the rule automatically whenever operations are performed on the polynomial.
Definition at line 168 of file Polynomial.h. |
|
returns degree of polynomial. Degree of the polynomial is the index of the highest degree non_null coeficient. This non-constant version of the function calls compact() before returning.
Definition at line 208 of file Polynomial.h. |
|
returns degree of polynomial. Degree of the polynomial is the index of the highest degree non_null coeficient.
Definition at line 188 of file Polynomial.h. |
|
evaluates polynomial value . Evaluates by Horner's recurence. No error checking is performed, so any validation for overflow, or underflow is the responsibility of the caller.
Definition at line 312 of file Polynomial.h. |
|
evaluates polynomial value p(a), and computes . Evaluates and polynomial at the same time by Horner's recurence. It can be shown that , which essentially means that is the remainder of . For the special case where is a root of (i.e. ), this operation is called deflation. That is, on exit will have all the roots of , except for the root . No error checking is performed, so any validation for overflow, or underflow is the responsibility of the caller.
Definition at line 339 of file Polynomial.h. |
|
evaluates and derivate at the same time. Evaluates efficiently and first derivative at the same time.
Definition at line 354 of file Polynomial.h. |
|
get derivative of . Computes the first derivative polynomial of polynomial .
Definition at line 287 of file Polynomial.h. |
|
indicates if polynomial is monic. Indicates if polynomial is monic. A polynomial is said monic if its highest degree coeficient equals 1.
Definition at line 273 of file Polynomial.h. |
|
makes polynomial monic. Makes polynomial monic. A polynomial is said monic if its highest degree coeficient equals 1.
Definition at line 257 of file Polynomial.h. |
|
in-place polynomial scaling. Polynomial scaling, performs the operation .
Definition at line 451 of file Polynomial.h. |
|
in-place polynomial multiply. Multiplies two polynomials by convolution. Performs the operation
Definition at line 433 of file Polynomial.h. |
|
in-place polynomial addition.
Performs the addition , storing the result stored in
Definition at line 397 of file Polynomial.h. |
|
in-place polynomial substraction. Performs the substraction and stores the resulting polynomial in
Definition at line 415 of file Polynomial.h. |
|
in-place polynomial scaling. Polynomial scaling, performs the operation .
Definition at line 468 of file Polynomial.h. |
|
make a copy of a polynomial. Make a copy of a polynomial.
Definition at line 379 of file Polynomial.h. |
|
shifts the coeficients. Shifts the polynomial coeficients left or right, effectively doing the operation . If n is negative, the remainder of the division is lost.
Definition at line 230 of file Polynomial.h. |
|
out-of_place polynomial scaling. Polynomial scaling.
Definition at line 548 of file Polynomial.h. |
|
out-of-place polynomial multiply. Multiplies two polynomials by convolution. It is advisable to use mul() instead, as it avoids the copying of the resulting polynomial off the stack.
Definition at line 530 of file Polynomial.h. |
|
out-of-place polynomial addition. Performs the addition . Calling directly the three parameters function add() is actually more efficient, as it will avoid an unnecessary copy of the polynomial.
Definition at line 489 of file Polynomial.h. |
|
out-of-place polynomial substraction. Performs the substraction .
Definition at line 509 of file Polynomial.h. |
|
out-of-place polynomial scaling. Polynomial scaling, performs the operation .
Definition at line 566 of file Polynomial.h. |