Why can't Mathematica 9 compile this code?
x = {1};
Compile[{}, x[[1]]++;,
{{x[[1]]++, _Integer}, {x[[1]], _Integer}}, CompilationTarget :> "C"][]
After printed message CompiledFunction::cflist
, the value of x
is (in my case) {2 + Compile`Variable$29510}
.
Searching this site for "Compile Increment" found Faster list duplicate 'limiter', where an Increment
is used in Compile
and run correctly. I tried to fix my code using Block
:
Compile[{}, Block[{x}, x = {1}; x[[1]]++; x[[1]] ], CompilationTarget :> "C"][]
Those also work.
x = {1}; Compile[{}, x[[1]] += 1;, CompilationTarget :> "C"][]
(replace ++
by +=1
) or
x = {1}; Compile[{}, x[[1]]++;][]
(remove CompilationTarget -> "C"
part)
This seems like a bug. Is that fixed in Mathematica 10?
x
callMainEvaluate
. You can useCompilePrint
to check that. $\endgroup${{x, _Integer, 1}}
, IMO). But actually changing that does not make a difference. There must be an error in theCCodeGenerator`
package. $\endgroup$