Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2008

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

Search the Archive

Re: Multiplying a vector over multiple vectors

  • To: mathgroup at smc.vnet.net
  • Subject: [mg91184] Re: Multiplying a vector over multiple vectors
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Fri, 8 Aug 2008 07:17:21 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, UK
  • References: <g7ed6u$2pm$1@smc.vnet.net>

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
> use MapThread as in: 
> 
> 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


  • Prev by Date: Re: Histogram package
  • Next by Date: Purchasing Mathematica
  • Previous by thread: Re: Multiplying a vector over multiple vectors
  • Next by thread: Re: Re: Multiplying a vector over multiple vectors