Problems? Is your data what you think it is? PerlMonks

problem with complex matrix multiplication using PDL

by smh (Acolyte)
 on Oct 06, 2012 at 01:59 UTC Need Help??
smh has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am trying to do complex matrix multiplication using PDL. I used an example from here, however, I get an error.

``` 2x2 matrix *** this works fine ****
#! /usr/bin/perl -w
use warnings;
use strict;
use PDL;
use PDL::Complex;

my \$matrixM = pdl [ [ 1+1*i, 2+1*i],
[ 1-2*i, 2-1*i]
];

print "Via Perl Data Language\n";
print "List assigned_values of matrices:\n";
print "\\$matrixM = ", \$matrixM,"\n";
print "\n Inv of \\$matrixM = ", \$matrixM->inv,"\n";

print "\n\n+++++++++++++++++++++++++\n\n";
exit(0);

I just extended the matrix from 2x2 to a 3x3, I get an error "PDL: inverse needs a square PDL as a matrix"

``` 3x3 matrix *** does not work ****
#! /usr/bin/perl -w
use warnings;
use strict;
use PDL;
use PDL::Complex;

my \$matrixM = pdl [ [ 1+1*i, 2+1*i, 3+1*i],
[ 1-2*i, 2-1*i, -2+1*i],
[ -1+1*i, 2+1*i, -1+1*i]
];

print "Via Perl Data Language\n";
print "List assigned_values of matrices:\n";
print "\\$matrixM = ", \$matrixM,"\n";
print "\n Inv of \\$matrixM = ", \$matrixM->inv,"\n";

print "\n\n+++++++++++++++++++++++++\n\n";
exit(0);

smh

Replies are listed 'Best First'.
Re: problem with complex matrix multiplication using PDL
by cord-bin (Friar) on Oct 06, 2012 at 06:07 UTC
Have you tried to compute the determinant of your matrix ? A square matrix has an inverse if and only if its determinant is not zero.
Re: problem with complex matrix multiplication using PDL
by zentara (Archbishop) on Oct 06, 2012 at 13:57 UTC
Re: problem with complex matrix multiplication using PDL
by Athanasius (Chancellor) on Oct 07, 2012 at 06:44 UTC

Would a non-PDL solution be of use to you? Here’s an example showing Math::Matrix and Math::Complex working together:

```#! /usr/bin/perl
use strict;
use warnings;
use Math::Complex;
use Math::Matrix;

package Math::Matrix
{
our \$Precision = 3;

sub print_complex
{
my (\$self, @args) = @_;
print @args if @args;

for (@\$self)
{
my @z;

for (@\$_)
{
my (\$re, \$im) = @{\$_->{cartesian}};
push @z, sprintf "%s%.*f%s%.*fi", (\$re < 0 ? '' : ' ')
+, \$Precision, \$re,
(\$im < 0 ? '' : '+')
+, \$Precision, \$im;
}

print '| ', join(' ', @z), " |\n";
}
}
}

package main;

my \$matrix_2x2 = new Math::Matrix(
[  1+1*i, 2+1*i ],
[  1-2*i, 2-1*i ]
);
\$matrix_2x2          ->print_complex("\n\\$matrix_2x2:\n\n");
\$matrix_2x2->invert()->print_complex("\nInverse of \\$matrix_2x2:\n\n")
+;

\$Math::Matrix::Precision = 5;
my \$matrix_3x3 = new Math::Matrix(
[  1+1*i, 2+1*i,  3+1*i ],
[  1-2*i, 2-1*i, -2+1*i ],
[ -1+1*i, 2+1*i, -1+1*i ]
);
\$matrix_3x3          ->print_complex("\n\\$matrix_3x3:\n\n");
\$matrix_3x3->invert()->print_complex("\nInverse of \\$matrix_3x3:\n\n")
+;

Output:

```\$matrix_2x2:

|  1.000+1.000i  2.000+1.000i |
|  1.000-2.000i  2.000-1.000i |

Inverse of \$matrix_2x2:

| -0.353-0.412i -0.118+0.529i |
|  0.529+0.118i  0.176-0.294i |

\$matrix_3x3:

|  1.00000+1.00000i  2.00000+1.00000i  3.00000+1.00000i |
|  1.00000-2.00000i  2.00000-1.00000i -2.00000+1.00000i |
| -1.00000+1.00000i  2.00000+1.00000i -1.00000+1.00000i |

Inverse of \$matrix_3x3:

|  0.05235+0.14079i  0.13718+0.23105i -0.31949-0.16968i |
|  0.09747-0.15162i  0.08303-0.01805i  0.26715+0.02888i |
|  0.22383-0.07040i -0.06859-0.11552i -0.09025+0.08484i |

Hope that helps,

Athanasius <°(((><contra mundum

Hi , Thanks a lot. I tried your code and keep getting this error.

```smh> perl -w math_matrix.pl
syntax error at math_matrix.pl line 9, near "{
"
Execution of math_matrix.pl aborted due to compilation errors.

If this works, it will do what I need. Regards, smh

Looks like you’re using an older version of Perl. Change the code to:

```{
package Math::Matrix;
...

This should remove the compile error. However, the inverse calculations will likely be incorrect. If so, upgrade Math::Complex to the latest version (1.59), and you should be set to go.

Athanasius <°(((><contra mundum

Create A New User
Node Status?
node history
Node Type: perlquestion [id://997562]
Approved by toolic
Front-paged by Tanktalus
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?