Skip to main content
Post Closed as "Not suitable for this site" by ciao, AsukaMinato, Bob Hanlon, MarcoB, Daniel Lichtblau
Became Hot Network Question
Code format
Source Link
ciao
  • 26k
  • 2
  • 61
  • 142

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}

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}}{{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}

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}}{{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}]

Cases[list2, {x_, y_} -> {x + y, x - y}]

Thank you very much!

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!

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!

Source Link

ReplaceAll command /. (a possible bug?)

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!