Compile&Which

*To*: mathgroup at smc.vnet.net*Subject*: [mg66007] Compile&Which*From*: gregorc <gregor.cernivec at fe.uni-lj.si>*Date*: Wed, 26 Apr 2006 04:38:23 -0400 (EDT)*Sender*: owner-wri-mathgroup at wolfram.com

Hello, I have trouble to understand how does the Compile function handle the Which statement. Following there are two examples (just as ilustration), both are compiled OK, but the second one does not evaluate as a compiled expression. Can someone explain the evaluation error? It is probably because the Which statement, but I do not know how to do this correctly. Much thanks! 1st example: f = Compile[{{x, _Real, 1}}, {x[[1]]}]; g = Compile[{{x, _Real, 1}}, {x[[2]]}]; r = Compile[{{x, _Real, 1}}, f[x] + g[x], {{f[x], _Real, 1}, {g[x], _Real, 1}}] CompiledFunction[{x}, f[x] + g[x], -CompiledCode-] r[{3, 4}] {7.} 2nd example: f = Compile[{{x, _Real, 1}}, {x[[1]]}]; g = Compile[{{x, _Real, 1}}, {x[[2]]}]; r = Compile[{{x, _Real, 1}}, Which[x[[1]] ? x[[2]], f[x] +g[x], x[[1]] < x[[2]], f[x] -g[x]], {{f[x], _Real, 1}, {g[x], _Real, 1}}] CompiledFunction[{x}, Which[x?1? ? x?2?, f[x] + g[x], x?1? < x?2?, f[x] - g[x]], -CompiledCode-] r[{3, 4}] CompiledFunction::cfse: Compiled expression {-1.} should be a machine-size \real number. CompiledFunction::cfex: External evaluation error at instruction 2; \proceeding with uncompiled evaluation. {-1.} Thanks, Cernivec Gregor