Re: Dot or Inner ... but not quite
- To: mathgroup at smc.vnet.net
- Subject: [mg79644] Re: Dot or Inner ... but not quite
- From: mcmcclure at unca.edu
- Date: Wed, 1 Aug 2007 04:55:56 -0400 (EDT)
- References: <f8n2rc$lp9$1@smc.vnet.net>
On Jul 31, 6:27 am, "Diamond, Mark" <d... at dot.dot> 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}
There are a number of ways to approach this and your
technique looks fine to me. Inner
doesn't work directly because it delves deeply
into nested structures as you can see by executing
something like:
Inner[f, {{{a}, {c}}}, {{d, e}}, List]
You can prevent this by changing the head at
the appropriate level; you can always switch it
back, if necessary. In your problem, you could
use:
l1 = {1, 4, 3};
l2 = {{1, 7}, {1, 9}, {5, 2}};
Inner[Times, l1, h /@ l2, List] /.
h[x_] -> x
You could also use Thread:
Times@@@Thread[f[l1, l2]]
If you are doing this for extremely long lists,
timing tests indicate that the Thread technique is
the fastest.
Mark