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:

funct = Compile[{{M, _Real, 2}, {l, _Integer}},
  Module[{matrix = M, lag = l },
   columns = Length[Transpose[matrix]];
      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.

  • $\begingroup$ 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? $\endgroup$ – Mike Honeychurch Apr 10 '14 at 1:48
  • $\begingroup$ @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. $\endgroup$ – zab Apr 12 '14 at 23:36

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];
            {Table[1., {columns - lag + 1}]},
                    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.


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

| improve this answer | |
  • $\begingroup$ 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. $\endgroup$ – zab Apr 8 '14 at 1:10
  • 1
    $\begingroup$ @zab I didn't get any error or warning :( Have you tried it in a fresh-started Mathematica? $\endgroup$ – Silvia Apr 8 '14 at 1:18
  • 2
    $\begingroup$ 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]]. $\endgroup$ – xzczd Apr 8 '14 at 2:10
  • 1
    $\begingroup$ 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. $\endgroup$ – zab Apr 8 '14 at 2:16
  • $\begingroup$ @xzczd You're right! I made a too hasty conclusion.. $\endgroup$ – Silvia Apr 8 '14 at 2:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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