List of all members | Public Member Functions | Protected Attributes
casadi::Polynomial Class Reference

Helper class for differentiating and integrating polynomials. More...

#include <polynomial.hpp>

Detailed Description

Author
Joel Andersson
Date
2014

Extra doc: https://github.com/casadi/casadi/wiki/L_8y

Definition at line 39 of file polynomial.hpp.

Inheritance diagram for casadi::Polynomial:
Inheritance graph
[legend]
Collaboration diagram for casadi::Polynomial:
Collaboration graph
[legend]

Public Member Functions

 Polynomial (double scalar=1)
 Construct a constant polynomial. More...
 
 Polynomial (double p0, double p1)
 Construct a linear polynomial. More...
 
 Polynomial (double p0, double p1, double p2)
 Construct a quadratic polynomial. More...
 
 Polynomial (double p0, double p1, double p2, double p3)
 Construct a cubic polynomial. More...
 
template<typename T >
 Polynomial (const std::vector< T > &coeff)
 Construct from a vector of polynomial coefficients. More...
 
template<typename T >
operator() (const T &x) const
 Evaluate numerically. More...
 
const std::vector< double > & coeff () const
 Coefficients of the polynomial. More...
 
casadi_int degree () const
 Degree of the polynomial. More...
 
double scalar () const
 Get scalar value (error if degree()!=0) More...
 
Polynomial derivative () const
 Create a new polynomial for the derivative. More...
 
Polynomial anti_derivative () const
 Create a new polynomial for the anti-derivative (primitive function) More...
 
void trim ()
 Remove excess zeros. More...
 
std::string type_name () const
 Readable name of the class. More...
 
void disp (std::ostream &stream, bool more=false) const
 Print a description of the object. More...
 
Polynomial operator+ (const Polynomial &b) const
 
Polynomialoperator+= (const Polynomial &b)
 
Polynomial operator- (const Polynomial &b) const
 
Polynomialoperator-= (const Polynomial &b)
 
Polynomial operator* (const Polynomial &b) const
 
Polynomialoperator*= (const Polynomial &b)
 
Polynomial operator/ (double b) const
 
Polynomialoperator/= (double b)
 

Protected Attributes

std::vector< double > p_
 

Constructor & Destructor Documentation

◆ Polynomial() [1/5]

casadi::Polynomial::Polynomial ( double  scalar = 1)

Definition at line 32 of file polynomial.cpp.

32  : p_(1, scalar) {
33  }
double scalar() const
Get scalar value (error if degree()!=0)
Definition: polynomial.cpp:76
std::vector< double > p_
Definition: polynomial.hpp:120

Referenced by anti_derivative(), derivative(), and operator*().

◆ Polynomial() [2/5]

casadi::Polynomial::Polynomial ( double  p0,
double  p1 
)

Definition at line 35 of file polynomial.cpp.

35  {
36  p_.resize(2);
37  p_[0] = p0;
38  p_[1] = p1;
39  }

References p_.

◆ Polynomial() [3/5]

casadi::Polynomial::Polynomial ( double  p0,
double  p1,
double  p2 
)

Definition at line 41 of file polynomial.cpp.

41  {
42  p_.resize(3);
43  p_[0] = p0;
44  p_[1] = p1;
45  p_[2] = p2;
46  }

References p_.

◆ Polynomial() [4/5]

casadi::Polynomial::Polynomial ( double  p0,
double  p1,
double  p2,
double  p3 
)

Definition at line 48 of file polynomial.cpp.

48  {
49  p_.resize(4);
50  p_[0] = p0;
51  p_[1] = p1;
52  p_[2] = p2;
53  p_[3] = p3;
54  }

References p_.

◆ Polynomial() [5/5]

template<typename T >
casadi::Polynomial::Polynomial ( const std::vector< T > &  coeff)
inline

Definition at line 56 of file polynomial.hpp.

56 : p_(coeff.begin(), coeff.end()) {}
const std::vector< double > & coeff() const
Coefficients of the polynomial.
Definition: polynomial.hpp:71

Member Function Documentation

◆ anti_derivative()

Polynomial casadi::Polynomial::anti_derivative ( ) const

Definition at line 149 of file polynomial.cpp.

149  {
150  std::vector<double> ret_p(p_.size()+1);
151  ret_p[0] = 0;
152  for (casadi_int k=0; k<p_.size(); ++k) {
153  ret_p[k+1] = p_[k]/static_cast<double>(k+1);
154  }
155  return Polynomial(ret_p);
156  }
Polynomial(double scalar=1)
Construct a constant polynomial.
Definition: polynomial.cpp:32

References p_, and Polynomial().

Referenced by casadi::collocation_coeff(), and casadi::Collocation::setup_step().

◆ coeff()

const std::vector<double>& casadi::Polynomial::coeff ( ) const
inline

Definition at line 71 of file polynomial.hpp.

71 { return p_; }

Referenced by casadi::reduce_index_gen().

◆ degree()

casadi_int casadi::Polynomial::degree ( ) const

Definition at line 72 of file polynomial.cpp.

72  {
73  return p_.size()-1;
74  }

References p_.

Referenced by scalar().

◆ derivative()

Polynomial casadi::Polynomial::derivative ( ) const

Definition at line 141 of file polynomial.cpp.

141  {
142  std::vector<double> ret_p(p_.size()-1);
143  for (casadi_int k=1; k<p_.size(); ++k) {
144  ret_p[k-1] = static_cast<double>(k)*p_[k];
145  }
146  return Polynomial(ret_p);
147  }

References p_, and Polynomial().

Referenced by casadi::collocation_coeff(), and casadi::Collocation::setup_step().

◆ disp()

void casadi::Polynomial::disp ( std::ostream &  stream,
bool  more = false 
) const

Definition at line 56 of file polynomial.cpp.

56  {
57  if (more) {
58  for (casadi_int d=0; d<p_.size(); ++d) {
59  if (d==0) {
60  stream << p_[d];
61  } else if (d==1) {
62  stream << " + " << p_[d] << "*x";
63  } else {
64  stream << " + " << p_[d] << "*x^" << d;
65  }
66  }
67  } else {
68  stream << p_;
69  }
70  }

References p_.

◆ operator()()

template<typename T >
T casadi::Polynomial::operator() ( const T &  x) const
inline

Definition at line 60 of file polynomial.hpp.

60  {
61  auto it = p_.rbegin();
62  T ret = *it++;
63  while (it!=p_.rend()) {
64  ret *= x;
65  ret += *it++;
66  }
67  return ret;
68  }

References casadi::T.

◆ operator*()

Polynomial casadi::Polynomial::operator* ( const Polynomial b) const

Definition at line 81 of file polynomial.cpp.

81  {
82  std::vector<double> p_ret(p_.size() + a.p_.size() - 1, 0);
83  for (casadi_int d=0; d<p_.size(); ++d) {
84  for (casadi_int d_a=0; d_a<a.p_.size(); ++d_a) {
85  p_ret[d+d_a] += p_[d] * a.p_[d_a];
86  }
87  }
88  return Polynomial(p_ret);
89  }

References p_, and Polynomial().

◆ operator*=()

Polynomial & casadi::Polynomial::operator*= ( const Polynomial b)

Definition at line 91 of file polynomial.cpp.

91  {
92  return *this = *this*d;
93  }

◆ operator+()

Polynomial casadi::Polynomial::operator+ ( const Polynomial b) const

Definition at line 108 of file polynomial.cpp.

108  {
109  Polynomial ret = *this;
110  return ret+=b;
111  }

◆ operator+=()

Polynomial & casadi::Polynomial::operator+= ( const Polynomial b)

Definition at line 113 of file polynomial.cpp.

113  {
114  p_.resize(std::max(p_.size(), b.p_.size()), 0);
115  transform(b.p_.begin(), b.p_.end(), p_.begin(), p_.begin(), std::plus<double>());
116  trim();
117  return *this;
118  }
void trim()
Remove excess zeros.
Definition: polynomial.cpp:133

References p_, and trim().

◆ operator-()

Polynomial casadi::Polynomial::operator- ( const Polynomial b) const

Definition at line 120 of file polynomial.cpp.

120  {
121  Polynomial ret = *this;
122  return ret-=b;
123  }

◆ operator-=()

Polynomial & casadi::Polynomial::operator-= ( const Polynomial b)

Definition at line 125 of file polynomial.cpp.

125  {
126  p_.resize(std::max(p_.size(), b.p_.size()), 0);
127  transform(p_.begin(), p_.begin()+b.p_.size(),
128  b.p_.begin(), p_.begin(), std::minus<double>());
129  trim();
130  return *this;
131  }

References p_, and trim().

◆ operator/()

Polynomial casadi::Polynomial::operator/ ( double  b) const

Definition at line 95 of file polynomial.cpp.

95  {
96  Polynomial ret = *this;
97  ret/=d;
98  return ret;
99  }

◆ operator/=()

Polynomial & casadi::Polynomial::operator/= ( double  b)

Definition at line 101 of file polynomial.cpp.

101  {
102  for (std::vector<double>::iterator it=p_.begin(); it!=p_.end(); ++it) {
103  *it /= d;
104  }
105  return *this;
106  }

References p_.

◆ scalar()

double casadi::Polynomial::scalar ( ) const

Definition at line 76 of file polynomial.cpp.

76  {
77  casadi_assert_dev(degree()==0);
78  return p_.front();
79  }
casadi_int degree() const
Degree of the polynomial.
Definition: polynomial.cpp:72

References degree(), and p_.

◆ trim()

void casadi::Polynomial::trim ( )

Definition at line 133 of file polynomial.cpp.

133  {
134  // Remove trailing zeros
135  size_t new_size = p_.size();
136  std::vector<double>::const_reverse_iterator it=p_.rbegin();
137  while (it!=p_.rend() && 0==*it++) new_size--;
138  p_.resize(new_size);
139  }

References p_.

Referenced by operator+=(), and operator-=().

◆ type_name()

std::string casadi::Polynomial::type_name ( ) const
inline

Definition at line 89 of file polynomial.hpp.

89 {return "Polynomial";}

Member Data Documentation

◆ p_

std::vector<double> casadi::Polynomial::p_
protected

The documentation for this class was generated from the following files: