Message Boards Message Boards

[?] Compile and infer the rank of tensor correctly?

Posted 7 years ago

In one of my projects, I encountered the following weird behavior of Compile:

In[94]:= Compile[{{r, _Integer}}, 
  Module[{gst = Total@RandomVariate[GammaDistribution[3, 1], r]}, 
   gst]][5]
Out[94]= 12.8387

In[93]:= Compile[{{r, _Integer}}, 
  Module[{gs = RandomVariate[GammaDistribution[3, 1], r]}, 
   Total@gs]][5]
During evaluation of In[93]:= CompiledFunction::cfse: Compiled expression {2.77053,4.99208,2.51185,1.84878,2.6716} should be a machine-size real number.
During evaluation of In[93]:= CompiledFunction::cfex: Could not complete external evaluation at instruction 2; proceeding with uncompiled evaluation.
Out[93]= 14.6549

In[92]:= Compile[{{r, _Integer}}, 
  Module[{gs = RandomVariate[NormalDistribution[3, 1], r]}, 
   Total@gs]][5]
Out[92]= 14.2334

It seems that the rank of RandomVariate[NormalDistribution[x,y],l] can be properly inferred, but not for GammaDistribution.

Thanks in advance!

POSTED BY: Jinxu Zhao
4 Replies
Posted 7 years ago

Thank you so much for the link and your quick reply!

POSTED BY: Jinxu Zhao

See: Which Distributions can be Compiled using RandomVariate

And for a list of the Heads of Compilable functions execute

Sort[Compile`CompilerFunctions[]]

(Note that some argument froms of the heads may not be compilable as you saw from the GammaDistributions)

POSTED BY: David Reiss
POSTED BY: David Reiss
Posted 7 years ago

Thanks for your reply!

But why some of them are not compilable? e.g. Does it imply that these functions are implemented differently? And how they differ?

POSTED BY: Jinxu Zhao
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract