calling an arbitrary function w/ arbitrary arguments

H

Honestmath

Hello everyone,

I'm using a root finding algorithm in a function that only takes as arguments a
function pointer and two variables that represent guesses at the roots of the
function.

int zbrac(float (*func)(float), float *x1, float *x2)
{
float f1,f2;
f1=(*func)(*x1);
f2=(*func)(*x2);
// other stuff here
}

Problem is, I'm not sure how to pass parameters to the root finding function.
For example, if I have an Nth-order polynomial, I could have loads of different
coefficients, but obviously the coefficients will change from function to
function.

I don't want to use global variables, but figured there was a way to modify the
above root-finder to accept and then pass a variable number of parameters to
the polynomial (or whatever) function.

How would I do this? Or, is there a better way?

Thanks!

Math
 
I

Ingo Nolden

Honestmath said:
Hello everyone,

I'm using a root finding algorithm in a function that only takes as arguments a
function pointer and two variables that represent guesses at the roots of the
function.

int zbrac(float (*func)(float), float *x1, float *x2)
{
float f1,f2;
f1=(*func)(*x1);
f2=(*func)(*x2);
// other stuff here
}

Problem is, I'm not sure how to pass parameters to the root finding function.
For example, if I have an Nth-order polynomial, I could have loads of different
coefficients, but obviously the coefficients will change from function to
function.

I don't want to use global variables, but figured there was a way to modify the
above root-finder to accept and then pass a variable number of parameters to
the polynomial (or whatever) function.

How would I do this? Or, is there a better way?

Hi Honestmath,

seems not much like c++ rather c what you wrote.

In c++ I would write a polynomial class. That could be derived from a
more general MathFunction class.

The polynomial class would look something like that:

class Poly
{
public:
unsigned get_Order( );
void set_Order( );
float operator[]( unsigned idx ); // would return a
// coefficient (matter of taste)
float Value( float x );
};

You could pass the object by reference, or by pointer and the root
solver could do with it whatever necessary.

For the "guesses", in case of c++ I would use a dynamic array. You could
simply pass a std::vector.

If you want to stick with c: a pointer to an array would do it:

int zbrac(float (*func)(float), float *x, unsigned n)
{
for( unsigned u = 0; u < n; ++u )
{
f = (*func)(x);
// do whatever whith f
}

}

If it should be fast however you should watch out for expression
templates. But it depends also on wether the equation is determined at
runtime or if it is defined at compile time.

cherrs Ingo
 
J

James Rafter

int zbrac(float (*func)(float), float *x1, float *x2)
{
float f1,f2;
f1=(*func)(*x1);
f2=(*func)(*x2);
// other stuff here
}

As a general rule you can wrap any function in another function in
order to make it's signature conform to the way you need to call it
from the above calling point. The function sets up the real function's
calling parameters except for the ones that zbrac will supply.

In C++, objects that accomplish this are call functors. The STL has
these and there are other libraries that implement more general forms.
For instance, the Loki library implements functors.
 
S

Siemel Naran

I'm using a root finding algorithm in a function that only takes as arguments a
function pointer and two variables that represent guesses at the roots of the
function.

int zbrac(float (*func)(float), float *x1, float *x2)
{
float f1,f2;
f1=(*func)(*x1);
f2=(*func)(*x2);
// other stuff here
}

Problem is, I'm not sure how to pass parameters to the root finding function.
For example, if I have an Nth-order polynomial, I could have loads of different
coefficients, but obviously the coefficients will change from function to
function.

Replace func of type float (*)(float) with a class. For example,

template <class Func>
int zbrac(const Func& func, float x1, float x2) {
float f1 = func(x1);
float f2 = func(x2);
}

One uses it thus:

class Quadratic {
explicit Quadratic(float a, float c, float c);
float operator()(double x) const {
return a*x*x + b*x + c;
}
};

An aside, why does the function return an int, and why did you pass x1 and
x2 by pointer, and why use float as opposed to double?

This method could result in much code bloat, as for each different Func that
I don't want to use global variables, but figured there was a way to modify the
above root-finder to accept and then pass a variable number of parameters to
the polynomial (or whatever) function.

Global variables would make it diffcult, if not impossible, if you have many
Quadratic objects. Nor are they elegant in this context.
 
J

Jeff Miller

I'm using a root finding algorithm in a function that only takes as arguments a
function pointer and two variables that represent guesses at the roots of the
function.

int zbrac(float (*func)(float), float *x1, float *x2)

Problem is, I'm not sure how to pass parameters to the root finding function.

I had the analogous problem with the simplex function minimization
method, and I found that the following sort of object-based solution
worked perfectly. (Pardon my fractured syntax--I'm really a
Pascal programmer at heart.)

First, define an abstract "RootFinder" class something like this:

class RootFinder {
int zbrac(float x1, float x2);
virtual float myfunc;
}

int RootFinder.zbrac(float x1, float x2)
{
the actual zbrac code goes here.
zbrac calls myfunc as necessary
}

float RootFinder.myfunc{}; // This is just a dummy

Now, you can descend any desired specific type of
rootfinder from this abstract class, for example:

class PolyRootFinder : RootFinder {
int Degree;
float *Coefficients;
virtual float myfunc;
}

zbrac need not be redefined--it is inherited from
the abstract class. Instead, you just define
the exact function that you want for this
particular rootfinder:

float PolyRootFinder.myfunc{
// Computations relevant to polynomial go here
// Note that this function has access to Degree
// and Coefficients as part of the class, so you
// don't need to pass these values "through" zbrac.
// That's what I really like about object-oriented
// numerical programming.
}

Similarly, you descend another new class for any type of function with
which you want to use zbrac.

HTH,
 
H

Honestmath

Thank you to all who responded, I got some very useful stuff. There are a few
different approaches here and that gives me some good ideas. I still need to
find out about the 'functors', though, never heard of those beasts. Can't
really find them in the MSDN library either.

Yes, the code snipped I provided is C code as I got it from Numerical Recipes
in C. I'm coming from a C background making the switch to C++ but I thought I
would use the same methodology. However, I knew a class would enter the
equation -- I just knew it would -- I mean, it had to, right? -- but wasn't
entirely sure how. There's a fair bit to chew on here so I'll be living
underground for awhile.

I feel I will end up taking the class function approach. If I wanted to stick
to the standard C aproach, it looks like I would have to pass zbrac a pointer
to an array that contained my arguments, and then pass that to the calculating
function. The array could be any size, so it just needs the one argument.

Thanks again!

Math
 

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
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top