PiecewisePolynomial.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include <array>
7 #include <cstddef>
8 #include <vector>
9 
10 #include "ControlSystem/FunctionOfTime.hpp"
11 #include "DataStructures/DataVector.hpp" // IWYU pragma: keep
12 
13 /// \ingroup ControlSystemGroup
14 /// Contains functions of time to support the dual frame system.
15 namespace FunctionsOfTime {
16 /// \ingroup ControlSystemGroup
17 /// A function that has a piecewise-constant `MaxDeriv`th derivative.
18 template <size_t MaxDeriv>
20  public:
22  double t,
23  std::array<DataVector, MaxDeriv + 1> initial_func_and_derivs) noexcept;
24 
25  ~PiecewisePolynomial() override = default;
26  PiecewisePolynomial(PiecewisePolynomial&&) noexcept = default;
27  PiecewisePolynomial& operator=(PiecewisePolynomial&&) noexcept = default;
29  PiecewisePolynomial& operator=(const PiecewisePolynomial&) = delete;
30 
31  /// Returns the function at an arbitrary time `t`.
32  std::array<DataVector, 1> func(double t) const noexcept override {
33  return func_and_derivs<0>(t);
34  }
35  /// Returns the function and its first derivative at an arbitrary time `t`.
36  std::array<DataVector, 2> func_and_deriv(double t) const noexcept override {
37  return func_and_derivs<1>(t);
38  }
39  /// Returns the function and the first two derivatives at an arbitrary time
40  /// `t`.
42  noexcept override {
43  return func_and_derivs<2>(t);
44  }
45 
46  /// Updates the `MaxDeriv`th derivative of the function at the given time.
47  /// `updated_max_deriv` is a vector of the `MaxDeriv`ths for each component
48  void update(double time_of_update, DataVector updated_max_deriv) noexcept;
49  /// Returns the domain of validity of the function.
50  std::array<double, 2> time_bounds() const noexcept override {
51  return {{deriv_info_at_update_times_.front().time,
52  deriv_info_at_update_times_.back().time}};
53  }
54 
55  private:
56  /// Returns the function and `MaxDerivReturned` derivatives at
57  /// an arbitrary time `t`.
58  /// The function has multiple components.
59  template <size_t MaxDerivReturned = MaxDeriv>
60  std::array<DataVector, MaxDerivReturned + 1> func_and_derivs(double t) const
61  noexcept;
62 
63  // There exists a DataVector for each deriv order that contains
64  // the values of that deriv order for all components.
66 
67  // Holds information at single time at which the `MaxDeriv`th
68  // derivative has been updated.
69  struct DerivInfo {
70  double time;
71  value_type derivs_coefs;
72 
73  // Constructor is needed for use of emplace_back of a vector
74  // (additionally, the constructor converts the supplied derivs to
75  // coefficients for simplified polynomial evaluation.)
76  DerivInfo(double t, value_type deriv) noexcept;
77  };
78 
79  /// Returns a DerivInfo corresponding to the closest element in the range of
80  /// DerivInfos with an update time that is less than or equal to `t`.
81  /// The function throws an error if `t` is less than all DerivInfo update
82  /// times. (unless `t` is just less than the earliest update time by roundoff,
83  /// in which case it returns the DerivInfo at the earliest update time.)
84  const DerivInfo& deriv_info_from_upper_bound(double t) const noexcept;
85 
86  std::vector<DerivInfo> deriv_info_at_update_times_;
87 };
88 } // namespace FunctionsOfTime
std::array< DataVector, 2 > func_and_deriv(double t) const noexcept override
Returns the function and its first derivative at an arbitrary time t.
Definition: PiecewisePolynomial.hpp:36
A function that has a piecewise-constant MaxDerivth derivative.
Definition: PiecewisePolynomial.hpp:19
void update(double time_of_update, DataVector updated_max_deriv) noexcept
Updates the MaxDerivth derivative of the function at the given time. updated_max_deriv is a vector of...
Definition: PiecewisePolynomial.cpp:57
std::array< DataVector, 3 > func_and_2_derivs(double t) const noexcept override
Returns the function and the first two derivatives at an arbitrary time t.
Definition: PiecewisePolynomial.hpp:41
Contains functions of time to support the dual frame system.
Definition: PiecewisePolynomial.hpp:15
std::array< double, 2 > time_bounds() const noexcept override
Returns the domain of validity of the function.
Definition: PiecewisePolynomial.hpp:50
Prefix indicating spatial derivatives.
Definition: PartialDerivatives.hpp:45
Stores a collection of function values.
Definition: DataVector.hpp:46
Base class for FunctionsOfTime.
Definition: FunctionOfTime.hpp:13
std::array< DataVector, 1 > func(double t) const noexcept override
Returns the function at an arbitrary time t.
Definition: PiecewisePolynomial.hpp:32