0
$\begingroup$

1,
I am trying to make Compilable function which takes Matrix of zeroes and ones f.e:

R= {{1, 1, 1, 0, 0, 0, 1, 1, 1, 0}, {1, 1, 0, 0, 0, 0, 1, 1, 1, 0}, {1, 
      0, 1, 0, 0, 1, 1, 0, 1, 1}, {0, 0, 0, 1, 1, 1, 0, 0, 0, 1}, {0, 0, 
      0, 1, 1, 1, 0, 0, 0, 1}, {0, 0, 1, 1, 1, 1, 0, 0, 1, 1}, {1, 1, 1, 
      0, 0, 0, 1, 1, 1, 0}, {1, 1, 0, 0, 0, 0, 1, 1, 1, 0}, {1, 1, 1, 0, 
      0, 1, 1, 1, 1, 0}, {0, 0, 1, 1, 1, 1, 0, 0, 0, 1}}

and will make a table of its diagonals, but the problem:CompiledFunction::cflist: Nontensor object generated; proceeding with uncompiled evaluation. occures. My function is:

f3 = Compile[{{R, _Real, 2}}, 
  Table[Flatten[{0, Table[R[[i]][[i + j]], {i, 1, Length[R] - j}], 
     0}], {j, 1, Length[R] - 1}], CompilationTarget -> "C", 
  RuntimeAttributes -> Listable, RuntimeOptions -> "Speed"]

Any suggestion how can I debug it please ?

2, Then I want to make again Compilable fction which can determinate from Flatten[f3[R]] the number of ones without neighbour, I mean, f.e. vector {1,0,1,0,1,1} has a 2 ones without neighbour.

Or even better Compilable function which can determinate the number of all the sequances of 1 11 111 1111 ... in Flatten[f3[R]].

$\endgroup$
1
  • 3
    $\begingroup$ A note: if the result of a function's evaluation is a "ragged array", such as a table of a matrix's diagonals, you can't compile. In any case: Table[ArrayPad[Diagonal[R, k], 1], {k, Length[R] - 1}] $\endgroup$ Apr 8, 2018 at 15:48

1 Answer 1

4
$\begingroup$

As @J.M already mentioned, so-called ragged arrays cannot be used in a compiled function. Additionally, I quite sure you wouldn't gain much speed in compiling this algorithm. Therefore, you should use the suggestion

Table[ArrayPad[Diagonal[R, k], 1], {k, Length[R] - 1}]

to create your diagonals. As for your second question, this can also be done with built-in list manipulation. Take the flattened list of diagonals and split them up. Then, you need to delete the elements containing zeroes and Tally does the rest

diag = Flatten@Table[ArrayPad[Diagonal[R, k], 1], {k, Length[R] - 1}];
Tally[Sort@DeleteCases[Split[diag], {0 ..}]]
(* {{{1}, 11}, {{1, 1}, 2}, {{1, 1, 1}, 1}, {{1, 1, 1, 1}, 1}} *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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