# forum.alglib.net

ALGLIB forum
 It is currently Wed Aug 14, 2024 10:16 am

 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.

 Page 1 of 1 [ 2 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Integrate a public but non static function memberPosted: Tue Jul 14, 2020 7:50 pm

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

 Post subject: Re: Integrate a public but non static function memberPosted: Wed Jul 15, 2020 12:42 am

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

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 2 posts ]

 All times are UTC

#### Who is online

Users browsing this forum: No registered users and 11 guests

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

Search for: