NAME

Math::CPWLF - interpolation using nested continuous piece-wise linear functions

VERSION

Version 0.15

SYNOPSIS

Math::CPWLF provides an interface for defining continuous piece-wise linear functions by setting knots with x,y pairs.

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.

$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)

FUNCTIONS

new

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).

    • extrapolate - Perform a linear extrapolation using the two nearest knots.

    • level - Return the y value of the nearest knot.

    • undef - Return 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' );

knot

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:

$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 );

AUTHOR

Dan Boorstein, <dan at boorstein.net>

BUGS

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.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc Math::CPWLF

You can also look for information at:

ACKNOWLEDGEMENTS

COPYRIGHT & LICENSE

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.