Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: PDL::Complex question

by drzowie (Initiate)
on Apr 01, 2007 at 17:10 UTC ( #607717=note: print w/replies, xml ) Need Help??

in reply to PDL::Complex question

The problem is that the 'x' operator is threading over the internal (real-valued) matrices rather than computing the correct complex products.

That, in turn, is because you are using the pdl() constructor around your matrices. Thus they are not blessed into the PDL::Complex class, but only into the PDL class. The pdl() constructor is pretty permissive and turns just about anything into a PDL, but can be confused on occasion. In this case, pdl() recognizes a collection of nested list refs, some of which contain PDLs (or at least PDL subclasses). It is trying to do the Right Thing by putting your inner PDL dims in the deepest (fastest-running) indices of the final constructed PDL. That looks almost right, as PDL::Complex is implemented with an extra 0th dim that runs over {real|imaginary}.

Because your matrices are PDLs and not PDL::Complexes, they are being multiplied differently than you expect: MatrixM is a 2x2x2, which is treated as a collection of two 2x2 matrices in (column, row, thread) order rather than a single complex matrix in ({r|i},column,row) order. MatrixX is a 2x2, which is being treated as a single matrix in (column, row) order rather than a complex row vector in ({r|i},column) order. You have a couple of ways around the complex issue:

  • (1) Use the correct constructor, "new PDL::Complex", rather than the incorrect one "pdl";
  • (2) Manually bless your PDLs back into PDL::Complex with 'bless($matrixM,"PDL::Complex");' (this is what PDL::Complex::new does anyway); or
  • (3) Assemble your matrices from independently constructed PDLs, as in '$matrix=pdl([5,4])+pdl([1,2])*i;'

I think that you are about to hit another problem, which is that $matrixX is a row vector rather than (as you probably think, given that you put it on the right hand side of a matrix) a column vector.

The row vector/column vector problem arises from the need to choose between (row,column) indexing order, which renders incorrectly on screen, or (column, row) indexing order, which is backwards but renders correctly on screen. PDL uses (column,row) order by default.

To make a row vector, you say '$row = pdl(1,2,3)' or (if you prefer) '$row = pdl [1,2,3]'.

To make a column vector you must insert a trivial 0th dim (of size 1), which you can do thusly:

  • (1) by nesting: $col = pdl([1],[2],[3])
  • (2) by transposition: $col = pdl(1,2,3)->transpose
  • (3) by dummy index: $col = pdl(1,2,3)->(*1)

Incidentally, there's one more index pitfall to watch for (though it isn't yet in your example): sometimes you may want to thread across row vectors (for example, if you want to multiply a collection of N row vectors by a collection of N matrices that are stored in a CxRxN PDL). Then you should remember to insert the dummy dimension explicitly. For example, if your vectors are (1,2), (4,5), and (7,8) you should say:

$matrices = sequence(2,2,3); $threadable_rows = pdl([1,2],[4,5],[7,8])->(:,*1,:); print $threadable_rows x $matrices;
which will yield a different answer than
$matrices = sequence(2,2,3); $wrong_rows = pdl([1,2],[4,5],[7,8]); print $wrong_rows x $matrices;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://607717]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2017-03-24 08:43 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (298 votes). Check out past polls.