# How to apply functions on elements of list

I want to apply a function on the following list and want to get of {n,{x,y}} the solution x^2-n*y^2 It can be done by:

 t3 = {{{11, {3, 1}}, {12, {3, 1}}}, {{11, {10, 3}}, {12, {7,
2}}}, {{11, {63, 19}}, {12, {45, 13}}}, {{11, {199,
60}}, {12, {97, 28}}}, {{11, {1257, 379}}, {12, {627, 181}}}};
tn = t3[[All, All, 1]];
tx = t3[[All, All, 2, 1]];
ty = t3[[All, All, 2, 2]];

tx^2 - tn*ty^2


resulting in

{{-2, -3}, {1, 1}, {-2, -3}, {1, 1}, {-2, -3}}


What would be a more elegant way to solve it?

Apply[{1, -#} . #2^2 &, t3, {2}]

{{-2, -3}, {1, 1}, {-2, -3}, {1, 1}, {-2, -3}}


Also

☺ = {1, -#}. #2^2 & @@@ # & /@ # &;
☺ @ t3

{{-2, -3}, {1, 1}, {-2, -3}, {1, 1}, {-2, -3}}


and

Map[{1, - First@#} .  Last[#]^2 &, t3, {2}]

{{-2, -3}, {1, 1}, {-2, -3}, {1, 1}, {-2, -3}}


and

ReplaceAll[{n_?NumericQ, {x_, y_}} :> x^2 - n y^2] @ t3

{{-2, -3}, {1, 1}, {-2, -3}, {1, 1}, {-2, -3}}

• writing {1, - #1} is more readable maybe? May 10, 2020 at 4:21
• @youyou, good point.
– kglr
May 10, 2020 at 4:24

Maybe this:

Partition[#, 2]& @ (#[[2, 1]]^2 - #[[1]]*#[[2, 2]]^2 & /@ Flatten[t3, 1])

{{-2, -3}, {1, 1}, {-2, -3}, {1, 1}, {-2, -3}}