Continuous Rand With Lagrange Interpolation (suck)


/ Published in: C++
Save to your folder(s)



Copy this code and paste it in your HTML
  1. template<class Rand>
  2. class ContinuousRandWithLagrangeInterpolation {
  3. public:
  4. int num;
  5. double x_max;
  6. Rand rand;
  7. std::vector<double> x, f, den;
  8. ContinuousRandWithLagrangeInterpolation(double _x_max, int _num):
  9. rand(), x_max(_x_max), num(_num),
  10. x(_num+1,0), f(_num+1,0), den(_num+1,0)
  11. {
  12. init();
  13. }
  14. void init(){
  15. int i;
  16. for(i=0;i<num+1;i++){
  17. x[i] = x_max/num*i;
  18. f[i] = rand();
  19. }
  20. for(i=0;i<num+1;i++){
  21. den[i] = mult(x[i], i);
  22. }
  23. }
  24. double mult(const double& y, const int& j){
  25. int i;
  26. double m=1;
  27. for(i=0;i<num+1;i++){
  28. if(i != j){
  29. m *= ( y - x[i] );
  30. }
  31. }
  32. return m;
  33. }
  34. double operator()(const double& y) {
  35. double p=0;
  36. int i;
  37. for(i=0;i<num+1;i++){
  38. p += f[i] * mult(y, i) / den[i];
  39. }
  40. return p;
  41. }
  42. };

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.