Hey Coe, I can explain this.
Total
is documented to take the elements of a list and add them together. In other words:
Total[{a,b,c,...}] === Plus@@{a,b,c,...} === Plus[a,b,c,...]
In standard notation, that means that Total[{a,b,c,...}]
just evaluates to a+b+c+...
. Nothing too surprising here, I think.
The misunderstanding likely comes from two places.
Since Wolfram Language is fully symbolic by default, we never said what a
, b
, c
and so on might be. They don't need to be reals or integers or even numbers—in fact, try this for me:
randomImages=Table[RandomImage[{0, 1}, {25, 25}], {2}]
Total@randomImages
If there is a sense in which the arguments inside the List
fed to Total
can be added, then Total
will add them because it is very literally just performing the pattern-matching term-rewriting transformation rule I expressed above.
Now that we know what Total
does, if we want to understand the output of Total[{{a, b, c}, {u, v, w}, {x, y, z}}]
, we need to ask: what does Plus
do when fed multiple List
objects?
You can try this out manually, but that's basically just rewriting the thing that we're not understanding in the first place. Instead, you might try Attributes[Plus]
. You get back several attributes, but the important one here is Listable
. If you check the documentation for that, you'll see examples which basically replicate the behavior that prompted this question:
In[1]:= {a, b, c} + {x, y, z}
Out[1]= {a + x, b + y, c + z}
This is to say: Listable
functions automatically thread over their arguments when those arguments are lists. Most built-in mathematics functions are listable, and this attribute allows for highly optimized code compared to looping or even mapping.
In the case of Plus
, this behavior also happens to correspond to the standard mathematical operation of addition acting on vectors, matrices, and tensors more generally. After all, if you have the vectors a, b, and c, you find their sum by element-wise addition, right? The same is true for matrices and so on. (And in fact, people are often lazy and will "add" a scalar to a vector when what they really mean is that they want to add a constant vector to a vector. For example, somebody might say x = <1,2,3>, then say something like x + 1, when really they mean x + <1,1,1>. This is also automatically handled by Listable
; feel free to evaluate {a,b,c}+1
and see the result, or even {{a,b},{c,d}}+1
.)