niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » C++
Arbitrary function with parameter
Post new topic   Reply to topic Page 1 of 1 [3 Posts] View previous topic :: View next topic
Author Message
darknails@gmail.com
*nix forums beginner


Joined: 21 Jul 2006
Posts: 1

PostPosted: Fri Jul 21, 2006 9:58 am    Post subject: Arbitrary function with parameter Reply with quote

Hi, I would like to know how to define a arbitrary mathematical
function with one variable and a list of paramter, like, f(x). For
example, in GSL library this is done as


/* Definition of an arbitrary function with parameters */

struct gsl_function_struct
{
double (* function) (double x, void * params);
void * params;
};

typedef struct gsl_function_struct gsl_function ;

#define GSL_FN_EVAL(F,x) (*((F)->function))(x,(F)->params)

and the usage is here:
http://www.gnu.org/software/gsl/manual/html_node/Providing-the-function-to-solve.html#Providing-the-function-to-solve

I wonder how one does it in C++?

Thanks.
Back to top
joosteto@gmail.com
*nix forums beginner


Joined: 09 Jul 2006
Posts: 20

PostPosted: Fri Jul 21, 2006 10:59 am    Post subject: Re: Arbitrary function with parameter Reply with quote

darknails@gmail.com wrote:
Quote:
Hi, I would like to know how to define a arbitrary mathematical

double (* function) (double x, void * params);
http://www.gnu.org/software/gsl/manual/html_node/Providing-the-function-to-solve.html#Providing-the-function-to-solve

I wonder how one does it in C++?

If you're using gnu stuff anyway, "man stdarg" should do the trick (has
an example).
googling should tell you how to use it on different systems too.
Back to top
mlimber
*nix forums Guru


Joined: 06 Sep 2005
Posts: 1249

PostPosted: Fri Jul 21, 2006 12:28 pm    Post subject: Re: Arbitrary function with parameter Reply with quote

darknails@gmail.com wrote:
Quote:
Hi, I would like to know how to define a arbitrary mathematical
function with one variable and a list of paramter, like, f(x). For
example, in GSL library this is done as


/* Definition of an arbitrary function with parameters */

struct gsl_function_struct
{
double (* function) (double x, void * params);
void * params;
};

typedef struct gsl_function_struct gsl_function ;

#define GSL_FN_EVAL(F,x) (*((F)->function))(x,(F)->params)

and the usage is here:
http://www.gnu.org/software/gsl/manual/html_node/Providing-the-function-to-solve.html#Providing-the-function-to-solve

I wonder how one does it in C++?

In C++, one would use a functor. See this for a description:

http://www.sgi.com/tech/stl/functors.html

See especially the example involving struct less_mag.

Cheers! --M
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [3 Posts] View previous topic :: View next topic
The time now is Sat Nov 22, 2008 9:09 pm | All times are GMT
navigation Forum index » Programming » C++
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Function Pointer Sikandar C 3 Fri Jul 21, 2006 1:23 pm
No new posts Is there C/C++ corresponding function in Linux for Java's... xiebopublic@gmail.com apps 4 Fri Jul 21, 2006 3:22 am
No new posts Is there C/C++ corresponding function in Linux for Java's... xiebopublic@gmail.com C++ 1 Fri Jul 21, 2006 2:44 am
No new posts can I call a internal function directly? minrobin@gmail.com shell 2 Fri Jul 21, 2006 2:17 am
No new posts SOAP::Lite and complex client call parameter gelbeiche Perl 0 Thu Jul 20, 2006 7:37 pm

Mortgage Calculator | Homes for Sale | Loans | Merchant Account | Acer Monitors
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1570s ][ Queries: 16 (0.0837s) ][ GZIP on - Debug on ]