Arbitrary function with parameter

D

darknails

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/man...to-solve.html#Providing-the-function-to-solve

I wonder how one does it in C++?

Thanks.
 
M

mlimber

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/man...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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top