Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Thank you for identifying the problem with the variable i when using CGI and PDL simultaneously. I eliminated the CGI loads (and therefore was forced to run the PDL scripts from my shell - wish I could have it both ways). However, there is still a problem.

Let's suppose we want to solve two (2) complex simultaneous equations in two unknowns:

Equation 1: (1+i)*Xsub1 + (2+i)*Xsub2 = 5+10i

Equation 2: (1-2i)*Xsub1 + (2-i)*Xsub2 = 8 -5i

The following code using only real PDL variables, is well-behaved and correctly solves for the variables Xsub1 and Xsub2 as 3+i, and 2+i respectively as the execution demonstrated forthwith after the code prooves.

.
#! /usr/bin/perl -w use warnings; use strict; use PDL; my $matrixM = pdl [ [ 1, 2,-1,-1], [ 1, 2, 2, 1], [ 1, 1, 1, 2], [-2,-1, 1, 2] ]; my $matrixB = pdl [ [5],[10], [8],[-5] ]; my $matrixX; print "\$matrixM = ", $matrixM,"<br>\n"; print "\$matrixB = ", $matrixB,"<br>\n"; print "\$matrixX = ", $matrixM->inv x $matrixB,"<br>\n"; exit(0);
The results from running the above script are:
$matrixM = [ [ 1 2 -1 -1] [ 1 2 2 1] [ 1 1 1 2] [-2 -1 1 2]] $matrixB = [ [ 5] [10] [ 8] [-5]] $matrixX = [ [ 3] [ 2] [ 1] [ 1]]
This says that Xsub1 = 3+i, and Xsub2 = 2+i .....which is the correct result!

Now, in an effort to simplify, let's use the following functionally equivalent (?) PDL with complex matrices to solve the same two same simultaneous equations; observe the results after 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] ];<br> my $matrixB = pdl [ 5+8*i, 10-5*i ]; my $matrixX; print "\$matrixM = ",$matrixM,"<br>\n"; print "\$matrixB = ", $matrixB,"<br>\n"; print "\$matrixX = ", $matrixM->inv x $matrixB,"<br>\n"; exit(0);
The results from running the above script are:
$matrixM =[ [ [1 1] [2 1] ] [ [ 1 -2] [ 2 -1] ] ] $matrixB =[ [ 5 8] [10 -5] ] $matrixX = [ [ [ 5 -13] [ 0 21] ] [ [ 5 -6] [ 0 -7] ] ]
So my question is why am I getting such a clearly erroneous result when I run the second piece of code?

In reply to Re^2: PDL works for real number matrix operations, but not working for complex number matrix operations. by gmacfadden
in thread PDL works for real number matrix operations, but not working for complex number matrix operations. by gmacfadden

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-03-29 14:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found