Skip to content

Instantly share code, notes, and snippets.

@YashasSamaga
Last active December 12, 2017 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YashasSamaga/4b3d7d6cd3116fe1854377e2b76ba217 to your computer and use it in GitHub Desktop.
Save YashasSamaga/4b3d7d6cd3116fe1854377e2b76ba217 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <iomanip>
#include <vector>
#include <functional>
#include <cmath>
typedef long double Type;
constexpr Type eps = 1E-12;
using analysisFunction = std::function<const Type(const Type, const Type)>;
class derivatives {
private:
class derivative;
public:
derivatives(std::vector<analysisFunction>& p_fList) { fList = &p_fList; };
derivative operator[](int index) {
return derivative((*fList).at(index));
}
private:
class derivative {
public:
derivative(analysisFunction& p_f) { f = &p_f; }
Type operator()(Type t, Type u) {
return (((*f)(t, u + eps)) - ((*f)(t,u)))/eps;
}
private:
analysisFunction *f;
};
std::vector<analysisFunction> *fList;
};
int main ()
{
std::vector <analysisFunction> numericalFunctions;
auto& f = numericalFunctions;
f.push_back([](Type t, Type u)->Type { return u*u; });
std::cout << std::setprecision(16);
std::cout << f[0](5.0, 10.0) << '\n';
derivatives dfdt(f);
std::cout << dfdt[0](5.0, 10.0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment