[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-gsl] Integration with C++
From: |
GAUTAM DIVGI |
Subject: |
Re: [Help-gsl] Integration with C++ |
Date: |
Sun, 29 Oct 2006 15:55:37 -0600 |
Hi,
Try this. I have marked the changes. Since c routines do not have
information about the object on which to call the method any method in a
class that you wish to pass as a function pointer to a c++ method has to be
static.
class Testo
{
private:
gsl_integration_workspace * w;
gsl_function * pIntegrand;
public:
Testo():
w( gsl_integration_workspace
_alloc(1000)),
pIntegrand( new gsl_function() )
{ ;};
/********************/
/* #(2) */
struct f_params{ double a; double b; };
// -------------- change method to static
-----------------------------
static double f (double x, void * params) {
struct f_params * pList = (struct f_params *) params;
double a = (pList->a);
double b = (pList->b);
return a+b*x;
};
/********************/
double Integrate()
{
double resIntegral, abserr;
struct f_params list= {0.0,1.0};
double x_start=0.0;
double x_end=1.0;
int limit=999;
double epsabs=0.0;
double epsrel=1e-6;
//----------------- change from &f -to- &(Testo::f)
----------------------------
pIntegrand->function = &(Testo::f);
pIntegrand->params = &list;
gsl_integration_qag (pIntegrand, x_start, x_end, epsabs, epsrel,
limit, 1,
w, &resIntegral, &abserr);
return resIntegral;
};
};
---------- Forwarded message ----------
From: "Ivan Liu" <address@hidden>
To: address@hidden
Date: Sun, 29 Oct 2006 17:29:45 +0100
Subject: [Help-gsl] Integration with C++
Hi,
I would like to use the numerical integration routine in a C++ class
environment, and I wrote the following test routine
#include <iostream>
#include <gsl/gsl_integration.h>
using namespace std;
/************************/
// Integrand
/* #(1)
struct f_params{ double a; double b; };
double f (double x, void * params) {
struct f_params * pList = (struct f_params *) params;
double a = (pList->a);
double b = (pList->b);
return a+b*x;
};
*/
/***************************/
class Testo
{
private:
gsl_integration_workspace * w;
gsl_function * pIntegrand;
public:
Testo():
w( gsl_integration_workspace_alloc(1000)),
pIntegrand( new gsl_function() )
{ ;};
/********************/
/* #(2) */
struct f_params{ double a; double b; };
double f (double x, void * params) {
struct f_params * pList = (struct f_params *) params;
double a = (pList->a);
double b = (pList->b);
return a+b*x;
};
/********************/
double Integrate()
{
double resIntegral, abserr;
struct f_params list= {0.0,1.0};
double x_start=0.0;
double x_end=1.0;
int limit=999;
double epsabs=0.0;
double epsrel=1e-6;
pIntegrand->function = &f;
pIntegrand->params = &list;
gsl_integration_qag (pIntegrand, x_start, x_end, epsabs, epsrel,
limit, 1,
w, &resIntegral, &abserr);
return resIntegral;
};
};
int main(void)
{
Testo G;
cout << G.Integrate() << endl;
return 0;
}
The program doesn't compile and the message is:
test.cpp: In member function 'double Testo::Integrate()':
test.cpp:62: error: ISO C++ forbids taking the address of an unqualified
or
parenthesized non-static member function to form a pointer to member
function. Say '&Testo::f'
test.cpp:62: error: cannot convert 'double (Testo::*)(double, void*)' to
'double (*)(double, void*)' in assignment
If I comment out the section marked #(2) and use #(1) instead, the program
works no problem,
but I'd like to put the integrand into the class.
How do I do that?
thanks,
Ivan Liu
_______________________________________________
Help-gsl mailing list
address@hidden
http://lists.gnu.org/mailman/listinfo/help-gsl