Re: Re: Multiplying a vector over multiple vectors

• To: mathgroup at smc.vnet.net
• Subject: [mg91200] Re: [mg91184] Re: Multiplying a vector over multiple vectors
• From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
• Date: Sat, 9 Aug 2008 07:45:46 -0400 (EDT)
• References: <g7ed6u\$2pm\$1@smc.vnet.net> <200808081117.HAA11833@smc.vnet.net>

```On 8 Aug 2008, at 13:17, Jean-Marc Gulliet wrote:

> Donald DuBois wrote:
>
>> How does one get a vector to distribute over
>> a list of vectors  under multiplication?
>>
>> For example:
>>
>> {{1,2}, {3,4}, {5,6}} times {a,b} should
>> equal  (under this operation)
>>
>> {{a, 2b}, {3a, 4b}, {5a, 6b}}
>>
>> If {a,b} is duplicated three times one can
>>
>> MapThread[Times, {{{1, 2}, {3, 4}, {5, 6}}, {{a, b}, {a, b}, {a,
>> b}}}]
>>
>> but duplicating {a,b} using Table is very slow
>> when the vectors are large (consisting of thousands
>> of elemets).
>
> FWIW, here are some possible ways with their respective timings:
>
> In[1]:= lst = RandomInteger[{-100, 100}, {10^5, 2}];
> Array[{lst[[#, 1]] a, lst[[#, 2]] b} &, Length@lst]; //
>   Timing // First
> # {a, b} & /@ lst; // Timing // First
> MapThread[Times, {lst, Table[{a, b}, {Length[lst]}]}]; //
>   Timing // First
> MapThread[Times, {lst, ConstantArray[{a, b}, Length[lst]]}]; //
>   Timing // First
> Table[{lst[[i, 1]] a, lst[[i, 2]] b}, {i, Length@lst}]; //
>   Timing // First
> Transpose[{a lst[[All, 1]], b lst[[All, 2]]}]; // Timing // First
>
> Out[2]= 0.555598
>
> Out[3]= 0.522737
>
> Out[4]= 0.510453
>
> Out[5]= 0.474941
>
> Out[6]= 0.407999
>
> Out[7]= 0.323102
>
> Regards,
> -- Jean-Marc
>

Somewhat surprisingly no-one seems to have suggested the approach that
seems to me the most obvious:

lst /. x_?VectorQ :> x {a, b}

This seems to be the second fastest approach, on my machine loosing
only slightly to Jean-Marc's last one.

Andrzej Kozlowski

```

• Prev by Date: Re: sum question
• Next by Date: Re: Help with StyleSheets
• Previous by thread: Re: Multiplying a vector over multiple vectors
• Next by thread: Re: Multiplying a vector over multiple vectors