Re: Dot or Inner ... but not quite
- To: mathgroup at smc.vnet.net
- Subject: [mg79697] Re: Dot or Inner ... but not quite
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Thu, 2 Aug 2007 03:47:11 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <f8n2rc$lp9$1@smc.vnet.net>
Diamond, Mark wrote:
> I have two equi-length lists, the first of number, the second of
> (equi-length) lists of numbers , such as
>
> l1={1,4,3};
> l2={{1,7},{1,9},{5,2}};
>
> I want to produce 1*{1,7}+4*{1,9}+3*{5,2}
>
> It looks so close to Inner that I thought I could use it in some form, but I
> have ended up using
>
> (#[[1]]*#[[2]])& /@ Transpose[{l1,l2}]
>
> Is there a better way using one of the builtin functions with which I am
> unfamiliar?
>
> Cheers,
>
> Mark Diamond
Note that you can use *Inner*. You have to transpose the result, however.
In[1]:= l1 = {1, 4, 3};
l2 = {{1, 7}, {1, 9}, {5, 2}};
In[3]:= Transpose[Inner[Times, l1, l2, List]]
Out[3]= {{1, 7}, {4, 36}, {15, 6}}
In[4]:= Thread[l1*l2, 2]
Out[4]= {{1, 7}, {4, 36}, {15, 6}}
In[5]:= MapThread[Times, {l1, l2}]
Out[5]= {{1, 7}, {4, 36}, {15, 6}}
In[6]:= Apply[Times, Partition[Riffle[l1, l2], 2], {1}]
Out[6]= {{1, 7}, {4, 36}, {15, 6}}
Regards,
Jean-Marc