On Fri, 2010-08-27 at 14:35 -0500, P Kishor wrote:
> I am enjoying all the fine tools that the masters have created. Like
> magic, with one command, I can apply the same function to millions of
> values.
>
> Now, I am itching to hit my head against the wall. I want to write my
> own function that can be applied to all the values in a piddle. So,
> following the convention, I envision
>
> $pdl->func to apply to the elements in the entire piddle and
> $pdl->function to apply to all the elements in the specified
> dimension. These are not necessarily grouping functions that result in
> an (n-1) D piddle, but they are more like sqrt that applies to every
> element.
>
> Where do I begin? Do I have write this in C? Or can I write a function
> in Perl and PDL-ify it?
>
Hi Puneet,
If you want to do $pdl->func, you just need to write a sub func{}, and
then export that into the PDL namespace. So, yes, you can do it in
perl/PDL. To see how to do this, look in the PDL source for some
function that you already do it with. For example, I use $pdl->wfits to
write a FITS file, and in FITS.pm there's a line
*wfits = \&PDL::wfits;
and then
sub PDL::wfits { #code}
I think you can also do it the other way:
*PDL::func = \&func;
sub func { #code}
I think you should think more about your desire for $pdl->function, and
what it will do for you. If you're not grouping or reducing
dimensionality, could the same thing be accomplished with
$pdl->slice(#something)->func ? Saves you from having to write two subs.
"to apply to all elements in the specified dimension" doesn't really
make any sense--can you show us what you mean with an example like
sequence(3,4)? For example, what if func() was just a shortcut for
squaring each element, what would function do?
_______________________________________________
Perldl mailing list
[email protected]
http://mailman.jach.hawaii.edu/mailman/listinfo/perldl