Math::CPWLF - interpolation using nested continuous piece-wise linear functions
Version 0.15
Math::CPWLF provides an interface for defining continuous piece-wise linear functions by setting knots with x,y pairs.
Math::CPWLF
use Math::CPWLF; $func = Math::CPWLF->new; $func->knot( 0 => 0 ); ## set the knot at f(0) equal to 0 $func->knot( 1 => 2 ); ## set the knot at f(1) equal to 2 $y = $func->( 0.5 ); ## interpolate f(0.5) ($y == 1)
Functions can be used in multiple dimensions, by specifying a Math::CPWLF object as the y value of a knot.
$nested_func = Math::CPWLF->new; $nested_func->knot( 0 => 0 ); $nested_func->knot( 1 => 3 ); $func->knot( 2 => $nested_func ); $deep_y = $func->( 1.5 )( 0.5 ); ## $deep_y == 1.75
As a convenience, you can specify arbitrarily deep knots by passing more than two values two the knot method.
knot
$func->knot( 2, 2 => 4 ); ## same as $nested_func->( 2 => 4);
If any of the intermediate knots do not exist they will be autovivified as Math::CPWLF objects, much like perl hashes.
$func->knot( 3, 2 => 4 ); ## autovivify top level f(3)
Construct a new Math::CPWLF function with no knots, and the default out of bounds behavior.
my $func = Math::CPWLF->new;
Optional parameters:
oob
The oob parameter controls how a function behaves when a given x value is out of bounds of the current minimum and maximum knots. If a function defines an oob method in its constructor, that method is also used for any nested functions that were not explicitly constructed with their own oob methods.
die - Throw an exception (default).
die
extrapolate - Perform a linear extrapolation using the two nearest knots.
extrapolate
level - Return the y value of the nearest knot.
level
undef - Return undef.
undef
Construct an instance that returns undef or empty list when the requested x is out of bounds:
my $func = Math::CPWLF->new( oob => 'undef' );
This instance method adds a knot with the given x,y values.
$func->knot( $x => $y );
Knots can be specified at arbitrary depth and intermediate knots will autovivify as needed. There are two alternate syntaxes for setting deep knots. The first involves passing 3 or more values to the knot() call, where the last value is the y value and the other values are the depth-ordered x values:
knot()
$func->knot( $x1, $x2, $x3 => $y );
The other syntax is a bit more hash-like in that it separates the x values. Note that it starts with invoking the knot() method with no arguments.
$func->knot->($x1)($x2)( $x3 => $y );
Dan Boorstein, <dan at boorstein.net>
<dan at boorstein.net>
Please report any bugs or feature requests to bug-math-cpwlf at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Math-CPWLF. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-math-cpwlf at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc Math::CPWLF
You can also look for information at:
RT: CPAN's request tracker
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Math-CPWLF
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/Math-CPWLF
CPAN Ratings
http://cpanratings.perl.org/d/Math-CPWLF
Search CPAN
http://search.cpan.org/dist/Math-CPWLF/
Copyright 2009 Dan Boorstein.
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.
To install Math::CPWLF, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Math::CPWLF
CPAN shell
perl -MCPAN -e shell install Math::CPWLF
For more information on module installation, please visit the detailed CPAN module installation guide.