Manually multiplying
(1+i)(3+i) + (2+i)(2+i) = 5+8i
(1-2i)(3+i) +(2-i)(2+i) = 10-5i
These two equations can be expressed in terms of the matrix equation MX = B, where
/ 1+i 2+i \ / 3+i \ / 5+8i \
M = | | X = | | and B = | |
\1-2i 2-i / \ 2+i / \ 10-5i /
One should therefore be able to use complex piddles to obtain B by multiplying M times X, or alternatively obtain X by multiplying the inverse of M times B using the following code:
#! /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] ];
my $matrixB_assigned_value = pdl [ 5+8*i,
10-5*i ];
my $matrixX_assigned_value = pdl [ 3+1*i,
2+1*i ];
my ($matrixB_computed_value, $matrixX_computed_value);
print "Via Perl Data Language\n";
print "List assigned_values of matrices:\n";
print "\$matrixM = ", $matrixM,"\n";
print "\$matrixX_assigned_value = ", $matrixX_assigned_value,"\n";
print "\$matrixB_assigned_value = ", $matrixB_assigned_value,"<br>\n";
print "List computed values of matrices:\n";
print "\$matrixB_computed_value = ",
$matrixM x $matrixX_assigned_value,"\n";
print "\$matrixX_computed_value = ",
$matrixM->inv x $matrixB_assigned_value,"\n";
exit(0);
When the above code is executed, the following output is created:
Via Perl Data Language
List assigned_values of matrices:
$matrixM =
[
[
[1 1]
[2 1]
]
[
[ 1 -2]
[ 2 -1]
]
]
$matrixX_assigned_value =
[
[3 1]
[2 1]
]
$matrixB_assigned_value =
[
[ 5 8]
[10 -5]
]
<br>
List computed values of matrices:
$matrixB_computed_value =
[
[
[5 2]
[8 3]
]
[
[-1 -1]
[ 4 1]
]
]
$matrixX_computed_value =
[
[
[ 5 -13]
[ 0 21]
]
[
[ 5 -6]
[ 0 -7]
]
]
Notice how the assigned values for M, X, and B dutifully print as expected. However the computed values for X and B are strange. Please can you explain what I need to change to correctly compute the values of X and B in my little example.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.