RE: Compile Module w/ Function inside?
- To: mathgroup at smc.vnet.net
- Subject: [mg42235] RE: [mg42225] Compile Module w/ Function inside?
- From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
- Date: Wed, 25 Jun 2003 01:53:30 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
>-----Original Message----- >From: frankeye at cox.net [mailto:frankeye at cox.net] To: mathgroup at smc.vnet.net >Sent: Tuesday, June 24, 2003 7:27 AM >To: mathgroup at smc.vnet.net >Subject: [mg42235] [mg42225] Compile Module w/ Function inside? > > >Hi, > > >I'm trying to speedup a Module. Poking about, I've learned that a >significant impediment to fast Compile'd code is the referencing of >global or otherwise external functions within the Compile'd Module >(indeed, one can check to see if the compiled opcode string is >"pure"). > >As my present Module *does* external function referencing, I wish to >bring these functions inside the Module, defining them using >Function[]. (Believe me, "in-lining" my functions' code within the >Module, every place they need to be invoked, is not an option). >Surely defining (pure) Functions inside Modules is allowed in general >(e.g., outside Compile[]), but I can't find an arrangement that is >Compile'able. > >Here is an example that goes nowhere, i.e., can be evaluated >(foo[3.]=6.), but does not use compiled code: > >foo = Compile[{x}, Module[{q = Function[p, p*2.]}, > q[x]], {{q[_], _Real}, {Function[___], _Real}}] > >In: foo[3.] > >CompiledFunction::cfse: Compiled expression Function[p,p 2.] should be >a \ >machine-size real number. > >Out: 6. > >Any means/hope to achieve my objective? > > >Thanks! > Frankeye, well, syntax is more delicate with Compile. Yet me, too, I couldn't make work your idea. And I don't think it is possible, since Compile can only handle the types Real, Integer, Complex, True|False. However, why can't you follow this simple pattern? In[30]:= q = Compile[{{x, _Real}}, 2. x] Out[30]= CompiledFunction[{x}, 2. x, "-CompiledCode-"] In[31]:= foo = Compile[{{x, _Real}}, q[x], {{q[_], _Real}}] Out[31]= CompiledFunction[{x}, q[x], "-CompiledCode-"] In[32]:= %[[4]] Out[32]= {{1, 4}, {21, Function[{x}, q[x]], 3, 0, 0, 3, 0, 1}, {2}} In[33]:= foo[3.] Out[33]= 6. If your code is slow, then not because of that reference to global q. -- Hartmut Wolf