Interpolator/Cubic.pm

filter: Statementstmt 100% 75 / 75 Branchbran 93.7% 15 / 16 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 99.0% 106 / 107 CC 11 SCAR 13.4
CC
11
Cov
99%
cubic
7.0
_spline_points
4.0
_sorted_Xs
2.0
CRAP 3.8
↑ Summary Interpolator/Quadratic.pm »
1 package Math::Function::Interpolator::Cubic;
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 $VERSION = '1.03'; ## VERSION
8  
9 4 our @ISA = qw(Math::Function::Interpolator);
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::Cubic
18  
19 =head1 SYNOPSIS
20  
21 use Math::Function::Interpolator::Cubic;
22  
23 my $interpolator = Math::Function::Interpolator::Cubic->new(
24 points => {1=>2,2=>3,3=>4,4=>5,5=>6,6=>7}
25 );
26  
27 $interpolator->cubic(2.5);
28  
29 =head1 DESCRIPTION
30  
31 Math::Function::Interpolator::Cubic helps you to do the interpolation calculation with cubic method.
32 It solves the interpolated_y given point_x and a minimum of 5 data points.
33  
34 =head1 FIELDS
35  
36 =head2 points (REQUIRED)
37  
38 HashRef of points for interpolations
39  
40 =cut
41  
42 23 ▶sub _sorted_Xs {
Subroutine _sorted_Xs: called CC=2 SCAR=6.9
43 23 my ($self) = @_;
44 23 ▶ return $self->{'_sorted_Xs'} if $self->{'_sorted_Xs'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_sorted_Xs"} 18 5 23
45 5 $self->{'_sorted_Xs'} = [sort { $a <=> $b } keys %{$self->points}];
46 5 return $self->{'_sorted_Xs'};
47 }
48  
49 18 ▶sub _spline_points {
Subroutine _spline_points: called CC=4 SCAR=13.9
50 18 my ($self) = @_;
51  
52 18 ▶ return $self->{'_spline_points'} if $self->{'_spline_points'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_spline_points"} 14 4 18
53  
54 4 my $points_ref = $self->points;
55 4 my $Xs = $self->_sorted_Xs;
56 4 my @Ys = map { $points_ref->{$_} } @$Xs;
57  
58 # First element is 0
59 # Second derivative of the Ys
60 4 my @y_2derivative = (0);
61 4 my @u = (0);
62 4 my $counter = @$Xs - 2;
63  
64 4 for my $i (1 .. $counter) {
65 12 my $sig = ($Xs->[$i] - $Xs->[$i - 1]) / ($Xs->[$i + 1] - $Xs->[$i - 1]);
66 12 my $p = $sig * $y_2derivative[$i - 1] + 2;
67 12 $y_2derivative[$i] = ($sig - 1) / $p;
68 12 $u[$i] = ($Ys[$i + 1] - $Ys[$i]) / ($Xs->[$i + 1] - $Xs->[$i]) - ($Ys[$i] - $Ys[$i - 1]) / ($Xs->[$i] - $Xs->[$i - 1]);
69 12 $u[$i] = (($u[$i] * 6) / ($Xs->[$i + 1] - $Xs->[$i - 1]) - $sig * $u[$i - 1]) / $p;
70 }
71  
72 # Last element is 0
73 4 push @y_2derivative, 0;
74  
75 4 ▶ for (my $i = $counter; $i > 0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 12 4 16
76 12 $y_2derivative[$i] = $y_2derivative[$i] * $y_2derivative[$i + 1] + $u[$i];
77 }
78  
79 4 my %y_2derivative_combined = map { $Xs->[$_] => $y_2derivative[$_] } 0 .. $#y_2derivative;
80  
81 4 $self->{'_spline_points'} = \%y_2derivative_combined;
82  
83 4 return $self->{'_spline_points'};
84 }
85  
86 8 ▶sub _extrapolate_spline {
Subroutine _extrapolate_spline: called CC=1 SCAR=0.0
87 8 my ($self, $args) = @_;
88 8 my $x = $args->{x};
89 8 my $first = $args->{first};
90 8 my $second = $args->{second};
91 8 my $derivative2 = $args->{derivative2};
92  
93 8 my $derivative1 = (($second->{y} - $first->{y}) / ($second->{x} - $first->{x})) - (($second->{x} - $first->{x}) * $derivative2) / 6;
94  
95 8 return $first->{y} - ($first->{x} - $x) * $derivative1;
96 }
97  
98 =head1 METHODS
99  
100 =head2 cubic
101  
102 cubic
103  
104 =cut
105  
106 # Returns the interpolated_y given point_x and a minimum of 5 data points
107 20 ▶sub cubic {
Subroutine cubic: called CC=7 SCAR=19.5
Pod documented
108 20 my ($self, $x) = @_;
109  
110 20 ▶ confess "sought_point[$x] must be a numeric" if !looks_like_number($x);
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($x) 1 19 20
111 19 my $ap = $self->points;
112 19 ▶ return $ap->{$x} if defined $ap->{$x}; # No interpolation needed.
Branch: 1 branch
BranchTrueFalseTotal
if defined $ap->{$x} 0 19 19
113  
114 19 my $Xs = $self->_sorted_Xs;
115 19 ▶ confess "cannot interpolate with fewer than 5 data points"
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$Xs < 5 1 18 19
116 if scalar @$Xs < 5;
117  
118 18 my $splines = $self->_spline_points;
119  
120 18 my $y;
121 18 ▶ if ($x < $Xs->[0] or $x > $Xs->[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $Xs->[0] or $x > $Xs->[-1]) { } 8 10 18
Condition100%3/3
$x < $Xs->[0] or $x > $Xs->[-1]
l!l&&r!l&&!r
4410
MC/DC100%2/2
$x < $Xs->[0] or $x > $Xs->[-1]
$x < $Xs->[0] $x > $Xs->[-1]
Truth table100%3/3
A or B
A= $x < $Xs->[0]B= $x > $Xs->[-1]
ABresult
1X1
011
000
122 8 ▶ my ($spline_key, $first, $second) =
Branch: 1 branch
BranchTrueFalseTotal
$x < $Xs->[0] ? : 4 4 8
123 $x < $Xs->[0]
124 ? ($Xs->[1], $Xs->[0], $Xs->[1])
125 : ($Xs->[-2], $Xs->[-2], $Xs->[-1]);
126 $y = $self->_extrapolate_spline({
127 x => $x,
128 derivative2 => $splines->{$spline_key},
129 first => {
130 x => $first,
131 y => $ap->{$first},
132 },
133 second => {
134 x => $second,
135 8 y => $ap->{$second},
136 },
137 },
138 );
139 } else {
140 10 my ($first, $second) = @{find_closest_numbers_around($x, $Xs, 2)};
141  
142 10 my $range = $second - $first;
143  
144 10 my $A = ($second - $x) / $range;
145 10 my $B = 1 - $A;
146 10 my $C = ($A**3 - $A) * ($range**2) / 6;
147 10 my $D = ($B**3 - $B) * ($range**2) / 6;
148  
149 10 $y = $A * $ap->{$first} + $B * $ap->{$second} + $C * $splines->{$first} + $D * $splines->{$second};
150 }
151  
152 18 return $y;
153 }
154  
155 =head1 AUTHOR
156  
157 Binary.com, C<< <perl at binary.com> >>
158  
159 =head1 BUGS
160  
161 Please report any bugs or feature requests to C<bug-math-function-interpolator at rt.cpan.org>, or through
162 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Math-Function-Interpolator>. I will be notified, and then you'll
163 automatically be notified of progress on your bug as I make changes.
164  
165  
166  
167  
168 =head1 SUPPORT
169  
170 You can find documentation for this module with the perldoc command.
171  
172 perldoc Math::Function::Interpolator
173  
174  
175 You can also look for information at:
176  
177 =over 4
178  
179 =item * RT: CPAN's request tracker (report bugs here)
180  
181 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Math-Function-Interpolator>
182  
183 =item * AnnoCPAN: Annotated CPAN documentation
184  
185 L<http://annocpan.org/dist/Math-Function-Interpolator>
186  
187 =item * CPAN Ratings
188  
189 L<http://cpanratings.perl.org/d/Math-Function-Interpolator>
190  
191 =item * Search CPAN
192  
193 L<http://search.cpan.org/dist/Math-Function-Interpolator/>
194  
195 =back
196  
197  
198 =head1 ACKNOWLEDGEMENTS
199  
200 =cut
201  
202 4 1; # End of Math::Function::Interpolator::Cubic
↑ Summary Interpolator/Quadratic.pm »