Reaktoro  v2.11.0
A unified framework for modeling chemically reactive systems
InterpolationUtils.hpp
1 // Reaktoro is a unified framework for modeling chemically reactive systems.
2 //
3 // Copyright © 2014-2024 Allan Leal
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library. If not, see <http://www.gnu.org/licenses/>.
17 
18 #pragma once
19 
20 // Reaktoro includes
21 #include <Reaktoro/Common/Types.hpp>
22 #include <Reaktoro/Common/Matrix.hpp>
23 
24 namespace Reaktoro {
25 
26 auto interpolate(
27  const Vec<double>& temperatures,
28  const Vec<double>& pressures,
29  const Vec<double>& scalars) -> Fn<real(real, real)>;
30 
31 auto interpolate(
32  const Vec<double>& temperatures,
33  const Vec<double>& pressures,
34  const Fn<double(double, double)>& func) -> Fn<real(real, real)>;
35 
36 auto interpolate(
37  const Vec<double>& temperatures,
38  const Vec<double>& pressures,
39  const Vec<Fn<double(double, double)>>& fs) -> Fn<ArrayXr(real, real)>;
40 
42 template<typename T, typename X, typename Y>
43 auto interpolateLinear(T const& x, X const& x0, X const& x1, Y const& y0, Y const& y1)
44 {
45  using R = std::decay_t<decltype(y0 + (y1 - y0)/(x1 - x0) * (x - x0))>;
46  assert(x0 <= x1);
47  if(x0 == x1) return static_cast<R>(y0);
48  return y0 + (y1 - y0)/(x1 - x0) * (x - x0);
49 }
50 
52 template<typename T, typename X, typename Y>
53 auto interpolateQuadratic(T const& x, X const& x0, X const& x1, X const& x2, Y const& y0, Y const& y1, Y const& y2)
54 {
55  assert(x0 <= x1 && x1 <= x2);
56  if(x0 == x1 || x1 == x2) return interpolateLinear(x, x0, x2, y0, y2);
57  const auto l0 = ((x - x1)*(x - x2))/((x0 - x1)*(x0 - x2));
58  const auto l1 = ((x - x0)*(x - x2))/((x1 - x0)*(x1 - x2));
59  const auto l2 = ((x - x0)*(x - x1))/((x2 - x0)*(x2 - x1));
60  return y0*l0 + y1*l1 + y2*l2;
61 }
62 
63 } // namespace Reaktoro
The namespace containing all components of the Reaktoro library.
Definition: Algorithms.hpp:29
std::vector< T > Vec
Convenient alias for std::vector<T>.
Definition: Types.hpp:66
autodiff::real real
The number type used throughout the library.
Definition: Real.hpp:26
auto interpolateLinear(T const &x, X const &x0, X const &x1, Y const &y0, Y const &y1)
Calculate a linear interpolation of y at x with given pairs *(x0, y0)* and *(x1, y1)*.
Definition: InterpolationUtils.hpp:43
std::function< F > Fn
Convenient alias for std::function<R(Args...)>.
Definition: Types.hpp:110
auto interpolateQuadratic(T const &x, X const &x0, X const &x1, X const &x2, Y const &y0, Y const &y1, Y const &y2)
Calculate a quadratic interpolation of y at x with given pairs *(x0, y0)* *(x1, y1)* and *(x2,...
Definition: InterpolationUtils.hpp:53
autodiff::ArrayXreal ArrayXr
Convenient alias to Eigen type.
Definition: Matrix.hpp:87