448,617 Members | 1,605 Online Need help? Post your question and get tips & solutions from a community of 448,617 IT Pros & Developers. It's quick & easy.

# Integrating an explicit function of x from a to b

 P: n/a Is this an efficient way to integrate an explicit function of x from a to b: #include using namespace std; class Integrate { public: Integrate(long double (*f)(long double& x)):fn(f){} long double operator()(long double& a, long double& b)const; private: long double (*fn)(long double& x); }; long double square(long double& x); int main() { long double a,b; cout << "Enter a lower and upper bound: "; cin >a >b; Integrate integrate(square); cout << "The integral of x^2 from " << a << " to " << b << " is " << integrate(a,b); return 0; } long double Integrate::operator()(long double& a, long double& b) const { long double sum=0.0; // Evaluate integral{a,b} f(x) dx for (long long n=0;n<=1000000000;n++) { long double x = (n/1000000000.0)*(b-a)+a; sum +=(*fn)(x)*(b-a)/1000000001; } return sum; } long double square(long double& x) { return (x*x); } Is there a way to do this at compile-time w/ template metaprogramming? What about parameterizing it w/ templates? Jul 6 '07 #1
7 Replies

 P: n/a On 2007-07-06 03:55, Protoman wrote: Is this an efficient way to integrate an explicit function of x from a to b: Supposing you've got the integration right then it's probably quite close to optimal for numeric integration, if it's to work for all functions. However there are groups better suited for discussions about mathematical computations on computers, you should try one of those. Of course, symbolic calculations would probably be faster and more accurate (but much harder to implement). #include using namespace std; class Integrate { public: Integrate(long double (*f)(long double& x)):fn(f){} long double operator()(long double& a, long double& b)const; private: long double (*fn)(long double& x); }; long double square(long double& x); I would probably pass these by value and not by reference. Should you use reference at least make it const. int main() { long double a,b; cout << "Enter a lower and upper bound: "; cin >a >b; Integrate integrate(square); cout << "The integral of x^2 from " << a << " to " << b << " is " << integrate(a,b); return 0; } long double Integrate::operator()(long double& a, long double& b) const { long double sum=0.0; // Evaluate integral{a,b} f(x) dx for (long long n=0;n<=1000000000;n++) { long double x = (n/1000000000.0)*(b-a)+a; sum +=(*fn)(x)*(b-a)/1000000001; } return sum; } You probably should add a third parameter (perhaps with a default) so that the user can specify the precision wanted. By using n/100000 I got values quite close but computational speed was much better. long double square(long double& x) { return (x*x); } Is there a way to do this at compile-time w/ template metaprogramming? What about parameterizing it w/ templates? Compile-time (and thus everything that deals with templates) means that everything have to be know when you compile the program. That included the upper and lower bounds, so even if it's possible with templates it would be stupid since you could instead calculate the integral by hand and enter the value as a constant. You could parameterise the Integrate-class for type though, for some calculations long double is not needed, as and example a linear function with a small interval will close to 0 a float will suffice (depending one the accuracy needed). -- Erik Wikström Jul 6 '07 #2

 P: n/a "Protoman" wrote: Is this an efficient way to integrate an explicit function of x from a to b: #include using namespace std; class Integrate { public: Integrate(long double (*f)(long double& x)):fn(f){} long double operator()(long double& a, long double& b)const; private: long double (*fn)(long double& x); }; long double square(long double& x); int main() { long double a,b; cout << "Enter a lower and upper bound: "; cin >a >b; Integrate integrate(square); cout << "The integral of x^2 from " << a << " to " << b << " is " << integrate(a,b); return 0; } long double Integrate::operator()(long double& a, long double& b) const { long double sum=0.0; // Evaluate integral{a,b} f(x) dx for (long long n=0;n<=1000000000;n++) { long double x = (n/1000000000.0)*(b-a)+a; sum +=(*fn)(x)*(b-a)/1000000001; } return sum; } long double square(long double& x) { return (x*x); } Is there a way to do this at compile-time w/ template metaprogramming? What about parameterizing it w/ templates? What is "this"? What is "it"? I have no idea what you are trying to do. You want to integrate a general function and have the answer pop out as a side effect of compiling a program? Jul 6 '07 #4

 P: n/a On 6 Jul, 06:21, "osmium" using namespace std; class Integrate { public: Integrate(long double (*f)(long double& x)):fn(f){} long double operator()(long double& a, long double& b)const; private: long double (*fn)(long double& x); }; long double square(long double& x); int main() { long double a,b; cout << "Enter a lower and upper bound: "; cin >a >b; Integrate integrate(square); cout << "The integral of x^2 from " << a << " to " << b << " is " << integrate(a,b); return 0; } long double Integrate::operator()(long double& a, long double& b) const { long double sum=0.0; // Evaluate integral{a,b} f(x) dx for (long long n=0;n<=1000000000;n++) { long double x = (n/1000000000.0)*(b-a)+a; sum +=(*fn)(x)*(b-a)/1000000001; } return sum; } long double square(long double& x) { return (x*x); } Is there a way to do this at compile-time w/ template metaprogramming? What about parameterizing it w/ templates? What is "this"? What is "it"? I have no idea what you are trying to do. You want to integrate a general function and have the answer pop out as a side effect of compiling a program?- Hide quoted text - - Show quoted text - Go read this book called "C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond" by David Abrahams --ISBN: 978-0321227256-- I can't believe you've never heard of template metaprogramming. Jul 6 '07 #5 