Math::Interpolator::Linear - lazy linear interpolation


Math-Interpolator documentation Contained in the Math-Interpolator distribution.

Index


Code Index:

NAME

Top

Math::Interpolator::Linear - lazy linear interpolation

SYNOPSIS

Top

	use Math::Interpolator::Linear;

	$ipl = Math::Interpolator::Linear->new(@points);

	$y = $ipl->y($x);
	$x = $ipl->x($y);

DESCRIPTION

Top

This is a subclass of the lazy interpolator class Math::Interpolator. This class implements linear interpolation. See Math::Interpolator for the interface.

This code is neutral as to numeric type. The coordinate values used in interpolation may be native Perl numbers, Math::BigRat objects, or possibly other types. Mixing types within a single interpolation is not recommended.

METHODS

Top

$ipl->y(X)
$ipl->x(Y)

These methods are part of the standard Math::Interpolator interface.

SEE ALSO

Top

Math::Interpolator

AUTHOR

Top

Andrew Main (Zefram) <zefram@fysh.org>

COPYRIGHT

Top

LICENSE

Top

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


Math-Interpolator documentation Contained in the Math-Interpolator distribution.
package Math::Interpolator::Linear;

{ use 5.006; }
use warnings;
use strict;

our $VERSION = "0.004";

use parent "Math::Interpolator";

sub _conv {
	my($self, $x_method, $y_method, $x) = @_;
	my $nhood_method = "nhood_$x_method";
	my($a, $b) = $self->$nhood_method($x, 1);
	my $xa = $a->$x_method;
	my $xb = $b->$x_method;
	my $ya = $a->$y_method;
	my $yb = $b->$y_method;
	return $ya + ($x - $xa) * (($yb - $ya) / ($xb - $xa));
}

sub y {
	my($self, $x) = @_;
	return $self->_conv("x", "y", $x);
}

sub x {
	my($self, $y) = @_;
	return $self->_conv("y", "x", $y);
}

1;