MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Array of arrays of various sizes and compile

  • To: mathgroup at smc.vnet.net
  • Subject: [mg115752] Re: Array of arrays of various sizes and compile
  • From: Daniel Lichtblau <danl at wolfram.com>
  • Date: Thu, 20 Jan 2011 06:26:59 -0500 (EST)

Ramiro Barrantes-Reynolds wrote:
> Thanks for your reply.  Let me explain myself better, I am going 
> through the Compile documentation but haven't found what I need yet:
> 
> What I really need to do is somewhat like the following (I am pasting 
> the code at the end just in case there are other suggestions):
> 
> example ==
>   Compile[{{m, _Real, 2}, {a, _Real}, {partition, _Integer, 2}},
>    Times @@ (With[{mp == m[[All, #]]}, Total[#]*a*Total[m]] & /@
>       partition)
>    ]
> 
> 1) The "partition" argument is a partition (e.g. {{1},{2},{3}}, 
> {{1,2},{3}}, {{1,3},{2}}, i.e. an array of vectors of varying sizes), 
> and that's why the compiler complains for having such a structure as 
> input.
> 
> 2) I am just using "Total[#]*a*Total[m]" as an example but it would be 
> something more complicated that calculates a probability of a block of 
> a partition (see code at the end).  I am sampling over partitions and 
> that is why it would be a two dimensional array with blocks of various 
> sizes.  Therefore, I am not sure if Listable would help me.
> 
> and indeed when I run it:
> 
> In[14]:== example[{{1.1, 1.2, 1.3}, {2.1, 2.2, 2.3}}, 1.5, {{1}, {2}, {3}}]
> 
> Out[14]== {663.552, 795.906, 944.784}
> 
> In[15]:== example[{{1.1, 1.2, 1.3}, {2.1, 2.2, 2.3}}, 1.5, {{1, 2}, {3}}]
> 
> During evaluation of In[15]:== CompiledFunction::cfta: Argument 
> {{1,2},{3}} at position 3 should be a rank 2 tensor of machine-size 
> integers. >>
> 
> Out[15]== {207.36, 234.09, 262.44}
> 
> Any suggestions?
> 
> Thanks you so much for your help,
> 
> Ramiro
> [...]

Easiest would be to make the ragged arrays into tensors. Could for 
example do

makeTensor[ll_] := With[
   {len = Length[Flatten[ll]]},
   PadRight[ll, {len, len}]]

Here are your examples.

samples = {{{1}, {2}, {3}}, {{1, 2}, {3}}, {{1, 3}, {2}}};

In[35]:= Map[makeTensor, samples]

Out[35]= {{{1, 0, 0}, {2, 0, 0}, {3, 0, 0}}, {{1, 2, 0}, {3, 0,
    0}, {0, 0, 0}}, {{1, 3, 0}, {2, 0, 0}, {0, 0, 0}}}

Other possibilites would include flattening the tensors, and adding as 
arguments another vector that delineates the breaks in the flattened 
partition. Although this will use less memory, it will probably be 
harder to work with in the sort of code that you show. And since you are 
working with partitions, my guess is they tend to be small so going from 
O(n) to O(n^2) memory use will not be an issue.

Daniel Lichtblau
Wolfram Research





  • Prev by Date: Re: Context Woes (questions!) in application design...
  • Next by Date: Re: avoiding non-machine numbers
  • Previous by thread: Re: Array of arrays of various sizes and compile
  • Next by thread: Re: Array of arrays of various sizes and compile