CUGL 1.3
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUPolynomial.h
1 //
2 // CUPolynomial.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides a class that represents a polynomial. It has basic
6 // methods for evaluation and root finding. The primary purpose of this class
7 // is to support CubicSpline and other beziers. However, we provide it publicly
8 // in case it is useful for other applications.
9 //
10 // Because math objects are intended to be on the stack, we do not provide
11 // any shared pointer support in this class.
12 //
13 // CUGL MIT License:
14 // This software is provided 'as-is', without any express or implied
15 // warranty. In no event will the authors be held liable for any damages
16 // arising from the use of this software.
17 //
18 // Permission is granted to anyone to use this software for any purpose,
19 // including commercial applications, and to alter it and redistribute it
20 // freely, subject to the following restrictions:
21 //
22 // 1. The origin of this software must not be misrepresented; you must not
23 // claim that you wrote the original software. If you use this software
24 // in a product, an acknowledgment in the product documentation would be
25 // appreciated but is not required.
26 //
27 // 2. Altered source versions must be plainly marked as such, and must not
28 // be misrepresented as being the original software.
29 //
30 // 3. This notice may not be removed or altered from any source distribution.
31 //
32 // Author: Walker White
33 // Version: 6/20/16
34 
35 #ifndef __CU_POLYNOMIAL_H__
36 #define __CU_POLYNOMIAL_H__
37 
38 #include <cugl/math/CUMathBase.h>
39 #include <cugl/util/CUDebug.h>
40 #include <iostream>
41 #include <vector>
42 
43 namespace cugl {
44 
67 class Polynomial : public std::vector<float>{
68 public:
69 #pragma mark Constants
70 
71  static const Polynomial ZERO;
73  static const Polynomial ONE;
74 
75 #pragma mark -
76 #pragma mark Constructors
77 
80  Polynomial() : std::vector<float>(1,0) { }
81 
89  Polynomial(long degree) : std::vector<float>(degree+1,0) {
90  (*this)[0] = 1;
91  }
92 
105  Polynomial(long degree, float value) : std::vector<float>(degree+1,value) {
106  }
107 
113  Polynomial(const Polynomial& poly) : std::vector<float>(poly) {
114  }
115 
121  Polynomial(Polynomial&& poly) : std::vector<float>(poly) {
122  }
123 
136  Polynomial(const_iterator first, const_iterator last) : std::vector<float>(first,last) {
137  CUAssertLog(isValid(), "The array data is invalid");
138  }
139 
150  Polynomial(float* array, unsigned int size, unsigned int offset=0) : std::vector<float>() {
151  assign(array+offset,array+size+offset);
152  CUAssertLog(isValid(), "The array data is invalid");
153  }
154 
158  virtual ~Polynomial() { }
159 
160 #pragma mark -
161 #pragma mark Attributes
162 
171  long degree() const { return (long)size()-1; }
172 
178  bool isConstant() const { return size() == 1; }
179 
188  bool isValid() const { return size() == 1 || (size() > 1 && at(0) != 0); }
189 
198  bool isZero() const { return size() == 1 && at(0) == 0; }
199 
200 
201 #pragma mark -
202 #pragma mark Calculation Methods
203 
211  Polynomial derivative() const;
212 
220  float evaluate(float value) const;
221 
228  void validate();
229 
238  float normalize();
239 
261  bool roots(vector<float>& roots, float epsilon=CU_MATH_EPSILON) const;
262 
263 #pragma mark -
264 #pragma mark Setters
265 
273  std::vector<float>::operator=(poly);
274  return *this;
275  }
276 
285  std::vector<float>::operator=(poly);
286  return *this;
287  }
288 
296  Polynomial& operator=(float value) {
297  return set(value);
298  }
299 
308  Polynomial& set(float* array, int size);
309 
317  Polynomial& set(float value) {
318  resize(1); at(0) = value;
319  return *this;
320  }
321 
322 #pragma mark -
323 #pragma mark Comparisons
324 
335  bool operator<(const Polynomial& p) const;
336 
348  bool operator<(float value) const {
349  return size() == 1 && at(0) < value;
350  }
351 
363  bool operator<=(const Polynomial& p) const;
364 
376  bool operator<=(float value) const {
377  return size() == 1 && at(0) <= value;
378  }
379 
391  bool operator>(const Polynomial& p) const;
392 
404  bool operator>(float value) const {
405  return size() > 1 || at(0) > value;
406  }
407 
419  bool operator>=(const Polynomial& p) const;
420 
432  bool operator>=(float value) const {
433  return size() > 1 || at(0) >= value;
434  }
435 
443  bool operator==(float value) const {
444  return size() == 1 && at(0) == value;
445  }
446 
454  bool operator!=(float value) const {
455  return size() > 1 || at(0) != value;
456  }
457 
458 #pragma mark -
459 #pragma mark Operators
460 
467  Polynomial& operator+=(const Polynomial& other);
468 
476  Polynomial& operator-=(const Polynomial& other);
477 
486  return *this = (*this)*other;
487  }
488 
498  Polynomial& operator/=(const Polynomial& other);
499 
509  Polynomial& operator%=(const Polynomial& other);
510 
518  Polynomial operator+(const Polynomial& other) const {
519  return Polynomial(*this) += other;
520  }
521 
529  Polynomial operator-(const Polynomial& other) const {
530  return Polynomial(*this) -= other;
531  }
532 
540  Polynomial operator*(const Polynomial& other) const;
541 
549  Polynomial operator/(const Polynomial& other) const {
550  return Polynomial(*this) /= other;
551  }
552 
560  Polynomial operator%(const Polynomial& other) const {
561  return Polynomial(*this) %= other;
562  }
563 
571  Polynomial& operator+=(float value);
572 
580  Polynomial& operator-=(float value);
581 
589  Polynomial& operator*=(float value);
590 
600  Polynomial& operator/=(float value);
601 
611  Polynomial& operator%=(float value);
612 
620  Polynomial operator+(float value) const {
621  return Polynomial(*this) += value;
622  }
623 
631  Polynomial operator-(float value) const {
632  return Polynomial(*this) -= value;
633  }
634 
642  Polynomial operator*(float value) const {
643  return Polynomial(*this) *= value;
644  }
645 
653  Polynomial operator/(float value) const {
654  return Polynomial(*this) /= value;
655  }
656 
664  Polynomial operator%(float value) const {
665  return Polynomial(*this) %= value;
666  }
667 
673  Polynomial operator-() const;
674 
675 #pragma mark -
676 #pragma mark Friend Functions
677 
685  friend Polynomial operator+(float left, const Polynomial& right);
686 
695  friend Polynomial operator-(float left, const Polynomial& right);
696 
705  friend Polynomial operator*(float left, const Polynomial& right);
706 
718  friend Polynomial operator/(float left, const Polynomial& right);
719 
730  friend Polynomial operator%(float left, const Polynomial& right);
731 
744  friend bool operator<(float left, const Polynomial& right) {
745  return right.size() > 1 || right[0] > left;
746  }
747 
760  friend bool operator<=(float left, const Polynomial& right) {
761  return right.size() > 1 || right[0] >= left;
762  }
763 
776  friend bool operator>(float left, const Polynomial& right) {
777  return right.size() == 1 && right[0] < left;
778  }
779 
792  friend bool operator>=(float left, const Polynomial& right) {
793  return right.size() == 1 && right[0] <= left;
794  }
795 
796 
797 #pragma mark -
798 #pragma mark Conversion Methods
799 
813  std::string toString(bool format=true) const;
814 
816  operator std::string() const { return toString(); }
817 
818 
819 #pragma mark -
820 #pragma mark Internal Helpers
821 protected:
834  static Polynomial iterative_multiply(const Polynomial& a, const Polynomial& b);
835 
852  static Polynomial recursive_multiply(const Polynomial& a, const Polynomial& b);
853 
870  Polynomial& synthetic_divide(const Polynomial& other);
871 
892  bool bairstow_factor(Polynomial& quad, Polynomial& result, float epsilon) const;
893 
904  void solve_quadratic(vector<float>& roots) const;
905 };
906 
907 }
908 
909 #endif /* __CU_POLYNOMIAL_H__ */
cugl::Polynomial::isValid
bool isValid() const
Definition: CUPolynomial.h:188
cugl::Polynomial::Polynomial
Polynomial(const_iterator first, const_iterator last)
Definition: CUPolynomial.h:136
cugl::Polynomial::operator*
Polynomial operator*(float value) const
Definition: CUPolynomial.h:642
cugl::Polynomial::~Polynomial
virtual ~Polynomial()
Definition: CUPolynomial.h:158
cugl::Polynomial::evaluate
float evaluate(float value) const
cugl::Polynomial::set
Polynomial & set(float *array, int size)
cugl::Polynomial::recursive_multiply
static Polynomial recursive_multiply(const Polynomial &a, const Polynomial &b)
cugl::Polynomial::operator>=
bool operator>=(float value) const
Definition: CUPolynomial.h:432
cugl::Polynomial::Polynomial
Polynomial(const Polynomial &poly)
Definition: CUPolynomial.h:113
cugl::Polynomial::operator/
Polynomial operator/(const Polynomial &other) const
Definition: CUPolynomial.h:549
cugl::Polynomial::operator-
Polynomial operator-(const Polynomial &other) const
Definition: CUPolynomial.h:529
cugl::Polynomial::operator<=
friend bool operator<=(float left, const Polynomial &right)
Definition: CUPolynomial.h:760
cugl::Polynomial::bairstow_factor
bool bairstow_factor(Polynomial &quad, Polynomial &result, float epsilon) const
cugl::Polynomial::normalize
float normalize()
cugl::Polynomial::operator%
Polynomial operator%(float value) const
Definition: CUPolynomial.h:664
cugl::Polynomial::synthetic_divide
Polynomial & synthetic_divide(const Polynomial &other)
cugl::Polynomial::operator>=
friend bool operator>=(float left, const Polynomial &right)
Definition: CUPolynomial.h:792
cugl::Polynomial::derivative
Polynomial derivative() const
cugl::Polynomial::Polynomial
Polynomial()
Definition: CUPolynomial.h:80
cugl::Polynomial::operator>
bool operator>(float value) const
Definition: CUPolynomial.h:404
cugl::Polynomial::operator<=
bool operator<=(float value) const
Definition: CUPolynomial.h:376
cugl::Polynomial::operator+
Polynomial operator+(const Polynomial &other) const
Definition: CUPolynomial.h:518
cugl::Polynomial::operator+=
Polynomial & operator+=(const Polynomial &other)
cugl::Polynomial::Polynomial
Polynomial(Polynomial &&poly)
Definition: CUPolynomial.h:121
cugl::Polynomial::operator=
Polynomial & operator=(const Polynomial &poly)
Definition: CUPolynomial.h:272
cugl::Polynomial::operator<
bool operator<(float value) const
Definition: CUPolynomial.h:348
cugl::Polynomial::operator==
bool operator==(float value) const
Definition: CUPolynomial.h:443
cugl::Polynomial::operator-
Polynomial operator-(float value) const
Definition: CUPolynomial.h:631
cugl::Polynomial::operator<=
bool operator<=(const Polynomial &p) const
cugl::Polynomial::operator<
bool operator<(const Polynomial &p) const
cugl::Polynomial
Definition: CUPolynomial.h:67
cugl::Polynomial::roots
bool roots(vector< float > &roots, float epsilon=CU_MATH_EPSILON) const
cugl::Polynomial::operator>
friend bool operator>(float left, const Polynomial &right)
Definition: CUPolynomial.h:776
cugl::Polynomial::validate
void validate()
cugl::Polynomial::operator/=
Polynomial & operator/=(const Polynomial &other)
cugl::Polynomial::operator*=
Polynomial & operator*=(const Polynomial &other)
Definition: CUPolynomial.h:485
cugl::Polynomial::iterative_multiply
static Polynomial iterative_multiply(const Polynomial &a, const Polynomial &b)
cugl::Polynomial::Polynomial
Polynomial(long degree)
Definition: CUPolynomial.h:89
cugl::Polynomial::set
Polynomial & set(float value)
Definition: CUPolynomial.h:317
cugl::Polynomial::operator*
Polynomial operator*(const Polynomial &other) const
cugl::Polynomial::isZero
bool isZero() const
Definition: CUPolynomial.h:198
cugl::Polynomial::solve_quadratic
void solve_quadratic(vector< float > &roots) const
cugl::Polynomial::operator%=
Polynomial & operator%=(const Polynomial &other)
cugl::Polynomial::operator>
bool operator>(const Polynomial &p) const
cugl::Polynomial::operator>=
bool operator>=(const Polynomial &p) const
cugl::Polynomial::operator!=
bool operator!=(float value) const
Definition: CUPolynomial.h:454
cugl::Polynomial::degree
long degree() const
Definition: CUPolynomial.h:171
cugl::Polynomial::operator<
friend bool operator<(float left, const Polynomial &right)
Definition: CUPolynomial.h:744
cugl::Polynomial::operator-=
Polynomial & operator-=(const Polynomial &other)
cugl::Polynomial::operator/
Polynomial operator/(float value) const
Definition: CUPolynomial.h:653
cugl::Polynomial::Polynomial
Polynomial(long degree, float value)
Definition: CUPolynomial.h:105
cugl::Polynomial::operator=
Polynomial & operator=(float value)
Definition: CUPolynomial.h:296
cugl::Polynomial::isConstant
bool isConstant() const
Definition: CUPolynomial.h:178
cugl::Polynomial::operator=
Polynomial & operator=(Polynomial &&poly)
Definition: CUPolynomial.h:284
cugl::Polynomial::operator+
Polynomial operator+(float value) const
Definition: CUPolynomial.h:620
cugl::Polynomial::operator-
Polynomial operator-() const
cugl::Polynomial::toString
std::string toString(bool format=true) const
cugl::Polynomial::ZERO
static const Polynomial ZERO
Definition: CUPolynomial.h:71
cugl::Polynomial::Polynomial
Polynomial(float *array, unsigned int size, unsigned int offset=0)
Definition: CUPolynomial.h:150
cugl::Polynomial::operator%
Polynomial operator%(const Polynomial &other) const
Definition: CUPolynomial.h:560
cugl::Polynomial::ONE
static const Polynomial ONE
Definition: CUPolynomial.h:73