forum.alglib.net

ALGLIB forum
It is currently Tue Dec 10, 2024 10:04 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  [ 5 posts ] 
Author Message
 Post subject: Optimizing non-static member function
PostPosted: Sun Feb 06, 2022 6:29 pm 
Offline

Joined: Sun Feb 06, 2022 6:24 pm
Posts: 13
Hi Everyone,

I need to optimize a function that is a non static member of the class. Because I can't pass a pointer to a non static member function, this command return an error:

Code:
minlmoptimize(state, func);


How can I pass the pointer to the function, considering that I can make it static?


Top
 Profile  
 
 Post subject: Re: Optimizing non-static member function
PostPosted: Sun Feb 06, 2022 10:03 pm 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
minlmoptimize() accepts additional ptr parameter (NULL by default) that is forwarded to the callback func.

You can make a wrapper function that accepts void* pointer, converts it to the pointer to an instance of your class and calls its method. Correct me if I wrong, but you may even use lambdas to create a wrapper function (if you refrain from capturing local variables - such lambdas can't be converted to function pointers).


Top
 Profile  
 
 Post subject: Re: Optimizing non-static member function
PostPosted: Sun Feb 06, 2022 11:25 pm 
Offline

Joined: Sun Feb 06, 2022 6:24 pm
Posts: 13
Yes, I think using lambdas is the best way, but I can't find the correct syntax.
I have tried this:

Code:
minlmoptimize(state, [](auto _) {return fun(_); });


where fun is defined as:

Code:
void myClass::fun(const real_1d_array& X, real_1d_array& f, void* ptr){...}


But this doesn't work because it doesn't recognize this lambda as a pointer to "fun". Where am I wrong?


Top
 Profile  
 
 Post subject: Re: Optimizing non-static member function
PostPosted: Mon Feb 07, 2022 3:05 pm 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
You should change lambda signature to match that of the callback accepted by minlpoptimize: (const real_1d_array& X, real_1d_array& f, void* ptr). What you have here can be cast to pointer to function, but this pointer has completely different signature.


Top
 Profile  
 
 Post subject: Re: Optimizing non-static member function
PostPosted: Mon Feb 07, 2022 3:39 pm 
Offline

Joined: Sun Feb 06, 2022 6:24 pm
Posts: 13
Thanks Sergey for your answer, but every syntax I try, it gives me an error.
Could you show me exactly what I should type?


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

All times are UTC


Who is online

Users browsing this forum: Bing [Bot] and 7 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:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group