0
$\begingroup$

Consider the following code:

comp1 = Compile[{{acccolumns, _Integer, 2}}, Module[{acccols, ncols},
   ncols = Length[acccolumns[[1]]];
   acccols = acccolumns;
   Do[Do[
     Compile`GetElement[acccols, j, i] == RandomInteger[{0, 1}], {i, 
      1, ncols, 1}], {j, 1, Length[acccols], 1}];
   acccols], CompilationTarget -> "C", RuntimeOptions -> "Speed", 
  RuntimeAttributes -> {Listable}]

It should take some table acccolumns, replace each of its elements with 0 or 1, and then return the changed table. In reality, it does not work: for

ncols = 4;
acccolumns1 = Table[1, ncols];
acccolumns = Table[acccolumns1, 10];

the evalution

comp1[acccolumns]

returns acccolumns. Could you please tell me where I made a mistake and how to fix it still being inside the compiled code?

$\endgroup$
7
  • 1
    $\begingroup$ You probably don't want the double equal sign (==), this is for comparison, not for assigning values. $\endgroup$
    – banone
    Jul 12 at 14:21
  • $\begingroup$ I'm probably missing the context here, but from what you show it would be easier and faster to just create the random array from scratch rather than replacing each value one by one. $\endgroup$
    – banone
    Jul 12 at 14:26
  • $\begingroup$ @banone : if changing the double sign to the single sign, it does not compile the corresponding line. $\endgroup$ Jul 12 at 14:28
  • 2
    $\begingroup$ Compile`GetElement is a getter-function, not a setter-function. Since Compile`SetElement does not exist, you have to use Part as in ordinary Mathematica code. Moreover, Compile does not allow call by reference, so you cannot modify the input array. (LibraryLink allows this however.) $\endgroup$ Jul 12 at 20:21
  • 1
    $\begingroup$ "does it mean that if I am restricted to use the construction similar to the above one, I am unavoidably limited by the timing of using Part?" Yes, unfortunately. $\endgroup$ Jul 13 at 5:23

1 Answer 1

1
$\begingroup$

Does this what you want?

comp1 = Compile[{{in, _Integer, 2}}, Module[{},
   RandomInteger[{0, 1}, {Length[in], 
     Length[in[[1]]]}]], {{Length[_], _Integer}}, 
  CompilationTarget -> "C", RuntimeOptions -> "Speed", 
  RuntimeAttributes -> {Listable}]

With this:

comp1[acccolumns]

{{1, 1, 0, 1}, {0, 1, 0, 0}, {0, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 
  0}, {1, 0, 0, 1}, {0, 1, 0, 1}, {1, 0, 1, 0}, {0, 1, 0, 1}, {0, 1, 
  0, 1}}
$\endgroup$
2
  • $\begingroup$ Thanks! However, the main ingredient is to have some pre-defined table acccolumns with an arbitrary content and then to be able to change only some of its columns. So this structure with two cycles seems to be essential. $\endgroup$ Jul 12 at 18:15
  • $\begingroup$ You may always change any element or columns after randomization. $\endgroup$ Jul 13 at 7:37

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.