1#ifndef UTIL_POLYNOMIAL_H
2#define UTIL_POLYNOMIAL_H
11#include <util/containers/GArray.h>
12#include <util/math/Rational.h>
13#include <util/math/Binomial.h>
14#include <util/containers/DArray.h>
28 template <
typename T = Rational>
96 using GArray<T>::operator [];
261 template <
typename T>
269 template <
typename T>
280 template <
typename T>
286 for (
int i = 0; i < coeffs.
capacity(); ++i) {
295 template <
typename T>
300 if (other.size() > 0) {
301 for (
int i = 0; i < other.size(); ++i) {
310 template <
typename T>
311 template <
typename U>
316 if (other.size() >= 0) {
317 if (other.size() > capacity()) {
321 for (
int i = 0; i < other.size(); ++i) {
332 template <
typename T>
340 template <
typename T>
348 template <
typename T>
352 int min = a.size() > size() ? size() : a.size();
354 for (
int i = 0; i < min; ++i) {
358 if (a.size() > size()) {
360 for (
int i = size(); i < a.size(); ++i) {
371 template <
typename T>
385 template <
typename T>
389 int min = a.size() > size() ? size() : a.size();
391 for (
int i = 0; i < min; ++i) {
395 if (a.size() > size()) {
397 for (
int i = size(); i < a.size(); ++i) {
408 template <
typename T>
422 template <
typename T>
427 for (
int i = 0; i < size(); ++i) {
437 template <
typename T>
442 for (
int i = 0; i < size(); ++i) {
452 template <
typename T>
464 int n = size() + a.size() - 1;
471 if (n > capacity()) {
478 for (i = 0; i < n; ++i) {
484 for (i = 0; i < a.size(); ++i) {
485 for (j = 0; j < b.
size(); ++j) {
488 (*this)[k] += a[i]*b[j];
500 template <
typename T>
512 for (
int i = 0; i < size(); ++i) {
513 coeffs[i+1] = (*this)[i];
514 coeffs[i+1] /= T(i+1);
526 template <
typename T>
541 for (
int i = 1; i < size(); ++i) {
542 coeffs[i-1] = (*this)[i];
555 template <
typename T>
562 for (
int i = 0; i < b.size(); ++i) {
574 template <
typename T>
580 int degree = size() - 1;
585 for (n = 1; n <= degree; ++n) {
587 for (m = 1; m <= n; ++m) {
600 template <
typename T>
604 int degree = size() - 1;
605 T value = (*this)[degree];
607 for (
int i = degree-1; i >= 0; --i) {
621 template <
typename T>
625 int degree = size()-1;
626 double value = (double)(*
this)[degree];
628 for (
int i = degree-1; i >= 0; --i) {
630 value += (double)(*
this)[i];
644 template <
typename T>
652 for (
int i = 0; i <= power; ++i) {
653 a.GArray<T>::append(zero);
674 template <
typename T>
677 if (a.size() != b.size())
return false;
679 for (
int i = 0; i < a.size(); ++i) {
680 if (a[i] != b[i])
return false;
693 template <
typename T>
695 {
return !(a == b); }
703 template <
typename T>
709 for (
int i = 0; i < a.size(); ++i) {
716 template <
typename T>
717 std::ostream&
operator << (std::ostream& out, Polynomial<T>
const & p)
721 for (
int i = 0; i < p.size(); ++i) {
Array container class template.
int capacity() const
Return allocated size.
static void setup(int nMax)
Precompute all combinations C(n, m) up to n = nMax.
static int coeff(int n, int m)
Return coefficient "n choose m", or C(n, m) = n!/(m!(n-m)!).
Dynamically allocatable contiguous array template.
void allocate(int capacity)
Allocate the underlying C array.
An automatically growable array, analogous to a std::vector.
int size() const
Return logical size of this array (i.e., current number of elements).
int capacity() const
Return physical capacity of array.
void reserve(int capacity)
Reserve memory for specified number of elements.
void clear()
Reset to empty state.
void append(Data const &data)
Append an element to the end of the sequence.
Polynomial< T > & operator/=(T a)
Divide this polynomial by a scalar.
Polynomial< T > & operator-=(Polynomial< T > const &a)
Subtract another polynomial from this one.
T operator()(T x) const
Evaluate polynomial at specific argument of type T.
Polynomial< T > & operator=(Polynomial< U > const &other)
Assignment from another polynomial.
Polynomial(Array< T > const &coeffs)
Construct a polynomial from array of coefficients.
int degree() const
Return degree of polynomial.
Polynomial< T > reflect() const
Compute and return reflected polynomial f(-x).
Polynomial(int capacity=10)
Construct a zero polynomial.
Polynomial< T > shift(T a) const
Compute and return shifted polynomial f(x+a).
static Polynomial< T > monomial(int n)
Return a monomial f(x) = x^{n}.
Polynomial< T > integrate() const
Compute and return indefinite integral of this polynomial.
Polynomial< T > & operator+=(Polynomial< T > const &a)
Add another polynomial to this one.
Polynomial(T c)
Construct a constant polynomial.
double evaluate(double x) const
Evaluate polynomial at specific floating point argument.
void setToZero()
Assign this polynomial a value of zero.
Polynomial< T > & operator*=(T a)
Multiply this polynomial by a scalar.
Polynomial(Polynomial< T > const &other)
Copy constructor.
Polynomial< T > differentiate() const
Compute and return derivative of this polynomial.
File containing preprocessor macros for error handling.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
#define UTIL_ASSERT(condition)
Assertion macro suitable for debugging serial or parallel code.
Utility classes for scientific computation.
void setToZero(int &value)
Set an int variable to zero.
bool operator==(Polynomial< T > const &a, Polynomial< T > const &b)
Equality operator for polynomials.
Polynomial< T > operator-(Polynomial< T > const &a)
Unary negation of polynomial.
std::ostream & operator<<(std::ostream &out, const Pair< Data > &pair)
Output a Pair to an ostream, without line breaks.
bool operator!=(Polynomial< T > const &a, Polynomial< T > const &b)
Inequality operator for polynomials.