gmacfadden has asked for the wisdom of the Perl Monks concerning the following question:

Is there a Perl module which corresponds to Math::MatrixReal for matrices that have both complex (real and imaginary) number components?

The below Perl code solves n-equations in n-unknowns (n = 4 in this case) for REAL numbers.

I want to achieve the same functionality for n-equations in n-unknowns for COMPLEX numbers. Since there is no perl module named Math::MatrixComplex, and seaching CPAN didn't result in anything helpful, please can you advise. Thanks.

#! /usr/bin/perl -wT use strict; use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use CGI qw(:standard escapeHTML); use Math::MatrixReal; my $matrix1 = Math::MatrixReal->new_from_rows( [ [1, 1, 1, 2], [1, 0, 2, 3],[2,-1, 0,-1], [0, 1, 0, 1] ]); my $matrix2 = Math::MatrixReal->new_from_rows([ [2],[1],[2],[0] ]); print header (), start_html (-title => "Solve Simultaneous Equations") +; print "Solution = ", $matrix1->inverse() * $matrix2,"<br>\n"; print end_html (); exit(0);

Replies are listed 'Best First'.
Re: Working with complex, not real number matrices
by lin0 (Curate) on Mar 14, 2007 at 21:18 UTC
      Also specifically PDL::Complex.

      HTH, andye

      (Thanks for the PDL cheat sheet lin0 - hadn't seen that before - very useful!)

Re: Working with complex, not real number matrices
by jettero (Monsignor) on Mar 14, 2007 at 20:01 UTC
    I'm not aware of anything off the top of my head... but if you don't find anything pre-build, my guess is that you can construct a complex scalar module with overload. It's remarkably easy to work with.

    Have you tried Math::Complex? It might just work.

    my $wakky = Math::Complex->make(5, 6); my $matrix1 = Math::MatrixReal->new_from_rows( [ [1, 1, 1, 2], [1, 0, $wakky, 3],[2,-1, 0,-1], [0, 1, 0, 1] ]);

    -Paul

      Thank you for you reply. Testing out your overloading hypothesis a la the below code, resulted in an HTTP 500 - Internal server error.

      #! /usr/bin/perl -wT use strict; use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use CGI qw(:standard escapeHTML); use Math::MatrixReal; use Math::Complex; #my $wakky = 7; my $wakky =Math::Complex->make(5, 6); my $matrix1 = Math::MatrixReal->new_from_rows([ [1,1,1,2],[1,0,$wakky,3],[2,-1,0,-1],[ 0,1,0,1] ]); print header (), start_html (-title => "Matrix Ops"); print "\$matrix1 = ", $matrix1 ,"<br>\n"; print end_html (); exit(0);
      However, if $wakky is changed to a real number Math::MatrixReal is happy. I noticed in the CPAN Math::Complex documention that there was no mention of overload working with matrices.

      If I've got something amiss, please let me know, I'd appreciate your further suggestion. In the interim, I'll start following up on the others' suggestions.

      Thanks and best regards - Gary

Re: Working with complex, not real number matrices
by Khen1950fx (Canon) on Mar 14, 2007 at 20:27 UTC
    There's Math::MatrixSparse. It has the basic matrix operations, and it was designed to overlap Math::MatrixReal.
      Well, I am in the (not) quite lucky situation to be forced to maintan modules relying on Math::MatrixSparse for $job, and I can tell you this module is, on the one hand, not prepared to do complex matrix arithmetics and, on the other hand, buggy and unmaintained. I have a plethora of patches and obvious fixes to this module and I even maintain an own updated version for $job purposes.

      Don't get me wrong: Math::MatrixSparse is a brilliant piece of Perl code. It is just that it is now old and it's stinking. Don't use it.

Re: Working with complex, not real number matrices
by randyk (Parson) on Mar 15, 2007 at 15:05 UTC

    If you don't mind a bit of algebra, you could write the equations in terms of their real and imaginary parts:

    M A = B (Re M + i Im M) (Re A + i Im A) = (Re B + i Im B) (Re M) (Re A) - (Im M) (Im A) = Re B (Re M) (Im A) + (Im M) (Re A) = Im B
    The latter two equations are two coupled (real) equations for the unknowns Re A and Im A, which you can solve for using Math::MatrixReal as in your example.

    Update: You could write these two coupled equations as one matrix equation:

    / Re M -Im M \ / Re A \ / Re B \ | | | | = | | \ Im M Re M / \ Im A / \ Im B /
    and then use Math::MatrixReal as before.

      Hi randyk,

      very closed form, elegant, and simple....THANKS!

      gmacfadden