Guest User

Untitled

a guest
Jan 23rd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. // innermost function
  2. double g(double x, vector<double> pars){
  3. return x * pars[0] * pars[0];
  4. }
  5. // some intermediate function
  6. template <typename F>
  7. auto f0(F const & f, double x, vector<double> pars) {
  8. return [f, pars](double x, vector<double> pars) {
  9. vector<double> pars_0(pars);
  10. pars_0[0] = 0;
  11. return f(x, pars_0);
  12. };
  13. }
  14. // another intermediate function
  15. template <typename F>
  16. auto f1(F const & f, double x, vector<double> pars) {
  17. return [f, pars](double x, vector<double> pars) {
  18. vector<double> pars_1(pars);
  19. pars_1[0] = 1;
  20. return f(x, pars_1);
  21. };
  22. }
  23. // selector function
  24. template <typename F>
  25. auto D(F const & f, double x, vector<double> pars, int degree) {
  26. if(0 == degree) {
  27. return f0(f, x, pars)(x, pars);
  28. } else {
  29. return f1(f, x, pars)(x, pars);
  30. }
  31. }
  32.  
  33. template <typename F>
  34. auto D(F const & f, double x, vector<double> pars, int degree) {
  35. if(0 == degree) {
  36. return [f, x, pars](F const & f, int x, vector<double> pars) {
  37. return D(f0(f, x, pars)(x, pars)); // <<< ?
  38. };
  39. } else {
  40. return // same stuff for the other function(s)
  41. }
  42. }
  43.  
  44. int sum = 0;
  45. for(int i = 0; i < 3; ++i ){
  46. for(int j = 0; j < 3; ++j ){
  47. for(int k = 0; k < 3; ++k ){
  48. ...
  49. sum += D(...(D(g, i), j)...,k) (pars of the lambdas);
  50. // number of for loops depends on size of pars passed to f
  51. }
  52. }
  53. }
Add Comment
Please, Sign In to add comment