forum.alglib.net
http://forum.alglib.net/

Integrate a public but non static function member
http://forum.alglib.net/viewtopic.php?f=2&t=4352
Page 1 of 1

Author:  lesa3111 [ Tue Jul 14, 2020 7:50 pm ]
Post subject:  Integrate a public but non static function member

Hello ALGLIB community.

I am trying to integrate a function-member of a class, and needs some help! I cannot declare the function static because this function uses non-static private members (more specifically use a private member which is another class).

I write here a Minimum (Non) Working Example that mimic my problem.

Code:
#include <functional>
#include <iostream>
#include <cmath>

#include "integration.h"

class Foo;
class Bar;

/*****************************
   Class Foo
*****************************/
class Foo
{
   public:
      // Constructor
      Foo(Bar *b, int toto);

      // Function to integrate
      void int_function_1_func(double x, double xminusa, double bminusx, double &y, void *ptr);
      
   private:
      Bar *m_bar;
      int m_toto;
};

Foo::Foo(Bar *b, int toto) : m_bar(b), m_toto(toto)
{}

void Foo::int_function_1_func(double x, double xminusa, double bminusx, double &y, void *ptr)
{
   double *param = (double *) ptr;
   double p1 = param[0];
   double p2 = param[1];

   y = exp(this->m_toto*x)/(p1 * p2);
}




/*****************************
   Class Bar
*****************************/
class Bar
{
   friend Foo;
   public:
      // Constructor
      Bar();
      
   private:
      int m_a, m_b;
};

Bar::Bar() : m_a(2), m_b(5)
{}

/*****************************
   Main program
*****************************/
int main(int argc, char *argv[])
{
   Bar* b = new Bar();

   Foo f(b, 87);

   double arrayParams[2] = {1, 2};
   double (*params)[2] = &arrayParams;

   alglib::autogkstate s;
   double v;
   alglib::autogkreport rep;
   alglib::autogksmooth(0, 1, s);
   alglib::autogkintegrate(s, ????, params);
   alglib::autogkresults(s, v, rep);

   return 0;
}


The problem is at the near end: what to put in
Code:
alglib::autogkintegrate(s, ????, params);


My guess was a pointer to a member function, but it does not work either... Maybe my hope is in the *ptr argument of alglib::autogkintegrate

Many thanks

Author:  lesa3111 [ Wed Jul 15, 2020 12:42 am ]
Post subject:  Re: Integrate a public but non static function member

In case of anyone needs the answer, I found it using http://www.newty.de/fpt/callback.html
Here is a code to answer my question, you can also find it at https://stackoverflow.com/questions/62904534/integrate-a-public-but-non-static-member-function-with-alglib/62905919#62905919

Code:

#include <functional>
#include <iostream>
#include <cmath>

#include "integration.h"

class Foo;
class Bar;

void* pt2Object; // global variable which points to an arbitrary object

/*****************************
   Class Foo
*****************************/
class Foo
{
   public:
      // Constructor
      Foo(Bar *b, int toto);

      // Function to integrate
      void f(double x, double xminusa, double bminusx, double &y, void *ptr);

      // Wrapper
      static void Wrapper_To_Call_f(double x, double xminusa, double bminusx, double &y, void *ptr);
      
   private:
      Bar *m_bar;
      int m_toto;
};

Foo::Foo(Bar *b, int toto) : m_bar(b), m_toto(toto)
{}

void Foo::f(double x, double xminusa, double bminusx, double &y, void *ptr)
{
   double *param = (double *) ptr;
   double p1 = param[0];
   double p2 = param[1];

   y = exp(this->m_toto*x)/(p1 * p2);
   // y = exp(x)/(p1 * p2);
}

void Foo::Wrapper_To_Call_f(double x, double xminusa, double bminusx, double &y, void *ptr)
{
   // explicitly cast global variable <pt2Object> to a pointer to TClassB
   // warning: <pt2Object> MUST point to an appropriate object!
   Foo* mySelf = (Foo*) pt2Object;

   // call member
   mySelf->f(x, xminusa, bminusx, y, ptr);
}

/*****************************
   Class Bar
*****************************/
class Bar
{
   friend Foo;
   public:
      // Constructor
      Bar();
      
   private:
      int m_a, m_b;
};

Bar::Bar() : m_a(2), m_b(5)
{}

/*****************************
   Main program
*****************************/
int main(int argc, char *argv[])
{
   Bar* b = new Bar();

   // Create Foo
   Foo myFoo(b, 1);
   // Assign global variable which is used in the static wrapper function
   // important: never forget to do this!!
   pt2Object = (void*) &myFoo;

   double arrayParams[2] = {1, 2};
   double (*params)[2] = &arrayParams;

   alglib::autogkstate s;
   double v;
   alglib::autogkreport rep;
   alglib::autogksmooth(0, 1, s);
   alglib::autogkintegrate(s, Foo::Wrapper_To_Call_f, params);
   alglib::autogkresults(s, v, rep);

   std::cout << v << std::endl;

   return 0;
}

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/