| 1 | =head1 NAME | ||
| 2 | |||
| 3 | Math::Interpolator::Linear - lazy linear interpolation | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | use Math::Interpolator::Linear; | ||
| 8 | |||
| 9 | $ipl = Math::Interpolator::Linear->new(@points); | ||
| 10 | |||
| 11 | $y = $ipl->y($x); | ||
| 12 | $x = $ipl->x($y); | ||
| 13 | |||
| 14 | =head1 DESCRIPTION | ||
| 15 | |||
| 16 | This is a subclass of the lazy interpolator class C<Math::Interpolator>. | ||
| 17 | This class implements linear interpolation. See L<Math::Interpolator> | ||
| 18 | for the interface. | ||
| 19 | |||
| 20 | This code is neutral as to numeric type. The coordinate values used | ||
| 21 | in interpolation may be native Perl numbers, C<Math::BigRat> objects, | ||
| 22 | or possibly other types. Mixing types within a single interpolation is | ||
| 23 | not recommended. | ||
| 24 | |||
| 25 | =cut | ||
| 26 | |||
| 27 | package Math::Interpolator::Linear; | ||
| 28 | |||
| 29 | 8 | ▶ | { use 5.006; } |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 30 | 8 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 31 | 8 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 32 | |||
| 33 | 8 | our $VERSION = "0.005"; | |
| 34 | |||
| 35 | 8 | ▶ | use parent "Math::Interpolator"; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 36 | |||
| 37 | =head1 METHODS | ||
| 38 | |||
| 39 | =over | ||
| 40 | |||
| 41 | =item $ipl->y(X) | ||
| 42 | |||
| 43 | =item $ipl->x(Y) | ||
| 44 | |||
| 45 | These methods are part of the standard C<Math::Interpolator> interface. | ||
| 46 | |||
| 47 | =cut | ||
| 48 | |||
| 49 | sub _conv { | ||
| 50 | 60 | ▶ | my($self, $x_method, $y_method, $x) = @_; |
|
Subroutine
_conv: called CC=1 SCAR=0.0
| |||
| 51 | 60 | my $nhood_method = "nhood_$x_method"; | |
| 52 | 60 | my($a, $b) = $self->$nhood_method($x, 1); | |
| 53 | 50 | my $xa = $a->$x_method; | |
| 54 | 50 | my $xb = $b->$x_method; | |
| 55 | 50 | my $ya = $a->$y_method; | |
| 56 | 50 | my $yb = $b->$y_method; | |
| 57 | 50 | return $ya + ($x - $xa) * (($yb - $ya) / ($xb - $xa)); | |
| 58 | } | ||
| 59 | |||
| 60 | sub y { | ||
| 61 | 51 | ▶ | my($self, $x) = @_; |
|
Subroutine
y: called CC=1 SCAR=0.0
Pod
documented
| |||
| 62 | 51 | return $self->_conv("x", "y", $x); | |
| 63 | } | ||
| 64 | |||
| 65 | sub x { | ||
| 66 | 9 | ▶ | my($self, $y) = @_; |
|
Subroutine
x: called CC=1 SCAR=0.0
Pod
documented
| |||
| 67 | 9 | return $self->_conv("y", "x", $y); | |
| 68 | } | ||
| 69 | |||
| 70 | =back | ||
| 71 | |||
| 72 | =head1 SEE ALSO | ||
| 73 | |||
| 74 | L<Math::Interpolator> | ||
| 75 | |||
| 76 | =head1 AUTHOR | ||
| 77 | |||
| 78 | Andrew Main (Zefram) <zefram@fysh.org> | ||
| 79 | |||
| 80 | =head1 COPYRIGHT | ||
| 81 | |||
| 82 | Copyright (C) 2006, 2007, 2009, 2010, 2012 | ||
| 83 | Andrew Main (Zefram) <zefram@fysh.org> | ||
| 84 | |||
| 85 | =head1 LICENSE | ||
| 86 | |||
| 87 | This module is free software; you can redistribute it and/or modify it | ||
| 88 | under the same terms as Perl itself. | ||
| 89 | |||
| 90 | =cut | ||
| 91 | |||
| 92 | 8 | 1; | |