[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-gsl] Integration with C++ part2

From: Ivan Liu
Subject: [Help-gsl] Integration with C++ part2
Date: Wed, 29 Nov 2006 14:31:41 +0100


I asked a question on how to use the integration routine with objects
in C++ a while ago. Now here is a related question.

The following test routine works. But I'd like to use the member functions
of class Testo in the integrand function:
static double f (double x, void * params)
without declaring another object of class Testo ( pT in the example).

How can I do that?

Ivan Liu

#include <iostream>
#include <cmath>
#include <gsl/gsl_math.h>
#include <gsl/gsl_integration.h>
using namespace std;

class Testo

   gsl_integration_workspace * w;
   gsl_function * pIntegrand;

       w( gsl_integration_workspace_alloc(1000)),
       pIntegrand( new gsl_function() )
       {  ;};

   double GetX(double x) ////////// member function I'd like to use in
static double f
       return x;

   struct f_params{ double a; double b; };

   static double f (double x, void * params) {
       struct f_params * pList = (struct f_params *) params;
       double a = (pList->a);
       double b = (pList->b);
       Testo * pT;  ////////////////////////////// I don't want to declare
another object of class Testo

       return a+b*(pT->GetX(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 = &(Testo::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;

reply via email to

[Prev in Thread] Current Thread [Next in Thread]