I'm flattered, but I think there is less there than meets the eye.

The new_from_specification could be a zeroes() call; it takes the same  
argument as zeroes().  The difference is that new_from_specification  
leaves your shiny new PDL full of whatever random stuff happened to be  
in the freshly allocated memory, so you save one access time.  The  
"0...@p" forces scalar context more tersely than "scalar(@p)" - you're  
making a PDL whose 0 dim runs across your initial array, and whose  
subsequent dims match the dims of the largest array element.

The computed assignment inside the loop copies each complete array  
into the corresponding subarray in the new PDL.  It seems like a  
nonsensical construct -- you're asking for a range that starts at the  
origin ("0*$dims"), and has the same size as a single element of @p  
("$dims") -- but some of the elements are undersized, so you need to  
handle the boundary condition.  The truncation ('t') boundary  
condition pads with zeroes.


On Jun 21, 2010, at 10:20 PM, P Kishor wrote:

> On Mon, Jun 21, 2010 at 11:17 PM, Craig DeForest
> <[email protected]> wrote:
>> Argh, that's what I get for dashing something off on the way out  
>> the door.
>>  The strategy is to create a PDL that is large enough to accommodate
>> everything, then to copy the subsidiary data into it one row at a  
>> time.  The
>> reason to use range() is that it handles boundary conditions  
>> neatly, so you
>> can tell it to truncate.
>>
>> The bug is that I interchanged the pdl() and map{} constructs.  I'm  
>> sorry to
>> have steered you down a wrong path.  You want:
>>
>>        $dims = cat( map { pdl($_->dims) } @p)->mv(-1,0)->maximum;
>>        $pdl = PDL->new_from_specification(0...@p, $dims->list);
>>        for my $i(0..$#p) {
>>                $pdl->(($i)) .= $p[$i]->range(0*$dims, $dims, 't');
>>        }
>>        $pdl = $pdl->mv(0,-1); # match cat() index placement
>>
>
> Yup. That does the trick. Thank you so much for your help and your
> patience. Now I will spend the next 30 mins staring at your code and
> learning each bit of it step by step.
>
>
>>
>>
>> On Jun 21, 2010, at 10:04 PM, P Kishor wrote:
>>
>>> On Mon, Jun 21, 2010 at 9:26 PM, Craig DeForest
>>> <[email protected]> wrote:
>>>>
>>>> Try "PDL->new_from_specification".
>>>
>>> much better, in that, no more error from the above call. That said,
>>> the rest of the code doesn't really solve my problem. Here is my
>>> sample code
>>>
>>> # add four 4x5 piddles to @p
>>> push @p, sequence(4, 5) for (0 .. 3);
>>>
>>> # add a 3x5 piddle to @p. This is the piddle with wrong dims.
>>> # It should be 4x5
>>> push @p, sequence(3, 5);
>>>
>>> # Craig's code suggestion hereon
>>> $dims = cat(pdl(map { $_->dims } @p))->mv(-1,0)->maximum;
>>>
>>> $pdl = PDL->new_from_specification(0...@p, $dims);
>>>
>>> for my $i (0 .. $#p) {
>>>   $pdl->( ($i) ) .= $p[$i]->range(0 * $dims, $dims, 't');
>>> }
>>>
>>> PDL: PDL::Ops::assgn(a,b): Parameter 'b'
>>> PDL: Mismatched implicit thread dimension 7: should be 4, is 5
>>>
>>> Actually, the last piddle needs to become 4x5
>>>
>>>
>>>
>>>>
>>>> (Mobile)
>>>>
>>>>
>>>> On Jun 21, 2010, at 6:27 PM, P Kishor <[email protected]> wrote:
>>>>
>>>>> On Mon, Jun 21, 2010 at 6:10 PM, Craig DeForest
>>>>> <[email protected]> wrote:
>>>>>>
>>>>>> Well, you have to copy every puddle anyway, to collate them as  
>>>>>> you
>>>>>> desire.
>>>>>>
>>>>>
>>>>> Ok. Then I need to understand your code first. Keep in mind, I am
>>>>> still in trainer-wheels.
>>>>>
>>>>>
>>>>>
>>>>> Hi, I think I am missing a lot here... here is test code I tried  
>>>>> out
>>>>>
>>>>> # add four 4x5 piddles to @p
>>>>> push @p, sequence(4, 5) for (0 .. 3);
>>>>>
>>>>> # add a 3x5 piddle to @p. This is the "wrong" piddle. It really  
>>>>> should
>>>>> be
>>>>> 4x5
>>>>> push @p, sequence(3, 5);
>>>>>
>>>>> $dims = cat(pdl(map { $_->dims } @p))->mv(-1,0)->maximum;
>>>>> print $dims;
>>>>>
>>>>> # Prints
>>>>> # [4 5 4 5 4 5 4 5 3 5]
>>>>>
>>>>> $pdl = new_from_specification(0...@p, $dims);
>>>>>
>>>>> # Error: Undefined subroutine &main::new_from_specification called
>>>>>
>>>>> # I change the above line to
>>>>> $pdl = PDL::new_from_specification(0...@p, $dims);
>>>>>
>>>>> # Now I get the following error
>>>>> # Error: File Core.pm; Line 1789: Can't call method "initialize"  
>>>>> without
>>>>> a
>>>>> # package or object reference <DATA> line 206
>>>>>
>>>>> =for
>>>>>
>>>>> 1772 sub PDL::new_from_specification{
>>>>> 1773     my $class = shift;
>>>>> 1774     my $type = ref($_[0]) eq 'PDL::Type' ? ${shift @_}[0]  :
>>>>> $PDL_D;
>>>>> 1775     my $nelems = 1; my @dims;
>>>>> 1776     for (@_) {
>>>>> 1777        if (ref $_) {
>>>>> 1778          barf "Trying to use non-piddle as dimensions?"  
>>>>> unless
>>>>> $_->isa('PDL');
>>>>> 1779          barf "Trying to use multi-dim piddle as dimensions?"
>>>>> 1780               if $_->getndims > 1;
>>>>> 1781          warn "creating > 10 dim piddle (piddle arg)!"
>>>>> 1782               if $_->nelem > 10;
>>>>> 1783          for my $dim ($_->list) {$nelems *= $dim; push  
>>>>> @dims, $dim}
>>>>> 1784        } else {
>>>>> 1785          barf "Dimensions must be positive" if $_<=0;
>>>>> 1786          $nelems *= $_; push @dims, $_
>>>>> 1787        }
>>>>> 1788     }
>>>>> 1789     my $pdl = $class->initialize();
>>>>> 1790     $pdl->set_datatype($type);
>>>>> 1791     $pdl->setdims([...@dims]);
>>>>> 1792     print "Dims: ",(join ',',@dims)," DLen: ",(length $
>>>>> {$pdl->get_dataref}),"\n" if $PDL::debug;
>>>>> 1793     return $pdl;
>>>>> 1794 }
>>>>>
>>>>> =cut
>>>>>
>>>>> for my $i (0 .. $#p) {
>>>>>  $pdl->( ($i) ) .= $p[$i]->range(0 * $dims, $dims, 't');
>>>>> }
>>>>>
>>>>> I haven't reached this far, but if I do, what am I going to get?  
>>>>> Is
>>>>> $pdl going to be the new list of piddles (with the correct  
>>>>> piddle). I
>>>>> am guessing @p is going to be the new list, with the piddle at  
>>>>> $p[4]
>>>>> corrected. And, what are its dims going to be? I want the 3x5  
>>>>> piddle
>>>>> to become 4x5 piddle.
>>>>>
>>>>
>>>
>>>
>>>
>>> --
>>> Puneet Kishor http://www.punkish.org
>>> Carbon Model http://carbonmodel.org
>>> Charter Member, Open Source Geospatial Foundation http://www.osgeo.org
>>> Science Commons Fellow, http://sciencecommons.org/about/whoweare/kishor
>>> Nelson Institute, UW-Madison http://www.nelson.wisc.edu
>>> -----------------------------------------------------------------------
>>> Assertions are politics; backing up assertions with evidence is  
>>> science
>>> = 
>>> = 
>>> = 
>>> ====================================================================
>>>
>>
>>
>
>
>
> -- 
> Puneet Kishor http://www.punkish.org
> Carbon Model http://carbonmodel.org
> Charter Member, Open Source Geospatial Foundation http://www.osgeo.org
> Science Commons Fellow, http://sciencecommons.org/about/whoweare/ 
> kishor
> Nelson Institute, UW-Madison http://www.nelson.wisc.edu
> -----------------------------------------------------------------------
> Assertions are politics; backing up assertions with evidence is  
> science
> = 
> ======================================================================
>


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

Reply via email to