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