[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Passing Method Name as Argument
From: |
Steve Emsley |
Subject: |
Passing Method Name as Argument |
Date: |
Mon, 19 Jan 1998 11:05:51 GMT |
Hi,
I need to take my utilitarian knowledge of Objective C syntax up a
notch ...
Anyone who has used Numerical Methods Runge Kutta with recognize this
function prototype:
void rk4(double y[], double dydt[], int n, double h, double yout[],
void (*derivs)(int, double [], double []))
The function derivs returns the derivatives of the n equations given
the initial values stored in y. The Runge Kutta advances the solution
for a step h and returns the answer in yout.
This routine is general since the function that calculates the
derivatives is passed as an argument.
I've been using this C version in my model but have now bit the bullet
and written the objectiveC methods:
-rk: (double *)y : (double *)dydt : (int)n : (float)h : (double *)yout
: ?? ;
My problem is that I cannot work ur how to pass this method the
method: -derivs: (int) x : (double *) y : (double *) dydt
Naively I tried passing it as: (id) derivs and all combinations, such
as (id) (derivs: (int) x : (double *) y : (double *) dydx), which is
analogous to the C function being passed as a pointer to the function.
This doesn't work - many compiler objections!
After trawling my cache of mailing list advice and the ObjC manual I
tried to pass it using a selector:
-rk: (double *)y : (double *)dydt : (int)n : (float)h : (double *)yout
: (SEL) deriv;
with the method call:
[self rk: x : initial : 5 : step : result : @selector(derivs3:::)]
This doesn't work either:
warning: `ModelClass' does not respond to `deriv:::'
error: ModelClass (instance)
ModelClass does not recognize deriv:::
IOT trap/Abort
I've tried all the combinations I can think of without success. I then
added the method call:
[self perform:@selector(deriv:::)withObject:2 :yt :dyt];
within the rk method leading to the error:
warning: `ModelClass' does not respond to `perform:withObject:::'
I'm lost ... could someone explain how I should perform this task. It
appears that arguments to selectors can only be type id. My arrays are
standard C arrays rather than swarm objects. I was thinking that I
would have to cast the arguments to type (id). The calls within the rk
method are, for example:
[self deriv: 3 : yt : dym];
How would I pass a double array to the selector.
Thanks in advance.
P.S. Not that my model is sensitive to imprecision in the 10th
sig. fig. does anyone have an explanation for this:
Using Objective C Runge Kutta
5.9584421922662844e-06 1.7028652804204555e-13
Using standard C Runge Kutta
5.9584421935048042e-06 1.7028652818251797e-13
The derivs routine is the same for both. In fact the only difference
is the form of the function header between C and ObjC. This precision
error doesn't propagate (at least I've checked for 24*365*10
iterations and the results from both methods are still the same except
in the 10th sig. fig). On my machine DBL_DIG = 16:
printf(" %25.20e %25.20e \n", (float) 1.e-18, (double) 1.e-18);
gives 1.00000004581370496574e-18 1.00000000000000007154e-18
I can live with that (even when calculating the amount of nutrient
uptake to a single phytoplankton cell in 15 minutes!!) but am curious
as to why C and ObjC lead to different results.
Steve
--
---------------------------------------------------------------------
Steve Emsley Ecosystems Analysis & Management Group
address@hidden University of Warwick, England
http://www.oikos.warwick.ac.uk/~sme/ (+44) (0)1598 753648
---------------------------------------------------------------------
==================================
Swarm-Support is for discussion of the technical details of the day
to day usage of Swarm. For list administration needs (esp.
[un]subscribing), please send a message to <address@hidden>
with "help" in the body of the message.
==================================
- Passing Method Name as Argument,
Steve Emsley <=