time_series_functor.h Source File

CPP API: time_series_functor.h Source File
time_series_functor.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: Rene Schmieding
5 *
6 * Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20 #ifndef MIO_MATH_TIME_SERIES_FUNCTOR_H
21 #define MIO_MATH_TIME_SERIES_FUNCTOR_H
22 
26 
27 #include <cassert>
28 #include <vector>
29 
30 namespace mio
31 {
32 
41 {
43 };
44 
45 template <class FP>
47 {
48 public:
56  : m_type(type)
57  , m_data(data)
58  {
59  // data shape checks and preprocessing
60  switch (m_type) {
62  // make sure data has the correct shape, i.e. a list of (time, value) pairs
63  assert(m_data.get_num_time_points() > 0 && "Need at least one time point for LinearInterpolation.");
64  assert(m_data.get_num_elements() == 1 && "LinearInterpolation requires exactly one value per time point.");
65  assert(m_data.is_strictly_monotonic());
66  break;
67  default:
68  assert(false && "Unhandled TimeSeriesFunctorType!");
69  break;
70  }
71  }
72 
79  TimeSeriesFunctor(TimeSeriesFunctorType type, std::vector<std::vector<FP>>&& table)
80  : TimeSeriesFunctor(type, TimeSeries<FP>{std::move(table)})
81  {
82  }
83 
89  {
90  }
91 
97  FP operator()(FP time) const
98  {
99  switch (m_type) {
101  return linear_interpolation(time, m_data)[0];
102  default:
103  assert(false && "Unhandled TimeSeriesFunctorType!");
104  return FP();
105  }
106  }
107 
110  {
111  return Members("TimeSeriesFunctor").add("type", m_type).add("data", m_data);
112  }
113 
114 private:
117 };
118 
119 } // namespace mio
120 
121 #endif
Definition: time_series_functor.h:47
TimeSeriesFunctor()
Creates a Zero functor.
Definition: time_series_functor.h:87
FP operator()(FP time) const
Function returning a scalar value.
Definition: time_series_functor.h:97
TimeSeries< FP > m_data
Data used by the functor to compute its values. Its shape depends on type.
Definition: time_series_functor.h:116
TimeSeriesFunctor(TimeSeriesFunctorType type, const TimeSeries< FP > &data)
Creates a functor using the given data.
Definition: time_series_functor.h:55
TimeSeriesFunctor(TimeSeriesFunctorType type, std::vector< std::vector< FP >> &&table)
Creates a functor using the given table.
Definition: time_series_functor.h:79
auto default_serialize()
This method is used by the default serialization feature.
Definition: time_series_functor.h:109
TimeSeriesFunctorType m_type
Determines what kind of functor this is, e.g. linear interpolation.
Definition: time_series_functor.h:115
stores vectors of values at time points (or some other abstract variable) the value at each time poin...
Definition: time_series.h:58
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30
auto linear_interpolation(const X &x_eval, const X &x_1, const X &x_2, const V &y1, const V &y2)
Linear interpolation between two data values.
Definition: interpolation.h:44
TimeSeriesFunctorType
Type of a TimeSeriesFunctor.
Definition: time_series_functor.h:41
List of a class's members.
Definition: default_serialize.h:113
Members< ValueTypes..., T > add(const char *member_name, T &member)
Add a class member.
Definition: default_serialize.h:139