blib/lib/Math/Spline.pm

filter: Statementstmt 84.4% 49 / 58 Branchbran 50.0% 4 / 8 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 90.9% 10 / 11 Podpod 0.0% 0 / 5 totaltotal 72.4% 63 / 87 CC 6 SCAR 12.7
CC
6
Cov
77%
linsearch
12.0
binsearch
3.0
spline
2.0
CRAP 3.6
↑ Summary
1 package Math::Spline;
2 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 #require Exporter;
7 #@ISA=qw(Exporter);
8 2 our @EXPORT_OK=qw(linsearch binsearch spline);
9 2 our $VERSION = 0.02;
10 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Math::Derivative qw(Derivative2);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
14 1 my $type=shift;
15 1 my $self=[];
16 1 push @{$self},shift; # x
17 1 push @{$self},shift; # y
18 1 my $y2=[Derivative2($self->[0],$self->[1])];
19 1 push @{$self},$y2;
20 1 bless $self,$type;
21 }
22  
23 1 ▶sub evaluate {
Subroutine evaluate: called CC=1 SCAR=0.0
Pod undocumented
24 1 my ($self,$v)=@_;
25 1 my $idx=binsearch($self->[0],$v);
26 1 spline($self->[0],$self->[1],$self->[2],$idx,$v);
27 }
28  
29 1 ▶sub spline {
Subroutine spline: called CC=2 SCAR=7.1
Pod undocumented
30 1 my ($x,$y,$y2,$i,$v)=@_;
31 1 my ($klo,$khi)=($i,$i+1);
32 1 my $h=$x->[$khi]-$x->[$klo];
33 0 ▶ if ($h==0) { croak "Zero interval in spline data.\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($h == 0) 0 1 1
34 1 my $a=($x->[$khi]-$v)/$h;
35 1 my $b=($v-$x->[$klo])/$h;
36 1 return $a*$y->[$klo] + $b*$y->[$khi]
37 +(($a*$a*$a-$a)*$y2->[$klo]
38 +($b*$b*$b-$b)*$y2->[$khi])*($h*$h)/6.0;
39 }
40  
41 1 ▶sub binsearch { # binary search routine finds index just below value
Subroutine binsearch: called CC=3 SCAR=11.1
Pod undocumented
42 1 my ($x,$v)=@_;
43 1 my ($klo,$khi)=(0,$#{$x});
44 1 my $k;
45 1 ▶ while (($khi-$klo)>1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($khi - $klo > 1) 1 1 2
46 1 $k=int(($khi+$klo)/2);
47 0 ▶ if ($x->[$k]>$v) { $khi=$k; } else { $klo=$k; }
Branch: 1 branch
BranchTrueFalseTotal
if ($x->[$k] > $v) { } 1 0 1
48 }
49 1 return $klo;
50 }
51  
52 0 ▶sub linsearch { # more efficient if repetatively doint it
Subroutine linsearch: not called CC=3 SCAR=24.8
Pod undocumented
53 0 my ($x,$v,$khi)=@_; $khi+=1;
54 0 my $n=$#{$x};
55 0 ▶ while($v>$x->[$khi] and $khi<$n) { $khi++; }
Branch: 1 branch
BranchTrueFalseTotal
if ($v > $x->[$khi] and $khi < $n) 0 0 0
Condition0%0/3
$v > $x->[$khi] and $khi < $n
!ll&&!rl&&r
000
MC/DC0%0/2
$v > $x->[$khi] and $khi < $n
$v > $x->[$khi] $khi < $n
Truth table0%0/3
A and B
A= $v > $x->[$khi]B= $khi < $n
ABresult
0X0
100
111
56 0 $_[2]=$khi-1;
57 }
58  
59 2 1;
60  
61 __END__
↑ Summary