line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Math::Function::Interpolator::Linear; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
37
|
use 5.006; |
|
2
|
|
|
|
|
7
|
|
4
|
2
|
|
|
2
|
|
10
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
40
|
|
5
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
148
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our @ISA = qw(Math::Function::Interpolator); |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = '1.02'; ## VERSION |
10
|
|
|
|
|
|
|
|
11
|
2
|
|
|
2
|
|
17
|
use Carp qw(confess); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
111
|
|
12
|
2
|
|
|
2
|
|
27
|
use Number::Closest::XS qw(find_closest_numbers_around); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
81
|
|
13
|
2
|
|
|
2
|
|
12
|
use Scalar::Util qw(looks_like_number); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
537
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 NAME |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
Math::Function::Interpolator::Linear - Interpolation made easy |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 SYNOPSIS |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
use Math::Function::Interpolator::Linear; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
my $interpolator = Math::Function::Interpolator::Linear->new( |
24
|
|
|
|
|
|
|
points => {1=>2,2=>3,3=>4} |
25
|
|
|
|
|
|
|
); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
$interpolator->linear(2.5); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head1 DESCRIPTION |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
Math::Function::Interpolator::Linear helps you to do the interpolation calculation with linear method. |
32
|
|
|
|
|
|
|
It solves for point_y linearly given point_x and an array of more than 2 data points. |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=head1 FIELDS |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=head2 points (REQUIRED) |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
HashRef of points for interpolations |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=cut |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=head1 METHODS |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=head2 linear |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
linear |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=cut |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
# Solves for point_y linearly given point_x and an array of points. |
51
|
|
|
|
|
|
|
sub linear { |
52
|
8
|
|
|
8
|
1
|
798
|
my ($self, $x) = @_; |
53
|
|
|
|
|
|
|
|
54
|
8
|
100
|
|
|
|
52
|
confess "sought_point[$x] must be a number" unless looks_like_number($x); |
55
|
7
|
|
|
|
|
29
|
my $ap = $self->points; |
56
|
7
|
50
|
|
|
|
46
|
return $ap->{$x} if defined $ap->{$x}; # no need to interpolate |
57
|
|
|
|
|
|
|
|
58
|
7
|
|
|
|
|
25
|
my @Xs = keys %$ap; |
59
|
7
|
100
|
|
|
|
27
|
confess "cannot interpolate with fewer than 2 data points" |
60
|
|
|
|
|
|
|
if scalar @Xs < 2; |
61
|
|
|
|
|
|
|
|
62
|
6
|
|
|
|
|
14
|
my ($first, $second); |
63
|
|
|
|
|
|
|
($first->{x}, $second->{x}) = |
64
|
6
|
|
|
|
|
9
|
@{find_closest_numbers_around($x, \@Xs, 2)}; |
|
6
|
|
|
|
|
66
|
|
65
|
|
|
|
|
|
|
($first->{y}, $second->{y}) = |
66
|
6
|
|
|
|
|
27
|
($ap->{$first->{x}}, $ap->{$second->{x}}); |
67
|
|
|
|
|
|
|
|
68
|
6
|
|
|
|
|
23
|
my $m = ($second->{y} - $first->{y}) / ($second->{x} - $first->{x}); |
69
|
6
|
|
|
|
|
19
|
my $c = $first->{y} - ($first->{x} * $m); |
70
|
|
|
|
|
|
|
|
71
|
6
|
|
|
|
|
46
|
return $m * $x + $c; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head1 AUTHOR |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
Binary.com, C<< >> |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head1 BUGS |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
81
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
82
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head1 SUPPORT |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
perldoc Math::Function::Interpolator |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
You can also look for information at: |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=over 4 |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker (report bugs here) |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
L |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
L |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=item * CPAN Ratings |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
L |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=item * Search CPAN |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
L |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=back |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=cut |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
1; # End of Math::Function::Interpolator::Linear |