[Israel.pm] PDL - simple vector and matrix operation

Shlomi Fish shlomif at iglu.org.il
Sat Nov 14 07:45:51 PST 2009


On Wednesday 11 Nov 2009 10:07:00 Gabor Szabo wrote:
> I am still not sure where *I* might need to use PDL but it
> is still fun to play with it. It has a command line utility in
> which I can play with it interactively.
> 

Naturally, as students of Electrical Engineering my fellow students and I 
found Matlab to be an indispensable tool, and I've also studied PDL to see 
what was Perl's answer to Matlab. People in other technical departments in the 
Technion told me that they also worked extensively with Matlab, which is kinda 
like the Perl of engineering.

Many people I talked with told me they did Matlab the wrong way, by using many 
nested loops, and not using the built-in tensor operations which I knew better 
than to fall into. Of course, I once tried to do something the right Matlaby 
way by implementing a fourier transform using large matrix operations and it 
overflowed the workstation's memory (it was 64 MB or 128 MB or so, which was a 
lot back then), so I ended up unrolling a dimension into a loop.

I also met a student who was purposely oblivious to learning C and decided to 
learn only Matlab.

In any case, I did make use of PDL for implementing an alogrithm I devised for 
implementing a certain optimisation task with Freecell Solver (yes, I hear the 
grogers):

http://www.shlomifish.org/lecture/Freecell-Solver/The-Next-Pres/slides/multi-
tasking/

(short URL - http://xrl.us/bf4fis ).

I later on extended it to support optimisation based on the number of moves in 
the solution:

http://tech.groups.yahoo.com/group/fc-solve-discuss/message/980

Maybe I could have done the same using Firebird or PostgreSQL and with some 
SQL hackery, but I was more comfortable thinking in the PDL/Matlab mind-set.

Now for your snippet:

> Let's see an example:
> 
> perldl> $x = pdl(1,2,3,4)
> 
> perldl> p $x
> [1 2 3 4]
> 
> perldl> p $x + 1               # add one to each value
> [2 3 4 5]
> 
> perldl> p $x + $x             # add the values pair wise
> [2 4 6 8]
> perldl> p $x * $x               # multiply the values pair wise
> [1 4 9 16]
> 
> perldl> p transpose $x      # from horizontal vector make a vertical vector
> 
> [
>  [1]
>  [2]
>  [3]
>  [4]
> ]
> 

Yes, indeed - the dimensions in PDL are very important and can be a source of 
no end of confusion. Also see << $x->xchg($dim_one, $dim_two) >> and other 
functions.

Here is my attempt to concatenate tensors:

<<<<<<<<<<shlomi:$trunk/fc-solve/source$ perldl                              
perlDL shell v1.352                                                
 PDL comes with ABSOLUTELY NO WARRANTY. For details, see the file  
 'COPYING' in the PDL distribution. This is free software and you  
 are welcome to redistribute it under certain conditions, see      
 the same file for details.                                        
ReadLines, NiceSlice, MultiLines  enabled                          
Reading PDL/default.perldlrc...                                    
Found docs database /usr/lib/perl5/vendor_perl/5.10.1/i386-linux-thread-
multi/PDL/pdldoc.db                                                                     
Type 'help' for online help                                                     
Type 'demo' for online demos                                                    
Loaded PDL v2.4.5 (supports bad values)                                         

Note: AutoLoader not enabled ('use PDL::AutoLoader' recommended)

perldl> $x = pdl([1,2,3,4])

perldl> $y = pdl([5,606,7000,88])

perldl> $x

perldl> p $x
[1 2 3 4]   
perldl> p $y
[5 606 7000 88]
perldl> p ($x . $y)
[1 2 3 4][5 606 7000 88]
perldl> p ($x m $y)    
Search pattern not terminated

perldl> p ($x , $y)
[1 2 3 4] [5 606 7000 88]
perldl> p cat($x,$y)     

[
 [   1    2    3    4]
 [   5  606 7000   88]
]                     

perldl> $x = pdl([1,2,3,4])

perldl> $y = pdl([5,606,7000,88])

perldl> p cat($x,$y)

[
 [   1    2    3    4]
 [   5  606 7000   88]
]                     

perldl> p transpose(cat(transpose($x),transpose($y)))

[
 [
  [1 2 3 4]
 ]         
 [         
  [   5  606 7000   88]
 ]                     
]                      

perldl> p transpose($x)

[
 [1]
 [2]
 [3]
 [4]
]   

perldl> p transpose($y)

[
 [   5]
 [ 606]
 [7000]
 [  88]
]      

perldl> p cat(transpose($x),transpose($y))

[
 [
  [1]
  [2]
  [3]
  [4]
 ]
 [
  [   5]
  [ 606]
  [7000]
  [  88]
 ]
]

perldl> p [$x,$y]
ARRAY(0x8f4f538)
perldl> p pdl([$x,$y])

[
 [   1    2    3    4]
 [   5  606 7000   88]
]

perldl>
>>>>>>>>>>>>>>>>>

As you can see I was not able to form [1,2,3,4,5,606,etc.]. Any insights would 
be appreciated.

Regards,

	Shlomi Fish

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
The Case for File Swapping - http://shlom.in/file-swap

Chuck Norris read the entire English Wikipedia in 24 hours. Twice.


More information about the Perl mailing list