Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

creating matrices for PDL module

by Angharad (Pilgrim)
on Jul 01, 2005 at 12:06 UTC ( [id://471677]=perlquestion: print w/replies, xml ) Need Help??

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

Hi. I'm trying to write what is probably a simple perl script to create five matrices (I want to be able to calculate the determinants for each matrix using PDL's determinant() function).
I have a text file which looks like this.

4 2 9 7 21 2 5 4 4 11.5 6 3 5 3 15.5 8 4 6 6 22

And I want to be able to create five matrices like this from the text file.

my $dmat = pdl [ [4, 2, 9, 7], [2, 5, 5, 4], [6, 3, 5, 3], [8, 4, 6, 6], ]; my $damat = pdl [ [21, 2, 9, 7], [11.5, 5, 5, 4], [15.5, 3, 5, 3], [22, 4, 6, 6], ]; my $dbmat = pdl [ [4, 21, 9, 7], [2, 11.5, 5, 4], [6, 15.5, 5, 3], [8, 22, 6, 6], ]; my $dcmat = pdl [ [4, 2, 21, 7], [2, 5, 11.5, 4], [6, 3, 15.5, 3], [8, 4, 22, 6], ]; my $ddmat = pdl [ [4, 2, 9, 21], [2, 5, 5, 11.5], [6, 3, 5, 15.5], [8, 4, 6, 22], ];

This is what I have managed so far. Right now I'm working on creating the first matrix only.

#!/usr/bin/perl -w use PDL; $matrixfile = $ARGV[0]; open(MATRIX, "$matrixfile") || die "Error: Can't open $matrixfile file + for reading: $!\n"; @matrix = <MATRIX>; for(my $i = 0; $i < @matrix; $i ++) { $a = substr($matrix[$i], 0, 5); $b = substr($matrix[$i], 6, 5); $c = substr($matrix[$i], 12, 5); $d = substr($matrix[$i], 18, 5); $cd = substr($matrix[$i], 24, 5); my $dmat = pdl [ [ $a, $b, $c, $d], } ];

But of course it doesn't work, it comes up with a syntax error ... because the last ]; is in the 'wrong' place .. but I need it 'there' so to speak to mark the end of the matrix if you see what I mean. Could anyone please point me in the right direction? Thanks.

READMORE tags added by Arunbear

Replies are listed 'Best First'.
Re: creating matrices for PDL module
by monarch (Priest) on Jul 01, 2005 at 12:28 UTC
    May I recommend an editor (other than notepad) which may have improved indenting facilities?

    The syntax error is your friend, if it tells you that ]; is bad, then it probably is. If not, then something near it is.

    If you were to indent your code in a more clear fashion your code might look like this:

    Instantly you'd see that maybe a closing bracket was missing off the my $dmat = pdl [ ... ]; command. And that the final curly brace should probably be swapped with the square bracket the syntax error is complaining about.

    Update: posted solution here:

Re: creating matrices for PDL module
by gam3 (Curate) on Jul 01, 2005 at 12:39 UTC
    I have no idea what 'there' you are talking about, but here is code that will build the arrays.
    #!/usr/bin/perl -w use PDL; $matrixfile = $ARGV[0]; open(MATRIX, "$matrixfile") || die "Error: Can't open $matrixfile file + for reading: $!\n"; @matrix = map({chomp; [split(/\s+/, $_)]} <MATRIX>); for my $ctl ( [ qw ( 0 1 2 3 ) ], [ qw ( 4 1 2 3 ) ], [ qw ( 0 4 2 3 ) ], [ qw ( 0 1 4 3 ) ], [ qw ( 0 1 2 4 ) ], ) { for my $data (@matrix) { for my $offset (@$ctl) { print $data->[$offset], ' '; } print "\n"; } print "\n"; }
    If you replace the print functions with pushes into an array you should end up with what you want.
    -- gam3
    A picture is worth a thousand words, but takes 200K.
Re: creating matrices for PDL module
by tlm (Prior) on Jul 01, 2005 at 12:49 UTC

    I don't know much about PDL, but your problem doesn't seem that difficult. You have a file with 4 rows and 5 columns, and you want to compute the determinant of 4x4 matrices obtained by picking, and possibly reordering, 4 out of the 5 columns. Though it's not rocket science to move around columns, it is simpler to move around rows, and since the determinant is not affected by taking the transpose, I'd work with the transpose. I.e. I'd compute the determinants of

    pdl [ [4, 2, 6, 8], [2, 5, 3, 4], [9, 5, 5, 6], [7, 4, 3, 6], ], pdl [ [21, 11.5, 15.5, 22], [2, 5, 3, 4], [9, 5, 5, 6], [7, 4, 3, 6], ],
    etc. So this is what I'd do (untested):
    my @rows; my $j = 0; { local @ARGV = $ARGV[ 0 ]; while ( <> ) { my $i = 0; $rows[ $i++ ][ $j ] = $_ for split; $j++; } } my $dmat = pdl [ @rows[ 0, 1, 2, 3 ] ]; my $damat = pdl [ @rows[ 4, 1, 2, 3 ] ]; my $dbmat = pdl [ @rows[ 0, 4, 2, 3 ] ]; my $dcmat = pdl [ @rows[ 0, 1, 4, 3 ] ]; my $ddmat = pdl [ @rows[ 0, 1, 3, 4 ] ]; # proceed to compute the determinants of the $dmat, $damat, etc.

    the lowliest monk

Re: creating matrices for PDL module
by polettix (Vicar) on Jul 01, 2005 at 13:03 UTC
    Take a look at the dice function as well. You can build up the whole PDL matrix with the input data, then extract the only columns you're interested into in the order you want.

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.
Re: creating matrices for PDL module
by bart (Canon) on Jul 01, 2005 at 13:31 UTC
    OK, here is something from the top of my head.
    my @rows = grep @$_, map [ split " " ], <>; my @col = map pop @$_, @rows; my $dmat = pdl(\@rows); my @mat = map { my $i = $_; pdl([ map { my $row = $_; [ map { $i==$_ ? $col[$i] : $row->[$_] } 0 .. $#$row ] } @rows ]) } 0 .. $#{$rows[0]};
    Your $damat to $ddmat are in my array @mat.
Re: creating matrices for PDL module
by Angharad (Pilgrim) on Jul 01, 2005 at 12:43 UTC
    Thanks for your replies. I'll try that :)
      This will create the matrix that you want. This is tested. Hope this helps;
      #!/usr/bin/perl -w open(MATRIX, "matrixfile.dat") || die "Error: Can't open matrixfile fi +le for reading: $!\n"; $i=0; while ($spool = <MATRIX>) { chomp($spool); @arr = split(/ /,$spool); for ($x=0;$x<= $#arr;$x++) { $matrix[$i][$x] = $arr[$x];} $i++; } # end while $x=$x-1; for ($j=0;$j<$i;$j++) { for ($r=0;$r<4;$r++) { print "[ "; $cntr=0;$c=$j; for($cntr=0;$cntr<4;$cntr++) { if($c gt $x) { $c=0; } print " $matrix[$r][$c] " ; $c++; } print " ] \n"; } print "\n"; }
      Oh .. i use emacs by the way, not notepad ;)
        Have you typed M-x perl-mode, selected all your text (perhaps using cua-mode) and typed in M-x indent-region?
Re: creating matrices for PDL module
by Angharad (Pilgrim) on Jul 01, 2005 at 12:23 UTC
    Well, I suppose there might be a PDL function to create the matrix from a file read from the command line, right?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://471677]
Approved by polettix
help
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: (7)
As of 2024-04-18 06:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found