The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

MooX::PDL2 - A Moo based PDL 2.X object

VERSION

version 0.01

SYNOPSIS

  use Moo;
  extends 'MooX::PDL2';

DESCRIPTION

This class provides the thinnest possible layer required to create a Moo object which is recognized by PDL.

PDL will treat a non-PDL blessed hash as a PDL object if it has a hash element with a key of PDL. That element may be a PDL piddle or a subroutine which returns a piddle.

This class provides a PDL method (which must not be overridden!) which returns the contents of the _PDL attribute. That attribute is yours to manipulate.

Classes without required constructor parameters

PDL does not pass any parameters to a class' initialize method when constructing a new object. Because of this, the default implementation of MooX::PDL2::initialize() returns a bare piddle, not an instance of MooX::PDL2, as it cannot know whether your class requires parameters during construction.

If your class does not require parameters be passed to the constructor, it is safe to overload the initialize method to return a fully fledged instance of your class:

 sub initialize { shift->new() }

Overloaded operators

PDL overloads a number of the standard Perl operators. For the most part it does this using subroutines rather than methods, which makes it difficult to manipulate them. Consider using overload::reify to wrap the overloads in methods, e.g.:

  package MyPDL;
  use Moo;
  extends 'MooX::PDL2';
  use overload::reify;

ATTRIBUTES

_PDL

The actual piddle associated with an object. PDL routines will transparently uses this when passed an object.

This attribute is

  • lazy

  • has a builder which returns PDL->null

  • will coerce its argument to be a piddle

  • has a clearer

See "EXAMPLES" for fun ways of combining it with Moo's facilities.

METHODS

new

  # null value
  $pdl = MooX::PDL2->new;

EXAMPLES

A class representing an evaluated polynomial

This class represents an evaluated polynomial. The polynomial coefficients and the values at which it is evaluated are attributes of the class. When they are changed they trigger a change in the underlying piddle.

Here's the definition:

 package PolyNomial;
 
 use PDL::Lite;
 
 use Moo;
 extends 'MooX::PDL2';
 
 has x => (
     is       => 'rw',
     required => 1,
     trigger  => sub { $_[0]->_clear_PDL },
 );
 
 has coeffs => (
     is       => 'rw',
     required => 1,
     trigger  => sub { $_[0]->_clear_PDL },
 );
 
 sub _build__PDL {
 
     my $self = shift;
 
     my $x     = $self->x;
     my $coeff = $self->coeffs;
 
     # this calculation is not robust at all
     my $pdl = $x->ones;
     $pdl *= $coeff->[0];
 
     for ( my $exp = 1 ; $exp < @$coeff + 1 ; ++$exp ) {
         $pdl += $coeff->[$exp] * $x**$exp;
     }
     $pdl;
 }
 
 1;

Note that the attributes use triggers to clear _PDL so that it will be recalculated when it is next accessed through the _PDL attribute accessor.

And here's how to use it

 use PDL::Lite;
 use PolyNomial;
 
 my $m = PolyNomial->new( coeffs => [ 3, 4 ], x => PDL->sequence(10) );
 print $m, "\n";
 
 $m *= 2;
 print $m, "\n";
 
 $m->x( PDL->sequence( 5 ) );
 print $m, "\n";

With sample output:

 [3 7 11 15 19 23 27 31 35 39]
 [6 14 22 30 38 46 54 62 70 78]
 [3 7 11 15 19]

BUGS AND LIMITATIONS

You can make new bug reports, and view existing ones, through the web interface at https://rt.cpan.org/Public/Dist/Display.html?Name=MooX-PDL2.

SEE ALSO

Please see those modules/websites for more information related to this module.

AUTHOR

Diab Jerius <djerius@cpan.org>

COPYRIGHT AND LICENSE

This software is Copyright (c) 2017 by Smithsonian Astrophysical Observatory.

This is free software, licensed under:

  The GNU General Public License, Version 3, June 2007