The first part below is already answered, but I've got another question which I think is closely related to this one, so I edited my question.
First part: I started compiling in Mathematica a few weeks ago. In the code below, I want to put the integer value 0 into the list "Pre". If I "Print" the list, it will look like {0,0,....,0}. But the output of the code is {0.,0.,0.,....,0.}. So, my question is, how can I manage it, that the output looks like the "Print", i.e. I want the list to have integer values and not real numbers.
In[1]:= CInit = Compile[{{v0, _Integer, 0}, {s0, _Integer, 0}},
Module[{s = s0, v = v0, d, Pre},
d = Table[$MaxMachineNumber, {v}];
Pre = Table[0, {v}];
Print[Pre];
d[[s]] = 0.;
{d, Pre}
], CompilationTarget -> "C"];
CInit[5, 1]
During evaluation of In[1]:= {0,0,0,0,0}
Out[2]= {{0., 1.79769*10^308, 1.79769*10^308, 1.79769*10^308,
1.79769*10^308}, {0., 0., 0., 0., 0.}}
Second part:
If I have an empty list and I want to put Integers into it, how can I let Mathematica know that it is a list of Integers and not Reals? I tried to manage this with the third argument in Compile, but that didn't work. For example:
In[22]:= CTest = Compile[{{s, _Integer}}, Module[{Perm = {s}},
Perm = {};
Append[Perm, 1]
], {{Perm, _Integer, 1}}, CompilationTarget -> "C"]
Compile::cset: Variable Perm of type {_Integer,1} encountered in assignment of type >{_Real,1}. >>
Compile::cset: Variable Perm of type {_Integer,1} encountered in assignment of type >{_Real,1}. >>
I can avoid this error by doing this rather unnecessary thing below:
In[25]:= CTest = Compile[{{s, _Integer}}, Module[{Perm = {s}},
Perm = Delete[Perm, 1];
Append[Perm, 1]
], CompilationTarget -> "C"]
So, is there another way to tell Mathematica that Perm is a list of Integers without doing it like in the last example?
Thanks in advance!
Compile
, you can't return a list where sublists are of different type, so integers are converted into reals. This is one of the limitations ofCompile
. To some extent, you can alleviate this by some kind of post-processing of the result, e.g.MapAt[Round, CInit[5, 1], 2]
. $\endgroup$$MaxMachineNumber
is not an integer, a conversion of your list to have entirely real entries is performed. To compare, try replacing$MaxMachineNumber
with some other integer. $\endgroup$Perm = Most@{0}
to create an empty list of integer type. $\endgroup$