[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Passing Method Name as Argument
From: |
Ken Cline |
Subject: |
Re: Passing Method Name as Argument |
Date: |
Mon, 19 Jan 1998 13:54:16 -0500 (EST) |
Did you try just:
-rk: (double *)y : (double *)dydt : (int)n
: (float)h : (double *)yout
: (void(*)(int, double [], double []))derivs ;
Of course, you might wish to use a typedef to make this more
readable.
I would suggest looking at the `setCompareFunction:' in the
collections lib for an example of how to pass function
pointers into ObjC methods.
As for the compiler warnings:
> warning: `ModelClass' does not respond to `deriv:::'
If you are going to use @selector then you'll need to
to define the particular method for the object receiving
the message. That is ModelClass will need a method like:
-(void) deriv: (int) i : (double []) dblArray1
: (double []) dblArray2 ;
> warning: `ModelClass' does not respond to
`perform:withObject:::'
I think you're looking for `perform:with:::'.
> 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).
Yes, you are correct that if you use selectors then the
arguments will be cast to type `id'. If you want/need to
use selectors (eg because you are putting some action on a
Swarm schedule) then I would follow the work around
suggested by Roger, see .../list-archive.9708/0128.html.
I hope that helps.
Ken.
On Mon, 19 Jan 1998, Steve Emsley wrote:
> 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.
> ==================================
>
_________________________________________________________
Ken Cline address@hidden
SAIC VOICE (410) 571-0413
Annapolis, MD FAX (301) 261-8427
==================================
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.
==================================