# functional-like programming

I'm working on a function that performs some numerical methods - let's say for now, I want to write a function that performs Newton's Method and it takes three parameters:
1.  double f(x) - some function
2.  double df(x) - the derivative of that function
3.  int initial_guess

f(x) and df(x) should be written/defined by the user

Newton's method takes these parameters and iteratively finds a root for f(x) closest to the initial guess.

Is it possible for me to define such a function Newton()?  If so, how do I go about doing it?  And what if I want parameter 1 to be f(x,y,z) instead of f(x) - in the case I want to perform something other than Newton's Method?

Currently, I have to write a new program each time I want to perform such a numerical method - I'm trying to find a way to generalize it.
LVL 2
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Author Commented:
Edited text of question.
0
Commented:
Ignoring the f(x,y,z) stuff for the moment, yes, it can be done.  You can simply use function pointers or (often better) functors.

details follow.
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
First to make it easier to read, declare types to be used as pointers to the two user-defined functions, like

double (*FunPtrTyp)(double x); // Function pointer.
double (*DerPtrTyp)(double x); // Derivative pointer.

Note the two pointer types are equavalent, so just one could be used.

Then define your newton's method function to take pointers to the user defined function, like

double Newton(FunPtrTyp FunPtr, // -> function evaluation procedure.
DerPtrTyp DerPtr, // -> derivative evlautation procedure.
double     Guess) // Initial guess.

Then you would write the "evaluation" functions like

double XSquared(double X)
{
return X*X;
};

double TwoX(double X)
{
return 2*X;
};

Tnen call the newton function like

double Root = Newton(XSquared,TwoX,10);
0
Commented:
If you wanted, you could use functors instead of function pointers.  These are classes that contain virtual functions and usually no data and are used in place of function pointers.   Breifly, what you would do is to define an abstract  base class for the functors that has a pure virtual function used to evaluate the function ("function" meaining f()).  like

class FunBas
{
public:
virtual double Evaluate(double x) const = 0;
}

Then derive classes from this for particular functions (meaning f()).  like

class XSquared : public FunBas
{
public:
virtual double Evaluate(double x) const
{
return x*x;
}
};

Then the newton function would take references (or pointers) to the base class functor object.  (Don't bass by value or it becomes the base class, right?)

double Newton(const FunBas &Fun, // function evaluation functor.
const FunBas &Der, //  derivative evlautation functor.
double     Guess)

Does that help?
0
Author Commented:
So the following statements are type definitions?

double (*FunPtrTyp)(double x);
double (*DerPtrTyp)(double x);

We are defining function pointers, right?  What happens if we want to define function objects instead?
0
Commented:
Now you probably can broaden this approach to deal with f(x,y,z) too, (In terms of C++ you definitely can, the probably part has to do with what you need to do in particular for your case.)  The problem is how does newton's method handle the extra variables?  (I don't remember newton's method anymore)  I know that it tries a bunch of points along an axis (like x) until it narrows in on a root.  but how does it handle it with 2 or 3 dimensions?

If you know how to do it for 2 or 3 dimensions, then I think you will find that the bulk of the code that goes into newton() itself will change depending on the number of dimensions.  If that is the case, you will then find that you have to pass a 4th parameter to newton()  This will be a function pointer or functor to the algorithm to be used by newton().  So this parameter will change depending on how many dimensions you are working with.

Let me know if you have any questions.
0
Author Commented:
oops - ignore that last comment of mine..
0
Author Commented:
I don't think newton's method works for multiple dimensions, but I was wondering about multivariable functions for other numerical methods, like the runge-kutta method for differential equations.

This is helping out quite a bit..
0
Commented:
Then you would probably need to do what I said, use another functor, an algorithm functor.

Like for example, in a 1D case the main function might call this algorithm functor with integer values from 1 to 10.  This algorithm functor would then call another functor (the function (f()) functor) using the value passed to the algorithm functor.)   In a 2D case, when the algorithm functor is called, it might then call the function functor a bunch of times (for different Y values) for each x falue that it is called with.  Make sense?

To make this work smoothly, the algorithm functor would probably store the function functor in itself as a data member.  Thus the function functor could have different numbers of parameters.  (i.e. the function functors would be of different types, because they have different parameters to their evaluetion function, but that is okay because they are not passed directly to the main function, instead they are stored in the derived algorithm functor classes.)
0
Author Commented:
Thanks for the tips - I'll try this out and see what I can get to work.
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.