P is for Practical  
PerlMonks 
Re: PDL::Complex questionby drzowie (Initiate) 
on Apr 01, 2007 at 17:10 UTC ( #607717=note: print w/replies, xml )  Need Help?? 
The problem is that the 'x' operator is threading over the internal (realvalued) 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 (fastestrunning) indices of the final constructed PDL. That looks almost right, as PDL::Complex is implemented with an extra 0th dim that runs over {realimaginary}. 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 ({ri},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 ({ri},column) order. You have a couple of ways around the complex issue:
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:
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: which will yield a different answer than
In Section
Seekers of Perl Wisdom

