1
$\begingroup$

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)?

$\endgroup$
3
  • 2
    $\begingroup$ 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. $\endgroup$
    – Kuba
    Sep 12, 2013 at 13:33
  • $\begingroup$ @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}}. $\endgroup$
    – kjo
    Sep 12, 2013 at 13:39
  • 2
    $\begingroup$ 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. $\endgroup$
    – Kuba
    Sep 12, 2013 at 13:59

2 Answers 2

3
$\begingroup$

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 *)
$\endgroup$
1
$\begingroup$
 i@l_ := l + {1, 2}
 i /@ {{5, 9}, {9, 7}, {1, 7}}
$\endgroup$
1
  • $\begingroup$ This is really what Kuba said. $\endgroup$ Sep 12, 2013 at 19:29

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