WeBWorK Main Forum

complex eigenvalues using R

complex eigenvalues using R

by Andras Balogh -
Number of replies: 9
In reply to Andras Balogh

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)
In reply to Arnold Pizer

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"?"
How would I load it if it is not loading? 

In reply to Andras Balogh

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
In reply to Arnold Pizer

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.
In reply to Andras Balogh

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.

In reply to Arnold Pizer

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();

loadMacros(
"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();
-----------
In reply to Andras Balogh

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?


In reply to Arnold Pizer

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. 

In reply to Andras Balogh

Re: complex eigenvalues using R

by Arnold Pizer -

Thanks.  I guess it's https://metacpan.org/pod/Moose

"The main goal of Moose is to make Perl 5 Object Oriented programming easier, more consistent, and less tedious. With Moose you can think more about what you want to do and less about the mechanics of OOP."  Wouldn't think this would be required for problem authors.

I'll update the documentation concerning the need to load Math:Complex for certain types of problems.  There might be analogous requirements for other situations.