| 1 | package Math::Function::Interpolator::Linear; | ||||||||||
| 2 | |||||||||||
| 3 | 4 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 4 | our @ISA = qw(Math::Function::Interpolator); | |||||||||
| 8 | |||||||||||
| 9 | 4 | our $VERSION = '1.03'; ## VERSION | |||||||||
| 10 | |||||||||||
| 11 | 4 | ▶ | use Carp qw(confess); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 4 | ▶ | use Number::Closest::XS qw(find_closest_numbers_around); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 4 | ▶ | use Scalar::Util qw(looks_like_number); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | =head1 NAME | ||||||||||
| 16 | |||||||||||
| 17 | Math::Function::Interpolator::Linear - Interpolation made easy | ||||||||||
| 18 | |||||||||||
| 19 | =head1 SYNOPSIS | ||||||||||
| 20 | |||||||||||
| 21 | use Math::Function::Interpolator::Linear; | ||||||||||
| 22 | |||||||||||
| 23 | my $interpolator = Math::Function::Interpolator::Linear->new( | ||||||||||
| 24 | points => {1=>2,2=>3,3=>4} | ||||||||||
| 25 | ); | ||||||||||
| 26 | |||||||||||
| 27 | $interpolator->linear(2.5); | ||||||||||
| 28 | |||||||||||
| 29 | =head1 DESCRIPTION | ||||||||||
| 30 | |||||||||||
| 31 | Math::Function::Interpolator::Linear helps you to do the interpolation calculation with linear method. | ||||||||||
| 32 | It solves for point_y linearly given point_x and an array of more than 2 data points. | ||||||||||
| 33 | |||||||||||
| 34 | =head1 FIELDS | ||||||||||
| 35 | |||||||||||
| 36 | =head2 points (REQUIRED) | ||||||||||
| 37 | |||||||||||
| 38 | HashRef of points for interpolations | ||||||||||
| 39 | |||||||||||
| 40 | =cut | ||||||||||
| 41 | |||||||||||
| 42 | =head1 METHODS | ||||||||||
| 43 | |||||||||||
| 44 | =head2 linear | ||||||||||
| 45 | |||||||||||
| 46 | linear | ||||||||||
| 47 | |||||||||||
| 48 | =cut | ||||||||||
| 49 | |||||||||||
| 50 | # Solves for point_y linearly given point_x and an array of points. | ||||||||||
| 51 | 8 | ▶ | sub linear { | ||||||||
|
Subroutine
linear: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 52 | 8 | my ($self, $x) = @_; | |||||||||
| 53 | |||||||||||
| 54 | 8 | ▶ | confess "sought_point[$x] must be a number" unless looks_like_number($x); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | 7 | my $ap = $self->points; | |||||||||
| 56 | 7 | ▶ | return $ap->{$x} if defined $ap->{$x}; # no need to interpolate | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | |||||||||||
| 58 | 7 | my @Xs = keys %$ap; | |||||||||
| 59 | 7 | ▶ | confess "cannot interpolate with fewer than 2 data points" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | if scalar @Xs < 2; | ||||||||||
| 61 | |||||||||||
| 62 | 6 | my ($first, $second); | |||||||||
| 63 | ($first->{x}, $second->{x}) = | ||||||||||
| 64 | 6 | @{find_closest_numbers_around($x, \@Xs, 2)}; | |||||||||
| 65 | ($first->{y}, $second->{y}) = | ||||||||||
| 66 | 6 | ($ap->{$first->{x}}, $ap->{$second->{x}}); | |||||||||
| 67 | |||||||||||
| 68 | 6 | my $m = ($second->{y} - $first->{y}) / ($second->{x} - $first->{x}); | |||||||||
| 69 | 6 | my $c = $first->{y} - ($first->{x} * $m); | |||||||||
| 70 | |||||||||||
| 71 | 6 | return $m * $x + $c; | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | =head1 AUTHOR | ||||||||||
| 75 | |||||||||||
| 76 | Binary.com, C<< <perl at binary.com> >> | ||||||||||
| 77 | |||||||||||
| 78 | =head1 BUGS | ||||||||||
| 79 | |||||||||||
| 80 | Please report any bugs or feature requests to C<bug-math-function-interpolator at rt.cpan.org>, or through | ||||||||||
| 81 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Math-Function-Interpolator>. I will be notified, and then you'll | ||||||||||
| 82 | automatically be notified of progress on your bug as I make changes. | ||||||||||
| 83 | |||||||||||
| 84 | |||||||||||
| 85 | |||||||||||
| 86 | |||||||||||
| 87 | =head1 SUPPORT | ||||||||||
| 88 | |||||||||||
| 89 | You can find documentation for this module with the perldoc command. | ||||||||||
| 90 | |||||||||||
| 91 | perldoc Math::Function::Interpolator | ||||||||||
| 92 | |||||||||||
| 93 | |||||||||||
| 94 | You can also look for information at: | ||||||||||
| 95 | |||||||||||
| 96 | =over 4 | ||||||||||
| 97 | |||||||||||
| 98 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||
| 99 | |||||||||||
| 100 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Math-Function-Interpolator> | ||||||||||
| 101 | |||||||||||
| 102 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 103 | |||||||||||
| 104 | L<http://annocpan.org/dist/Math-Function-Interpolator> | ||||||||||
| 105 | |||||||||||
| 106 | =item * CPAN Ratings | ||||||||||
| 107 | |||||||||||
| 108 | L<http://cpanratings.perl.org/d/Math-Function-Interpolator> | ||||||||||
| 109 | |||||||||||
| 110 | =item * Search CPAN | ||||||||||
| 111 | |||||||||||
| 112 | L<http://search.cpan.org/dist/Math-Function-Interpolator/> | ||||||||||
| 113 | |||||||||||
| 114 | =back | ||||||||||
| 115 | |||||||||||
| 116 | |||||||||||
| 117 | =head1 ACKNOWLEDGEMENTS | ||||||||||
| 118 | |||||||||||
| 119 | =cut | ||||||||||
| 120 | |||||||||||
| 121 | 4 | 1; # End of Math::Function::Interpolator::Linear | |||||||||