1

As expected,

{a,b,c} - {a,a,a}
(* {0, -a+b, -a+c} *)

Or the same result can be obtained with less typing, thanks to implicit duplication of the subtrahend:

{a,b,c} - a
(* {0, -a+b, -a+c} *)

Likewise as expected,

{{a,b,c}, {d,e,f}, {g,h,i}} - {{a,b,c}, {a,b,c}, {a,b,c}}
(* {{0,0,0}, {-a+d, -b+e, -c+f}, {-a+g, -b+h, -c+i}} *)

But in this case the abbreviated query acts quite differently:

{{a,b,c}, {d,e,f}, {g,h,i}} - {a,b,c}
(* {{0, -a+b, -a+c}, {-b+d, -b+e, -b+f}, {-c+g, -c+h, -c+i}} *)

What is the distinction I am missing here?

bbgodfrey
  • 61,439
  • 17
  • 89
  • 156
Ralph Dratman
  • 1,250
  • 12
  • 20
  • 1
    You have participated in this community sufficiently to be aware of how code should be formatted in questions. So why aren't formatting your code in the community style? – m_goldberg Feb 22 '15 at 20:00
  • What you are missing is a thorough understanding of the Listable property of arithmetic functions. You should study the documentation concerning Listable carefully. Especially, the Details section. – m_goldberg Feb 22 '15 at 20:10
  • 1
  • I would like to format my code, but I don't know where to find the instructions for doing so. Of course I see other people's formatting results, but I don't see how they are achieving them. – Ralph Dratman Feb 23 '15 at 04:02
  • @RalphDratman http://mathematica.stackexchange.com/help/formatting – Kuba Feb 23 '15 at 14:32

2 Answers2

3

The reason that something like {a, b} + {c, d} gives {a + b, c + d} is because Plus has the Listable attribute, meaning that it automatically threads over lists. (You might consider the result "as expected" but you should keep in mind that not all functions behave this way. For example Rule is not listable and so {a, b} -> {c, d} gives {a, b} -> {c, d}, i.e. no change.) As noted in your question and in the documentation, "Arguments that are not lists are copied as many times as there are elements in the lists", so that {a, b} + c gives {a + c, b + c}.

An important feature, not well explained in the documentation, is that this automatic threading starts at the outermost level of the expression. So for example when you do:

{{a, b}, {c, d}} + {e, f}

Mathematica sees "a list of 2 items" plus "another list of 2 items" and threads Plus over those lists. At this initial step the fact that {a, b} is itself a list is not important, it could equally well be an image of a horse or any other expression.

After threading Plus over the outer lists the expression looks like

{{a, b} + e, {c, d} + f}

at which point Mathematica evaluates the subexpressions {a, b} + e and {c, d} + f, which involves more automatic threading over the lists, giving the final result:

{{a + e, b + e}, {c + f, d + f}}

You can see the order of evaluation for yourself using Trace:

Trace[{{a, b}, {c, d}} + {e, f}] // Column
{{a, b}, {c, d}} + {e, f}
{{a, b} + e, {c, d} + f}
{{a, b} + e, {a + e, b + e}}
{{c, d} + f, {c + f, d + f}}
{{a + e, b + e}, {c + f, d + f}}
Simon Woods
  • 84,945
  • 8
  • 175
  • 324
  • I appreciate your taking the time to explain that. "... not well explained in the documentation." The documentation and help system definitely is improving, but it is still not a straightforward task to master this language. – Ralph Dratman Feb 23 '15 at 04:08
  • This comment was a mistake, and now I can't figure out how to delete it. I am sorry. – Ralph Dratman Feb 23 '15 at 04:58
  • @RalphDratman, if you haven't already, I recommend reading the page on Evaluation, it will help you to understand how Mathematica proceeds through the evaluation and why attributes like Listable work the way they do. – Simon Woods Feb 23 '15 at 19:18
  • Simon Woods, thank you. I now see from the "closely related" answer, pointed out by Kuba, that you have thought about this and related issues at length. Apparently my mind is not properly wired for nested lists. In fact I spend far too much Mathematica time figuring out whether to write {2}, or maybe 2, or possibly 3 at the end of a Map or Apply construct. – Ralph Dratman Feb 23 '15 at 20:43
0
desired = {{a, b, c}, {d, e, f}, {g, h, i}} - {{a, b, c}, {a, b, c}, {a, b, 
c}}

{{0, 0, 0}, {-a + d, -b + e, -c + f}, {-a + g, -b + h, -c + i}}

{{a, b, c}, {d, e, f}, {g, h, i}} - {a, b, c} is interpreted as

{{a, b, c}, {d, e, f}, {g, h, i}} - {a, b, c} ==
 {{a, b, c} - a, {d, e, f} - b, {g, h, i} - c}

True

What you are seeking is

desired == (# - {a, b, c} & /@ {{a, b, c}, {d, e, f}, {g, h, i}})

True

Bob Hanlon
  • 157,611
  • 7
  • 77
  • 198