1 /*
2  *    This file is part of CasADi.
3  *
4  *    CasADi -- A symbolic framework for dynamic optimization.
5  *    Copyright (C) 2010-2014 Joel Andersson, Joris Gillis, Moritz Diehl,
6  *                            K.U. 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 
33   /** \brief Helper class for differentiating and integrating polynomials
34       \author Joel Andersson
35       \date 2014
36   */
37   class CASADI_EXPORT Polynomial : public Printable<Polynomial> {
38   public:
39 
40     /// Construct a constant polynomial
41     Polynomial(double scalar=1);
42 
43     /// Construct a linear polynomial
44     Polynomial(double p0, double p1);
45 
46     /// Construct a quadratic polynomial
47     Polynomial(double p0, double p1, double p2);
48 
49     /// Construct a cubic polynomial
50     Polynomial(double p0, double p1, double p2, double p3);
51 
52     /// Construct from a vector of polynomial coefficients
53     template<typename T>
Polynomial(const std::vector<T> & coeff)54     Polynomial(const std::vector<T>& coeff) : p_(coeff.begin(), coeff.end()) {}
55 
56     /// Evaluate numerically
57     template<typename T>
operator ()(const T & x) const58     T operator()(const T& x) const {
59       auto it = p_.rbegin();
60       T ret = *it++;
61       while (it!=p_.rend()) {
62         ret *= x;
63         ret += *it++;
64       }
65       return ret;
66     }
67 
68     /// Degree of the polynomial
69     casadi_int degree() const;
70 
71     /// Get scalar value (error if degree()!=0)
72     double scalar() const;
73 
74     /// Create a new polynomial for the derivative
75     Polynomial derivative() const;
76 
77     /// Create a new polynomial for the anti-derivative (primitive function)
78     Polynomial anti_derivative() const;
79 
80     /// Remove excess zeros
81     void trim();
82 
83     /// Readable name of the class
type_name() const84     std::string type_name() const {return "Polynomial";}
85 
86     /// Print a description of the object
87     void disp(std::ostream& stream, bool more=false) const;
88 
89     // Add
90     Polynomial operator+(const Polynomial& b) const;
91 
92     // Add (in-place)
93     Polynomial& operator+=(const Polynomial& b);
94 
95     // Subtract
96     Polynomial operator-(const Polynomial& b) const;
97 
98     // Subtract (in-place)
99     Polynomial& operator-=(const Polynomial& b);
100 
101     // Multiply
102     Polynomial operator*(const Polynomial& b) const;
103 
104     // Multiply (in-place)
105     Polynomial& operator*=(const Polynomial& b);
106 
107     // Divide by constant
108     Polynomial operator/(double b) const;
109 
110     // Divide by constant (in-place)
111     Polynomial& operator/=(double b);
112 
113 
114   protected:
115     std::vector<double> p_;
116   };
117 
118 } // namespace casadi
119 
120 
121 #endif // CASADI_POLYNOMIAL_HPP
122