1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
#include <cmath>
#include "HAM.h"
//num_t dn_dxn(level of differentiation, base function, base function argument array, which argument to differentiate)
template <typename T, typename U>
T dn_dxn(unsigned int n, T (*f)(U[]), U args[], unsigned int narg)
{
T h = pow(1e-9, 1 / n); // Smallest reasonable change in x; Value determined by experimentation.
T sum = 0;
for (unsigned int k = 0; k <= n; k++)
{
args[narg] += (n / 2 - k) * h;
sum += pow(-1, k) * nCr(n, k) * f(args);
args[narg] -= (n / 2 - k) * h;
}
return sum / pow(h, n);
}
template <typename T, typename U>
T nCr(T n, U k)
{
return factorial(n) / (factorial(k) * factorial(n - k));
}
template <typename T>
T factorial(T n)
{
if (n < 0)
return NAN;
else if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
template typename dn_dxn<long double, long double>;
template typename dn_dxn<long double, double>;
template typename dn_dxn<double, long double>;
template typename dn_dxn<double, double>;
template typename nCr<unsigned int, unsigned int>;
template typename nCr<unsigned int, unsigned long>;
template typename nCr<unsigned long, unsigned int>;
template typename nCr<unsigned long, unsigned long>;
template typename factorial<unsigned int>;
template typename factorial<unsigned long>;
| |