MathGroup Archive 2002

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

Search the Archive

Making Tensor operations more efficient

  • To: mathgroup at smc.vnet.net
  • Subject: [mg33787] Making Tensor operations more efficient
  • From: Luci Ellis <luci at verbeia.com>
  • Date: Tue, 16 Apr 2002 03:50:42 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

Dear MathGroupers,

It's been a while since I had to do any Mathematica programming and it
shows! I am trying to implement an econometric procedure in Mathematica, and
while it works, I'm having problems making it efficient enough to be usable
with larger datasets on a desktop computer.

I won't bore you with the details of the rest of the procedure. Judicious
use of TimeUsed[] shows that the two commands within the function that take
the most time are the following ones. Each of these commands takes 10-20
seconds on tensors of the required size (say 400*7*100), which is not ideal
as the econometric procedure requires doing the whole thing more than 1000
times. If anyone could make some suggestions on making this more efficient,
I'd be very grateful.

The first is: 

Zk=MapThread[
      Transpose[
          Join[Transpose[#1],Transpose[#2]]]&,{KroneckerProduct[{#},Inn]&/@
          Transpose[Vmat],KroneckerProduct[{#},Inn]&/@zbar }];
(* where Inn is just a conformable IdentityMatrix, Vmat and zbar are
matrices *)

That is, I have two 3D tensors that I need to join at the second level: one
is Tj*h*p and the other is Tj*h*q, so I should end up with a Tj*h*(p+q)
result.  Note that AppendRows from the MatrixManipulation package is about
fifty times slower than Transpose[Join[Transpose -- I've checked. I tried
Zk=With[{bit1=KroneckerProduct[{#},Inn]&/@Transpose[Vmat],
        bit2=KroneckerProduct[{#},Inn]&/@zbar },
      Table[Transpose[ Join[Transpose[bit1[[i]],
            Transpose[bit2[[i]] ]]],{i, Length[bit1]}]];
But this gives pretty much the same speed and memory requirements.

The second problem command is
  Do[zSz= zSz+ TZk[[i]].izk[[i]];,{i,Tj}] ;  (* where TZk =
Transpose[Zk,{1,3,2}]  and izk is a 3D tensor with conformable elements *)

So here I have two 3D tensors and I need the element-by-element sum of the
dot products of the corresponding (matrix) elements from each tensor. I
originally tried a Plus @@ MapThread[] construct but the memory requirements
were so enormous the kernel kept quitting (I'm using a Mac G4 with Mac OS
9).

KroneckerProduct is just the one given in the Mathematica book:
KroneckerProduct[a_?MatrixQ,b_?MatrixQ]:=BlockMatrix[Outer[Times,a,b]]

Thanks in advance,
Luci Ellis
http://www.verbeia.com/mathematica/



  • Prev by Date: Re: Subscript[x, y] and the Symbol x
  • Next by Date: Re: Question regarding Compile
  • Previous by thread: RE: Passing arguments and pattern matching
  • Next by thread: ReadList under Mac OS X and Mac OS 9