Re: Speed of repeated matrix calculation
- To: mathgroup at smc.vnet.net
- Subject: [mg49774] Re: [mg49717] Speed of repeated matrix calculation
- From: DrBob <drbob at bigfoot.com>
- Date: Fri, 30 Jul 2004 06:02:26 -0400 (EDT)
- References: <200407291143.HAA10250@smc.vnet.net>
- Reply-to: drbob at bigfoot.com
- Sender: owner-wri-mathgroup at wolfram.com
Here are three ways to do it, with timings for a smaller problem:
n=1000;
x=Array[f,{n,3}];
v=Array[h,{3,3}];
Timing[a=Tr[x.v.Transpose[x],List];]
Timing[b=Table[x[[i]].v.x[[i]],{i,1,n}];]
Timing[c=(#.v.#&)/@x;]
a == b == c
{5.594 Second,Null}
{0.031 Second,Null}
{0.032 Second,Null}
True
The first method clearly isn't competitive. Eliminating it and increasing the problem size, I get:
n=7000;
x=Array[f,{n,3}];
v=Array[h,{3,3}];
Timing[b=Table[x[[i]].v.x[[i]],{i,1,n}];]
Timing[c=(#.v.#&)/@x;]
b == c
{0.234 Second,Null}
{0.203 Second,Null}
True
For the other problem, the following methods are very fast.
n=7000;
x=Array[f,{n,3}];
y=Array[g,{n,3}];
v=Array[h,{3,3}];
Timing[a=Table[x[[i]].y[[i]],{i,1,n}];]
Timing[b=Inner[Dot,dum@@x,dum@@y,List];]
a == b
{0.093 Second,Null}
{0.063 Second,Null}
True
But the first is really the same as your method; are you using an older version of Mathematica, perhaps?
Bobby
On Thu, 29 Jul 2004 07:43:39 -0400 (EDT), Gregory Lypny <gregory.lypny at videotron.ca> wrote:
> Hello everyone,
>
> Just curious to know whether I'm doing this repeated matrix calculation
> efficiently by using the Table command.
>
> I've got matrices x and y, which are both 7000x3, and a matrix V which
> is 3x3.
>
> If I create a 7000x1 vector, q, whose elements are equal to each of the
> row quadratic forms x.V.x, I can do it by creating a table.
>
> q = Table[{x[[i, All]].V.x[[i, All]]}, {i, 1, 7000}];
>
> The calculation is done in a split second on a G4 iBook.
>
> However, if I use Table to create a 7000x1 vector, d, of the dot
> products of the rows of x and y, the calculation takes more than two
> minutes!
>
> d = Table[{x[[i, All]].y[[i, All]]}, {i, 1, 7000}];
>
> Why the big difference in time? Is there a better way?
>
> Greg
>
>
>
--
DrBob at bigfoot.com
www.eclecticdreams.net
- References:
- Speed of repeated matrix calculation
- From: Gregory Lypny <gregory.lypny@videotron.ca>
- Speed of repeated matrix calculation