# How do I operate on a set of ordered pairs? [closed]

I have a calculation that will give me a lot of ordered pairs. For each such pair in a list, I want to square the elements and add them together. Here is an example of the kind of list I have

{{-3 a, 3 a}, {-1 a,  a}, {-1 a, a}, {-1 a,  a}}


In this instance I would have 18 a^2 for the first pair and so on.

• There are several options (and many variants have been asked before here)... the simplest: pairs /. {a_, b_} :> a^2 + b^2 – rm -rf May 14 '13 at 21:36
• you aren't using proper Mathematica syntax. Lists go between { } pairs – Dr. belisarius May 14 '13 at 21:43
• Plus @@@ (data^2). Power is Listable. – Verbeia May 15 '13 at 2:56

data = {{-3 a, 3 a}, {-a, a}, {-a, a}, {-a, a}};


rm-rf's comment:

data /. {a_, b_} :> a^2 + b^2


{18 a^2, 2 a^2, 2 a^2, 2 a^2}

Power is Listable, meaning:

data^2


{{9 a^2, 9 a^2}, {a^2, a^2}, {a^2, a^2}, {a^2, a^2}}

Thus, Verbeia's comment:

Plus @@@ (data^2)


{18 a^2, 2 a^2, 2 a^2, 2 a^2}

Or:

Total[data^2, {2}]


{18 a^2, 2 a^2, 2 a^2, 2 a^2}

Perhaps for Mathematica newcomers mapping an operator over the list of pairs will seem more intuitive. Let's take this in two steps to make it easy to understand what's going on. First define the operator

operator[{x_, y_}] := x^2 + y^2


Second map it over the data list

Map[operator, data]


{18 a^2, 2 a^2, 2 a^2, 2 a^2}

Of course there is a way to express this in one line which is not so easy to understand.

(#1^2 + #2^2)&[Sequence @@ #]& /@ data


{18 a^2, 2 a^2, 2 a^2, 2 a^2}