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)?
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$A, B, C, D
are all of some type X for which addition is defined, I'm saying thatA + {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${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$