http://www.perlmonks.org?node_id=607717


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:

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:

$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;

Replies are listed 'Best First'.
Re^2: PDL::Complex question
by etj (Deacon) on Jun 20, 2022 at 14:41 UTC
    Note from THE FUTURE: in PDL 2.040 in April 2021, "native complex" data (with types cfloat or cdouble) worked properly, eliminating the 0th "real or imaginary" dimension. That then made the x operator work in standard PDL with native complex numbers. Around the same time, PDL::LinearAlgebra and PDL::FFTW3 were made to also work with "native complex" data, allowing it to be used with those fast C/Fortran libraries.