| 1 | package Math::Function::Interpolator; | ||||||||||
| 2 | |||||||||||
| 3 | 3 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 3 | ▶ | use Carp qw(confess); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 3 | ▶ | use Scalar::Util qw(looks_like_number); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 3 | ▶ | use Number::Closest::XS qw(find_closest_numbers_around); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 3 | ▶ | use List::MoreUtils qw(pairwise indexes); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 3 | ▶ | use List::Util qw(min max); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 3 | ▶ | use Math::Function::Interpolator::Linear; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 3 | ▶ | use Math::Function::Interpolator::Quadratic; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 3 | ▶ | use Math::Function::Interpolator::Cubic; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | =head1 NAME | ||||||||||
| 19 | |||||||||||
| 20 | Math::Function::Interpolator - Interpolation made easy | ||||||||||
| 21 | |||||||||||
| 22 | =head1 SYNOPSIS | ||||||||||
| 23 | |||||||||||
| 24 | use Math::Function::Interpolator; | ||||||||||
| 25 | |||||||||||
| 26 | my $interpolator = Math::Function::Interpolator->new( | ||||||||||
| 27 | points => {1=>2,2=>3,3=>4} | ||||||||||
| 28 | ); | ||||||||||
| 29 | |||||||||||
| 30 | $interpolator->linear(2.5); | ||||||||||
| 31 | |||||||||||
| 32 | $interpolator->quadratic(2.5); | ||||||||||
| 33 | |||||||||||
| 34 | $interpolator->cubic(2.5); | ||||||||||
| 35 | |||||||||||
| 36 | =head1 DESCRIPTION | ||||||||||
| 37 | |||||||||||
| 38 | Math::Function::Interpolator helps you to do the interpolation calculation with linear, quadratic and cubic methods. | ||||||||||
| 39 | |||||||||||
| 40 | 1. Linear method (needs more than 1 data point) | ||||||||||
| 41 | 1. Quadratic method (needs more than 2 data points) | ||||||||||
| 42 | 1. Cubic method, it's a Cubic Spline method (needs more than 4 data points) | ||||||||||
| 43 | |||||||||||
| 44 | =head1 FIELDS | ||||||||||
| 45 | |||||||||||
| 46 | =head2 points (REQUIRED) | ||||||||||
| 47 | |||||||||||
| 48 | HashRef of points for interpolations | ||||||||||
| 49 | |||||||||||
| 50 | =cut | ||||||||||
| 51 | |||||||||||
| 52 | 3 | our $VERSION = '1.02'; | |||||||||
| 53 | |||||||||||
| 54 | =head1 METHODS | ||||||||||
| 55 | |||||||||||
| 56 | =head2 new | ||||||||||
| 57 | |||||||||||
| 58 | New instance method | ||||||||||
| 59 | |||||||||||
| 60 | =cut | ||||||||||
| 61 | |||||||||||
| 62 | sub new { ## no critic (RequireArgUnpacking) | ||||||||||
| 63 | 17 | ▶ | my $class = shift; | ||||||||
|
Subroutine
new: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||
| 64 | 17 | ▶ | my %params_ref = ref($_[0]) ? %{$_[0]} : @_; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | |||||||||||
| 66 | confess "points are required to do interpolation" | ||||||||||
| 67 | 17 | ▶ | unless $params_ref{'points'}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | |||||||||||
| 69 | # We can't interpolate properly on undef values so make sure we know | ||||||||||
| 70 | # they are missing by removing them entirely. | ||||||||||
| 71 | 16 | my $points = $params_ref{points}; | |||||||||
| 72 | $params_ref{points} = { | ||||||||||
| 73 | 56 | map { $_ => $points->{$_} } | |||||||||
| 74 | 16 | grep { defined $points->{$_} } keys %$points | |||||||||
| 75 | }; | ||||||||||
| 76 | |||||||||||
| 77 | my $self = { | ||||||||||
| 78 | 16 | _points => $params_ref{'points'}, | |||||||||
| 79 | _linear_obj => 0, | ||||||||||
| 80 | _cubic_obj => 0, | ||||||||||
| 81 | _quadratic_obj => 0 | ||||||||||
| 82 | }; | ||||||||||
| 83 | 16 | my $obj = bless $self, $class; | |||||||||
| 84 | |||||||||||
| 85 | 16 | return $obj; | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | =head2 points | ||||||||||
| 89 | |||||||||||
| 90 | points | ||||||||||
| 91 | |||||||||||
| 92 | =cut | ||||||||||
| 93 | |||||||||||
| 94 | sub points { | ||||||||||
| 95 | 43 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
points: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 96 | 43 | return $self->{'_points'}; | |||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | =head2 linear | ||||||||||
| 100 | |||||||||||
| 101 | This method do the linear interpolation. It solves for point_y linearly given point_x and an array of points. | ||||||||||
| 102 | This method needs more than 1 data point. | ||||||||||
| 103 | |||||||||||
| 104 | =cut | ||||||||||
| 105 | |||||||||||
| 106 | sub linear { | ||||||||||
| 107 | 1 | ▶ | my ($self, $x) = @_; | ||||||||
|
Subroutine
linear: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 108 | 1 | my $linear_obj = $self->{'_linear_obj'}; | |||||||||
| 109 | 1 | ▶ | if (!$linear_obj) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | 1 | $linear_obj = Math::Function::Interpolator::Linear->new(points => $self->points); | |||||||||
| 111 | 1 | $self->{'_linear_obj'} = $linear_obj; | |||||||||
| 112 | } | ||||||||||
| 113 | 1 | return $linear_obj->linear($x); | |||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | =head2 quadratic | ||||||||||
| 117 | |||||||||||
| 118 | This method do the quadratic interpolation. It solves the interpolated_y value given point_x with 3 data points. | ||||||||||
| 119 | This method needs more than 2 data point. | ||||||||||
| 120 | |||||||||||
| 121 | =cut | ||||||||||
| 122 | |||||||||||
| 123 | sub quadratic { | ||||||||||
| 124 | 1 | ▶ | my ($self, $x) = @_; | ||||||||
|
Subroutine
quadratic: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 125 | 1 | my $quadratic_obj = $self->{'_quadratic_obj'}; | |||||||||
| 126 | 1 | ▶ | if (!$quadratic_obj) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 127 | 1 | $quadratic_obj = Math::Function::Interpolator::Quadratic->new(points => $self->points); | |||||||||
| 128 | 1 | $self->{'_quadratic_obj'} = $quadratic_obj; | |||||||||
| 129 | } | ||||||||||
| 130 | 1 | return $quadratic_obj->quadratic($x); | |||||||||
| 131 | } | ||||||||||
| 132 | |||||||||||
| 133 | =head2 cubic | ||||||||||
| 134 | |||||||||||
| 135 | This method do the cubic interpolation. It solves the interpolated_y given point_x and a minimum of 5 data points. | ||||||||||
| 136 | This method needs more than 4 data point. | ||||||||||
| 137 | |||||||||||
| 138 | =cut | ||||||||||
| 139 | |||||||||||
| 140 | sub cubic { | ||||||||||
| 141 | 1 | ▶ | my ($self, $x) = @_; | ||||||||
|
Subroutine
cubic: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 142 | 1 | my $cubic_obj = $self->{'_cubic_obj'}; | |||||||||
| 143 | 1 | ▶ | if (!$cubic_obj) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 144 | 1 | $cubic_obj = Math::Function::Interpolator::Cubic->new(points => $self->points); | |||||||||
| 145 | 1 | $self->{'_cubic_obj'} = $cubic_obj; | |||||||||
| 146 | } | ||||||||||
| 147 | 1 | return $cubic_obj->cubic($x); | |||||||||
| 148 | } | ||||||||||
| 149 | |||||||||||
| 150 | =head2 closest_three_points | ||||||||||
| 151 | |||||||||||
| 152 | Returns the the closest three points to the sought point. | ||||||||||
| 153 | The third point is chosen based on the point which is closer to mid point | ||||||||||
| 154 | |||||||||||
| 155 | =cut | ||||||||||
| 156 | |||||||||||
| 157 | sub closest_three_points { | ||||||||||
| 158 | 7 | ▶ | my ($self, $sought, $all_points) = @_; | ||||||||
|
Subroutine
closest_three_points: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 159 | |||||||||||
| 160 | 7 | my @ap = sort { $a <=> $b } @{$all_points}; | |||||||||
| 161 | 7 | my $length = scalar @ap; | |||||||||
| 162 | |||||||||||
| 163 | my ($first, $second) = | ||||||||||
| 164 | 7 | @{find_closest_numbers_around($sought, $all_points, 2)}; | |||||||||
| 165 | 7 | ▶ | my @indexes = indexes { $first == $_ or $second == $_ } @ap; | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||
| 166 | 7 | ▶ | my $third_index = | ||||||||
|
Branch: 1 branch
| |||||||||||
| 167 | (max(@indexes) < $length - 2) ? max(@indexes) + 1 : min(@indexes) - 1; | ||||||||||
| 168 | 7 | my @sorted = sort { $a <=> $b } ($first, $second, $ap[$third_index]); | |||||||||
| 169 | |||||||||||
| 170 | 7 | return @sorted; | |||||||||
| 171 | } | ||||||||||
| 172 | |||||||||||
| 173 | =head1 AUTHOR | ||||||||||
| 174 | |||||||||||
| 175 | Binary.com, C<< <perl at binary.com> >> | ||||||||||
| 176 | |||||||||||
| 177 | =head1 BUGS | ||||||||||
| 178 | |||||||||||
| 179 | Please report any bugs or feature requests to C<bug-math-function-interpolator at rt.cpan.org>, or through | ||||||||||
| 180 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Math-Function-Interpolator>. I will be notified, and then you'll | ||||||||||
| 181 | automatically be notified of progress on your bug as I make changes. | ||||||||||
| 182 | |||||||||||
| 183 | |||||||||||
| 184 | =head1 SUPPORT | ||||||||||
| 185 | |||||||||||
| 186 | You can find documentation for this module with the perldoc command. | ||||||||||
| 187 | |||||||||||
| 188 | perldoc Math::Function::Interpolator | ||||||||||
| 189 | |||||||||||
| 190 | |||||||||||
| 191 | You can also look for information at: | ||||||||||
| 192 | |||||||||||
| 193 | =over 4 | ||||||||||
| 194 | |||||||||||
| 195 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||
| 196 | |||||||||||
| 197 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Math-Function-Interpolator> | ||||||||||
| 198 | |||||||||||
| 199 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 200 | |||||||||||
| 201 | L<http://annocpan.org/dist/Math-Function-Interpolator> | ||||||||||
| 202 | |||||||||||
| 203 | =item * CPAN Ratings | ||||||||||
| 204 | |||||||||||
| 205 | L<http://cpanratings.perl.org/d/Math-Function-Interpolator> | ||||||||||
| 206 | |||||||||||
| 207 | =item * Search CPAN | ||||||||||
| 208 | |||||||||||
| 209 | L<http://search.cpan.org/dist/Math-Function-Interpolator/> | ||||||||||
| 210 | |||||||||||
| 211 | =back | ||||||||||
| 212 | |||||||||||
| 213 | |||||||||||
| 214 | =head1 ACKNOWLEDGEMENTS | ||||||||||
| 215 | |||||||||||
| 216 | =cut | ||||||||||
| 217 | |||||||||||
| 218 | 3 | 1; # End of Math::Function::Interpolator | |||||||||