NAME

Tie::Hash::Interpolate - tied mathematical interpolation/extrapolation

VERSION

Version 0.07

SYNOPSIS

use Tie::Hash::Interpolate;

## use tie interface

tie my %lut, 'Tie::Hash::Interpolate', extrapolate => 'linear';

$lut{3} = 4;
$lut{5} = 6;

print $lut{4};    ## prints 5
print $lut{6.5};  ## prints 7.5

## or constructor interface

my $lut = Tie::Hash::Interpolate->new( extrapolate => 'linear' );

$lut->{3} = 4;
$lut->{5} = 6;

print $lut->{4};    ## prints 5
print $lut->{6.5};  ## prints 7.5

DESCRIPTION

Tie::Hash::Interpolate provides a mechanism for using a hash as a lookup table for interpolated and extrapolated values.

Hashes can either be tied using the tie builtin or by constructing one with the new() method.

After your hash is tied (NOTE: key-value pairs added prior to the tie will be ignored), insert your known key-value pairs. If you then fetch a key that does not exist, an interpolation or extrapolation will be performed as necessary. If you fetch a key that does exist, the value stored for that key will be returned.

FUNCTIONS

new

OPTIONS

Options can be passed to tie after the Tie::Hash::Interpolate name is given, or directly to new() as key-value pairs.

tie my %lut, 'Tie::Hash::Interpolate', extrapolate => 'fatal';

## or

my $lut = Tie::Hash::Interpolate->new( one_key => 'constant' );

extrapolate

This option controls the behavior of the tied hash when a key is requested outside the range of known keys. Valid extrapolate values include:

  • linear (default)

    extrapolate linearly based on the two nearest points

  • constant

    keep the nearest value constant rather than extrapolating

  • fatal

    throw a fatal exception

  • undef

    return undef

one_key

This option controls the behavior of the tied hash when a key is requested and only one key exists in the hash. Valid one_key values include:

  • fatal (default)

    throw a fatal exception

  • constant

    all fetches return the one value that exists

  • undef

    return undef

TO DO

- support multiple dimensions
- support autovivification of tied hashes
- set a per-instance mode for insertion or lookup
- be smarter (proximity based direction) about searching when doing interpolation

AUTHOR

Daniel B. Boorstein, <danboo@cpan.org>

COPYRIGHT AND LICENSE

Copyright 2004 by Daniel B. Boorstein

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.