Pathologically Eclectic Rubbish Lister PerlMonks

### Re: problem with complex matrix multiplication using PDL

by Athanasius (Chancellor)
 on Oct 07, 2012 at 06:44 UTC ( #997662=note: print w/replies, xml ) Need Help??

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

Replies are listed 'Best First'.
Re^2: problem with complex matrix multiplication using PDL
by smh (Acolyte) on Oct 09, 2012 at 04:27 UTC

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: note [id://997662]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2017-12-14 19:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
What programming language do you hate the most?

Results (407 votes). Check out past polls.

Notices?