I want to compile this function:

fun = With[{a = Range[-300, -200]}, 
        Compile[{{t, _Integer}, {window, _Integer, 2}}, 
          Pick[a + t, #, 1] & /@ window, 
        CompilationTarget -> "C", RuntimeOptions -> "Speed"]];



returns MainEvaluate at Pick. Is there a workaround and is Pick not compilable in C?

edit: window is a sparse array and fun is actually a more complicated function that contains Pick.

  • $\begingroup$ Here's a discussion of compilable functions: mathematica.stackexchange.com/questions/1096/… $\endgroup$ – Roman Feb 1 at 14:11
  • 1
    $\begingroup$ Also Pick with packed arrays is already extremely fast, especially when the filtering function is optimized for packed arrrays, like Unitize, UnitStep etc. Compiling it would not give much boost. $\endgroup$ – Marius Ladegård Meyer Feb 1 at 14:18
  • $\begingroup$ I'm using Pick inside a more complicated function, but the only point it evaluates at the main kernel is Pick so that's how I phrased the question. $\endgroup$ – thedude Feb 1 at 14:29
  • $\begingroup$ You cannot use sparse array in Compile. $\endgroup$ – Henrik Schumacher Feb 1 at 15:10

Neither Pick nor SparseArrays can be used in compiled function. But the following shows you how to achieve the desired operation more quickly without Compile, just by SparseArray techniques:

a = Range[-300, -200];
n = 100000;
m = 1000000;
i = RandomInteger[{1, n}, m];
j = RandomInteger[{1, Length[a]}, m];
vals = RandomInteger[{-2, 2}, m];
window = SparseArray[Transpose[{i, j}] -> vals, {n, Length[a]}];
t = RandomInteger[100];

result0 = Table[Pick[a + t, w, 1], {w, window}]; // AbsoluteTiming // First

result = With[{A = SparseArray[ Clip[window, {0, 1}, {0, 0}].DiagonalMatrix[SparseArray[a + t]]]},
     Internal`PartitionRagged[A["NonzeroValues"], Differences[A["RowPointers"]]]
     ]; // AbsoluteTiming // First
result0 == result





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.