codac 2.0.0
Loading...
Searching...
No Matches
codac2_TrajBase.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#include "codac2_Interval.h"
13#include "codac2_Wrapper.h"
15#include "codac2_ValueType.h"
16
17namespace codac2
18{
19 template<typename T>
20 class SampledTraj;
21
22 template<typename T>
23 class TrajBase
24 {
25 public:
26
27 TrajBase()
28 { }
29
30 virtual Index size() const = 0;
31 virtual std::pair<Index,Index> shape() const = 0;
32 virtual bool is_empty() const = 0;
33 virtual Interval tdomain() const = 0;
34 virtual void truncate_tdomain(const Interval& new_tdomain) = 0;
35 virtual typename Wrapper<T>::Domain codomain() const = 0;
36 virtual T operator()(double t) const = 0;
37 virtual typename Wrapper<T>::Domain operator()(const Interval& t) const = 0;
38
39 auto nan_value() const
40 {
41 if constexpr(std::is_same_v<T,double> || std::is_same_v<typename ValueType<T>::Type,ScalarType>)
42 return std::numeric_limits<double>::quiet_NaN();
43
44 else
45 return T((*this)(tdomain().lb())) // we obtain the output dimension by an evalution...
46 .init(std::numeric_limits<double>::quiet_NaN());
47 }
48
49 virtual SampledTraj<T> sampled(double dt) const
50 {
51 assert_release(dt > 0.);
52 assert_release(!is_empty());
53
54 auto tdom = tdomain();
55 SampledTraj<T> straj;
56 for(double t = tdom.lb() ; t < tdom.ub() ; t+=dt)
57 straj.set(t, (*this)(t));
58 straj.set(tdom.ub(), (*this)(tdom.ub()));
59 return straj;
60 }
61
62 template<typename Q>
63 SampledTraj<T> sampled_as(const SampledTraj<Q>& x) const
64 {
65 assert_release(x.tdomain().is_subset(this->tdomain()));
66
67 SampledTraj<T> straj;
68 for(const auto& [ti,dump] : x)
69 straj.set(ti, (*this)(ti));
70 return straj;
71 }
72
73 SampledTraj<T> primitive(const T& y0, double dt) const
74 {
75 assert_release(dt > 0.);
76 assert_release(!is_empty());
77
78 SampledTraj<T> p;
79 double t = tdomain().lb(), last_t = t;
80 p.set(t, y0); t += dt;
81 T y = y0;
82
83 while(t < tdomain().ub())
84 {
85 y += ((*this)(last_t)+(*this)(t))*dt/2.;
86 p.set(t, y);
87 last_t = t;
88 t += dt;
89 }
90
91 t = tdomain().ub();
92 y += ((*this)(last_t)+(*this)(t))*(t-last_t)/2.;
93 p.set(t, y);
94
95 return p;
96 }
97
98 // Implementation in codac2_Trajectory_operator.h
99 AnalyticFunction<typename ValueType<T>::Type> as_function() const;
100 };
101}