PPCKO: Principal Predictive Components for Estimating an Autoregressive Operator
 
Loading...
Searching...
No Matches
interp1D_util.hpp
Go to the documentation of this file.
1// Copyright (c) 2024 Andrea Enrico Franzoni (andreaenrico.franzoni@gmail.com)
2//
3// This file is part of PPCKO
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of PPCKO and associated documentation files (the PPCKO software), to deal
7// PPCKO without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of PPCKO, and to permit persons to whom PPCKO is
10// furnished to do so, subject to the following conditions:
11//
12// PPCKO IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17// OUT OF OR IN CONNECTION WITH PPCKO OR THE USE OR OTHER DEALINGS IN
18// PPCKO.
19
20#ifndef EXAMPLES_SRC_INTERP1D_INTERP1D_UTIL_HPP_
21#define EXAMPLES_SRC_INTERP1D_INTERP1D_UTIL_HPP_
22#include "interp1D.hpp"
23#include <numeric>
24#include <vector>
25
26
33
34
35namespace apsc
36{
37//
49template <typename A, typename CompOper = std::less<double>>
50double
51interp1D(std::vector<A> const &v, double const &keyVal,
52 CompOper const &comp = std::less<double>{})
53{
54 return interp1D(
55 v.cbegin(), v.cend(), keyVal, [](A const &x) { return x[0]; },
56 [](A const &x) { return x[1]; }, comp);
57}
58
59namespace internals
60{
62 template <class T> class map2Vectors
63 {
64 using KeyVec = std::vector<double>;
65 using ValueVec = std::vector<T>;
66 using const_iterator = std::vector<std::size_t>::const_iterator;
67
68 public:
69 map2Vectors(KeyVec const &k, ValueVec const &v)
70 : keys{k}, values{v}, indexes{}
71 {
72 indexes.resize(keys.size());
73 std::iota(indexes.begin(), indexes.end(), 0);
74 }
75
76 const_iterator
77 cbegin() const noexcept
78 {
79 return indexes.cbegin();
80 }
81 const_iterator
82 cend() const noexcept
83 {
84 return indexes.cend();
85 }
86
87 KeyVec const & keys;
88 ValueVec const &values;
89
90 private:
91 std::vector<std::size_t> indexes;
92 };
93
95 template <class T> class extractKey_map2Vectors
96 {
97 public:
98 extractKey_map2Vectors(map2Vectors<T> const &map) : map2v{map} {}
99 double
100 operator()(size_t const &i) const
101 {
102 return map2v.keys[i];
103 }
104
105 private:
106 map2Vectors<T> const &map2v;
107 };
108
110 template <class T> class extractValue_map2Vectors
111 {
112 public:
113 extractValue_map2Vectors(map2Vectors<T> const &map) : map2v{map} {}
114 T
115 operator()(size_t const &i) const
116 {
117 return map2v.values[i];
118 }
119
120 private:
121 map2Vectors<T> const &map2v;
122 };
123} // namespace internals
124//
125
138template <typename T, typename CompOper = std::less<double>>
139T
140interp1D(std::vector<double> const &keys, std::vector<T> const &values,
141 double const &keyVal, CompOper const &comp = std::less<double>{})
142{
143 using namespace internals;
144 map2Vectors<T> map(keys, values);
145 return interp1D(map.cbegin(), map.cend(), keyVal,
146 extractKey_map2Vectors<T>{map},
147 extractValue_map2Vectors<T>{map}, comp);
148}
149} // namespace apsc
150
151#endif /* EXAMPLES_SRC_INTERP1D_INTERP1D_UTIL_HPP_ */
Maps vector of keys and vector of values in a single entity.
Definition interp1D_util.hpp:63
Contains the template class for interpolating an unidimensional-univariate function in a given point.
auto interp1D(RAIterator const &begin, RAIterator const &end, Key const &keyVal, ExtractKey const &extractKey, ExtractValue const &extractValue, CompareKey const &comp=std::less< Key >())
Definition interp1D.hpp:92