Angharad has asked for the wisdom of the Perl Monks concerning the following question:

Hi Fellow Monks and Monkettes
I have hit on a very frustrating problem. I have been working with PDL for a while now. I have been 'translating' matlab commands to PDL code, for something I have to do at work (and yes I know matlab can create a executable if one has the right add-on but I don't and in any case, it's not appropriate for my needs).
Anyway .. I am trying to do a matrix multiplication which worls fine in matab but not in PDL.
Matlab command is below
X = A x B x C x D
Where A is 35 columns by 71 rows, B is 7 columns by 35 rows, C is 35 columns by 35 rows and D is 7 columns by 35 rows. In matlab it works. In PDL it does not. I get the following error:
Error in inner:Wrong dims
Program code follows
use PDL; use PDL::Matrix; $file1 = $ARGV[0]; $file2 = $ARGV[1]; open(DATA1, "$file1") || die "Error: Unable to open $file1:$!\n"; open(DATA2, "$file2") || die "Error: Unable to open $file2:$!\n"; my $fileone; my $filetwo; while ( <DATA1> ) { chomp; #my @l = split /,\s+/; my @line1 = split(/,/); # for loop for($i = 0; $i < 35; $i++) { push @{$fileone->[$i]}, $line1[$i]; } } $fileone = PDL::Matrix->pdl($fileone); while ( <DATA2> ) { chomp; my @line2 = split (/,/); for($j = 0; $j < 35; $j++) { push @{$filetwo->[$j]}, $line2[$j]; } } $filetwo = PDL::Matrix->pdl($filetwo); $m = $fileone->transpose(); ($V, $S, $U) = svd($m); @valuesS = list $S; $SqS = stretcher($S); $Vs = $V->slice('0:7,0:34'); $Ut = $U->transpose(); $Ss = $SqS->slice('0:7,0:7'); $Us = $Ut->slice('0:70,0:7'); $A = $Us x $Ss x $Vs; ($Uf, $Sf, $Vf) = svd($sec); $B = $Vf->transpose(); @valuesSf = list $Sf; $SqSf = stretcher($Sf); $shortS = $SqSf->slice('0:6,0:6'); $invSf = $shortS->inv(); my $zeros = zeroes(28,7); $invSftransposed=transpose($invSf); $C = $invSftransposed->append($zeros); $C=transpose($C); $D = $Uf->slice('0:6,0:6'); # PDL coughs at the next command. $x = A x B x C x D;
Any help/suggestions much appreciated.

Replies are listed 'Best First'.
Re: Why dies this work in matlab and not PDL?
by puploki (Hermit) on Sep 20, 2005 at 12:32 UTC

    My maths is a bit poor, but are the dimensions of the matrices incorrect for multiplication? I seem to remember if you write down [ rows : columns ] x [ rows : columns ] then the two inner numbers have to be the same.

    In this case:

    [ 71 : 35] x [ 35 : 7 ] x [ 35 : 35 ] x [ 35 : 7 ]

    So, the B x C operation doesn't work. Have you tried replacing these by square matricies all of the same size, or just doing A x B x D?

Re: Why dies this work in matlab and not PDL?
by newroz (Monk) on Sep 20, 2005 at 15:29 UTC
    Hi, Relevant code might work with matlab, but pdl has a different look while constructing matrices.
    First you write dimension of columns, then rows, which is reverse of matlab and octave.
    forexample, sequence(4,3) create a 3 x 4 matrix which should be confusing for a matlab user.
    #!/usr/bin/perl use PDL; use PDL::Matrix; $a = sequence(2,3); $b = sequence(3,3); $z= $a x $b; print $z;
    Error in inner:Wrong dims
    Insert below snippet before $x = A x B x C x D;
    print $A->getdim(0); #column print $A->getdim(1); #row ....and for the rest matrices
    May be you got confused of matrices of pdl. Cheers
      Thats a distinct possibility. Thanks ;)
Re: Why dies this work in matlab and not PDL?
by Angharad (Pilgrim) on Sep 20, 2005 at 15:15 UTC
    Thanks for all the advice. Much appreciated. The problem has been sorted out now. Apparently its to do with the fact that PDL treats an N-PDL as a row-vector and a 1xN-PDL as a column vector, unlike Matlab.
    This took care of that.
    $X = $A x $B x $C->xchg(0,1) x $D;
    I'm still not quite understanding but hey, it works.
Re: Why dies this work in matlab and not PDL?
by thor (Priest) on Sep 20, 2005 at 14:26 UTC
    I'd be interested to see what kind of answer matlab is coming up with. For the dimensions that you mention, matrix multiplication is not possible. For two matricies A (with Ar rows and Ac columns) and B (with Br rows and Bc columns), the matrix product A x B = C is possible iff Ac = Br and C will have Ar rows and Bc columns.

    thor

    Feel the white light, the light within
    Be your own disciple, fan the sparks of will
    For all of us waiting, your kingdom will come

Re: Why dies this work in matlab and not PDL?
by Anonymous Monk on Sep 20, 2005 at 12:43 UTC
    $x = A x B x C x D;
    Might it have something to do with the missing dollar-signs?
      lol. Good catch, but no. I promise you the dollar signs are in the code ;)
Re: Why does this work in matlab and not PDL?
by Angharad (Pilgrim) on Sep 20, 2005 at 13:52 UTC
    Ok, first of all I must apologise I realise now one of matrix dimensions were incorrect.
    Anyway ... given the matrices A = 71 x 35 B = 35 x 35 C = 35 x 7 D = 7 x 7
    The matrix multiplication should be I hope
    (71x35) x (35 x 35) x (35 X 7) x (7 x 7)
    or
    A x B x C x D
    But it doesnt work. Any suggestions much appreciated.
      Have you tried breaking it up into one multiplication at a time?
      $X = $A x $B; $X = $X x $C; $X = $X x $D;
      That should tell you (at least) which of the multiplications PDL thinks has the wrong dimensions. Is it possible to have PDL tell you what dimensions it thinks each variable has?

      Caution: Contents may have been coded under pressure.
        This shouldn't be necessary. Matrix multiplication is associative, meaning that A x (B x C) == (A x B) x C, so it really shouldn't matter in which order PDL is evaluating the multiplications. However, given that there is an error at all would suggest that there's something else going on and I do agree with your approach to debugging it. :)

        thor

        Feel the white light, the light within
        Be your own disciple, fan the sparks of will
        For all of us waiting, your kingdom will come

Re: Why dies this work in matlab and not PDL?
by ghenry (Vicar) on Sep 20, 2005 at 12:21 UTC

    Maybe use warnings; might show if the error is from somewhere else in your code?

    Walking the road to enlightenment... I found a penguin and a camel on the way.....
    Fancy a yourname@perl.me.uk? Just ask!!!
      Thanks but that didnt show up anything. No, I think it might be due more to perhaps PDL not being able to cope quite as well as Matlab ... although I hope I am wrong :(