1
$\begingroup$

I would like to apply Eigenvalues[] to a list of matrices as in the following

foo[x_,y_]=x+y;

({{foo[#1, #2], foo[#1, #1]},
 {foo[#1, #1], foo[#1, #2]}}) & @@@ {{1, 2}, {1, 2}}

--> {{{3, 2}, {2, 3}}, {{3, 2}, {2, 3}}}

Eigenvalues @@@ %

However, I get the error-message "Eigenvalues::matsq: Argument 3 at position 1 is not a non-empty square matrix". What is the problem?

$\endgroup$
1
  • 1
    $\begingroup$ You need to use Map, not Apply. Additional braces are not necessary. $\endgroup$
    – Szabolcs
    Mar 25, 2014 at 14:17

2 Answers 2

4
$\begingroup$

Padding additional brackets is not the right way to do it. You should use the right function for the task, which is Map:

matrices = {{{3, 2}, {2, 3}}, {{3, 2}, {2, 3}}};
Eigenvalues /@ matrices

If you're insistent on using Apply (why?), then the following ways work:

Eigenvalues[{##}] & @@@ matrices
Eigenvalues @@@ List /@ matrices
$\endgroup$
3
  • $\begingroup$ EigenValues @@@ {matrices} does not do the same thing as the others. $\endgroup$
    – RunnyKine
    Mar 25, 2014 at 14:47
  • $\begingroup$ @RunnyKine Thanks for catching that! Fixed now. $\endgroup$
    – rm -rf
    Mar 25, 2014 at 15:05
  • $\begingroup$ Sure thing. Thanks for fixing it. +1 $\endgroup$
    – RunnyKine
    Mar 25, 2014 at 15:12
2
$\begingroup$

I got it: one has to put additional brackets around the first pure function:

{({{foo[#1, #2], foo[#1, #1]},
 {foo[#1, #1], foo[#1, #2]}})} & @@@ {{1, 2}, {1, 2}}
$\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.