PSCF v1.5.0
Polynomial.h
1#ifndef UTIL_POLYNOMIAL_H
2#define UTIL_POLYNOMIAL_H
3
4/*
5* Util Package - C++ Utilities for Scientific Computation
6*
7* Copyright 2010 - 2017, David Morse (morse012@um.edu)
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include <util/containers/GArray.h> // base class
12#include <util/math/Rational.h> // default template argument
13#include <util/math/Binomial.h> // used in implementation
14#include <util/containers/DArray.h> // used in implementation
15#include <util/global.h>
16
17
18#include <iostream>
19
20namespace Util
21{
22
28 template <typename T = Rational>
29 class Polynomial : private GArray<T>
30 {
31
32 public:
33
35
36
47
55 explicit Polynomial(T c);
56
66 Polynomial(Array<T> const & coeffs);
67
73 Polynomial(Polynomial<T> const & other);
74
80 template <typename U>
82
89 void setToZero();
90
92
94
95 // Get a specific coefficient by index.
96 using GArray<T>::operator [];
97
104 int degree() const;
105
106 // Logical size of the coefficient array.
107 using GArray<T>::size;
108
109 // Physical capacity of the coefficient array.
110 using GArray<T>::capacity;
111
113
115
124
133
142
151
160
169
178
180
182
191
200
212
222
224
226
233 T operator () (T x) const;
234
241 double evaluate (double x) const;
242
244
245 // Static public member functions
246
252 static Polynomial<T> monomial(int n);
253
254 };
255
256 // Inline methods
257
258 /*
259 * Constructor.
260 */
261 template <typename T>
262 inline
265
266 /*
267 * Construct a constant polynomial f(x) = c.
268 */
269 template <typename T>
270 inline
276
277 /*
278 * Construct from array of coefficients.
279 */
280 template <typename T>
281 inline
283 {
284 if (coeffs.capacity() > 0) {
286 for (int i = 0; i < coeffs.capacity(); ++i) {
287 GArray<T>::append(coeffs[i]);
288 }
289 }
290 }
291
292 /*
293 * Copy constructor
294 */
295 template <typename T>
296 inline
298 {
299 if (other.capacity() > 0) {
300 GArray<T>::reserve(other.capacity());
301 }
302 if (other.size() > 0) {
303 for (int i = 0; i < other.size(); ++i) {
304 GArray<T>::append(other[i]);
305 }
306 }
307 }
308
309 /*
310 * Assignment from another polynomial.
311 */
312 template <typename T>
313 template <typename U>
314 inline
316 {
317 setToZero();
318 if (other.size() >= 0) {
319 if (other.size() > capacity()) {
320 GArray<T>::reserve(other.capacity());
321 }
322 T element;
323 for (int i = 0; i < other.size(); ++i) {
324 element = other[i];
325 GArray<T>::append(element);
326 }
327 }
328 return *this;
329 }
330
331 /*
332 * Set this polynomial to zero.
333 */
334 template <typename T>
335 inline
338
339 /*
340 * Return degree of polynomial (size of coeff. array - 1).
341 */
342 template <typename T>
343 inline
345 { return GArray<T>::size() - 1; }
346
347 /*
348 * Addition assignment operator : add another polynomial to this one.
349 */
350 template <typename T>
352 {
353 if (a.size() > 0) {
354 int min = a.size() > size() ? size() : a.size();
355 if (min > 0) {
356 for (int i = 0; i < min; ++i) {
357 (*this)[i] += a[i];
358 }
359 }
360 if (a.size() > size()) {
361 UTIL_CHECK(min == size());
362 for (int i = size(); i < a.size(); ++i) {
363 GArray<T>::append(a[i]);
364 }
365 }
366 }
367 return *this;
368 }
369
370 /*
371 * Add a constant to this polynomial.
372 */
373 template <typename T>
375 {
376 if (size() == 0) {
377 (*this).append(a);
378 } else {
379 (*this)[0] += a;
380 }
381 return *this;
382 }
383
384 /*
385 * Subtract assignment operator : subtract another polynomial from this.
386 */
387 template <typename T>
389 {
390 if (a.size() > 0) {
391 int min = a.size() > size() ? size() : a.size();
392 if (min > 0) {
393 for (int i = 0; i < min; ++i) {
394 (*this)[i] -= a[i];
395 }
396 }
397 if (a.size() > size()) {
398 UTIL_CHECK(min == size());
399 for (int i = size(); i < a.size(); ++i) {
400 GArray<T>::append(-a[i]);
401 }
402 }
403 }
404 return *this;
405 }
406
407 /*
408 * Subtract a constant from this polynomial.
409 */
410 template <typename T>
412 {
413 if (size() == 0) {
414 (*this).append(-a);
415 } else {
416 (*this)[0] -= a;
417 }
418 return *this;
419 }
420
421 /*
422 * Multipication assignment operator : multiply this by a scalar.
423 */
424 template <typename T>
425 inline
427 {
428 if (size() >= 0) {
429 for (int i = 0; i < size(); ++i) {
430 (*this)[i] *= a;
431 }
432 }
433 return *this;
434 }
435
436 /*
437 * Division by scalar assignment operator.
438 */
439 template <typename T>
440 inline
442 {
443 if (size() >= 0) {
444 for (int i = 0; i < size(); ++i) {
445 (*this)[i] /= a;
446 }
447 }
448 return *this;
449 }
450
451 /*
452 * Multiplication assignment operator : multiply this by a polynomial.
453 */
454 template <typename T>
456 {
457 // If this polynomial is zero (size == 0), do nothing. Otherwise:
458 if (size() > 0) {
459
460 if (a.size() == 0) {
461 // If polynomial a is zero, set this one to zero (clear it).
462 setToZero();
463 } else {
464
465 // Compute size of new array of coefficients (degree + 1)
466 int n = size() + a.size() - 1;
467
468 // Make a copy of coefficients of this polynomial
469 GArray<T> b(*this);
470
471 // Clear this array of coefficients and reserve enough space
472 setToZero();
473 if (n > capacity()) {
475 }
476
477 // Set all coefficients of resized array to zero
478 int i;
479 T zero = 0;
480 for (i = 0; i < n; ++i) {
481 GArray<T>::append(zero);
482 }
483
484 // Compute new coefficients as a double sum
485 int j, k;
486 for (i = 0; i < a.size(); ++i) {
487 for (j = 0; j < b.size(); ++j) {
488 k = i + j;
489 UTIL_ASSERT(k < n);
490 (*this)[k] += a[i]*b[j];
491 }
492 }
493
494 }
495 }
496 return *this;
497 }
498
499 /*
500 * Compute and return indefinite integral of this polynomial.
501 */
502 template <typename T>
504 {
505 if (size() == 0) {
507 b.setToZero();
508 return b;
509 } else {
510 // Construct and compute coefficient array for integral
511 DArray<T> coeffs;
512 coeffs.allocate(size()+1);
513 coeffs[0] = T(0);
514 for (int i = 0; i < size(); ++i) {
515 coeffs[i+1] = (*this)[i];
516 coeffs[i+1] /= T(i+1);
517 }
518
519 // Construct and return associated Polynomial
520 Polynomial<T> b(coeffs);
521 return b;
522 }
523 }
524
525 /*
526 * Compute and return the derivatvie of this polynomial.
527 */
528 template <typename T>
530 {
531 if (size() <= 1) {
532
533 // If this polynomial is null or constant, return null
535 b.setToZero();
536 return b;
537
538 } else {
539
540 // Construct coefficient array for derivative polynomial
541 DArray<T> coeffs;
542 coeffs.allocate(size()-1);
543 for (int i = 1; i < size(); ++i) {
544 coeffs[i-1] = (*this)[i];
545 coeffs[i-1] *= T(i);
546 }
547
548 // Construct and return associated Polynomial
549 Polynomial<T> b(coeffs);
550 return b;
551 }
552 }
553
554 /*
555 * Compute and return reflection g(x) = f(-x) this polynomial f(x).
556 */
557 template <typename T>
559 {
560 // Make copy
561 Polynomial<T> b(*this);
562
563 // Reverse odd power coefficients
564 for (int i = 0; i < b.size(); ++i) {
565 if (i%2 != 0) {
566 b[i] *= -1;
567 }
568 }
569
570 return b;
571 }
572
573 /*
574 * Compute and return reflection g(x) = f(-x) this polynomial f(x).
575 */
576 template <typename T>
578 {
579 // Make copy of this
580 Polynomial<T> b(*this);
581
582 int degree = size() - 1;
583 if (degree > 0) {
585 int n, m;
586 T p;
587 for (n = 1; n <= degree; ++n) {
588 p = b[n]*a;
589 for (m = 1; m <= n; ++m) {
590 b[n -m] += Binomial::coeff(n, m)*p;
591 p *= a;
592 }
593 }
594 }
595
596 return b;
597 }
598
599 /*
600 * Evaluate polynomial at specific argument.
601 */
602 template <typename T>
603 inline T Polynomial<T>::operator () (T x) const
604 {
605 if (size() > 0) {
606 int degree = size() - 1;
607 T value = (*this)[degree];
608 if (degree > 0) {
609 for (int i = degree-1; i >= 0; --i) {
610 value *= x;
611 value += (*this)[i];
612 }
613 }
614 return value;
615 } else {
616 return T(0);
617 }
618 }
619
620 /*
621 * Evaluate polynomial at specific floating point argument.
622 */
623 template <typename T>
624 inline double Polynomial<T>::evaluate (double x) const
625 {
626 if (size() > 0) {
627 int degree = size()-1;
628 double value = (double)(*this)[degree];
629 if (degree > 0) {
630 for (int i = degree-1; i >= 0; --i) {
631 value *= x;
632 value += (double)(*this)[i];
633 }
634 }
635 return value;
636 } else {
637 return 0.0;
638 }
639 }
640
641 // Static function
642
646 template <typename T>
648 {
649 UTIL_CHECK(power >= 0);
650 Polynomial<T> a(power+1);
651
652 // Append zero coefficients.
653 T zero(0);
654 for (int i = 0; i <= power; ++i) {
655 a.GArray<T>::append(zero);
656 }
657
658 // Set coefficient of highest power to unity
659 a[power] = T(1);
660
661 return a;
662 }
663
664 // Related non-member functions
665
676 template <typename T>
677 bool operator == (Polynomial<T> const & a, Polynomial<T> const & b)
678 {
679 if (a.size() != b.size()) return false;
680 if (a.size() > 0) {
681 for (int i = 0; i < a.size(); ++i) {
682 if (a[i] != b[i]) return false;
683 }
684 }
685 return true;
686 }
687
695 template <typename T>
696 bool operator != (Polynomial<T> const & a, Polynomial<T> const & b)
697 { return !(a == b); }
698
705 template <typename T>
706 inline
708 {
709 Polynomial<T> b(a);
710 if (a.size() > 0) {
711 for (int i = 0; i < a.size(); ++i) {
712 b[i] *= -1;
713 }
714 }
715 return b;
716 }
717
718 template <typename T>
719 std::ostream& operator << (std::ostream& out, Polynomial<T> const & p)
720 {
721 out << "( ";
722 if (p.size() > 0) {
723 for (int i = 0; i < p.size(); ++i) {
724 out << p[i] << " ";
725 }
726 }
727 out << ")";
728 return out;
729 }
730}
731#endif
Array container class template.
Definition Array.h:41
int capacity() const
Return allocated size.
Definition Array.h:250
static void setup(int nMax)
Precompute all combinations C(n, m) up to n = nMax.
Definition Binomial.cpp:17
static int coeff(int n, int m)
Return coefficient "n choose m", or C(n, m) = n!
Definition Binomial.cpp:55
Dynamically allocatable contiguous array template.
Definition DArray.h:32
void allocate(int capacity)
Allocate the underlying C array.
Definition DArray.h:262
int size() const
Definition GArray.h:450
int capacity() const
Definition GArray.h:443
void reserve(int capacity)
Reserve memory for specified number of elements.
Definition GArray.h:252
void clear()
Reset to empty state.
Definition GArray.h:296
void append(Data const &data)
Append an element to the end of the sequence.
Definition GArray.h:303
A Polynomial (i.e,.
Definition Polynomial.h:30
Polynomial< T > & operator/=(T a)
Divide this polynomial by a scalar.
Definition Polynomial.h:441
Polynomial< T > & operator-=(Polynomial< T > const &a)
Subtract another polynomial from this one.
Definition Polynomial.h:388
T operator()(T x) const
Evaluate polynomial at specific argument of type T.
Definition Polynomial.h:603
Polynomial< T > & operator=(Polynomial< U > const &other)
Assignment from another polynomial.
Definition Polynomial.h:315
int degree() const
Return degree of polynomial.
Definition Polynomial.h:344
Polynomial< T > reflect() const
Compute and return reflected polynomial f(-x).
Definition Polynomial.h:558
Polynomial(int capacity=10)
Construct a zero polynomial.
Definition Polynomial.h:263
Polynomial< T > shift(T a) const
Compute and return shifted polynomial f(x+a).
Definition Polynomial.h:577
static Polynomial< T > monomial(int n)
Return a monomial f(x) = x^{n}.
Definition Polynomial.h:647
Polynomial< T > integrate() const
Compute and return indefinite integral of this polynomial.
Definition Polynomial.h:503
Polynomial< T > & operator+=(Polynomial< T > const &a)
Add another polynomial to this one.
Definition Polynomial.h:351
double evaluate(double x) const
Evaluate polynomial at specific floating point argument.
Definition Polynomial.h:624
void setToZero()
Assign this polynomial a value of zero.
Definition Polynomial.h:336
Polynomial< T > & operator*=(T a)
Multiply this polynomial by a scalar.
Definition Polynomial.h:426
Polynomial< T > differentiate() const
Compute and return derivative of this polynomial.
Definition Polynomial.h:529
File containing preprocessor macros for error handling.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
#define UTIL_ASSERT(condition)
Assertion macro suitable for debugging serial or parallel code.
Definition global.h:75
Utility classes for scientific computation.
bool operator==(Polynomial< T > const &a, Polynomial< T > const &b)
Equality operator for polynomials.
Definition Polynomial.h:677
Polynomial< T > operator-(Polynomial< T > const &a)
Unary negation of polynomial.
Definition Polynomial.h:707
std::ostream & operator<<(std::ostream &out, const Pair< Data > &pair)
Output a Pair to an ostream, without line breaks.
Definition Pair.h:57
bool operator!=(Polynomial< T > const &a, Polynomial< T > const &b)
Inequality operator for polynomials.
Definition Polynomial.h:696