Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2006
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Compile&Which

  • To: mathgroup at smc.vnet.net
  • Subject: [mg66015] Re: Compile&Which
  • From: "Jens-Peer Kuska" <kuska at informatik.uni-leipzig.de>
  • Date: Thu, 27 Apr 2006 02:25:58 -0400 (EDT)
  • Organization: Uni Leipzig
  • References: <e2ne3s$3b7$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Hi,

Compile[] don't know what Which[] returns and
r = Compile[{{x, _Real, 1}},

      Block[{l1 = {0.}},
        Which[x[[1]] == x[[2]], l1 = f[x] + g[x],
              x[[1]] < x[[2]], l1 = f[x] - g[x]];

        l1], {{f[_], _Real, 1}, {g[_], _Real, 1}}]

work fine.

Regards

 Jens



"gregorc" <gregor.cernivec at fe.uni-lj.si> schrieb 
im Newsbeitrag news:e2ne3s$3b7$1 at smc.vnet.net...
| 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
|
| 



  • Prev by Date: Re: Bug in calculating -3^49999.0
  • Next by Date: Re: Bug in calculating -3^49999.0
  • Previous by thread: Compile&Which
  • Next by thread: Re: Re: Compile&Which