Hey, January,

You can assign elements between piddles en masse using the computed  
assignment operator, ".=", as in

  $pdl2 .= $pdl1

which will take every single element of $pdl1, cast it to whatever  
type $pdl2 has, and copy it into the corresponding element of $pdl2.   
(The two piddles have to already exist and also have thread-compatible  
sizes:
you can't stuff 7 elements into a 5-element piddle that way...)  If  
you want to replace only a part, you can
slice one or both piddles.  There is a "slice()" operator, but you  
probably want to use the niceslice syntax (say "man PDL::NiceSlice"  
from your shell, or "?NiceSlice" from perldl):

  $pdl2->(2:5) .= $pdl1->(0:3);

I'm not sure why you're stuffing things into a magic variable (your  
"main matrix") -- if you are doing it instead of passing parameters  
around, well, you should be passing parameters around.  If you are  
doing it because your main matrix is much larger than 20x20, then you  
can do something like:

   $matrix->($x:$x+19,$y:$y+19) .= $scoring;

If you are using set() to load up your piddles, you shouldn't be.  The  
piddle constructor is a much faster way of translating a collection of  
perl scalars into a piddle.  Say "?pdl" in the perldl shell for more  
detail.  If your scoring matrix already exists as an array of array  
refs, you can construct your scoring matrix just by calling "pdl()":

    $scoring = pdl( @rows );

Cheers,
Craig


On Feb 10, 2008, at 4:03 AM, January Weiner wrote:

> Dear Karl,
>
>> If you write things in the same element by element way as regular  
>> Perl
>> then PDL will be even slower. set() and at() are functions of last
>> resort.
>
> Thanks for the explanation.
>
>> The idea of PDL is  to use fast internal functions for vector ops.
>> What you want can be accomplished in one line with PDL's
>> Image2D::conv2d(...) and will be a bazillion times faster than  
>> regular
>> perl
>
> I imagined so much (although I just considered adding up slices of the
> matrix which would have the same effect). But this is not even the
> step that takes the most time. The initial filling up of the matrix
> with scores is what really takes a lot time.
>
> I have one scoring matrix, 20x20 (call it $scoring), containing a
> score for each pair of the amino acids. I have two sequences, which
> constitute the matrix that I am calculating. For each two amino acids
> from the two sequences, I look up the scoring matrix and enter the
> respective value into my main matrix ($matrix). I can't imagine of a
> way of doing it fast if set() and at() is slow. Is an assignement
> between elements of two piddles faster? Like that:
>
> set($matrix, $i, $j, at( $scoring, $p, $q) )
>
> maybe a better way of doing that?
>
> Cheers
>
> j.
>
>>
>> Karl
>>
>>
>> On 10/02/2008, at 8:26 PM, January Weiner wrote:
>>
>>> Hello,
>>>
>>> in my program, I am using a matrix which has usually a size of
>>> 2000x2000 or
>>> similar. It contains short integers.  The matrix is filled in one by
>>> one in
>>> a loop over rows and columns.
>>>
>>> The only calculation that I am doing with this matrix is averaging
>>> over a window sliding along the diagonals. Finally, I need to access
>>> each
>>> matrix element one after another in a loop to calculate something.
>>>
>>> At first, I used a regular Perl matrix, constructed as [ [ .... ],
>>> [ ... ],
>>> .... ]. This was quite slow and took a lot of memory (the memory
>>> footprint
>>> of my program grews up by roughly 50MB), so I googled and found PDL
>>> -- Perl
>>> Data Language.
>>>
>>> PDL is supposed to be much faster and to have a smaller memory
>>> footprint.
>>>
>>> While I can see the latter (the footprint is now negligible compared
>>> to the
>>> whole program), it is roughly three to four times slower than the
>>> regular
>>> perlish way. I am creating the matrix as follows [please bear with
>>> me -- I
>>> am not posting actual code, because it is rather complex, and you
>>> will see
>>> that answering my question doesn't require finding out whether my
>>> code is
>>> correct]:
>>>
>>> $matrix = short(zeroes($l1, $l2)) ;
>>>
>>> (where $l1 and $l2 are dimensions of the matrix), and accessing /
>>> setting
>>> the elements using at() and set():
>>>
>>> set( $matrix, $i, $j, $value ) ;
>>> $value = at( $matrix, $i, $j ) ;
>>>
>>> There is another way of doing it using PDL::NiceSlice, which uses
>>> constructs like $matrix->($i, $j), but I found it to be even slower.
>>>
>>> QUESTION: Is this a normal behaviour? Is it normal that a standard
>>> perlish
>>> matrix is few times faster than the PDL implementation? Or should I
>>> start
>>> finding out where I messed things up?
>>>
>>> Thank you in advance,
>>>
>>> January
>>>
>>>
>>>
>>> --
>>> ------------ January Weiner 3  ---------------------+---------------
>>> Division of Bioinformatics, University of Muenster  |  Schloßplatz 4
>>> (+49)(251)8321634                                   |  D48149  
>>> Münster
>>> http://www.uni-muenster.de/Evolution/ebb/           |  Germany
>>>
>>> _______________________________________________
>>> Perldl mailing list
>>> [email protected]
>>> http://mailman.jach.hawaii.edu/mailman/listinfo/perldl
>>
>>
>
>
>
> -- 
> ------------ January Weiner 3  ---------------------+---------------
> Division of Bioinformatics, University of Muenster  |  Schloßplatz 4
> (+49)(251)8321634                                   |  D48149 Münster
> http://www.uni-muenster.de/Evolution/ebb/           |  Germany
>
> _______________________________________________
> Perldl mailing list
> [email protected]
> http://mailman.jach.hawaii.edu/mailman/listinfo/perldl
>


_______________________________________________
Perldl mailing list
[email protected]
http://mailman.jach.hawaii.edu/mailman/listinfo/perldl

Reply via email to