Re: Array of arrays of various sizes and compile
- To: mathgroup at smc.vnet.net
- Subject: [mg115787] Re: Array of arrays of various sizes and compile
- From: Oliver Ruebenkoenig <ruebenko at wolfram.com>
- Date: Fri, 21 Jan 2011 04:28:26 -0500 (EST)
On Wed, 19 Jan 2011, 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. Right now, I can not think of any other way to do this beside what I have shown in a previous post. I think listablity and using two (inlined) compiled functions is the way to go. Oliver > > 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 > > > > calculateH == > Compile[{{count, _Real, > 2}, {\[Alpha], _Real}, {\[Beta], _Real}, {\[Theta], _Real}, \ > {treeLengths, _Real, 1}, {event, _Integer, 2}, {partition, _Integer, > 2}}, > Times @@ (With[{counts == counts[[All, #]], events == event[[#]], > calculatedTree == Transpose[event[[#]]]}, > Module[{all, posZero, zerosA, zerosB, concerted, posOnes, > onesA, onesB, zeroes}, > zeroes == Count[events, 0, 2]; > all == Plus @@ ((Plus @@ events) /. (Length[counts[[1]]] -> 1)); > posZero == Position[calculatedTree, 0]; > zerosA == > If[MatchQ[posZero, {}], {}, > counts[[#[[1]], #[[2]]]] & /@ posZero]; > zerosB == > If[MatchQ[posZero, {}], {}, > treeLengths[[#]] & /@ posZero[[All, 1]]]; > posOnes == Position[calculatedTree, 1]; > onesA == > If[MatchQ[posOnes, {}], {}, > counts[[#[[1]], #[[2]]]] & /@ posOnes]; > onesB == > If[MatchQ[posOnes, {}], {}, > treeLengths[[#]] & /@ posOnes[[All, 1]]]; > If[ > MatchQ[onesA, {}], \[Beta]^\[Alpha]* > Exp[LogGamma[ > Total[zerosA] + \[Alpha]] - (Plus @@ (LogGamma[ > zerosA + 1]) + LogGamma[\[Alpha]]) + > Plus @@ (zerosA* > Log[zerosB]) - (Total[zerosA] + \[Alpha])* > Log[Total[zerosB] + \[Beta]]], > > If[MatchQ[zerosA, {}], \[Beta]^\[Alpha]* > Exp[LogGamma[ > Total[onesA] + \[Alpha]] - (Plus @@ (LogGamma[ > onesA + 1]) + LogGamma[\[Alpha]]) + > Plus @@ (onesA*Log[onesB]) - (Total[onesA] + \[Alpha])* > Log[Total[onesB] + \[Beta]]], \[Beta]^\[Alpha]* > Exp[LogGamma[ > Total[onesA] + \[Alpha]] - (Plus @@ (LogGamma[ > onesA + 1]) + LogGamma[\[Alpha]]) + > Plus @@ (onesA*Log[onesB]) - (Total[onesA] + \[Alpha])* > Log[Total[onesB] + \[Beta]]]*\[Beta]^\[Alpha]* > Exp[LogGamma[ > Total[zerosA] + \[Alpha]] - (Plus @@ (LogGamma[ > zerosA + 1]) + LogGamma[\[Alpha]]) + > Plus @@ (zerosA* > Log[zerosB]) - (Total[zerosA] + \[Alpha])* > Log[Total[zerosB] + \[Beta]]]]]*\[Theta]^ > all*(1 - \[Theta])^zeroes > ] > ] & /@ partition) > ] > > > >