Alexandria 2.31.0
SDC-CH common library for the Euclid project
Loading...
Searching...
No Matches
FiniteDifference.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2012-2021 Euclid Science Ground Segment
3 *
4 * This library is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU Lesser General Public License as published by the Free
6 * Software Foundation; either version 3.0 of the License, or (at your option)
7 * any later version.
8 *
9 * This library is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12 * details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
26#include <cmath>
27#include <limits>
28
29namespace Euclid {
30namespace MathUtils {
31
32static double guess_h(double initial_h, double x) {
33 volatile double xh = x + initial_h;
34 volatile double h = xh - x;
35 if (h == 0) {
37 }
38 return h;
39}
40
41double derivative(const Function& f, const double x) {
43 h = guess_h(h, x);
44
45 double yh = f(x + h);
46 double y0 = f(x);
47 return (yh - y0) / h;
48}
49
50double derivative2nd(const Function& f, const double x) {
52 h = guess_h(h, x);
53
54 double ymh = f(x - h);
55 double y = f(x);
56 double yph = f(x + h);
57
58 return (yph - 2 * y + ymh) / (h * h);
59}
60
61} // end namespace MathUtils
62} // end namespace Euclid
Interface class representing a function with an arbitrary number of parameters.
Definition Function.h:104
static double guess_h(double initial_h, double x)
double derivative2nd(const Function &f, const double x)
std::array< std::vector< double >, N > Coordinates
Used to pass the grid coordinates to interpn. Internally will make a copy of the required values.
double derivative(const Function &f, const double x)
T nextafter(T... args)
T sqrt(T... args)