Hey, nice one, Derek....

Er, you can also use the threading engine:

        perldl> ($n,$m) = (3,2);
        perldl> ($a,$b) = map { sequence($_) } ($n,$m);
        perldl> $c = cat($a->(:,*2), $b->(*3,:))->clump(2)->mv(-1,0);
        perldl> print " a=$a \n b=$b \n c=$c \n";
         a=[0 1 2]
         b=[0 1]
         c=
        [
         [0 0]
         [1 0]
         [2 0]
         [0 1]
         [1 1]
         [2 1]
        ]
This works by padding each array with a dummy dimension: $a->(:,*3) is  
a 2x3-PDL, as is $b->(*2,:).  Catting them together gives a 2x3x2-PDL,  
whose 0th dim runs across elements of $a, whose 1st dim runs across  
elements of $b, and whose 2nd dim runs between the $a and $b pdls.  
Clumping the first two dimensions gives a 6x2 PDL whose 0th dim runs  
across index and whose 1st dim runs between $a and $b.  The final mv  
changes it to a 2x6 pdl, which is what you asked for.

This sort of operation (padding dummied variables) is related to the  
outer product.  In fact, outer product can be implemented as:

        sub outer {
         my ($a,$b)=...@_;
         return $a->(:,*($b->dim(0))) * $b->(*($a->dim(0)),:);
        }

The difference is that, instead of multiplication, we're doing  
assembly of vectors.

Best,
Craig


print $a->(*1)

On Feb 17, 2009, at 2:25 PM, Derek Lamb wrote:

> InSuk Joung wrote:
>> Hi Users,
>> I am wondering if there is a function that make a permutation piddle.
>>
>> For example,
>>
>> $a = sequence(2);
>> $b = sequence(3);
>>
>> Using the two piddles above, I want to create
>>
>> [
>> [ 0, 0 ],
>> [ 0, 1 ],
>> [ 0, 2 ],
>> [ 1, 0 ],
>> [ 1, 1 ],
>> [ 1, 2 ],
>> ]
>>
>> Any suggestion would be appreciated!
>>
> It's OK to use a for-loop sometimes!  That's probably the easiest.   
> But
> since you asked:
>
> $n = $a->nelem * $b->nelem;
> use PDL::Transform;
> $ap = $a->dummy(0)->map(t_identity,[1,$n],{method=>'s'});
> $bp = $b->range(0,$n,'p')->dummy(0) ;
> $p = append($ap,$bp);
>
> cheers,
> Derek
>
>
> _______________________________________________
> 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