teqp 0.19.1
Loading...
Searching...
No Matches
finite_derivs.hpp
Go to the documentation of this file.
1#pragma once
2
3namespace teqp{
4
18template<int Nderiv, int Norder, typename Function, typename Scalar>
19auto centered_diff(const Function &f, const Scalar x, const Scalar h) {
20
21 struct DiffCoeffs {
22 std::valarray<int> k;
23 std::valarray<Scalar> c;
24 };
25
26 // See https://en.wikipedia.org/wiki/Finite_difference_coefficient#Central_finite_difference
27 // Watch out that if you would like to use extended precision, you also need to keep
28 // all the coefficients in extended precision too
29 //
30 // This is because 1.0/2.0 is different than casting each of 1.0 and 2.0 to extended precision
31 // and then taking their ratio
32 using r = Scalar;
33 static std::map<std::tuple<int, int>, DiffCoeffs> CentralDiffCoeffs = {
34 {{1, 2}, {{-1,1}, {-r(1)/r(2), r(1)/r(2)}} },
35 {{1, 4}, {{-2,-1,1,2}, {r(1)/ r(12), -r(2)/r(3), r(2)/r(3), -r(1)/r(12)}} },
36 {{1, 6}, {{-3,-2,-1,1,2,3}, {-r(1)/r(60), r(3)/r(20), -r(3)/r(4), r(3)/r(4), -r(3)/r(20), r(1)/r(60)}} },
37
38 {{2, 2}, {{-1,0,1}, {1, -2, 1} }},
39 {{2, 4}, {{-2,-1,0,1,2}, {r(-1)/r(12), r(4)/r(3), r(-5)/r(2), r(4)/r(3), r(-1)/r(12)}} },
40 {{2, 6}, {{-3,-2,-1,0,1,2,3}, {r(1)/r(90), r(-3)/r(20), r(3)/r(2), r(-49)/r(18), r(3)/r(2), r(-3)/r(20), r(1)/r(90)}} },
41
42 {{3, 2}, {{-2, -1, 0, 1, 2}, {r(-1)/r(2), r(1), 0, r(-1), r(1)/r(2)}} },
43 {{3, 4}, {{-3,-2,-1,0,1,2,3}, {r(1)/r(8), r(-1), r(13)/r(8), 0, r(-13)/r(8), r(1), r(-1)/r(8)}} },
44 {{3, 6}, {{-4,-3,-2,-1,0,1,2,3,4}, {r(-7)/r(240), r(3)/r(10), r(-169)/r(120), r(61)/r(30), 0, r(-61)/r(30), r(169)/r(120), r(-3)/r(10), r(7)/r(240)}} },
45
46 {{4, 2}, {{-2,-1,0,1,2}, {1,-4,6,-4,1}} },
47 {{4, 4}, {{-3,-2,-1,0,1,2,3}, {-r(1)/r(6), r(2), -r(13)/r(2), r(28)/r(3.0), -r(13)/r(2), r(2), -r(1)/r(6)}} },
48 {{4, 6}, {{-4,-3,-2,-1,0,1,2,3,4}, {r(7)/r(240), -r(2)/r(5), r(169)/r(60), -r(122)/r(15), r(91)/r(8), -r(122)/r(15), r(169)/r(60), -r(2)/r(5), r(7)/r(240)}} },
49 };
50
51 auto [k, c] = CentralDiffCoeffs[std::make_tuple(Nderiv, Norder)];
52 if (c.size() == 0) {
53 throw std::invalid_argument("Cannot obtain the necessary finite differentiation coefficients");
54 }
55 // Sanity check...
56 if (c.size() != k.size()) {
57 throw std::invalid_argument("Finite differentiation coefficient arrays not the same size");
58 }
59 Scalar num = 0.0;
60 for (auto i = 0U; i < k.size(); ++i) {
61 num = num + c[i]*f(x + h*k[i]);
62 }
63 auto val = num / pow(h, Nderiv);
64 return val;
65}
66
67template<typename Function, typename Scalarx, typename Scalary>
68auto centered_diff_xy(const Function &f, const Scalarx x, const Scalary y, const Scalarx dx, const Scalary dy) {
69 return (f(x+dx, y+dy) - f(x+dx, y-dy) - f(x-dx, y+dy) + f(x-dx, y-dy))/(4*dx*dy);
70}
71
72}; // namespace teqp
auto centered_diff(const Function &f, const Scalar x, const Scalar h)
auto centered_diff_xy(const Function &f, const Scalarx x, const Scalary y, const Scalarx dx, const Scalary dy)
auto pow(const double &x, const double &e)
Definition types.hpp:189