Message Boards Message Boards

0
|
8050 Views
|
2 Replies
|
2 Total Likes
View groups...
Share
Share this post:

CCodeStringGenerate on functions defined in notebook

Posted 10 years ago
Hello,

we are currently evaluating Mathematica as an platform for implementation of our calculations. Thus I am interested in the compilation feature for C.

I have a notebook which contains functions defined in the usual way:

In[210]:= func1[a_, b_] := a*b

In[211]:= func1[2, 3]

Out[211]= 6


I would like to compile this function into C code:

In[212]:= << CCodeGenerator`

In[213]:= cFun = Compile[{a, b}, func1];
CCodeStringGenerate[cFun, "bla"]

During evaluation of In[213]:= CCodeGenerate::wmreq: The expression Function[{a,b},func1] requires Mathematica to be evaluated. The function will be generated but can be expected to fail with a nonzero error code when executed. >>

if I put a*b directly into the Compile[] statement it works.
Why is that? is there a way around that or do I have to repeat all the functions defined in the notebook to get them into C?

Kind regards,
Mathias
POSTED BY: Mathias Breuss
2 Replies
Compile has attributes HoldAll.  Try Compile[{a,b}, Evaluate @ func1[a,b]].  That will give you a function compiled to the Wolfram Virtual Machine.  You can then use CCodeGenerate on that.
POSTED BY: Frank Kampas
Posted 10 years ago
That seems to work. Thank You!
POSTED BY: Mathias Breuss
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