Re: Vector operations,
- To: mathgroup at smc.vnet.net
- Subject: [mg70015] Re: Vector operations,
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Sat, 30 Sep 2006 05:12:46 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <efitqn$lu4$1@smc.vnet.net>
mickey wrote:
> Hi,
>
> I have a list of vectors such as
>
> {{1/2, 1/2}, {1, 0}, {0, 1}, {0, 0}}
>
> I want to rotate them all by the same angle. Right now, what I do is
> multiply each by RotationMatrix2D[t] and iterate over the list. Is there
> a more efficient way to do this?
>
> Thanks,
> -M
>
The fastest computation is the dot product of the list of vectors with
components given is machine-precision numbers over the rotation matrix
(see In[13]).
In[1]:=
Needs["Geometry`Rotations`"]
In[2]:=
(* Here we use exact arithmetic *)
In[3]:=
m = RotationMatrix2D[Pi/4]
Out[3]=
{{1/Sqrt[2], 1/Sqrt[2]}, {-(1/Sqrt[2]), 1/Sqrt[2]}}
In[4]:=
(* We generate 100,000 2-dimensional vectors *)
In[5]:=
vecs = Partition[Range[1, 200000], 2];
In[6]:=
t1 = Timing[s1 = (Rotate2D[#1, -(Pi/4)] & ) /@
vecs; ][[1]]
Out[6]=
11.735 Second
In[7]:=
t2 = Timing[s2 = vecs . m; ][[1]]
Out[7]=
5.718 Second
In[8]:=
(* Now we switch to hardware-precision arithmetic *)
In[9]:=
vecs = N[vecs];
theta = N[-(Pi/4)];
m = N[m];
In[12]:=
t3 = Timing[s3 = (Rotate2D[#1, theta] & ) /@ vecs; ][[
1]]
Out[12]=
2.297 Second
In[13]:=
t4 = Timing[s4 = vecs . m; ][[1]]
Out[13]=
0.015 Second
In[14]:=
Count[Chop[s3 - s4], {0, 0}]
Out[14]=
100000
In[15]:=
Count[Chop[s1 - s4], {0, 0}]
Out[15]=
100000
In[16]:=
s1 == s2
Out[16]=
True
In[17]:=
t1/t4
Out[17]=
782.333
Regards,
Jean-Marc