0
$\begingroup$

I want to prepend an array with a position index in the first column using a compiled function.

For some reason, the position index is always returned as a float and not as an integer. How can I force Compile to return an integer in the first column?

    cftest = Compile[{{dat, _Real, 2}},
       Block[{len},
        len = Length[dat];
        MapThread[Prepend, {dat, Range[len]}]], 
       CompilationTarget -> "C", RuntimeAttributes -> {Listable}, 
       Parallelization -> True, RuntimeOptions -> {"Speed"}];

cftest[{RandomReal[1., 10], RandomReal[1., 10]} // Transpose]

{{1., 0.0612128, 0.790475}, {2., 0.718777, 0.657061}, {3., 0.402669, 
  0.0223461}, {4., 0.466128, 0.399008}, {5., 0.189905, 0.459928}, {6.,
   0.951218, 0.142375}, {7., 0.574289, 0.309128}, {8., 0.434694, 
  0.906499}, {9., 0.940341, 0.150518}, {10., 0.174969, 0.763484}}

Or is it impossible for Compile to return arrays of mixed types (i.e. integers and floats)? Then of course I would need to post-process it or do it without Compile.

$\endgroup$
  • 1
    $\begingroup$ "Or is it impossible for Compile to return arrays of mixed types" Yes, I think it is impossible. $\endgroup$ – Szabolcs Jun 28 at 13:10
  • $\begingroup$ Yes, just thought so, too. Trying to apply IntegerPart at the first column within Compile also has no effect. $\endgroup$ – Display Name Jun 28 at 13:12

Your Answer

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

Browse other questions tagged or ask your own question.