Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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:

Input:

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

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

2 Answers 2

up vote 13 down vote accepted

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.

share|improve this answer
    
Why community wiki, if I may ask? –  Ajasja May 14 '13 at 18:39
1  
@Ajasja, I don't want to get reputation for this answer. (I'm choosing to interpret "CW" as "Credit Waived".) –  J. M. May 14 '13 at 18:40
3  
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) –  Ajasja May 14 '13 at 19:04
3  
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... –  cormullion May 14 '13 at 19:27
    
@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. –  J. M. 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
share|improve this answer
    
Thanks for your reply and those edits @j-m. I'll try to (remember to) adhere to that styling in the future. –  zentient May 15 '13 at 9:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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