MathGroup Archive 2002

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

Search the Archive

Re: Finding Maxima in tensors

  • To: mathgroup at smc.vnet.net
  • Subject: [mg34654] Re: [mg34625] Finding Maxima in tensors
  • From: Andrzej Kozlowski <andrzej at platon.c.u-tokyo.ac.jp>
  • Date: Fri, 31 May 2002 04:26:33 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

First I quickly constructed a version that is simpler and  faster than 
yours:

opt1[p_, z_] :=
   Array[Max[p[[#1, #2, All, #3]] + z[[#1]]] &, Drop[Dimensions[p], {3}]]

However, when I tired to compile it I found difficulties and gave up. 
Instead I turned to your original program which  compiled without 
problems:

opt3 = Compile[{{p, _Real, 4}, {z, _Real, 2}},
     Block[{a, b, d, dima, dimb, dimc, dimd},
	  {dima, dimb, dimc, dimd} = Dimensions[p];
       Table[
		Max[MapThread[Plus,
			{p[[a, b, All, d]], z[[a]]}]],
		{a, dima}, {b, dimb}, {d, dimd}]]]

Here is how they performed on my 400 megaherz PowerBook G4;

p = Array[Random[] &, {20, 20, 20, 20}];
z = Array[Random[] &, {20, 20}];

In[58]:=
(z1=opt[p,z];)//Timing

Out[58]=
{1.93 Second,Null}

In[59]:=
(z2=opt1[p,z];)//Timing

Out[59]=
{0.58 Second,Null}

In[60]:=
(z3=opt3[p,z];)//Timing

Out[60]=
{0.75 Second,Null}

In[62]:=
z1==z2==z3

Out[62]=
True

So my uncompiled opt1 seems faster even than the compiled version of 
your opt, opt3, even though in earlier tests for tensors of smaller size 
opt3 had the advantage. I am sure it can all be very much improved since 
I have spent very little time on it. In particular, I think it should be 
possible to make some changes to opt1 that will allow it to be compiled.

Andrzej Kozlowski
Toyama International University
JAPAN
http://platon.c.u-tokyo.ac.jp/andrzej/


On Thursday, May 30, 2002, at 03:55  PM, Johannes Ludsteck wrote:

> I have a rank 4 tensor p and a matrix z. My task is
> to find the maxima of the vectors
> p[a,b,All,d] + z[[a]]
> for all a,b, and d (a,b, and d are simply indices)
> and collect them in a rank 3 tensor.
>
> Here is my implementation:
>
> opt[p_,z_]:=Block[{a,b,d,dima,dimb,dimc,dimd},
> 	  {dima,dimb,dimc,dimd}=Dimensions[p];
>       Table[
> 		Max[MapThread[Plus,
> 			{p[[a,b,All,d]],z[[a]]}]],
> 		{a,dima},{b,dimb},{d,dimd}]]
>
> (Of course, the dimensions of p and z do match, i.e.
> Dimensions[p][[{1,3}]]==Dimensions[z].)
>
> The implemtation works but seems to be awkward.
> Since I have to apply the maximum search for tensors of
> even higher rank and opt[...] is evaluated some thousand times,
> a more concise implementation would help.
> I feel that the problem should have a shorter and simpler
> Mathematica code representation, but I cannot find it.
>
> Any suggestions?
>
> Best regards,
> 	Johannes Ludsteck
>
>
>
> <><><><><><><><><><><><>
> Johannes Ludsteck
> Economics Department
> University of Regensburg
> Universitaetsstrasse 31
> 93053 Regensburg
> Phone +49/0941/943-2741
>
>
>



  • Prev by Date: Re: Finding Maxima in tensors
  • Previous by thread: Re: Finding Maxima in tensors
  • Next by thread: Help with Select[]