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.

This question already has an answer here:

I'll start with a couple of examples (since this is all one can get from the documentation anyway).

First, adding lists of numbers equal length is done term-by-term. E.g. {8, 2, 5} + {3, 0, 3} evaluates to {11, 2, 8}.

Second, adding a list of numbers to a single number is done by, effectively, "broadcasting" the single number into a list of the same size as that of the other addend, and then the same term-by-term addition as before is applied. For example {1, 2, 3} + 5 evaluates to {6, 7, 8}.

Conceptually, one could imagine extending this scheme to the case of adding a list L to a list-of-lists L^2. First "broadcast" L into a list of the appropriate number of copies of L, and then add the two lists-of-lists "term-by-term". For example, following this scheme, {1, 2} + {{5, 9}, {9, 7}, {1, 7}} would evaluate to {{6, 11}, {10, 9}, {2, 9}}.

To my surprise, however, Mathematica won't play along: The last sum would fail with an error beginning with Thread::tdlen: Objects of unequal length ... (The same goes for the remaining "basic operations", -, *, and /.)

What's the simplest way to carry out this proposed extension of arithmetic between a single object of type X and a List of objects of type X (assuming that arithmetic between single objects of type X is defined)?

share|improve this question
2  
Map is the simplest approach. This question seems to be a duplicate of How to make threading more flexible. Also, notice that you have said: "add the two lists-of-lists "term-by-term"" ant then you are doing something different. –  Kuba Sep 12 '13 at 13:33
    
@Kuba: thanks for your comments. I can't follow your last point. Just to be clear, if A, B, C, D are all of some type X for which addition is defined, I'm saying that A + {B, C, D} should evaluate to {A + B, A + C, A + D}. If X happens to be "List of 2 integers", then the foregoing implies that {1, 2} + {{5, 9}, {9, 7}, {1, 7}} would evaluate to {{1, 2} + {5, 9}, {1, 2} + {9, 7}, {1, 2} + {1, 7}} = {{6, 11}, {10, 9}, {2, 9}}. –  kjo Sep 12 '13 at 13:39
2  
but why do you think {1,2}+{{5,9},.. should evaluate to {{1,2}+{5,9}, .... This would be inconsistent with your next wish that {a,b} + {c,d}->{a+c,b+d}. Those are different ways to proceed even though they look similar. Mathematica does automatically as much as it can (here), then you have to specify what do you want to do. –  Kuba Sep 12 '13 at 13:59
add comment

marked as duplicate by Kuba, Artes, m_goldberg, Mr.Wizard Sep 12 '13 at 23:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers

up vote 2 down vote accepted

Here's an alternative to Map:

{1, 2} + Transpose@{{5, 9}, {9, 7}, {1, 7}} // Transpose
(* {{6, 11}, {10, 9}, {2, 9}} *)

It can be faster than Map, too:

m = RandomInteger[{1, 9}, {10^6, 2}];

{1, 2} + Transpose@m // Transpose // AbsoluteTiming // First
(* 0.025621 *)

{1, 2} + # & /@ m // AbsoluteTiming // First
(* 0.148701 *)
share|improve this answer
add comment
 i@l_ := l + {1, 2}
 i /@ {{5, 9}, {9, 7}, {1, 7}}
share|improve this answer
    
This is really what Kuba said. –  J. W. Perry Sep 12 '13 at 19:29
add comment

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