Suppose you have a list, with each element an ordered pair {a,b} We want to create a new list where each element {a,b} is replaced by {a+b,a-b}
If the original list has three or more ordered pairs, the following works:
list1 = {{a, b}, {c, d}, {e, f}}; (* This list has three ordered pairs *)
list1 /. {x_, y_} -> {x + y, x - y}
The output is, as expected: {{a + b, a - b}, {c + d, c - d}, {e + f, e - f}}
However, if the list has ONLY TWO ordered pairs, the above procedure doesn't seem to work, as shown below:
list2 = {{a, b}, {c, d}}; (* This list has ONLY TWO ordered pairs *)
list2 /. {x_, y_} -> {x + y, x - y}
The output is, unexpectedly(?) {{a + c, b + d}, {a - c, b - d}}
Can someone please tell me why this doesn't work? I would appreciate any insight.
I know I can do something like the following to get the desired result:
Cases[list2, {x_, y_} -> {x + y, x - y}]
Thank you very much!
Replace[list1, {x_, y_} -> {x + y, x - y}, {1}]
for what you're after. $\endgroup$ReplaceAll
, trylist2 /. {Except[_List, x_], y_} :> {x + y, x - y}
. $\endgroup$