Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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}];
          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!

share|improve this question
Using 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 of Compile. To some extent, you can alleviate this by some kind of post-processing of the result, e.g. MapAt[Round, CInit[5, 1], 2]. – Leonid Shifrin Apr 3 '13 at 10:16
In short: since $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. – J. M. Apr 3 '13 at 10:32
It seems when the compiled function returns an array it is always made homogeneous. One float and the whole thing is cast as float. Makes sense, but really how hard would it be for the good folks at wolfram to just say that in the docs..? – george2079 Apr 3 '13 at 14:01
Thanks for your help @Leonid Shifrin. The "MapAt"-function helped me a lot. – Carolin Apr 6 '13 at 18:14
You can use Perm = Most@{0} to create an empty list of integer type. – Simon Woods Jun 18 '13 at 20:02
up vote 3 down vote accepted

Regarding the second part of the question, you can use Most@{0} to create an empty list of integer type:

CTest = Compile[{{s, _Integer}},
  Module[{Perm = Most@{0}}, Append[Perm, 1]],
  CompilationTarget -> "C"]
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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