473,785 Members | 2,419 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2963
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()(doub le 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.zbra c(float x1, float x2)
{
the actual zbrac code goes here.
zbrac calls myfunc as necessary
}

float RootFinder.myfu nc{}; // 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
2071
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 function that sums them up... hard coding them is tedious. How might I make this dynamic so that it can handle any amount of expenses? def tot_expenses(self, e0, e1, e2, e3): pass
8
2964
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 p3); The parameters here can be read either in the forward order from p1 till p3 or reverse order from p3 till p1. Can anyone explain what is the advantage/disadvantage of either of
8
3193
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() { } Client.prototype.fullname = "John Smith"; var s = "Client"; eval("var o = new " + s + "();"); alert(o.fullname);
13
3360
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
4359
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 can use dlopen/whatever to convert the function name into a pointer to that function, but actually calling it, with the right number of parameters, isn't easy. As far as I can see, there are only two solutions: 1) This one is portable. If...
4
4802
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 completes it invokes a method from the calling thread. Sort event driven concept with threads. Thanks. Ed Gomez
12
1304
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.. Is there any technique to make the callee to return to some other point(within the current process) other than the callee by changing the call stack in callee... My code runs on 86 processor (If this thread is irrelevent to this group please...
2
1693
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 each time they are used in the second function. So far I'm trying to do something like this: def process_args( ):
0
1107
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
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9489
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10162
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10101
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4063
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.