line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Math::Function::Interpolator; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
143208
|
use 5.006; |
|
2
|
|
|
|
|
30
|
|
4
|
2
|
|
|
2
|
|
11
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
50
|
|
5
|
2
|
|
|
2
|
|
23
|
use warnings; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
57
|
|
6
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
11
|
use Carp qw(confess); |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
103
|
|
8
|
2
|
|
|
2
|
|
14
|
use Scalar::Util qw(looks_like_number); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
83
|
|
9
|
|
|
|
|
|
|
|
10
|
2
|
|
|
2
|
|
875
|
use Number::Closest::XS qw(find_closest_numbers_around); |
|
2
|
|
|
|
|
1213
|
|
|
2
|
|
|
|
|
110
|
|
11
|
2
|
|
|
2
|
|
1115
|
use List::MoreUtils qw(pairwise indexes); |
|
2
|
|
|
|
|
24710
|
|
|
2
|
|
|
|
|
14
|
|
12
|
2
|
|
|
2
|
|
2209
|
use List::Util qw(min max); |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
207
|
|
13
|
|
|
|
|
|
|
|
14
|
2
|
|
|
2
|
|
847
|
use Math::Function::Interpolator::Linear; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
65
|
|
15
|
2
|
|
|
2
|
|
783
|
use Math::Function::Interpolator::Quadratic; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
74
|
|
16
|
2
|
|
|
2
|
|
883
|
use Math::Function::Interpolator::Cubic; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
1298
|
|
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
|
|
|
|
|
|
|
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
|
|
|
17
|
1
|
31862
|
my $class = shift; |
64
|
17
|
50
|
|
|
|
66
|
my %params_ref = ref($_[0]) ? %{$_[0]} : @_; |
|
0
|
|
|
|
|
0
|
|
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
confess "points are required to do interpolation" |
67
|
17
|
100
|
|
|
|
61
|
unless $params_ref{'points'}; |
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
|
|
|
|
|
34
|
my $points = $params_ref{points}; |
72
|
|
|
|
|
|
|
$params_ref{points} = { |
73
|
56
|
|
|
|
|
133
|
map { $_ => $points->{$_} } |
74
|
16
|
|
|
|
|
56
|
grep { defined $points->{$_} } keys %$points |
|
56
|
|
|
|
|
123
|
|
75
|
|
|
|
|
|
|
}; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
my $self = { |
78
|
16
|
|
|
|
|
69
|
_points => $params_ref{'points'}, |
79
|
|
|
|
|
|
|
_linear_obj => 0, |
80
|
|
|
|
|
|
|
_cubic_obj => 0, |
81
|
|
|
|
|
|
|
_quadratic_obj => 0 |
82
|
|
|
|
|
|
|
}; |
83
|
16
|
|
|
|
|
38
|
my $obj = bless $self, $class; |
84
|
|
|
|
|
|
|
|
85
|
16
|
|
|
|
|
57
|
return $obj; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head2 points |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
points |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=cut |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub points { |
95
|
43
|
|
|
43
|
1
|
78
|
my ($self) = @_; |
96
|
43
|
|
|
|
|
157
|
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
|
|
|
1
|
1
|
8
|
my ($self, $x) = @_; |
108
|
1
|
|
|
|
|
6
|
my $linear_obj = $self->{'_linear_obj'}; |
109
|
1
|
50
|
|
|
|
4
|
if (!$linear_obj) { |
110
|
1
|
|
|
|
|
3
|
$linear_obj = Math::Function::Interpolator::Linear->new(points => $self->points); |
111
|
1
|
|
|
|
|
4
|
$self->{'_linear_obj'} = $linear_obj; |
112
|
|
|
|
|
|
|
} |
113
|
1
|
|
|
|
|
5
|
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
|
|
|
1
|
1
|
4
|
my ($self, $x) = @_; |
125
|
1
|
|
|
|
|
2
|
my $quadratic_obj = $self->{'_quadratic_obj'}; |
126
|
1
|
50
|
|
|
|
4
|
if (!$quadratic_obj) { |
127
|
1
|
|
|
|
|
4
|
$quadratic_obj = Math::Function::Interpolator::Quadratic->new(points => $self->points); |
128
|
1
|
|
|
|
|
3
|
$self->{'_quadratic_obj'} = $quadratic_obj; |
129
|
|
|
|
|
|
|
} |
130
|
1
|
|
|
|
|
5
|
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
|
|
|
1
|
1
|
3
|
my ($self, $x) = @_; |
142
|
1
|
|
|
|
|
3
|
my $cubic_obj = $self->{'_cubic_obj'}; |
143
|
1
|
50
|
|
|
|
4
|
if (!$cubic_obj) { |
144
|
1
|
|
|
|
|
3
|
$cubic_obj = Math::Function::Interpolator::Cubic->new(points => $self->points); |
145
|
1
|
|
|
|
|
3
|
$self->{'_cubic_obj'} = $cubic_obj; |
146
|
|
|
|
|
|
|
} |
147
|
1
|
|
|
|
|
4
|
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
|
|
|
7
|
1
|
3728
|
my ($self, $sought, $all_points) = @_; |
159
|
|
|
|
|
|
|
|
160
|
7
|
|
|
|
|
22
|
my @ap = sort { $a <=> $b } @{$all_points}; |
|
77
|
|
|
|
|
132
|
|
|
7
|
|
|
|
|
35
|
|
161
|
7
|
|
|
|
|
14
|
my $length = scalar @ap; |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
my ($first, $second) = |
164
|
7
|
|
|
|
|
13
|
@{find_closest_numbers_around($sought, $all_points, 2)}; |
|
7
|
|
|
|
|
49
|
|
165
|
7
|
100
|
|
42
|
|
62
|
my @indexes = indexes { $first == $_ or $second == $_ } @ap; |
|
42
|
|
|
|
|
134
|
|
166
|
7
|
100
|
|
|
|
53
|
my $third_index = |
167
|
|
|
|
|
|
|
(max(@indexes) < $length - 2) ? max(@indexes) + 1 : min(@indexes) - 1; |
168
|
7
|
|
|
|
|
30
|
my @sorted = sort { $a <=> $b } ($first, $second, $ap[$third_index]); |
|
25
|
|
|
|
|
38
|
|
169
|
|
|
|
|
|
|
|
170
|
7
|
|
|
|
|
36
|
return @sorted; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=head1 AUTHOR |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
Binary.com, C<< >> |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=head1 BUGS |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
180
|
|
|
|
|
|
|
the web interface at L. 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 |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
L |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=item * CPAN Ratings |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
L |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=item * Search CPAN |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
L |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=back |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=cut |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
1; # End of Math::Function::Interpolator |