FuncSection: User Contributed Perl Documentation (3)Updated: 2000-04-29 |
FuncSection: User Contributed Perl Documentation (3)Updated: 2000-04-29 |
use PDL::Func; use PDL::Math;
# somewhat pointless way to estimate cos and sin,
# but is shows that you can thread if you want to
# (and the library lets you)
#
my $obj = PDL::Func->init( Interpolate => "Hermite" );
#
my $x = pdl( 0 .. 45 ) * 4 * 3.14159 / 180;
my $y = cat( sin($x), cos($x) );
$obj->set( x => $x, y => $y, bc => "simple" );
#
my $xi = pdl( 0.5, 1.5, 2.5 );
my $yi = $obj->interpolate( $xi );
#
print "sin( $xi ) equals ", $yi->slice(':,(0)'), "\n";
sin( [0.5 1.5 2.5] ) equals [0.87759844 0.070737667 -0.80115622]
#
print "cos( $xi ) equals ", $yi->slice(':,(1)'), "\n";
cos( [0.5 1.5 2.5] ) equals [ 0.4794191 0.99768655 0.59846449]
#
print sin($xi), "\n", cos($xi), "\n";
[0.47942554 0.99749499 0.59847214]
[0.87758256 0.070737202 -0.80114362]
Throughout this documentation, $x and $y refer to the function to be interpolated whilst $xi and $yi are the interpolated values.
The avaliable types, or schemes, of interpolation are listed below. Also given are the valid attributes for each scheme: the flag value indicates whether it can be set (s), got (g), and if it is required (r) for the method to work.
The valid attributes are:
Attribute Flag Description x sgr x positions of data y sgr function values at x positions err g error flag
The valid attributes are:
Attribute Flag Description x sgr x positions of data y sgr function values at x positions bc sgr boundary conditions g g estimated gradient at x positions err g error flag
Given the initial set of points "(x,y)", an estimate of the gradient is made at these points, using the given boundary conditions. The gradients are stored in the "g" attribute, accessible via:
$gradient = $obj->get( 'g' );
However, as this gradient is only calculated 'at the last moment', "g" will only contain data after one of "interpolate", "gradient", or "integrate" is used.
An example would be
$obj->set( bc => { start => [ 1, 0 ], end => [ 1, -1 ] } )
which sets the first derivative at the first point to 0, and at the last point to -1.
$obj = PDL::Func->init( Interpolate => "Hermite", x => $x, y => $y );
$obj = PDL::Func->init( { x => $x, y => $y } );
Create a PDL::Func object, which can interpolate, and possibly integrate and calculate gradients of a dataset.
If not specified, the value of Interpolate is taken to be "Linear", which means the interpolation is performed by PDL::Primitive::interpolate. A value of "Hermite" uses piecewise cubic Hermite functions, which also allows the integral and gradient of the data to be estimated.
Options can either be provided directly to the method, as in the first example, or within a hash reference, as shown in the second example.
my $nset = $obj->set( x = $newx, $y => $newy );
my $nset = $obj->set( { x = $newx, $y => $newy } );
Set attributes for a PDL::Func object.
The return value gives the number of the supplied attributes which were actually set.
my $x = $obj->get( x ); my ( $x, $y ) = $obj->get( qw( x y ) );
Get attributes from a PDL::Func object.
Given a list of attribute names, return a list of their values; in scalar mode return a scalar value. If the supplied list contains an unknown attribute, "get" returns a value of "undef" for that attribute.
my $scheme = $obj->scheme;
Return the type of interpolation of a PDL::Func object.
Returns either "Linear" or "Hermite".
my $status = $obj->status;
Returns the status of a PDL::Func object.
This method provides a high-level indication of the success of the last method called (except for "get" which is ignored). Returns 1 if everything is okay, 0 if there has been a serious error, and -1 if there was a problem which was not serious. In the latter case, "$obj->get("err")" may provide more information, depending on the particular scheme in use.
my $name = $obj->routine;
Returns the name of the last routine called by a PDL::Func object.
This is mainly useful for decoding the value stored in the "err" attribute.
$obj->attributes; PDL::Func->attributes;
Print out the flags for the attributes of a PDL::Func object.
Useful in case the documentation is just too opaque!
PDL::Func->attributes; Flags Attribute SGR x SGR y G err
my $yi = $obj->interpolate( $xi );
Returns the interpolated function at a given set of points (PDL::Func).
A status value of -1, as returned by the "status" method, means that some of the $xi points lay outside the range of the data. The values for these points were calculated by extrapolation (the details depend on the scheme being used).
my $gi = $obj->gradient( $xi ); my ( $yi, $gi ) = $obj->gradient( $xi );
Returns the derivative and, optionally, the interpolated function for the "Hermite" scheme (PDL::Func).
my $ans = $obj->integrate( index => pdl( 2, 5 ) ); my $ans = $obj->integrate( x => pdl( 2.3, 4.5 ) );
Integrate the function stored in the PDL::Func object, if the scheme is "Hermite".
The integration can either be between points of the original "x" array ("index"), or arbitrary x values ("x"). For both cases, a two element piddle should be given, to specify the start and end points of the integration.
If the "status" method returns a value of -1, then one or both of the integration limits did not lie inside the "x" array. Caveat emptor with the result in such a case.
In the documentation, the methods are preceeded by "PDL::Func::" to avoid clashes with functions such as "set" when using the "help" or "apropos" commands within perldl.
Thanks to Robin Williams, Halldór Olafsson, and Vince McIntyre.