Interpolator.pm

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