1
$\begingroup$

In some case the Compile's syntax is quite straightforward. Es. for rank 1 e rank 2 tensors:

Quiet[Remove[cf]];
cf = Compile[
   {{x, _Real, 1}}
   , Total[x]
   ];

and

Quiet[Remove[cf]];
cf = Compile[
   {{x, _Real, 2}}
   , Inverse[x] (*Inverse isn't compileable: here is used merely as signpost*)

   ];

Or, even,

Quiet[Remove[cf]];
cf = Compile[
   {{x, _Real, 2}, {y, _Real, 2}}
   , Det[x] + Det[y] (*Det isn't compileable: here is used merely as signpost*)
   ];
cf[matrixA, matrixB]

But what if the arguments are intricated ? What is the syntax needed ?

A workaround to circumvent this question was proposed here : Not the most elegant but could you flatten and join and then "unflatten" and separate afterwards ?

Please, can you give examples for a function having as argument (all atomic expression are understood Real):

  • a matrix of lists
  • a matrix of matrices
  • a matrix whose element are {x_Real , a matrix }
  • a matrix whose element are {x_List , a matrix }
  • etc.

Addendum

This question has been put on hold, but in IMHO the answer given below (see Jokeur), clarifying what is possible and what is not, fully dissolves the doubt.

$\endgroup$
2
  • $\begingroup$ I have restricted the question to few cases. Please, can you propose a further edit to avoid the "too broad" problem ? $\endgroup$ Commented Mar 21, 2016 at 19:05
  • $\begingroup$ Is the argument the only irregular part? I mean, when the argument is passed into the body of Compile ,will they still be irregular, or be broken up into regular lists? $\endgroup$
    – xzczd
    Commented Mar 22, 2016 at 2:44

1 Answer 1

1
$\begingroup$

We get something out of the way first: Det and Inverse are not compileable.

OK, to the question: as noted in docs Compile can only handle lists of lists that are tensors, which makes your third and fourth examples not suitable for Compile.

Your first and second examples are suitable: in the first case you have a rank-3 tensor (ArrayDepth is equal to 3), and in the second case you have a rank-4 tensor. So, those will work.

Thus, here is an alternative for your last two cases: the function can take two arguments, one argument being the matrix of the first components, and the other being the last component of your lists.


Anyway here is an example asked for by Marco B. If your hypothetical routine takes an argument like

{
  {
    {1, HilbertMatrix[2]},
    {2, ConstantArray[1, {2, 2}]}
  },
  {
    {3, Array[Min, {2, 2}]},
    {4, HilbertMatrix[2]}
  }
}

consider rewriting the routine so that it takes the arguments {{1, 2}, {3, 4}} and {{HilbertMatrix[2], ConstantArray[1, {2, 2}]}, {Array[Min, {2, 2}], HilbertMatrix[2]}} instead. Now the two arguments are admissible tensors for Compile, unlike in the previous format. Similarly for the other example.

$\endgroup$
1
  • $\begingroup$ Jokeur, Would you consider perhaps adding a code example for the workaround you proposed at the end of your answer? (+1) $\endgroup$
    – MarcoB
    Commented Mar 22, 2016 at 6:19

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