Cornell Cocos
Cornell Extensions to Cocos2d
All Classes Functions Variables Enumerations Enumerator Friends
CUPolynomial.h
1 //
2 // CUPolynomial.h
3 // Cornell Extensions to Cocos2D
4 //
5 // This module provides a class that represents a polynomial. It has basic methods
6 // for evaluation and root finding. The primary purpose of this class is to support
7 // CubicBezier and other splines. However, we provide it publicly in case it is
8 // useful for other applications.
9 //
10 // Math data types are much lighter-weight than other objects, and are intended to
11 // be copied. That is why we do not use reference counting for these objects.
12 //
13 // Author: Walker White
14 // Version: 11/24/15
15 //
16 #ifndef __CU_POLYNOMIAL_H__
17 #define __CU_POLYNOMIAL_H__
18 
19 #include <cocos2d.h>
20 #include <iostream>
21 #include <vector>
22 
23 using namespace std;
24 
25 NS_CC_BEGIN
26 
27 
28 #pragma mark -
29 #pragma mark Polynomial
30 
50 class Polynomial : public vector<float> {
51 public:
52 #pragma mark Constructors
53 
56  Polynomial() : std::vector<float>(1,0) { }
57 
65  Polynomial(long degree) : vector<float>(degree+1,0) {
66  (*this)[0] = 1;
67  }
68 
80  Polynomial(long degree, float value) : vector<float>(degree+1,value) {
81  }
82 
88  Polynomial(const Polynomial& poly) : vector<float>(poly) {
89  }
90 
102  Polynomial(const_iterator first, const_iterator last) : vector<float>(first,last) {
103  CCASSERT(isValid(), "The array data is invalid");
104  }
105 
116  Polynomial(float* array, unsigned int size, unsigned int offset=0) : vector<float>() {
117  assign(array+offset,array+size+offset);
118  CCASSERT(isValid(), "The array data is invalid");
119  }
120 
124  virtual ~Polynomial() { }
125 
126 
127 #pragma mark Accessors
128 
136  long degree() const { return (long)size()-1; }
137 
143  bool constant() const { return size() == 1; }
144 
153  bool isValid() const { return size() == 1 || (size() > 1 && at(0) != 0); }
154 
163  bool isZero() const { return size() == 1 && at(0) == 0; }
164 
165 
166 #pragma mark Basic Methods
167 
175  Polynomial derivative() const;
176 
184  float evaluate(float value) const;
185 
192  void validate();
193 
202  float normalize();
203 
225  bool roots(vector<float>& roots, float epsilon) const;
226 
241  string toString(bool format=true) const;
242 
243 
244 #pragma mark Operators
245 
252  Polynomial& operator+=(const Polynomial& other);
253 
261  Polynomial& operator-=(const Polynomial& other);
262 
271  return *this = (*this)*other;
272  }
273 
283  Polynomial& operator/=(const Polynomial& other);
284 
294  Polynomial& operator%=(const Polynomial& other);
295 
303  Polynomial operator+(const Polynomial& other) const {
304  return Polynomial(*this) += other;
305  }
306 
314  Polynomial operator-(const Polynomial& other) const {
315  return Polynomial(*this) -= other;
316  }
317 
325  Polynomial operator*(const Polynomial& other) const;
326 
334  Polynomial operator/(const Polynomial& other) const {
335  return Polynomial(*this) /= other;
336  }
337 
345  Polynomial operator%(const Polynomial& other) const {
346  return Polynomial(*this) %= other;
347  }
348 
356  Polynomial& operator+=(float value);
357 
365  Polynomial& operator-=(float value);
366 
374  Polynomial& operator*=(float value);
375 
385  Polynomial& operator/=(float value);
386 
396  Polynomial& operator%=(float value);
397 
405  Polynomial operator+(float value) const {
406  return Polynomial(*this) += value;
407  }
408 
416  Polynomial operator-(float value) const {
417  return Polynomial(*this) += value;
418  }
419 
427  Polynomial operator*(float value) const {
428  return Polynomial(*this) += value;
429  }
430 
438  Polynomial operator/(float value) const {
439  return Polynomial(*this) /= value;
440  }
441 
449  Polynomial operator%(float value) const {
450  return Polynomial(*this) /= value;
451  }
452 
453 
454 #pragma mark Friend Functions
455 
463  friend Polynomial operator+(float left, const Polynomial& right);
464 
473  friend Polynomial operator-(float left, const Polynomial& right);
474 
483  friend Polynomial operator*(float left, const Polynomial& right);
484 
496  friend Polynomial operator/(float left, const Polynomial& right);
497 
508  friend Polynomial operator%(float left, const Polynomial& right);
509 
521  friend ostream& operator<<(ostream& os, const Polynomial& poly);
522 
523 
524 #pragma mark Internal Helpers
525 protected:
538  static Polynomial iterative_multiply(const Polynomial& a, const Polynomial& b);
539 
556  static Polynomial recursive_multiply(const Polynomial& a, const Polynomial& b);
557 
574  Polynomial& synthetic_divide(const Polynomial& other);
575 
596  bool bairstow_factor(Polynomial& quad, Polynomial& result, float epsilon) const;
597 
607  void solve_quadratic(vector<float>& roots) const;
608 };
609 
610 NS_CC_END
611 
612 #endif /* defined(__CU_POLYNOMIAL_H__) */
long degree() const
Definition: CUPolynomial.h:136
bool constant() const
Definition: CUPolynomial.h:143
Polynomial()
Definition: CUPolynomial.h:56
Polynomial(long degree)
Definition: CUPolynomial.h:65
Polynomial operator+(float value) const
Definition: CUPolynomial.h:405
Definition: CUPolynomial.h:50
Polynomial(const Polynomial &poly)
Definition: CUPolynomial.h:88
Polynomial(float *array, unsigned int size, unsigned int offset=0)
Definition: CUPolynomial.h:116
Polynomial operator%(const Polynomial &other) const
Definition: CUPolynomial.h:345
bool isValid() const
Definition: CUPolynomial.h:153
Polynomial operator%(float value) const
Definition: CUPolynomial.h:449
Polynomial(const_iterator first, const_iterator last)
Definition: CUPolynomial.h:102
Polynomial operator+(const Polynomial &other) const
Definition: CUPolynomial.h:303
Polynomial(long degree, float value)
Definition: CUPolynomial.h:80
Polynomial operator/(float value) const
Definition: CUPolynomial.h:438
virtual ~Polynomial()
Definition: CUPolynomial.h:124
Polynomial operator/(const Polynomial &other) const
Definition: CUPolynomial.h:334
Polynomial operator-(const Polynomial &other) const
Definition: CUPolynomial.h:314
Polynomial operator*(float value) const
Definition: CUPolynomial.h:427
bool isZero() const
Definition: CUPolynomial.h:163
Polynomial & operator*=(const Polynomial &other)
Definition: CUPolynomial.h:270
Polynomial operator-(float value) const
Definition: CUPolynomial.h:416