forum.alglib.net

ALGLIB forum
It is currently Wed Sep 23, 2020 10:00 pm

All times are UTC


Forum rules


1. This forum can be used for discussion of both ALGLIB-related and general numerical analysis questions
2. This forum is English-only - postings in other languages will be removed.



Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Integrate a public but non static function member
PostPosted: Tue Jul 14, 2020 7:50 pm 
Offline

Joined: Tue Jul 14, 2020 7:28 pm
Posts: 2
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


Top
 Profile  
 
 Post subject: Re: Integrate a public but non static function member
PostPosted: Wed Jul 15, 2020 12:42 am 
Offline

Joined: Tue Jul 14, 2020 7:28 pm
Posts: 2
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;
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group