Alexandria  2.19
Please provide a description of the project.
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 
29 namespace Euclid {
30 namespace MathUtils {
31 
32 static 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 
41 double 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 
50 double 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
Euclid::MathUtils::derivative
double derivative(const Function &f, const double x)
Definition: FiniteDifference.cpp:41
Euclid::MathUtils::derivative2nd
double derivative2nd(const Function &f, const double x)
Definition: FiniteDifference.cpp:50
Euclid::MathUtils::Function
Interface class representing a function.
Definition: Function.h:46
std::sqrt
T sqrt(T... args)
FiniteDifference.h
std::nextafter
T nextafter(T... args)
Euclid::MathUtils::guess_h
static double guess_h(double initial_h, double x)
Definition: FiniteDifference.cpp:32
Euclid
Definition: InstOrRefHolder.h:29
std::numeric_limits