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>
299 if (other.capacity() > 0) {
302 if (other.size() > 0) {
303 for (
int i = 0; i < other.size(); ++i) {
312 template <
typename T>
313 template <
typename U>
318 if (other.size() >= 0) {
323 for (
int i = 0; i < other.size(); ++i) {
334 template <
typename T>
342 template <
typename T>
350 template <
typename T>
354 int min = a.size() >
size() ?
size() : a.size();
356 for (
int i = 0; i < min; ++i) {
360 if (a.size() >
size()) {
362 for (
int i =
size(); i < a.size(); ++i) {
373 template <
typename T>
387 template <
typename T>
391 int min = a.size() >
size() ?
size() : a.size();
393 for (
int i = 0; i < min; ++i) {
397 if (a.size() >
size()) {
399 for (
int i =
size(); i < a.size(); ++i) {
410 template <
typename T>
424 template <
typename T>
429 for (
int i = 0; i <
size(); ++i) {
439 template <
typename T>
444 for (
int i = 0; i <
size(); ++i) {
454 template <
typename T>
466 int n =
size() + a.size() - 1;
480 for (i = 0; i < n; ++i) {
486 for (i = 0; i < a.size(); ++i) {
487 for (j = 0; j < b.
size(); ++j) {
490 (*this)[k] += a[i]*b[j];
502 template <
typename T>
514 for (
int i = 0; i <
size(); ++i) {
515 coeffs[i+1] = (*this)[i];
516 coeffs[i+1] /= T(i+1);
528 template <
typename T>
543 for (
int i = 1; i <
size(); ++i) {
544 coeffs[i-1] = (*this)[i];
557 template <
typename T>
564 for (
int i = 0; i < b.size(); ++i) {
576 template <
typename T>
587 for (n = 1; n <=
degree; ++n) {
589 for (m = 1; m <= n; ++m) {
602 template <
typename T>
607 T value = (*this)[
degree];
609 for (
int i =
degree-1; i >= 0; --i) {
623 template <
typename T>
628 double value = (double)(*
this)[
degree];
630 for (
int i =
degree-1; i >= 0; --i) {
632 value += (double)(*
this)[i];
646 template <
typename T>
654 for (
int i = 0; i <= power; ++i) {
655 a.GArray<T>::append(zero);
676 template <
typename T>
679 if (a.size() != b.size())
return false;
681 for (
int i = 0; i < a.size(); ++i) {
682 if (a[i] != b[i])
return false;
695 template <
typename T>
697 {
return !(a == b); }
705 template <
typename T>
711 for (
int i = 0; i < a.size(); ++i) {
718 template <
typename T>
719 std::ostream&
operator << (std::ostream& out, Polynomial<T>
const & p)
723 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!
Dynamically allocatable contiguous array template.
void allocate(int capacity)
Allocate the underlying C 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.
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.
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< 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.
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.