Maestro 0.3.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{
20
22{
23 public:
24 // assumes the values in the proper order, if they are not, sort them out before calling this
25 void SetSamples(const std::vector<std::vector<double>> &x, const std::vector<double> &y)
26 {
27 assert(x.size() == y.size());
28 assert(!x.empty());
29 assert(x[0].size() == 2);
30
31 if (x.empty() || y.empty())
32 return;
33
34 interpolators.clear();
35 xValues.clear();
36
37 interpolators.emplace_back();
38 interpolators.back().SetTrueInterpolation(trueInterpolation);
39 xValues.push_back(x[0][0]);
40
41 std::vector<double> xvals;
42 std::vector<double> yvals;
43
44 for (size_t i = 0; i < x.size(); ++i)
45 {
46 if (xValues.back() != x[i][0])
47 {
48 interpolators.back().SetSamples(xvals, yvals);
49 xvals.clear();
50 yvals.clear();
51 interpolators.emplace_back();
52 interpolators.back().SetTrueInterpolation(trueInterpolation);
53 xValues.push_back(x[i][0]);
54 }
55
56 xvals.push_back(x[i][1]);
57 yvals.push_back(y[i]);
58 }
59
60 if (!xvals.empty())
61 interpolators.back().SetSamples(xvals, yvals);
62 }
63
64 double Predict(const std::vector<double> &x) const
65 {
66 assert(x.size() == 2);
67
68 if (x.size() < 2)
69 return 0;
70
71 std::vector<double> vals;
72 vals.reserve(interpolators.size());
73
74 for (size_t i = 0; i < interpolators.size(); ++i)
75 {
76 const double pred = interpolators[i].Predict(x[1]);
77 // std::cout << "Pred: " << pred << std::endl;
78 vals.push_back(pred);
79 }
80
81 HermiteInterpolation secondInterpolator;
82 secondInterpolator.SetTrueInterpolation(trueInterpolation);
83 secondInterpolator.SetSamples(xValues, vals);
84
85 const double val = secondInterpolator.Predict(x[0]);
86
87 if (trueInterpolation)
88 return val;
89
90 const auto m = 1E-12;
91 if (val < m)
92 return m;
93
94 return val;
95 }
96
97 void SetTrueInterpolation(bool reg)
98 {
99 trueInterpolation = reg;
100
101 for (auto &interpolator : interpolators)
102 interpolator.SetTrueInterpolation(reg);
103 }
104
105 private:
106 std::vector<HermiteInterpolation> interpolators;
107 std::vector<double> xValues;
108
109 bool trueInterpolation = false;
110};
111
112} // namespace Utils
113
114#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)
Portable dense contraction kernels.