interpolation.h Source File

CPP API: interpolation.h Source File
interpolation.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: David Kerkmann, Sascha Korf, Khoa Nguyen
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_INTERPOLATION_H_
21 #define MIO_MATH_INTERPOLATION_H_
22 #include "memilio/utils/logging.h"
25 
26 #include <cassert>
27 #include <vector>
28 #include <algorithm>
29 
30 namespace mio
31 {
32 
43 template <typename X, typename V>
44 auto linear_interpolation(const X& x_eval, const X& x_1, const X& x_2, const V& y1, const V& y2)
45 {
46  const auto weight = evaluate_intermediate<X>((x_eval - x_1) / (x_2 - x_1));
47  return evaluate_intermediate<V>(y1 + weight * (y2 - y1));
48 }
49 
60 template <class FP>
62 {
63  assert(data.get_num_time_points() > 0 && "Interpolation requires at least one time point.");
64  const auto tp_range = data.get_times();
65  // find next time point in data (strictly) after time
66  const auto next_tp = std::upper_bound(tp_range.begin(), tp_range.end(), time);
67  // interpolate in between values if possible, otherwise return first/last value
68  if (next_tp == tp_range.begin()) { // time is before first data point
69  return data.get_value(0);
70  }
71  else if (next_tp == tp_range.end()) { // time is past or equal to last data point
72  return data.get_last_value();
73  }
74  else { // time is in between data points
75  const auto i = next_tp - tp_range.begin(); // index of strict upper bound
76  return linear_interpolation(time, data.get_time(i - 1), data.get_time(i), data.get_value(i - 1),
77  data.get_value(i));
78  }
79 }
80 
88 template <typename X, typename Y>
89 Y linear_interpolation_of_data_set(std::vector<std::pair<X, Y>> vector, const X& x_eval)
90 {
91  // If the vector is empty or has only 1 node, throw an error
92  if (vector.empty() || vector.size() == 1) {
93  log_error("The vector provided in linear_interpolation_of_data_set() must have more than 1 node.");
94  return 0.0;
95  }
96 
97  // Find the corresponding upper position of the node in the data set
98  size_t upper_pos = std::upper_bound(vector.begin(), vector.end(), x_eval,
99  [](X value, const std::pair<X, Y>& node) {
100  return value <= node.first;
101  }) -
102  vector.begin();
103 
104  if (upper_pos == 0) {
105  return vector[upper_pos].second;
106  }
107 
108  // If the x_eval is between two identifiable nodes in the dataset.
109  if (upper_pos < vector.size()) {
110  return linear_interpolation(x_eval, vector[upper_pos - 1].first, vector[upper_pos].first,
111  vector[upper_pos - 1].second, vector[upper_pos].second);
112  }
113  return 0;
114 }
115 
116 } // namespace mio
117 
118 #endif // MIO_MATH_INTERPOLATION_H_
stores vectors of values at time points (or some other abstract variable) the value at each time poin...
Definition: time_series.h:58
Eigen::Ref< const Vector > get_value(Eigen::Index i) const
reference to value vector at time point i
Definition: time_series.h:298
Eigen::Matrix< FP, Eigen::Dynamic, 1 > Vector
base type of expressions of vector values at a time point
Definition: time_series.h:63
Range< time_iterator > get_times()
Definition: time_series.h:453
Eigen::Index get_num_time_points() const
number of time points in the series
Definition: time_series.h:197
FP & get_time(Eigen::Index i)
time of time point at index i
Definition: time_series.h:272
Eigen::Ref< const Vector > get_last_value() const
reference to value vector at time point (num_timepoints - 1)
Definition: time_series.h:320
trait_value< T >::RETURN_TYPE & value(T &x)
Definition: ad.hpp:3308
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30
Y linear_interpolation_of_data_set(std::vector< std::pair< X, Y >> vector, const X &x_eval)
Linear interpolation between two points of a dataset, which is represented by a vector of pairs of no...
Definition: interpolation.h:89
auto i
Definition: io.h:809
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
void log_error(spdlog::string_view_t fmt, const Args &... args)
Definition: logging.h:100