Non-numeric arguments to Compile-d functions
- To: mathgroup at smc.vnet.net
- Subject: [mg82117] Non-numeric arguments to Compile-d functions
- From: "Andrew Moylan" <andrew.j.moylan at gmail.com>
- Date: Fri, 12 Oct 2007 03:02:15 -0400 (EDT)
Here's a compiled function that does some (made-up) operation on a real-valued function f: Compile[{}, Do[If[f[i] < 0, Return[{i, f[i]}]], {i, 10}], {{f[_], _Real}} ] (It finds the first integer i such that f[i] is negative.) For the usual reasons of modularity, I would like to make compiled functions like this not refer to global variables named f. (We can't put f as one of the elements of the first argument to compile, because those must all be numeric scalars or arrays.) Here's one way to do this: Module[ {f, compiledpart}, compiledpart = Compile[{}, Do[If[f[i] < 0, Return[{i, f[i]}]], {i, 10}], {{f[_], _Real}} ]; findfirstnegative[tempf_] := (compiledpart /. f -> tempf)[] ] I don't really like this solution because we needed to use two names (f and tempf). Using two names per argument could get messy if there are several non-numeric arguments instead of just f. Is this the recommended method for compiling "as much of a function as possible"? Note that the following attempt (which doesn't need a tempf) doesn't work: With[{rhs = compiledpart}, findfirstnegative[f_] := rhs] It fails because Mathematica automatically renames the f in the CompiledFunction in compiledpart to *avoid* the very naming clash I am trying to create. The With statement effectively creates: findfirstnegative[f$_] := CompiledFunction[{}, Do[If[f$851[i]<0, Return[{i,f$851[i]}]], {i,10}], -CompiledCode- ] What's the neatest, "right" way to make compiled functions that take non-numeric arguments?