# Need help with C Code Generator

I'm trying to use the Mathematica C Code Generator package to generate code that is to be included in a web application by a developer. I don't know any C, and this seems to be the only way to integrate my model with the web-app. I am having a lot of difficulties getting off the ground.

A persistent error seems to be that my iteration indices are not recognised as integers. Here's an example:

Needs["CCodeGenerator"]
funct = Compile[{{M, _Real, 2}, {l, _Integer}},
Module[{matrix = M, lag = l },
columns = Length[Transpose[matrix]];
Flatten[
Join[ {{{Table[1, {columns - lag + 1}]}},
Table[matrix[[;; , lag - i + 1 ;; columns - i + 1]] , {i, 1, lag}]} ] , 2]
], {{i, _Integer}}
]


This produces the following error:

Compile::cpintlt: "1;;All at position 2 of matrix[[1;;All,lag-i+1;;columns+i+1]] should be either a nonzero integer or a vector of nonzero integers; evaluation will use the uncompiled function."

Finally when I export the C code, I get errors like

CCodeGenerate::wmreq: "The expression Function[{M,l,iCompile$18},Block[{i=iCompile$18},{All,i}]] requires Mathematica to be evaluated. The function will be generated but can be expected to fail with a nonzero error code when executed. "

Any help would be greatly appreciated.

-
So you want to generate C from Mathematica and then use that C code to deploy in some sort of web app? To digress have you thought have just using webMathematica so that the Mathematica code gets deployed directly? –  Mike Honeychurch Apr 10 '14 at 1:48
@MikeHoneychurch, unfortunately, the client's security protocols don't allow us to install 3rd party software on their server; only what we write for them ourselves. It's an embuggerance. –  zab Apr 12 '14 at 23:36

## 1 Answer

It looks like Compile doesn't handle 1;;All in Span, so why not just use explicit total number of rows?

Compile[{{matrix, _Real, 2}, {lag, _Integer}},
Module[{rows, columns},
{rows, columns} = Dimensions[matrix];
Join[
{Table[1., {columns - lag + 1}]},
Flatten[
Table[
matrix[[1 ;; rows, lag - i + 1 ;; columns - i + 1]],
{i, 1, lag}],
1]]]] //
CCodeGenerate[ #, "fun", "generatedfunc.c"] &


Also please note you leave the columns as an external variable, which will cause the MainEvaluate call.

## Edit:

However, as pointed out by xzczd, Compile does handle All correctly as matrix[[All, lag - i + 1 ;; columns - i + 1]].

-
Thanks so much Silvia. Now I get a new error: CCodeGenerate::wmreq: The expression Compile GetElement requires Mathematica to be evaluated. The function will be generated but can be expected to fail with a nonzero error code when executed. –  zab Apr 8 '14 at 1:10
@zab I didn't get any error or warning :( Have you tried it in a fresh-started Mathematica? –  Silvia Apr 8 '14 at 1:18
Personally I really feel this limit of Compile unreasonable… BTW, another way to circumvent it is to just use All i.e. matrix[[All, lag - i + 1 ;; columns - i + 1]]`. –  xzczd Apr 8 '14 at 2:10
Thanks so much Silvia. It is working now. Looks like my understanding of local and external variables is lacking. I'll read up. I am finding the documentation on Compile and CCodeGenerator less than comprehensive. –  zab Apr 8 '14 at 2:16
@xzczd You're right! I made a too hasty conclusion.. –  Silvia Apr 8 '14 at 2:24