[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Passing function to function called by qromb etc.?




jwilson@shifty.cs.colorado.edu (John Wilson) writes:
> I would like to numerically integrate f(x) * x in IDL.  This is coming
> up within another function which takes the function f as one of its
> parameters.  What I would like to do is define a function xfx which
> computes f(x) * x for a given x and call qromb with the function
> xfx with a parameter of the function f.  However, I cannot figure out
> how to do this.
> 
> I thought of 
> function xfx,x,func=f
> return, call_function(func,x) * x
> end
> 
> foo=qromb(xfx,a,b,func='bar')
> 
> but that does not work.  What I really need is a capability like the
> bind argument facility of the STL.

What you are really looking for is a way for QROMB to pass keyword
arguments to the integrand.  Unfortunately QROMB doesn't allow you to
do that.  It would be perfectly possible to implement...

The next-best solution is to make your own QROMB_X function and pass
the information via a common block.  I know, it stinks, but IDL leaves
no choice.  You can put all of this into a single file called
qromb_x.pro:

function qromb_xfx, x
  common qromb_x_common, funcname
  return, call_function(funcname, x)*x
end
function qromb_x, fname, a, b, _extra=extra
  common qromb_x_common, funcname
  funcname = fname
  return, qromb('qromb_xfx', a, b, _extra=extra)
end

We basically store the name of the function in the common block where
QROMB_XFX can find it when called.

Here the use of common blocks is not so problematic, since (a) the
common block has a unique name, (b) the common is used only for
internal purposes, and (c) no unintended state is maintained in the
common after returning.  There is some error checking that needs to be
done however, which I omitted because of simplicity and lethargy.

Craig

-- 
--------------------------------------------------------------------------
Craig B. Markwardt, Ph.D.         EMAIL:    craigmnet@cow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
--------------------------------------------------------------------------