NASA Astrobee Robot Software  0.19.1
Flight software for the Astrobee robots operating inside the International Space Station.
polynomial_basis.h
Go to the documentation of this file.
1 /* Copyright (c) 2017, United States Government, as represented by the
2  * Administrator of the National Aeronautics and Space Administration.
3  *
4  * All rights reserved.
5  *
6  * The Astrobee platform is licensed under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations
16  * under the License.
17  */
18 
19 #ifndef TRAJ_OPT_BASIC_POLYNOMIAL_BASIS_H_
20 #define TRAJ_OPT_BASIC_POLYNOMIAL_BASIS_H_
21 
23 #include <traj_opt_basic/types.h>
24 
25 #include <boost/make_shared.hpp>
26 #include <boost/math/special_functions/binomial.hpp>
27 #include <boost/math/tools/polynomial.hpp>
28 #include <cassert>
29 #include <cmath>
30 #include <memory>
31 #include <vector>
32 
33 namespace traj_opt {
34 
35 // Basis virtual class
36 // //////////////////////////////////////////////////////////////////////////
37 class Basis {
38  public:
39  explicit Basis(uint n_p_); // dimension of basis
40  virtual void differentiate() = 0;
41  virtual void integrate() = 0;
42 
43  // evaluates ith element of basis at x where x is normalized to 0 to 1
44  virtual decimal_t evaluate(decimal_t x, uint i) const = 0;
45  // innerproduct bewtween ith and jth basis functions
46  virtual decimal_t innerproduct(uint i, uint j) const = 0;
47 
48  virtual uint dim();
49  const PolyType &type() const { return type_; }
50  bool orthogonal() { return orthogonal_; }
51 
52  protected:
53  uint n_p;
55  bool orthogonal_{false};
56 };
57 
58 // Poly Calculus Functions
59 // //////////////////////////////////////////////////////////////////////////
60 
61 typedef boost::math::tools::polynomial<decimal_t> Poly;
62 
63 class PolyCalculus {
64  public:
65  static Poly integrate(const Poly &p);
66  // indefinate integral of polynomial with constant 0
67  static Poly differentiate(const Poly &p);
68  // differentiates polynomial
69 };
70 
71 class BasisBundle { // bundles the basis with its derrivatives
72  public:
73  // constructs using arbitrary basis
74  BasisBundle(PolyType type, uint n_p_, uint k_r_);
75  // constructs using legendre
76  BasisBundle(uint n_p_, uint k_r_);
77  // constructs using bezier
78  explicit BasisBundle(int n);
80 
81  // ~BasisBundle();
83  decimal_t x, decimal_t dt, uint coeff,
84  int derr) const; // returns value of basis at value x, with time
85  // dt, basis function coeff, and derrivative
86  // derr
87  boost::shared_ptr<Basis> getBasis(int i);
88 
89  std::vector<boost::shared_ptr<Basis>> derrivatives;
90  std::vector<boost::shared_ptr<Basis>> integrals;
91 
92  protected:
93  uint n_p, k_r;
94 
95  // std::vector<LegendreBasis> derrivatives;
96 };
97 
98 // Polynomial Bases
99 // A trig basis could be implemented, but we can approximate sin and cos with
100 // a Lagrange error bound of 1e-6 with a 7th order polynomial over the input
101 // domain 0 to pi/4.
102 // //////////////////////////////////////////////////////////////////////////
103 
104 // 1, t, t^2, ...
105 class StandardBasis : public Basis {
106  public:
107  explicit StandardBasis(uint n_p_);
108  virtual void differentiate();
109  virtual void integrate();
110  virtual decimal_t evaluate(decimal_t x, uint coeff) const;
111  friend std::ostream &operator<<(std::ostream &os, const StandardBasis &lb);
112  virtual decimal_t innerproduct(uint i, uint j) const;
113  virtual Poly getPoly(uint i) const;
114 
115  protected:
116  std::vector<Poly> polys; // basis polynomials
117  uint k_r;
118 };
119 
120 } // namespace traj_opt
121 #endif // TRAJ_OPT_BASIC_POLYNOMIAL_BASIS_H_
traj_opt::BasisBundle::integrals
std::vector< boost::shared_ptr< Basis > > integrals
Definition: polynomial_basis.h:90
traj_opt::BasisBundle::getBasis
boost::shared_ptr< Basis > getBasis(int i)
Definition: polynomial_basis.cpp:142
types.h
traj_opt::PolyType
PolyType
Definition: traj_data.h:27
traj_opt::Basis::type
const PolyType & type() const
Definition: polynomial_basis.h:49
traj_opt::Basis::orthogonal
bool orthogonal()
Definition: polynomial_basis.h:50
traj_opt::Basis::Basis
Basis(uint n_p_)
Definition: polynomial_basis.cpp:119
traj_data.h
traj_opt::StandardBasis
Definition: polynomial_basis.h:105
traj_opt::StandardBasis::k_r
uint k_r
Definition: polynomial_basis.h:117
traj_opt::Basis::n_p
uint n_p
Definition: polynomial_basis.h:53
traj_opt::BasisBundle::getVal
decimal_t getVal(decimal_t x, decimal_t dt, uint coeff, int derr) const
Definition: polynomial_basis.cpp:96
traj_opt::StandardBasis::getPoly
virtual Poly getPoly(uint i) const
Definition: polynomial_basis.cpp:128
traj_opt::BasisBundle
Definition: polynomial_basis.h:71
traj_opt::BasisBundle::n_p
uint n_p
Definition: polynomial_basis.h:93
traj_opt::BasisBundle::BasisBundle
BasisBundle()
Definition: polynomial_basis.h:79
traj_opt::StandardBasis::evaluate
virtual decimal_t evaluate(decimal_t x, uint coeff) const
Definition: polynomial_basis.cpp:78
traj_opt::decimal_t
double decimal_t
Definition: types.h:35
traj_opt::StandardBasis::differentiate
virtual void differentiate()
Definition: polynomial_basis.cpp:65
traj_opt::Basis::orthogonal_
bool orthogonal_
Definition: polynomial_basis.h:55
traj_opt::StandardBasis::StandardBasis
StandardBasis(uint n_p_)
Definition: polynomial_basis.cpp:129
traj_opt::Basis::innerproduct
virtual decimal_t innerproduct(uint i, uint j) const =0
traj_opt
Definition: msg_traj.h:27
traj_opt::StandardBasis::polys
std::vector< Poly > polys
Definition: polynomial_basis.h:116
traj_opt::PolyCalculus::integrate
static Poly integrate(const Poly &p)
Definition: polynomial_basis.cpp:33
traj_opt::BasisBundle::k_r
uint k_r
Definition: polynomial_basis.h:93
traj_opt::Basis::integrate
virtual void integrate()=0
traj_opt::StandardBasis::innerproduct
virtual decimal_t innerproduct(uint i, uint j) const
Definition: polynomial_basis.cpp:122
traj_opt::Basis::type_
PolyType type_
Definition: polynomial_basis.h:54
traj_opt::Basis::differentiate
virtual void differentiate()=0
traj_opt::StandardBasis::operator<<
friend std::ostream & operator<<(std::ostream &os, const StandardBasis &lb)
Definition: polynomial_basis.cpp:88
traj_opt::PolyCalculus
Definition: polynomial_basis.h:63
traj_opt::PolyCalculus::differentiate
static Poly differentiate(const Poly &p)
Definition: polynomial_basis.cpp:45
traj_opt::Basis::dim
virtual uint dim()
Definition: polynomial_basis.cpp:58
traj_opt::Basis::evaluate
virtual decimal_t evaluate(decimal_t x, uint i) const =0
traj_opt::BasisBundle::derrivatives
std::vector< boost::shared_ptr< Basis > > derrivatives
Definition: polynomial_basis.h:89
traj_opt::Poly
boost::math::tools::polynomial< decimal_t > Poly
Definition: polynomial_basis.h:61
traj_opt::StandardBasis::integrate
virtual void integrate()
Definition: polynomial_basis.cpp:71
traj_opt::Basis
Definition: polynomial_basis.h:37