4
$\begingroup$

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?

$\endgroup$

2 Answers 2

5
$\begingroup$
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}}
$\endgroup$
2
  • 1
    $\begingroup$ writing {1, - #1} is more readable maybe? $\endgroup$
    – youyou
    May 10, 2020 at 4:21
  • $\begingroup$ @youyou, good point. $\endgroup$
    – kglr
    May 10, 2020 at 4:24
4
$\begingroup$

Maybe this:

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

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.