calling an arbitrary function w/ arbitrary arguments

Discussion in 'C++' started by Honestmath, Dec 12, 2004.

  1. Honestmath

    Honestmath Guest

    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
     
    Honestmath, Dec 12, 2004
    #1
    1. Advertising

  2. Honestmath

    Ingo Nolden Guest

    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);
    // 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
     
    Ingo Nolden, Dec 12, 2004
    #2
    1. Advertising

  3. Honestmath

    James Rafter Guest

    >
    > 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.
     
    James Rafter, Dec 12, 2004
    #3
  4. Honestmath

    Siemel Naran Guest

    "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.
     
    Siemel Naran, Dec 12, 2004
    #4
  5. Honestmath

    Jeff Miller Guest

    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,
     
    Jeff Miller, Dec 12, 2004
    #5
  6. Honestmath

    Honestmath Guest

    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
     
    Honestmath, Dec 13, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rbt
    Replies:
    4
    Views:
    379
    it's me
    Jan 2, 2005
  2. jmborr
    Replies:
    1
    Views:
    435
    Stargaming
    Nov 3, 2007
  3. John O'Hagan
    Replies:
    3
    Views:
    239
    Peter Otten
    Sep 27, 2010
  4. Navaneeth
    Replies:
    4
    Views:
    559
    Kenny McCormack
    Nov 20, 2010
  5. Marcin
    Replies:
    4
    Views:
    101
    Jorge
    Nov 27, 2008
Loading...

Share This Page