Theoretica
Scientific Computing
Loading...
Searching...
No Matches
theoretica::polynomial< Type > Class Template Reference

A polynomial of arbitrary order with coefficients of a specified type. More...

#include <polynomial.h>

Public Member Functions

 polynomial ()=default
 Default constructor.
 
 polynomial (Type a)
 Initialize as a constant.
 
template<typename Vector , enable_vector< Vector > = true>
 polynomial (const Vector &c)
 Initialize from a vector of coefficients where the n-th order coefficient is given by the n-th element of the vector.
 
 polynomial (std::initializer_list< Type > l)
 Initialize from an std::initializer_list.
 
template<typename ... Args>
 polynomial (Args... args)
 Construct a polynomial from its coefficients, where the n-th order coefficient is given by the n-th element of the vector.
 
const Typeat (unsigned int i) const
 Get i-th coefficient by constant reference, with bound checking.
 
Typeat (unsigned int i)
 Access i-th coefficient by reference, with bound checking.
 
const Typeoperator[] (unsigned int i) const
 Get the n-th order coefficient by constant reference.
 
Typeoperator[] (unsigned int i)
 Get the n-th order coefficient by reference.
 
template<typename InputType = Type>
Type eval (InputType x) const
 Evaluate the polynomial for a given value.
 
template<typename InputType = Type>
Type operator() (InputType x) const
 Evaluate the polynomial for a given value.
 
unsigned int degree (real tolerance=MACH_EPSILON) const
 Find the true order of the polynomial (ignoring trailing null coefficients)
 
void trim (real tolerance=MACH_EPSILON)
 Remove trailing zero coefficients.
 
const vec< Type > & coeffs () const
 Get the coefficients of the polynomial as const reference.
 
vec< Type > & coeffs ()
 Get the coefficients of the polynomial as reference.
 
size_t size () const
 Get the number of coefficients.
 
void resize (size_t sz)
 Change the number of coefficients of the polynomial.
 
polynomial operator+ (const polynomial &p) const
 Sum two polynomials.
 
polynomial operator- (const polynomial &p) const
 Subtract a polynomial from another.
 
polynomial operator* (const polynomial &p) const
 Multiply two polynomials.
 
polynomial operator/ (const polynomial &d) const
 Polynomial division.
 
template<typename ScalarType , disable_vector< ScalarType > = true>
polynomial operator* (ScalarType a) const
 Multiply a polynomial by a scalar.
 
template<typename ScalarType , disable_vector< ScalarType > = true>
polynomial operator/ (ScalarType a) const
 Divide a polynomial by a scalar, provided it has coefficients supporting division.
 
polynomialoperator+= (const polynomial &p)
 Sum a polynomial to this one.
 
polynomialoperator-= (const polynomial &p)
 Subtract a polynomial from this one.
 
polynomialoperator*= (const polynomial &p)
 Multiply two polynomials.
 
template<typename ScalarType , disable_vector< ScalarType > = true>
polynomialoperator*= (ScalarType a)
 Multiply a polynomial by a scalar value.
 
polynomialoperator/= (const polynomial &a)
 Divide a polynomial by a polynomial.
 
template<typename ScalarType , disable_vector< ScalarType > = true>
polynomialoperator/= (ScalarType a)
 Divide a polynomial by a scalar value, provided it has coefficients supporting division.
 
bool operator== (const polynomial &other) const
 Check whether two polynomials are equal.
 
auto begin ()
 Get an iterator for the first coefficient of the polynomial.
 
auto end ()
 Get an iterator for the one plus last coefficient of the polynomial.
 
auto cbegin () const
 Get a const iterator for the first coefficient of the polynomial.
 
auto cend () const
 Get a const iterator for the one plus last coefficient of the polynomial.
 
vec< complex<>, 2 > quadratic_roots () const
 Compute the roots of a quadratic polynomial.
 
std::string to_string (const std::string &unknown="x", const std::string &exponentiation="^") const
 Convert the polynomial to string representation.
 
 operator std::string ()
 Convert the polynomial to string representation.
 

Static Public Member Functions

template<typename Vector , enable_vector< Vector > = true>
static polynomial< Typefrom_roots (const Vector &roots)
 Construct a polynomial from its roots Uses numerically stable pairwise multiplication with sorted roots.
 
static polynomial< Typemonomial (Type c, unsigned int order)
 Returns a monomial of the given degree and coefficient.
 

Friends

std::ostream & operator<< (std::ostream &out, const polynomial &obj)
 Stream the polynomial in string representation to an output stream (std::ostream)
 

Detailed Description

template<typename Type = real>
class theoretica::polynomial< Type >

A polynomial of arbitrary order with coefficients of a specified type.

The n-th order coefficient is accessed with p[n] and corresponds to the term \(x^n\).

Constructor & Destructor Documentation

◆ polynomial()

template<typename Type = real>
template<typename Vector , enable_vector< Vector > = true>
theoretica::polynomial< Type >::polynomial ( const Vector c)
inlineexplicit

Initialize from a vector of coefficients where the n-th order coefficient is given by the n-th element of the vector.

The constructor may be used explicitly, as polynomial(v).

Member Function Documentation

◆ eval()

template<typename Type = real>
template<typename InputType = Type>
Type theoretica::polynomial< Type >::eval ( InputType  x) const
inline

Evaluate the polynomial for a given value.

Horner's method is used for efficient evaluation.

◆ operator()()

template<typename Type = real>
template<typename InputType = Type>
Type theoretica::polynomial< Type >::operator() ( InputType  x) const
inline

Evaluate the polynomial for a given value.

Horner's method is used for efficient evaluation.


The documentation for this class was generated from the following file: