On Wed, Jun 23, 2010 at 10:57 PM, P Kishor <[email protected]> wrote:

> Thanks Chris, thanks so much. After using Perl for a while, just when
> I thought I started getting a hang of it, in comes PDL. Its like
> learning a brand new language. Your and others' answers to all my
> novice questions really help in learning this new language.
>
> I am writing a super-beginners manual, really documenting my own baby
> steps. Hopefully, it will become something I can share with others,
> and someone else may benefit from it as well.
>

I remember trying to do the same thing myself when I started on PDL. Maybe I
can twist your arm in a few months to turn it into a colloquium. :-D


> >> That seems the various methods don't seem to work analogously. For
> >> example, $a->reshape() changes $a, but $b->dummy() doesn't change $b.
> >
> > I believe reshape works in place.  I would prefer that it
> > work otherwise unless inplace is requested.  You are right
> > about the inconsistency....
>

reshape is particularly misbehaved. Not only does it always work inplace,
but reshape also severely breaks data flow. Consider this PDL shell session:

$a = sequence(5)   # create $a
$b = $a(0:3)   # create $b as a slice of $a
$b(1) .= -3   # modify $b and, thanks to data flow, $a
p $a
# get [0 -3 2 3 4]
p $b
# get [0 -3 2 3]
$b->reshape(2,2)   # reshape $b to be a matrix
$b(1,1) .= -10   # modify part of $b, BUT NOT part of $a???
p $a
# get [0 -3 2 3 4]
p $b
# get:
#[
# [  0  -3]
# [  2 -10]
#]

Even this does not work as expected:

$a = sequence(4)
$b = $a
$b->reshape(2,2)
# Did we reshape $a?
p $a
# get [0 1 2 3]
# Nope. What happens if we modify $a?
$a(0) .= -5;
p $a
# get [-5 1 2 3]
p $b
# get
#[
# [0 1]
# [2 3]
#]

I find this very frustrating. The docs say that reshape has to make a
physical piddle to keep from crashing, but I would really like if somebody
could figure out a way to make dataflow work with reshaping.

Of course, one of the nice features about reshape is that you can supply new
dimensions that do not add up to the original dimensions, and it will cut
out the extras or pad the new space with zeroes. In the latter case, you
would *have* to have a new piddle and data flow is not possible. Since there
could be code out there that uses this non-data-flowing feature of reshape,
it would be wise to create a new function, perhaps 'unclump', which was not
so permissive but keeps dataflow intact.

Thankfully these warts are documented, though perhaps they could do a better
job of explaining what happens when.

Yes, there are a few more. For example, in FastRaw, the writefraw command is
>
>    writefraw($pdl,"fname");
>
> while in FlexRaw, the analogous writeflex command is
>
>    $hdr = writeflex($file, $pdl1, $pdl2,...)
>

I also found this annoying when I first encountered it. One reason this is
sorta a good thing is because writefraw really is a single piddle method, so
it would make sense to invoke it as

   $pdl->writefraw("fname");

On the other hand, writeflex is a general method that can be applied to
multiple piddles, so this would be rather confusing:

   $pdl1->writeflex($pdl2, $pdl3, "fname");

However, that's a pretty weak reason for having the argument order
hard-baked into the code. It would not be difficult to rewrite the option
processing for either function (or both) in such a way as to maintain
backwards compatibility while bringing them in line with each other. I know
I've thought about it before, but I've just not gotten around to working on
it. This is actually a relatively low-hanging piece of fruit, if you're
looking for your first code contribution to PDL.

>From the docs, FlexRaw is made to be a smarter version of FastRaw, but
> reverses the order of $pdl and filename.
>
> There are others as well.
>
> >> [
> >>   [0 1 x]
> >>   [2 3 x]
> >>   [4 5 x]
> >>   [0 0 x]
> >> ]
> >>
> >> Where 'x' is a custom value. For example, I want a 0 for every 'x', or
> >> I want a random number between 20 and 30 for every 'x'. How do I do
> >> that? I know there is the 'random' method. But that creates a new
> >> piddle with random values between 0 and 1. So, I tried a different
> >> tactic
> >
> > $x = $a((2),:)
> > $x .= floor($x->random * 10 + 20)
>
> Ok. So the above is very interesting. $x is just a reference to the
> 3rd column in $a (the column marked with 'x'). Then you modify $x,
> which modifies $a.
>
> So, is this the normal idiom for wanting to modify a part of a piddle?
> Create a reference to the part you want to modify. That reference is a
> piddle. Then, modify that reference piddle which in turn will modify
> the referent piddle.
>

YES! This is one of the unique selling points of PDL: 'raw' piddles and
slices of piddles behave exactly the same. This is due to the facilities in
PDL::PP, with which most low-level PDL code is written. Basically the exact
same almost-C code (written in PDL::PP) works just as well for slices as for
raw piddles.

What if I wanted to apply a custom function to $x? How would I apply
> my_func() to $x?
>

>From the Perl level, you should operate on $x as if it were a normal piddle
and it will Do What You Mean. :-)


>  <snip>
> Well, I wasn't really confusing operation on piddle with the perl
> function int(). I was confused about how to apply int() to a piddle.
> Now I have learned that I can't. So, while I can do '$a = $a * 4'
> whereby every element of $a is multiplied by 4, I can't convert every
> element of $a to int() using the perl int() function. Yet, funnily, I
> can do sin($a), and that works. That, to me, seems inconsistent.
>

That is an interesting idea. I've not given much thought to overloading the
int() Perl function, but you're right, that is an inconsistency. Hmm.

Thanks for your thoughts and keep them coming!
David

-- 
Sent via my carrier pigeon.
_______________________________________________
Perldl mailing list
[email protected]
http://mailman.jach.hawaii.edu/mailman/listinfo/perldl

Reply via email to