I need to replace Do loops by something else because it takes forever with big programs. I have been hinted towards Inner, but even though I tried, I did not manage to get what I need. Here's a toy example of my actual problem:


u = {{u1a, u1b}, {u2a, u2b}, {u3a, u3b}, {u4a, u4b}};
v = {{v1a, v1b}, {v2a, v2b}, {v3a, v3b}, {v4a, v4b}};
f = {f1, f2, f3, f4};

The output I am looking for:

d = {{u1a*v1a*f1 + u2a*v2a*f2 + u3a*v3a*f3 +u4a*v4a*f4},
    {u1b*v1b*f1 + u2b*v2b*f2 + u3b*v3b*f3 + u4b*v4b*f4}}

It is important not to have any loop constructs; only Inner, Table, etc. which are generally faster than loops

  • 2
    $\begingroup$ Like f.(u v)? (No Inner[] here, sorry.) $\endgroup$ – J. M.'s technical difficulties May 14 '13 at 18:19
  • $\begingroup$ What do you mean by (u v)? This is the straight-forward wau to do it: $\endgroup$ – Mencia May 14 '13 at 18:23
  • $\begingroup$ Do[d[[i]] = Sum[V[[l, i]]*U[[l, i]]*f[[l]], {l, 1, 4, 1}], {i, 1, 2, 1}]; $\endgroup$ – Mencia May 14 '13 at 18:24
  • 2
    $\begingroup$ Why not execute that piece of code I gave to see what I mean? (On that note, please look up Dot[] in the docs.) $\endgroup$ – J. M.'s technical difficulties May 14 '13 at 18:24
  • $\begingroup$ @J.M. thankyouuuuuuuuuuu!!! :)))) It definitely works! I appreciate it! $\endgroup$ – Mencia May 14 '13 at 18:29

To settle this question:

You certainly can use Inner[] for the task, like so:

Inner[Times, f, u v, Plus]

but you should know that Mathematica, conveniently enough, knows how to do a dot product. The function for doing this is named, appropriately enough, Dot[]:

Dot[f, u v]

but I find that a bit too long to write, so I use a syntax that is a bit more understandable, at least to me:

f.(u v)

where the parentheses are needed to give (Hadamard) multiplication higher priority than the dot product.

  • $\begingroup$ Why community wiki, if I may ask? $\endgroup$ – Ajasja May 14 '13 at 18:39
  • 1
    $\begingroup$ @Ajasja, I don't want to get reputation for this answer. (I'm choosing to interpret "CW" as "Credit Waived".) $\endgroup$ – J. M.'s technical difficulties May 14 '13 at 18:40
  • 3
    $\begingroup$ Yes, I gathered that. But I was wondering why you don't want to get the reputation (or why you care about this). Is this answer to simple? (Personally I think that every useful answer deserves to be upvoted) $\endgroup$ – Ajasja May 14 '13 at 19:04
  • 3
    $\begingroup$ The upvote would be for your expertise in diagnosing the problem, not the quantity of work visible in the answer, like in the old story of the man with a hammer... $\endgroup$ – cormullion May 14 '13 at 19:27
  • $\begingroup$ @Ajasja, if I consider the (not few!) answers I wanted to get reputation for, but don't; versus the (also not few!) answers I really didn't want to get rep for, but did, this seems like a way I'd prefer to proceed. I have nicer answers languishing for lack of even a glance from other people; should I have to beg attention for those? I think not. $\endgroup$ – J. M.'s technical difficulties May 15 '13 at 1:53

One option is to use MapThread, which gets you there:

List /@ MapThread[Plus, u v f]

But the infix notation for the Dot product is more elegant (as J.M. proposed in the comment above):

List /@ (f.(u v))

Note that if you're doing a lot of these computations, that f.(u v) has a very slight computational edge, which you can test with the Timing command:

    List /@ MapThread[Plus, u v f] // Timing
    List /@ (f.(u v)) // Timing
|improve this answer|||||
  • $\begingroup$ Thanks for your reply and those edits @j-m. I'll try to (remember to) adhere to that styling in the future. $\endgroup$ – TransferOrbit May 15 '13 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.