Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > calling an arbitrary function w/ arbitrary arguments

Reply
Thread Tools

calling an arbitrary function w/ arbitrary arguments

 
 
Honestmath
Guest
Posts: n/a
 
      12-12-2004
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

 
Reply With Quote
 
 
 
 
Ingo Nolden
Guest
Posts: n/a
 
      12-12-2004
Honestmath wrote:
> 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[u]);
// 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
 
Reply With Quote
 
 
 
 
James Rafter
Guest
Posts: n/a
 
      12-12-2004
>
> 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.

 
Reply With Quote
 
Siemel Naran
Guest
Posts: n/a
 
      12-12-2004
"Honestmath" <> wrote in message
news:...

> 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
compiler would instantiate a new zbrac<Func>. There are ways to avoid this
problem, such as using abstract classes.

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



 
Reply With Quote
 
Jeff Miller
Guest
Posts: n/a
 
      12-12-2004

On 12 Dec 2004 16:52:00 GMT, (Honestmath) wrote:
>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,

 
Reply With Quote
 
Honestmath
Guest
Posts: n/a
 
      12-13-2004
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling a function that expects variable arguments from a functionwith variable arguments Navaneeth C Programming 4 11-20-2010 05:35 AM
Calling an arbitrary function with the right arguments John O'Hagan Python 3 09-27-2010 08:42 AM
how to pass a function name and its arguments inside the arguments of other function? jmborr Python 1 11-03-2007 08:20 AM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
arbitrary number of arguments in a function declaration rbt Python 4 01-02-2005 11:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57