blib/lib/Math/Interpolator/Robust.pm

filter: Statementstmt 95.4% 42 / 44 Branchbran 100% 4 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 85.7% 6 / 7 Podpod 100% 2 / 2 totaltotal 94.7% 54 / 57 CC 3 SCAR 5.8
CC
3
Cov
96%
_conv
3.0
x
2.0
BEGIN
1.0
CRAP 1.8
« blib/lib/Math/Interpolator/Source.pm ↑ Summary blib/lib/Math/Interpolator/Knot.pm »
1 =head1 NAME
2  
3 Math::Interpolator::Robust - lazy robust interpolation
4  
5 =head1 SYNOPSIS
6  
7 use Math::Interpolator::Robust;
8  
9 $ipl = Math::Interpolator::Robust->new(@points);
10  
11 $y = $ipl->y($x);
12 $x = $ipl->x($y);
13  
14 =head1 DESCRIPTION
15  
16 This is a subclass of the lazy interpolator class C<Math::Interpolator>.
17 This class implements a robust smooth interpolation. See
18 L<Math::Interpolator> for the interface. The algorithm is the same one
19 implemented by C<robust_interpolate> in the eager interpolator module
20 C<Math::Interpolate>.
21  
22 This code is neutral as to numeric type. The coordinate values used
23 in interpolation may be native Perl numbers, C<Math::BigRat> objects,
24 or possibly other types. Mixing types within a single interpolation is
25 not recommended.
26  
27 Only interior points are handled. Interpolation will be refused at the
28 edges of the curve.
29  
30 =cut
31  
32 package Math::Interpolator::Robust;
33  
34 2 ▶{ use 5.006; }
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 2 our $VERSION = "0.005";
39  
40 2 ▶use parent "Math::Interpolator";
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 =head1 METHODS
43  
44 =over
45  
46 =item $ipl->y(X)
47  
48 =item $ipl->x(Y)
49  
50 These methods are part of the standard C<Math::Interpolator> interface.
51  
52 =cut
53  
54 sub _conv {
55 72 ▶ my($self, $x_method, $y_method, $x) = @_;
Subroutine _conv: called CC=3 SCAR=11.0
56 72 my $nhood_method = "nhood_$x_method";
57 72 my @points = $self->$nhood_method($x, 2);
58 70 my($xa, $xb, $xc, $xd) = map { $_->$x_method } @points;
59 70 my($ya, $yb, $yc, $yd) = map { $_->$y_method } @points;
60 70 my $hxab = $xb - $xa;
61 70 my $hxbc = $xc - $xb;
62 70 my $hxcd = $xd - $xc;
63 70 my $hyab = $yb - $ya;
64 70 my $hybc = $yc - $yb;
65 70 my $hycd = $yd - $yc;
66 70 my $hab = $hxab*$hxab + $hyab*$hyab;
67 70 my $hbc = $hxbc*$hxbc + $hybc*$hybc;
68 70 my $hcd = $hxcd*$hxcd + $hycd*$hycd;
69 70 my $sb = ($hyab*$hbc + $hybc*$hab) / ($hxab*$hbc + $hxbc*$hab);
70 70 my $sc = ($hybc*$hcd + $hycd*$hbc) / ($hxbc*$hcd + $hxcd*$hbc);
71 70 my $y0 = $yb + ($x - $xb) * ($hybc / $hxbc);
72 70 my $dyb = $yb + $sb * ($x - $xb) - $y0;
73 70 my $dyc = $yc + $sc * ($x - $xc) - $y0;
74 70 my $pdy = $dyb * $dyc;
75 70 ▶ if($pdy == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pdy == 0) { } 4 66 70
elsif ($pdy > 0) { } 38 28 66
76 4 return $y0;
77 } elsif($pdy > 0) {
78 38 return $y0 + $pdy/($dyb + $dyc);
79 } else {
80 28 return $y0 + $pdy * (($x - $xb) + ($x - $xc)) /
81 (($dyb - $dyc) * $hxbc);
82 }
83 }
84  
85 sub y {
86 72 ▶ my($self, $x) = @_;
Subroutine y: called CC=1 SCAR=0.0
Pod documented
87 72 return $self->_conv("x", "y", $x);
88 }
89  
90 sub x {
91 0 ▶ my($self, $y) = @_;
Subroutine x: not called CC=1 SCAR=6.9
Pod documented
92 0 return $self->_conv("y", "x", $y);
93 }
94  
95 =back
96  
97 =head1 SEE ALSO
98  
99 L<Math::Interpolate>,
100 L<Math::Interpolator>
101  
102 =head1 AUTHOR
103  
104 Andrew Main (Zefram) <zefram@fysh.org>
105  
106 =head1 COPYRIGHT
107  
108 Copyright (C) 2006, 2007, 2009, 2010, 2012
109 Andrew Main (Zefram) <zefram@fysh.org>
110  
111 =head1 LICENSE
112  
113 This module is free software; you can redistribute it and/or modify it
114 under the same terms as Perl itself.
115  
116 =cut
117  
118 2 1;
« blib/lib/Math/Interpolator/Source.pm ↑ Summary blib/lib/Math/Interpolator/Knot.pm »