1 #ifndef SPLINE_POLYNOMIAL_H 2 #define SPLINE_POLYNOMIAL_H 13 template <
class T=
double>
32 coef.resize(p.coef.size());
33 for(
size_t i=0;i<coef.size();i++)
34 coef[i] = T(p.coef[i]);
37 void Resize(
size_t s);
49 std::ostream&
operator << (std::ostream& out)
const;
213 for(
size_t i=0;i<coef.size();i++)
214 if(coef[coef.size()-1-i]!=T(0))
215 return int(coef.size()-1-i);
230 if(i >= coef.size())
return T(0);
237 for(
size_t i=0;i<coef.size();i++) {
238 out << coef[coef.size()-1-i] <<
"x^" << coef.size()-1-i;
239 if(i+1 != coef.size())
249 T s = coef[coef.size()-1];
250 for (
size_t i=1;i<coef.size();i++)
251 s = coef[coef.size()-i-1] + ( x * s );
259 for (
size_t i=1;i<coef.size();i++) {
270 for (
size_t i=1;i<coef.size();i++) {
271 s += T(i)*coef[i]*xi;
294 deriv.coef.resize(coef.size()-1);
295 for(
size_t i=0;i+1<coef.size();i++)
296 deriv.coef[i] = (T(i+1))*coef[i+1];
308 anti.coef.resize(coef.size()+1);
309 for(
size_t i=0;i<coef.size();i++)
310 anti.coef[i+1] = coef[i]/(T(i+1));
323 if(n == 0)
return *
this;
332 if(coef.empty()) Resize(1);
333 for(
size_t i=0;i<coef.size();i++) coef[i] += val;
339 if(coef.empty()) Resize(1);
340 for(
size_t i=0;i<coef.size();i++) coef[i] -= val;
346 for (
size_t i=0;i<coef.size();i++)
353 for (
size_t i=0;i<coef.size();i++)
360 if(b.coef.size() > coef.size()) Resize(b.
Size());
361 for(
size_t i=0;i<b.coef.size();i++) coef[i] += b.coef[i];
367 if(b.coef.size() > coef.size()) Resize(b.
Size());
368 for(
size_t i=0;i<b.coef.size();i++) coef[i] -= b.coef[i];
375 for (
size_t i=0;i<coef.size();i++)
376 for (
size_t j=0;j<b.coef.size();j++)
377 newCoef[i+j] += (coef[i] * b.coef[j]);
A simple polynomial class, p(x) = sum_{i=0}^n coef[i]*x^i.
Definition: Polynomial.h:14
void SetCoef(size_t i, T value)
Sets coefficient i. Automatically resizes the coefficient vector.
Definition: Polynomial.h:220
Polynomial()
Initializes to the 0 polynomial.
Definition: Polynomial.h:20
std::ostream & operator<<(std::ostream &out) const
Pretty-prints this.
Definition: Polynomial.h:235
void operator+=(T val)
Adds a constant offset.
Definition: Polynomial.h:330
T GetCoef(size_t i) const
Gets coefficient i. If i is out of bounds of the coefficient vector, returns 0.
Definition: Polynomial.h:228
Polynomial< T > Differentiate() const
Returns the derivative of this polynomial.
Definition: Polynomial.h:287
T operator()(T x) const
The operator (x) is equivalent to evaluation.
Definition: Polynomial.h:72
Polynomial< T > AntiDifferentiate() const
Returns the antiderivative of this polynomial.
Definition: Polynomial.h:301
Definition: BSpline.cpp:9
void operator*=(T b)
Scales by a constant.
Definition: Polynomial.h:344
void operator-=(T val)
Subtracts a constant offset.
Definition: Polynomial.h:337
size_t Size() const
Size is the size of the coefficient vector. Similar to Degree but disregarding leading 0 coefficients...
Definition: Polynomial.h:208
The logging system used in KrisLibrary.
T Derivative(T x) const
Evaluates the derivative at x.
Definition: Polynomial.h:266
T Evaluate(T x) const
Evaluates the polynomial at x.
Definition: Polynomial.h:246
Polynomial(const Polynomial< T2 > &p)
cast constructor
Definition: Polynomial.h:30
int Degree() const
The degree of the polynomial. Correctly handles leading 0 coefficients.
Definition: Polynomial.h:211
void operator/=(T b)
Divides by a constant (if b is 0, unexpected results will hold)
Definition: Polynomial.h:351