1
$\begingroup$

Consider this code:

fun = Compile[{}, 
  Catch[Do[
    rn = RandomChoice[{0.8, 0.1, 0.1} -> {1, 2, 3}];
    If[rn > 1, Throw[j]]
    , {j, 1, 10}]]
  , CompilationTarget -> "C"]

At each iteration a number randomly is chosen and if it is greater than 1, then the code must return the number of the iteration in which the condition is satisfied.

If I don't compile to C, then everything is fine, but if I compile to C then I get this error message:

CompiledFunction::cflist: Nontensor object generated; proceeding with uncompiled evaluation. >>

In the documents says it happen when someone tries to add two unequal-in-length lists. I can't see how this cause the problem as I don't add any two lists in the code.

Using Return intead of Throw makes a similar error.

Edit

I think this question might be relevant to this one. I think because sometimes the Throw returns Null there is error.

$\endgroup$
2
  • $\begingroup$ Is it possible to define the output so that it can returns Null too? $\endgroup$
    – MOON
    Commented Feb 11, 2015 at 14:16
  • $\begingroup$ Suppose I give the result of the If statement to another variable like x. Is it possible to check whether x is Null and if so set it equal to 0? Like If[x==Null,x=0] $\endgroup$
    – MOON
    Commented Feb 11, 2015 at 14:29

1 Answer 1

2
$\begingroup$

If you localize rn, then it works.

fun = Compile[{}, 
  Module[{rn}, 
   Catch[Do[rn = RandomChoice[{0.8, 0.1, 0.1} -> {1, 2, 3}];
     If[rn > 1, Throw[j]], {j, 1, 10}]]], CompilationTarget -> "C"]


Table[fun[], {10}]
(*  {1, 1, 2, 3, 3, 10, 2, 11, 2, 7}  *)
$\endgroup$
4
  • $\begingroup$ why does this ever return 11? $\endgroup$
    – george2079
    Commented Feb 11, 2015 at 21:57
  • $\begingroup$ @george2079 In the WVM code, j is 11 when the loop ends and the instruction right after the loop assigns the value j to the return register. See yashar's other post, mathematica.stackexchange.com/questions/73786/…, for a discussion. (I don't think it ought to return 11, of course. It's arguably a bug in the compiler.) $\endgroup$
    – Michael E2
    Commented Feb 11, 2015 at 22:35
  • $\begingroup$ Is there a way to see the generated c code? in any case putting ';0' , or whatever value you want, after the Do inside Catch should let you control the default return value.. $\endgroup$
    – george2079
    Commented Feb 11, 2015 at 23:37
  • $\begingroup$ @george2079 I made such a comment to the OP's q., which I since deleted because it contained an incorrect suggestion of what was going wrong. yashar didn't seem interested. The C code is stored in a directory. Compile with Compiler`$CCompilerOptions = {"CleanIntermediate" -> False} and inspect fun // InputForm; the directory will be in a subdirectory of the directory containing the .dylib file (at least on Mac OSX, Mma V10.0.2). $\endgroup$
    – Michael E2
    Commented Feb 12, 2015 at 0:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.