Maestro 0.2.11
Unified interface for quantum circuit simulation
Loading...
Searching...
No Matches
BivariateHermiteInterpolation.h
Go to the documentation of this file.
1
10
11#pragma once
12
13#ifndef __UTILS_BIVARIATE_HERMITE_INTERPOLATION_H__
14#define __UTILS_BIVARIATE_HERMITE_INTERPOLATION_H__
15
17
18namespace Utils {
19
21 public:
22 // assumes the values in the proper order, if they are not, sort them out before calling this
23 void SetSamples(const std::vector<std::vector<double>>& x, const std::vector<double>& y)
24 {
25 assert(x.size() == y.size());
26 assert(!x.empty());
27 assert(x[0].size() == 2);
28
29 if (x.empty() || y.empty())
30 return;
31
32 interpolators.clear();
33 xValues.clear();
34
35 interpolators.emplace_back();
36 interpolators.back().SetTrueInterpolation(trueInterpolation);
37 xValues.push_back(x[0][0]);
38
39 std::vector<double> xvals;
40 std::vector<double> yvals;
41
42 for (size_t i = 0; i < x.size(); ++i)
43 {
44 if (xValues.back() != x[i][0])
45 {
46 interpolators.back().SetSamples(xvals, yvals);
47 xvals.clear();
48 yvals.clear();
49 interpolators.emplace_back();
50 interpolators.back().SetTrueInterpolation(trueInterpolation);
51 xValues.push_back(x[i][0]);
52 }
53
54 xvals.push_back(x[i][1]);
55 yvals.push_back(y[i]);
56 }
57
58 if (!xvals.empty())
59 interpolators.back().SetSamples(xvals, yvals);
60 }
61
62 double Predict(const std::vector<double>& x) const
63 {
64 assert(x.size() == 2);
65
66 if (x.size() < 2)
67 return 0;
68
69 std::vector<double> vals;
70 vals.reserve(interpolators.size());
71
72 for (size_t i = 0; i < interpolators.size(); ++i)
73 {
74 const double pred = interpolators[i].Predict(x[1]);
75 //std::cout << "Pred: " << pred << std::endl;
76 vals.push_back(pred);
77 }
78
79 HermiteInterpolation secondInterpolator;
80 secondInterpolator.SetTrueInterpolation(trueInterpolation);
81 secondInterpolator.SetSamples(xValues, vals);
82
83 const double val = secondInterpolator.Predict(x[0]);
84
85 if (trueInterpolation)
86 return val;
87
88 const auto m = 1E-12;
89 if (val < m)
90 return m;
91
92 return val;
93 }
94
95 void SetTrueInterpolation(bool reg)
96 {
97 trueInterpolation = reg;
98
99 for (auto& interpolator : interpolators)
100 interpolator.SetTrueInterpolation(reg);
101 }
102
103 private:
104 std::vector<HermiteInterpolation> interpolators;
105 std::vector<double> xValues;
106
107 bool trueInterpolation = false;
108 };
109
110}
111
112#endif // __UTILS_BIVARIATE_HERMITE_INTERPOLATION_H__
void SetSamples(const std::vector< std::vector< double > > &x, const std::vector< double > &y)
double Predict(const std::vector< double > &x) const
double Predict(double x) const
void SetSamples(const std::vector< T > &x, const std::vector< double > &y)
Definition Alias.h:22