## WeBWorK Main Forum

### complex eigenvalues using R

by Andras Balogh -
Number of replies: 9

I am trying to use R to calculate eigenvalues (possibly for matrices of sizes 5x5 or 6x6 as computational exercise).

It works fine for a 3x3 matrix with real eigenvalues:
rserve_eval("eigsys=eigen(matrix(c(7,2,-4,4,5,-4,14,10,-11),nrow=3,ncol=3))");

But not for a 3x3 matrix with complex eigenvalues:
rserve_eval("eigsys=eigen(matrix(c(-1,-1,6, -7,8,8, 3,-5,4),nrow=3,ncol=3))");

I get the error message:
Can't locate object method "make" via package "Math::Complex" (perhaps you forgot to load "Math::Complex"?) at /usr/share/perl/5.30/Math/Complex.pm line 329
Died within Math::Complex::cplx called at line 351 of /usr/local/share/perl/5.30.0/Statistics/R/IO/QapEncoding.pm
from within Statistics::R::IO::QapEncoding::decode called at line 236 of /usr/local/share/perl/5.30.0/Statistics/R/IO/Rserve.pm
from within Statistics::R::IO::Rserve::eval called at line 26 of [PG]/lib/Rserve.pm
from within Rserve::try_eval called at line 146 of [PG]/macros/RserveClient.pl
from within main::rserve_eval called at line 40 of (eval 4456)

### Re: complex eigenvalues using R

by Arnold Pizer -

Looks like you need the perl package Math:Complex. If you are using Ubuntu I think the package you want is

libmath-complex-perl (e.g. sudo apt install libmath-complex-perl)

In general you can install Math:Complex using cpan
(e.g. see https://webwork.maa.org/wiki/Installation_Manual_for_2.15_on_Ubuntu_20.04_Server#Installing_Additional_Perl_Modules_from_CPAN)

### Re: complex eigenvalues using R

by Andras Balogh -

Math::Complex is installed.
The first line of the error message even references the file /usr/share/perl/5.30/Math/Complex.pm
The other part of that line mentions "perhaps you forgot to load "Math::Complex"?"

### Re: complex eigenvalues using R

by Arnold Pizer -
Does this work OK directly in R?

> eigsys=eigen(matrix(c(7,2,-4,4,5,-4,14,10,-11),nrow=3,ncol=3))
> eigsys
eigen() decomposition
$values [1] -3 3 1$vectors
[,1] [,2] [,3]
[1,] -0.5773503 -7.071068e-01 0.4082483
[2,] -0.5773503 7.071068e-01 0.8164966
[3,] 0.5773503 -1.838548e-16 -0.4082483

> eigsys=eigen(matrix(c(-1,-1,6, -7,8,8, 3,-5,4),nrow=3,ncol=3))
> eigsys
eigen() decomposition
$values [1] 6.088007+3.500761i 6.088007-3.500761i -1.176014+0.000000i$vectors
[,1] [,2] [,3]
[1,] -0.4806719+0.0398584i -0.4806719-0.0398584i -0.7905999+0i
[2,] 0.6552318+0.0000000i 0.6552318+0.0000000i 0.2243077+0i
[3,] 0.3466941-0.4667337i 0.3466941+0.4667337i 0.5697701+0i

### Re: complex eigenvalues using R

by Andras Balogh -
Yes, it works in R from the command line for both the real and the complex values.

I looked at the R installation again at https://webwork.maa.org/wiki/Installation_Manual_for_2.15_on_Ubuntu_20.04_Server#Install_the_R_server
Then I noticed that in our localOverrides.conf file I copied over from an old installation I have some extra lines
push @{$pg{modules}}, [qw( Moose )]; Then I found some old posts about similar issues at https://webwork.maa.org/moodle/mod/forum/discuss.php?d=4324 Although we now have Ubuntu server, loading the Math::Complex directly solved the problem. push @{$pg{modules}},
[qw( Math::Complex )],
[qw( Moose )];

I am not sure why this is needed, but it works now to calculate complex eigenvalues in WeBWorK.

### Re: complex eigenvalues using R

by Arnold Pizer -

So the instructions at https://webwork.maa.org/wiki/Installation_Manual_for_2.15_on_Ubuntu_20.04_Server#Install_the_R_server

are not sufficient.  Can you post a copy of your WeBWorK problem that I can use for testing.

Thanks.

### Re: complex eigenvalues using R

by Andras Balogh -
This is a stripped-down version. It is more interesting for larger matrix. For example, I am using it for 7x7 companion matrices.
--------------

DOCUMENT();

"PGstandard.pl", # Standard macros for PG language
"MathObjects.pl",
"PGML.pl", # allows code to be displayed on certain sites.
"RserveClient.pl",
);

TEXT(beginproblem());

$A=Matrix([[-1,-1,6], [-7,8,8], [3,-5,4]]); @A_p_arr =$A->value; # Convert from a MathObject matrix to a perl array
@A_flat = map {@$_} @A_p_arr; # Flatten into a 1 dimensional array$A_str = join(',',@A_flat); # Convert into a comma-separated string

rserve_start();
rserve_eval("Am_r <- t(matrix(c($A_str), nrow=3, ncol=3, byrow=TRUE ))"); #rserve matrix rserve_eval("ev <- eigen(Am_r)"); # eigensystem @eigv = rserve_eval("eigv <- ev$values"); # eigenvalues
rserve_finish();

Context()->texStrings;
BEGIN_PGML

The eigenvalues of matrix [[$A]] are [[$eigv[0][0]]]
[[$eigv[0][1]]] [[$eigv[0][2]] ]

END_PGML
Context()->normalStrings;

ENDDOCUMENT();
-----------

### Re: complex eigenvalues using R

by Arnold Pizer -

Thanks.  I can verify that you need to add Math::Complex to @{\$pg{modules}} in my set up for your problem to work.  What does the Moose module do?

### Re: complex eigenvalues using R

by Andras Balogh -

I have no idea what the Moose module does. It was there and I was afraid to remove it. 😂
Although when I removed it things seemed to work.