"Dave" <Da***********@gmail.comwrote in message
news:11**********************@n76g2000hsh.googlegr oups.com...
: I make this question clearer. Here is the routin in the NR
:
: void NR::amoeba(Mat_IO_DP &p, Vec_IO_DP &y, const DP ftol, DP
: funk(Vec_I_DP &), int &nfunk)
:
: You can see that DP funk(Vec_I_DP &) is a function call. Inside
: NR::amoeba, funk is called several times to do the calculation.
: The user are supposed to provide a function of this type. For example,
: the following;
:
: DP func(Vec_I_DP &x)
: {
: return 0.6-
: NR::bessj0(SQR(x[0]-0.5)+SQR(x[1]-0.6)+SQR(x[2]-0.7));
: }
:
: But my function needs another extra parameter, my function looks like
: this:
:
: DP myfunc(Vec_I_DP &x, Vec_I_DP &y)
: {
: return x[0]*y[i]+x[1]*(y[i]*y[i-1])+x[2]*exp(-y[i]);
: }
:
: if I use NR::amoeba NR::amoeba(Mat_IO_DP &p, Vec_IO_DP &y, const DP
: ftol, DP myfunk(Vec_I_DP &), int &nfunk), then it will not work.
: How can I get around this? I don't want to change NR::amoeba. At this
: moment, I declare Vec_I_DP y as a global variable. But I don't like
: it.
You are right to dislike this approach -- but the poorly designed
NR::amoeba leaves you no choice.
To avoid this limitation, in C++, function objects should be used
instead of function pointers. See for example the 3-parameter
version of std::sort in the standard library, header <algorithm>.
Your preferred C++ book should explain how it is used.
In a C library, an alternative is to pass add additional "cookie"
parameter - an additional void* pointer that is passed to the
"engine" function, which the engine function passes through to
the callback.
The Numerical Recipes in C++ library suffers from being a
straightforward translation of the original Fortran code.
It is far from following good C++ design practices, and
would have to be heavily reworked to make it usable
in production C++ code...
I hope this helps,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <
http://www.brainbench.com