blib/lib/Math/Interpolator.pm

filter: Statementstmt 88.3% 68 / 77 Branchbran 71.7% 33 / 46 Conditioncond 66.6% 10 / 15 MC/DCmcdc 60.0% 6 / 10 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 80.7% 130 / 161 CC 29 SCAR 34.3
CC
29
Cov
80%
_nhood
40.2
BEGIN
1.0
BEGIN
1.0
CRAP 30.9
↑ Summary Build »
1 =head1 NAME
2  
3 Math::Interpolator - interpolate between lazily-evaluated points
4  
5 =head1 SYNOPSIS
6  
7 use Math::Interpolator;
8  
9 $ipl = Math::Interpolator->new(@points);
10  
11 @points = $ipl->nhood_x($x, 1);
12 @points = $ipl->nhood_y($y, 1);
13  
14 =head1 DESCRIPTION
15  
16 This class supports interpolation of a curve between known points,
17 known as "knots", with the knots being lazily evaluated. An object of
18 this type represents a set of knots on a one-dimensional curve, the knots
19 possibly not being predetermined. The methods implemented in this class
20 extract knots, forcing evaluation as required. Subclasses implement
21 interpolation by various algorithms.
22  
23 This code is neutral as to numeric type. The coordinate values used
24 in interpolation may be native Perl numbers, C<Math::BigRat> objects,
25 or possibly other types. Mixing types within a single interpolation is
26 not recommended.
27  
28 =cut
29  
30 package Math::Interpolator;
31  
32 10 ▶{ use 5.006; }
Subroutine BEGIN: called CC=1 SCAR=0.0
33 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 10 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 10 our $VERSION = "0.005";
39  
40 =head1 CONSTRUCTOR
41  
42 =over
43  
44 =item Math::Interpolator->new(POINT ...)
45  
46 A list of zero or more point objects must be supplied. They are wrapped
47 up as an interpolator object, which is returned.
48  
49 The objects in the list must each implement the interface of either
50 C<Math::Interpolator::Knot> or C<Math::Interpolator::Source>. It is
51 not necessary for them to actually be of those classes; reimplementing
52 the same interfaces is acceptable. They do not need to all be of the
53 same class. A knot-interface object represents a single point on the
54 curve, whereas a source-interface object represents an undetermined set
55 of knots within a particular range of x coordinates.
56  
57 The point objects must be sorted such that their x coordinates are
58 monotonically non-decreasing. The range of x coordinates covered by
59 each source must not include any individual knot nor overlap the range
60 of any other source.
61  
62 If reverse interpolation (determining an x coordinate given a y
63 coordinate) is to be performed, then the y coordinates must be similarly
64 sorted.
65  
66 Normally it would not be desired to instantiate this class directly,
67 because it contains no interpolation methods. A subclass such as
68 C<Math::Interpolator::Linear> or C<Math::Interpolator::Robust> should
69 be instantiated instead.
70  
71 =cut
72  
73 sub new {
74 16 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
75 16 return bless(\@_, $class);
76 }
77  
78 =back
79  
80 =head1 METHODS
81  
82 =over
83  
84 =item $ipl->nhood_x(X, N)
85  
86 Returns a list of 2*N consecutive knots (with the
87 C<Math::Interpolator::Knot> interface) defining the curve in the
88 neighbourhood of x=X. There will be N knots on either side of x=X.
89 If one of the knots has x=X exactly then that knot will be treated as
90 if it had x<X. N must be a positive integer.
91  
92 =item $ipl->nhood_y(Y, N)
93  
94 Does the same thing as C<nhood_x>, but for y coordinates. This is
95 only possible if the y coordinates are monotonically non-decreasing for
96 increasing x.
97  
98 =back
99  
100 =cut
101  
102 sub _expand {
103 12 ▶ my($self, $n) = @_;
Subroutine _expand: called CC=1 SCAR=0.0
104 12 my $exp = $self->[$n]->expand;
105 12 splice @$self, $n, 1, @$exp;
106 12 return @$exp - 1;
107 }
108  
109 sub _nhood {
110 136 ▶ my($self, $x_method, $x, $n) = @_;
Subroutine _nhood: called CC=29 SCAR=36.9
111 START:
112 137 ▶ if(@$self < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$self < 2) 0 137 137
113 0 ▶ while(@$self == 1 && $self->[0]->role eq "SOURCE") {
Branch: 1 branch
BranchTrueFalseTotal
if (@$self == 1 and $self->[0]->role eq "SOURCE") 0 0 0
Condition0%0/3
@$self == 1 and $self->[0]->role eq "SOURCE"
!ll&&!rl&&r
000
MC/DC0%0/2
@$self == 1 and $self->[0]->role eq "SOURCE"
@$self == 1 $self->[0]->role eq "SOURCE"
Truth table0%0/3
A and B
A= @$self == 1B= $self->[0]->role eq "SOURCE"
ABresult
0X0
100
111
114 0 $self->_expand(0);
115 }
116 0 ▶ croak "no useful data" if @$self < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @$self < 2 0 0 0
117 }
118 137 my $min = 0;
119 137 my $max = @$self - 1;
120 BINSEARCH:
121 148 ▶ while($max != $min + 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max != $min + 1) 317 148 465
122 10 ▶ my $try = do { use integer; ($min + $max) / 2 };
Subroutine BEGIN: called CC=1 SCAR=0.0
123 317 ▶ if($x >= $self->[$try]->$x_method) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x >= $self->[$try]->$x_method) { } 181 136 317
124 181 $min = $try;
125 } else {
126 136 $max = $try;
127 }
128 }
129 148 ▶ if($min == 0 && $x < $self->[$min]->$x_method) {
Branch: 2 branches
BranchTrueFalseTotal
if ($min == 0 and $x < $self->[$min]->$x_method) { } 7 141 148
elsif ($max == @$self - 1 and $x > $self->[$max]->$x_method) { } 7 247 254
Condition100%3/3
$min == 0 and $x < $self->[$min]->$x_method
!ll&&!rl&&r
122197
MC/DC100%2/2
$min == 0 and $x < $self->[$min]->$x_method
$min == 0 $x < $self->[$min]->$x_method
Truth table100%3/3
A and B
A= $min == 0B= $x < $self->[$min]->$x_method
ABresult
0X0
100
111
130 7 ▶ croak "data does not extend to $x_method=$x"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->[0]->role eq "SOURCE" 5 2 7
131 unless $self->[0]->role eq "SOURCE";
132 2 $max += $self->_expand(0);
133 2 ▶ goto START if $min == $max;
Branch: 1 branch
BranchTrueFalseTotal
if $min == $max 0 2 2
134 2 goto BINSEARCH;
135 ▶ } elsif($max == @$self-1 && $x > $self->[$max]->$x_method) {
Condition100%3/3
$max == @$self - 1 and $x > $self->[$max]->$x_method
!ll&&!rl&&r
113217
MC/DC100%2/2
$max == @$self - 1 and $x > $self->[$max]->$x_method
$max == @$self - 1 $x > $self->[$max]->$x_method
Truth table100%3/3
A and B
A= $max == @$self - 1B= $x > $self->[$max]->$x_method
ABresult
0X0
100
111
136 7 ▶ croak "data does not extend to $x_method=$x"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->[$max]->role eq "SOURCE" 5 2 7
137 unless $self->[$max]->role eq "SOURCE";
138 2 $max += $self->_expand($max);
139 2 ▶ goto START if $min == $max;
Branch: 1 branch
BranchTrueFalseTotal
if $min == $max 0 2 2
140 2 goto BINSEARCH;
141 }
142 134 ▶ if($self->[$min]->role eq "SOURCE") {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->[$min]->role eq 'SOURCE') { } 2 132 134
elsif ($self->[$max]->role eq 'SOURCE') { } 6 126 132
143 2 $max += $self->_expand($min);
144 2 ▶ goto START if $min == $max;
Branch: 1 branch
BranchTrueFalseTotal
if $min == $max 0 2 2
145 2 ▶ $min-- unless $min == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $min == 0 0 2 2
146 2 goto BINSEARCH;
147 } elsif($self->[$max]->role eq "SOURCE") {
148 6 $max += $self->_expand($max);
149 6 ▶ goto START if $min == $max;
Branch: 1 branch
BranchTrueFalseTotal
if $min == $max 1 5 6
150 5 ▶ $max++ unless $max == @$self - 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $max == @$self - 1 3 2 5
151 5 goto BINSEARCH;
152 }
153 126 while(1) {
154 200 ▶ croak "non-knot in region"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->[$min]->role eq "KNOT" and $self->[$max]->role eq "KNOT" 0 200 200
Condition33%1/3
$self->[$min]->role eq "KNOT" and $self->[$max]->role eq "KNOT"
!ll&&!rl&&r
00200
MC/DC0%0/2
$self->[$min]->role eq "KNOT" and $self->[$max]->role eq "KNOT"
$self->[$min]->role eq "KNOT" $self->[$max]->role eq "KNOT"
Truth table33%1/3
A and B
A= $self->[$min]->role eq "KNOT"B= $self->[$max]->role eq "KNOT"
ABresult
0X0
100
111
155 unless $self->[$min]->role eq "KNOT" &&
156 $self->[$max]->role eq "KNOT";
157 200 ▶ last unless --$n;
Branch: 1 branch
BranchTrueFalseTotal
unless --$n 124 76 200
158 76 while(1) {
159 76 ▶ croak "data does not extend to $x_method=$x"
Branch: 1 branch
BranchTrueFalseTotal
if $min == 0 or $max == @$self - 1 2 74 76
Condition100%3/3
$min == 0 or $max == @$self - 1
l!l&&r!l&&!r
1174
MC/DC100%2/2
$min == 0 or $max == @$self - 1
$min == 0 $max == @$self - 1
Truth table100%3/3
A or B
A= $min == 0B= $max == @$self - 1
ABresult
1X1
011
000
160 if $min == 0 || $max == @$self - 1;
161 74 my $expanded;
162 74 ▶ if($self->[$min-1]->role eq "SOURCE") {
Branch: 1 branch
BranchTrueFalseTotal
if ($$self[$min - 1]->role eq "SOURCE") 0 74 74
163 0 my $diff = $self->_expand($min-1);
164 0 $min += $diff;
165 0 $max += $diff;
166 0 $expanded = 1;
167 }
168 74 ▶ if($self->[$max+1]->role eq "SOURCE") {
Branch: 1 branch
BranchTrueFalseTotal
if ($$self[$max + 1]->role eq "SOURCE") 0 74 74
169 0 $self->_expand($max+1);
170 0 $expanded = 1;
171 }
172 74 ▶ last unless $expanded;
Branch: 1 branch
BranchTrueFalseTotal
unless $expanded 74 0 74
173 }
174 74 $min--;
175 74 $max++;
176 }
177 124 return @{$self}[$min..$max];
178 }
179  
180 sub nhood_x {
181 126 ▶ my($self, $x, $n) = @_;
Subroutine nhood_x: called CC=1 SCAR=0.0
Pod documented
182 126 return $self->_nhood("x", $x, $n);
183 }
184  
185 sub nhood_y {
186 10 ▶ my($self, $y, $n) = @_;
Subroutine nhood_y: called CC=1 SCAR=0.0
Pod documented
187 10 return $self->_nhood("y", $y, $n);
188 }
189  
190 =pod
191  
192 The following two methods are not implemented in this class, but are
193 the standard interpolation interface to be implemented by subclasses.
194  
195 =over
196  
197 =item $ipl->y(X)
198  
199 Interpolates a y value for the given x coordinate, and returns it.
200  
201 =item $ipl->x(Y)
202  
203 Interpolates an x value for the given y coordinate, and returns it. This
204 is only possible if the y coordinates are monotonically non-decreasing
205 for increasing x.
206  
207 =back
208  
209 =head1 SEE ALSO
210  
211 L<Math::Interpolator::Knot>,
212 L<Math::Interpolator::Linear>,
213 L<Math::Interpolator::Robust>,
214 L<Math::Interpolator::Source>
215  
216 =head1 AUTHOR
217  
218 Andrew Main (Zefram) <zefram@fysh.org>
219  
220 =head1 COPYRIGHT
221  
222 Copyright (C) 2006, 2007, 2009, 2010, 2012
223 Andrew Main (Zefram) <zefram@fysh.org>
224  
225 =head1 LICENSE
226  
227 This module is free software; you can redistribute it and/or modify it
228 under the same terms as Perl itself.
229  
230 =cut
231  
232 10 1;
↑ Summary Build »