473,387 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

calling an arbitrary function w/ arbitrary arguments

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

Jul 22 '05 #1
5 2925
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
Jul 22 '05 #2
>
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.

Jul 22 '05 #3
"Honestmath" <ho********@aol.com> wrote in message
news:20***************************@mb-m04.aol.com...
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.

Jul 22 '05 #4

On 12 Dec 2004 16:52:00 GMT, ho********@aol.com (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,

Jul 22 '05 #5
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
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: rbt | last post by:
How do I set up a function so that it can take an arbitrary number of arguments? For example, I have a bunch of expenses which may grow or shrink depending on the client's circumstance and a...
8
by: Muthu | last post by:
I've read calling conventions to be the order(reverse or forward) in which the parameters are being read & understood by compilers. For ex. the following function. int Add(int p1, int p2, int...
8
by: Steve Neill | last post by:
Can anyone suggest how to create an arbitrary object at runtime WITHOUT using the deprecated eval() function. The eval() method works ok (see below), but is not ideal. function Client() { }...
13
by: leaf | last post by:
How can i call arbirary functions at runtime, that with arbirary parameters and types? Can BOOST.Bind do that?
18
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
4
by: Edwin Gomez | last post by:
I'm a C# developer and I'm new to Python. I would like to know if the concept of Asynchronous call-backs exists in Python. Basically what I mean is that I dispatch a thread and when the thread...
12
by: KIRAN | last post by:
hi, the grammer for any programming language says that when a function is called by another function,the callee after executing it's body should return to the point where it left in the caller.....
2
by: John O'Hagan | last post by:
Hi Pythonistas, I'm looking for the best way to pass an arbitrary number and type of variables created by one function to another. They can't be global because they may have different values...
0
by: John O'Hagan | last post by:
On Tue Sep 30 11:32:41 CEST 2008, Steven D'Aprano Thanks, both to you and Bruno for pointing this out, I'll certainly be using it in future.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.