Imagine you have a list of pairs of numbers, such as
{{2,-2}, {1,5}, {1,-3},{-3,3}}
The task is to reduce the given list to a list of pairs of type {a,-a}, where a may be any real number, i.e. our initial list should be reduced to {{2,-2},{-3,3}}.
I have come up with a somewhat cumbersome solution
DeleteCases[{{2, -2}, {1, 5}, {1, -3}, {-3, 3}}, {a_, b_} /; a + b != 0]
which is, on top of that, not particularly fast. The following code
Ta = Flatten[Table[{i, j}, {i, -1000, 1000}, {j, -1000, 1000}], 1];
Timing[DeleteCases[Ta, {a_, b_} /; a != -b]]`
yields
4.945, {{-1000, 1000}, {-999, 999}, (*...I have cut out the rest*)}
Now the question is:
- Is this an overkill?
- Can I perform the task in a more efficient manner?
- Can I maybe define the pattern in a more natural way, which happens to be more efficient as well?
Thanks a lot!
P.S.:
The question might be a bit ambiguous but I'd like to hear all your suggestions. :)