polynomial.hpp
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl,
6  * KU Leuven. All rights reserved.
7  * Copyright (C) 2011-2014 Greg Horn
8  *
9  * CasADi is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 3 of the License, or (at your option) any later version.
13  *
14  * CasADi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with CasADi; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 
26 #ifndef CASADI_POLYNOMIAL_HPP
27 #define CASADI_POLYNOMIAL_HPP
28 
29 # include "printable.hpp"
30 
31 namespace casadi {
32 
39  class CASADI_EXPORT Polynomial : public Printable<Polynomial> {
40  public:
41 
43  Polynomial(double scalar=1);
44 
46  Polynomial(double p0, double p1);
47 
49  Polynomial(double p0, double p1, double p2);
50 
52  Polynomial(double p0, double p1, double p2, double p3);
53 
55  template<typename T>
56  Polynomial(const std::vector<T>& coeff) : p_(coeff.begin(), coeff.end()) {}
57 
59  template<typename T>
60  T operator()(const T& x) const {
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  }
69 
71  const std::vector<double>& coeff() const { return p_; }
72 
74  casadi_int degree() const;
75 
77  double scalar() const;
78 
81 
84 
86  void trim();
87 
89  std::string type_name() const {return "Polynomial";}
90 
92  void disp(std::ostream& stream, bool more=false) const;
93 
94  // Add
95  Polynomial operator+(const Polynomial& b) const;
96 
97  // Add (in-place)
99 
100  // Subtract
101  Polynomial operator-(const Polynomial& b) const;
102 
103  // Subtract (in-place)
105 
106  // Multiply
107  Polynomial operator*(const Polynomial& b) const;
108 
109  // Multiply (in-place)
111 
112  // Divide by constant
113  Polynomial operator/(double b) const;
114 
115  // Divide by constant (in-place)
116  Polynomial& operator/=(double b);
117 
118 
119  protected:
120  std::vector<double> p_;
121  };
122 
123 } // namespace casadi
124 
125 
126 #endif // CASADI_POLYNOMIAL_HPP
Helper class for differentiating and integrating polynomials.
Definition: polynomial.hpp:39
Polynomial operator+(const Polynomial &b) const
double scalar() const
Get scalar value (error if degree()!=0)
Polynomial(double scalar=1)
Construct a constant polynomial.
void trim()
Remove excess zeros.
Polynomial & operator+=(const Polynomial &b)
Polynomial(double p0, double p1, double p2, double p3)
Construct a cubic polynomial.
Polynomial operator*(const Polynomial &b) const
Polynomial anti_derivative() const
Create a new polynomial for the anti-derivative (primitive function)
Polynomial operator-(const Polynomial &b) const
Polynomial & operator*=(const Polynomial &b)
Polynomial derivative() const
Create a new polynomial for the derivative.
void disp(std::ostream &stream, bool more=false) const
Print a description of the object.
Polynomial operator/(double b) const
const std::vector< double > & coeff() const
Coefficients of the polynomial.
Definition: polynomial.hpp:71
std::string type_name() const
Readable name of the class.
Definition: polynomial.hpp:89
Polynomial(double p0, double p1)
Construct a linear polynomial.
T operator()(const T &x) const
Evaluate numerically.
Definition: polynomial.hpp:60
Polynomial & operator-=(const Polynomial &b)
Polynomial(const std::vector< T > &coeff)
Construct from a vector of polynomial coefficients.
Definition: polynomial.hpp:56
Polynomial & operator/=(double b)
std::vector< double > p_
Definition: polynomial.hpp:120
Polynomial(double p0, double p1, double p2)
Construct a quadratic polynomial.
casadi_int degree() const
Degree of the polynomial.
The casadi namespace.