b2api
B2000++ API Reference Manual, VERSION 4.6
 
Loading...
Searching...
No Matches
b2numerical_differentiation.H
1//------------------------------------------------------------------------
2// b2numerical_differentiation.H --
3//
4// written by Mathias Doreille
5//
6// (c) 2011 SMR Engineering & Development SA
7// 2502 Bienne, Switzerland
8//
9// All Rights Reserved. Proprietary source code. The contents of
10// this file may not be disclosed to third parties, copied or
11// duplicated in any form, in whole or in part, without the prior
12// written permission of SMR.
13//------------------------------------------------------------------------
14
15#ifndef B2NUMERICAL_DIFFERENTIATION_H_
16#define B2NUMERICAL_DIFFERENTIATION_H_
17
18#include <algorithm>
19#include <cmath>
20#include <vector>
21
22namespace b2000 {
23
30void get_central_coefficient(int l, int z, int p, std::vector<double>& a);
31
38void get_forward_coefficient(int l, int z, int p, std::vector<double>& a);
39
52template <typename FUNCTION, typename T, typename T1>
54 FUNCTION& f, const double x, std::vector<T>& res, int derivative_order = 1,
55 int approximation_order = 2, int first_nonzero_derivative_order = 0, double h = 0);
56
69template <typename FUNCTION, typename T, typename T1>
71 FUNCTION& f, const double x, std::vector<T>& res, int derivative_order = 1,
72 int approximation_order = 2, int first_nonzero_derivative_order = 0, double h = 0);
73
75// implementation
77
78template <typename FUNCTION, typename T, typename T1>
80 FUNCTION& f, const double x, std::vector<T>& res, int derivative_order,
81 int approximation_order, int first_nonzero_derivative_order, double h) {
82 std::vector<std::vector<double> > a(res.size());
83 for (unsigned int i = 0; i != res.size(); ++i) {
85 derivative_order + i, first_nonzero_derivative_order, approximation_order, a[i]);
86 }
87 T1 r1, r2;
88 T1 r1_m_r2;
89 T1 r1_p_r2;
90 const double inv_h = 1 / h;
91 const double inv_h_l = std::pow(inv_h, derivative_order);
92 for (unsigned int j = 1; j <= a.back().size() - 1; ++j) {
93 f(x + h * j, r1);
94 f(x - h * j, r2);
95 bool b_r1_m_r2 = false;
96 bool b_r1_p_r2 = false;
97 double inv_h_li = inv_h_l;
98 for (unsigned int i = 0; i != res.size(); ++i, inv_h_li *= inv_h) {
99 if (j >= a[i].size()) { continue; }
100 if ((derivative_order + i) % 2 == 0) {
101 if (!b_r1_p_r2) {
102 r1_p_r2 = r1 + r2;
103 b_r1_p_r2 = true;
104 }
105 res[i] += (inv_h_li * a[i][j]) * r1_p_r2;
106 } else {
107 if (!b_r1_m_r2) {
108 r1_m_r2 = r1 - r2;
109 b_r1_m_r2 = true;
110 }
111 res[i] += (inv_h_li * a[i][j]) * r1_m_r2;
112 }
113 }
114 }
115 {
116 double inv_h_li = inv_h_l;
117 for (unsigned int i = 0; i != res.size(); ++i, inv_h_li *= inv_h) {
118 if (a[i].empty()) { continue; }
119 bool isc = false;
120 if (a[i][0] != 0) {
121 if (!isc) {
122 f(x, r1);
123 isc = true;
124 }
125 res[i] += (inv_h_li * a[i][0]) * r1;
126 }
127 }
128 }
129}
130
131template <typename FUNCTION, typename T, typename T1>
133 FUNCTION& f, const double x, std::vector<T>& res, int derivative_order,
134 int approximation_order, int first_nonzero_derivative_order, double h) {
135 std::vector<std::vector<double> > a(res.size());
136 for (unsigned int i = 0; i != res.size(); ++i) {
138 derivative_order + i, first_nonzero_derivative_order, approximation_order, a[i]);
139 }
140 const int max_m =
141 derivative_order + res.size() + approximation_order - first_nonzero_derivative_order;
142 T1 r;
143 const double inv_h = 1 / h;
144 const double inv_h_l = std::pow(inv_h, derivative_order);
145 for (int j = 0; j <= max_m; ++j) {
146 bool isc = false;
147 double inv_h_li = inv_h_l;
148 for (unsigned int i = std::max(0, int(res.size()) + j - 1 - max_m); i != res.size();
149 ++i, inv_h_li *= inv_h) {
150 if (a[i][j] != 0) {
151 if (!isc) {
152 f(x + h * j, r);
153 isc = true;
154 }
155 res[i] += (inv_h_li * a[i][j]) * r;
156 }
157 }
158 }
159}
160
161} // namespace b2000
162
163#endif /* B2NUMERICAL_DIFFERENTIATION_H_ */
Contains the base classes for implementing Finite Elements.
Definition b2boundary_condition.H:32
void forward_numerical_differentiation(FUNCTION &f, const double x, std::vector< T > &res, int derivative_order=1, int approximation_order=2, int first_nonzero_derivative_order=0, double h=0)
Definition b2numerical_differentiation.H:132
void get_central_coefficient(int l, int z, int p, std::vector< double > &a)
Definition b2numerical_differentiation.C:1996
void central_numerical_differentiation(FUNCTION &f, const double x, std::vector< T > &res, int derivative_order=1, int approximation_order=2, int first_nonzero_derivative_order=0, double h=0)
Definition b2numerical_differentiation.H:79
void get_forward_coefficient(int l, int z, int p, std::vector< double > &a)
Definition b2numerical_differentiation.C:1982