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

Shlomi Fish shlomif at iglu.org.il
Sat Nov 14 10:01:16 PST 2009


Hi all!

First of all, let me ask if anyone is planning to come to/from the Rehovot.pm 
PDL meeting from the Tel Aviv area, and can give me a ride or join me on a 
train ride?

On Saturday 14 Nov 2009 17:45:51 Shlomi Fish wrote:
> 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.
> 
> 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.
> 

Found it! Thanks to icke on IRC:

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

perldl> $y = pdl([5,6])

perldl> p $x
[1 2 3 4]
perldl> p $y
[5 6]
perldl> p $x->append($y)
[1 2 3 4 5 6]
>>>>>>>>>>>>>>>>>

So just use the append method.

I also wanted to demonstrate the "x" operator which does matrix 
multiplication:

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

perldl> $y = pdl([5,6,7,8])

perldl> p ($x x $y)
PDL: Dim mismatch in matmult of [4x1] x [4x1]: 4 != 1
Caught at file (eval 57), line 4, pkg main

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

[
 [70]
]

perldl> p ($x->transpose() x $y)

[
 [ 5  6  7  8]
 [10 12 14 16]
 [15 18 21 24]
 [20 24 28 32]
]
>>>>>>>>>>>>>>>>>>>>>

Matrix multiplication is not equivalent to element by element multiplication. 
Every element is a row-by-column sum-of-multiples of the equivalent row. This 
is actually more useful than it seems because multiplying a vector by a matrix 
is very common in engineering disciples.

I should note that regular expressions were considered a highly mathematical 
excursion until Ken Thompson decided to integrate them into ed and later other 
UNIX tools and they immediately proved of great utility to most UNIX 
programmers.

Regards,

	Shlomi Fish


-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Original Riddles - http://www.shlomifish.org/puzzles/

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


More information about the Perl mailing list