blib/lib/Text/NumericData/App/txdderiv.pm

filter: Statementstmt 86.5% 129 / 149 Branchbran 71.2% 47 / 66 Conditioncond 50.0% 6 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 7 totaltotal 76.1% 191 / 251 SCAR 41.1
CC
45
Cov
80%
central_diff_samegrid
16.3
prepare
13.6
process_file
12.1
CRAP 60.8
« blib/lib/Text/NumericData/App.pm ↑ Summary blib/lib/Text/NumericData/Calc.pm »
1 package Text::NumericData::App::txdderiv;
2  
3 1 ▶use Text::NumericData::App;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 # Not using Math::Derivative, central difference mid-interval suits me better.
5 # Actually changing my mind there ... computing on the points again.
6 # You got the dualgrid option now.
7  
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # This is just a placeholder because of a past build system bug.
11 # The one and only version for Text::NumericData is kept in
12 # the Text::NumericData module itself.
13 1 our $VERSION = '1';
14 1 $VERSION = eval $VERSION;
15  
16 #the infostring says it all
17 1 my $infostring = "compute derivative of data sets using central differences
18  
19 Usage:
20 $0 [parameters] [ycol|xcol ycol [ycol2 ...]] < data.dat
21  
22 You can provide the x and y column(s) to work on on the command line as plain numbers of as values for the --xcol and --ycol parameters. The latter are overridden by the former.";
23  
24 1 our @ISA = ('Text::NumericData::App');
25  
26 sub new
27 {
28 1 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
29 1 my @pars =
30 (
31 'xcol',1,'x','abscissa column'
32 ,'ycol',[2],'y','ordinate column, or columns as array'
33 ,'dualgrid',0,'','define the derivative on points between the initial sampling points, thusly creating a new grid; otherwise, the central differences are computed on each input point with its neighbours'
34 ,'append',0,'','do not replace data with the derivatives, append them as additional columns instead (only works if dual-grid mode is off)'
35 ,'sort',1,'','sort the data according to x column (usually desirable unless you got a specific order wich should be monotonic)'
36 );
37 1 return $class->SUPER::new
38 ({
39 parconf=>
40 {
41 info=>$infostring # default version
42 # default author
43 # default copyright
44 }
45 ,pardef=>\@pars
46 ,filemode=>1
47 ,pipemode=>1
48 ,pipe_init=>\&prepare
49 ,pipe_file=>\&process_file
50 });
51 }
52  
53 sub prepare
54 {
55 7 ▶ my $self = shift;
Subroutine prepare: called CC=8 SCAR=26.1
Pod undocumented
56 7 my $p = $self->{param};
57  
58 return $self->error("Append mode and dualgrid don't mix.")
59 7 ▶ if($p->{append} and $p->{dualgrid});
Branch: 1 branch
BranchTrueFalseTotal
if $p->{"append"} and $p->{"dualgrid"} 0 7 7
Condition66%2/3
$p->{"append"} and $p->{"dualgrid"}
!ll&&!rl&&r
430
MC/DC0%0/2
$p->{"append"} and $p->{"dualgrid"}
$p->{"append"} $p->{"dualgrid"}
Truth table66%2/3
A and B
A= $p->{"append"}B= $p->{"dualgrid"}
ABresult
0X0
100
111
60  
61 7 ▶ if(@{$self->{argv}})
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"argv"};}) 0 7 7
62 {
63 0 ▶ if(@{$self->{argv}} == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{'argv'};} == 1) { } 0 0 0
64 {
65 0 $p->{ycol} = [shift @{$self->{argv}}];
66 }
67 else
68 {
69 0 $p->{xcol} = shift @{$self->{argv}};
70 0 @{$p->{ycol}} = @{$self->{argv}};
71 }
72 }
73  
74 return $self->error("Bad x column index!")
75 7 ▶ unless(valid_column([$p->{xcol}]));
Branch: 1 branch
BranchTrueFalseTotal
unless valid_column([$p->{"xcol"}]) 0 7 7
76 return $self->error("Bad y column index/indices!")
77 7 ▶ unless(valid_column($p->{ycol}));
Branch: 1 branch
BranchTrueFalseTotal
unless valid_column($p->{"ycol"}) 0 7 7
78  
79 # Translate to plain 0-based indices.
80 7 $self->{xi} = $p->{xcol}-1;
81 7 $self->{yi} = [@{$p->{ycol}}];
82 7 for(@{$self->{yi}}){ --$_; }
83  
84 7 return 0;
85 }
86  
87  
88 sub process_file
89 {
90 7 ▶ my $self = shift;
Subroutine process_file: called CC=12 SCAR=25.0
Pod undocumented
91 7 my $p = $self->{param};
92 7 my $txd = $self->{txd};
93  
94 # sort out titles
95 7 my @cidx = ($p->{xcol}-1);
96 7 my $cols = $txd->columns();
97 7 ▶ unless($cols > 0)
Branch: 1 branch
BranchTrueFalseTotal
unless ($cols > 0) 0 7 7
98 {
99 0 print STDERR "No data columns?\n";
100 0 $txd->write_all($self->{out});
101 0 return;
102 }
103  
104 7 my @derivtitles;
105 7 for my $y (@{$p->{ycol}})
106 {
107 8 push(@derivtitles, derivtitle($txd->{titles}[$y-1], $y));
108 8 push(@cidx, $y-1);
109 }
110  
111 7 ▶ if(@{$txd->{titles}})
Branch: 1 branch
BranchTrueFalseTotal
if (@{$txd->{"titles"};}) 4 3 7
112 {
113 4 ▶ if($p->{append})
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{'append'}) { } 2 2 4
114 {
115 2 ▶ $txd->{titles}[$cols-1] = '' unless defined $txd->{titles}[$cols-1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $txd->{"titles"}[$cols - 1] 0 2 2
116 2 push(@{$txd->{titles}}, @derivtitles);
117 }
118 else
119 {
120 2 my $xt = $txd->{titles}[$p->{xcol}-1];
121 2 @{$txd->{titles}} = ($xt, @derivtitles);
122 }
123 }
124  
125 7 ▶ if(@{$txd->{raw_header}}){ $txd->write_header($self->{out}); }
Branch: 1 branch
BranchTrueFalseTotal
if (@{$txd->{"raw_header"};}) 4 3 7
126 7 ▶ if(@{$txd->{titles}}){ print {$self->{out}} ${$txd->title_line()}; }
Branch: 1 branch
BranchTrueFalseTotal
if (@{$txd->{"titles"};}) 4 3 7
127  
128 # compute derivatives, after sorting data
129 7 ▶ $txd->sort_data([$p->{xcol}-1], [0]) if $p->{sort};
Branch: 1 branch
BranchTrueFalseTotal
if $p->{"sort"} 7 0 7
130 my $diffdata = $p->{dualgrid}
131 ? central_diff_dualgrid($txd->{data}, $self->{xi}, $self->{yi})
132 7 ▶ : central_diff_samegrid($txd->{data}, $self->{xi}, $self->{yi});
Branch: 1 branch
BranchTrueFalseTotal
$p->{'dualgrid'} ? : 2 5 7
133 7 ▶ if($p->{append})
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{'append'}) { } 3 4 7
134 {
135 3 ▶ for(my $i=0; $i<=$#{$diffdata}; ++$i)
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$diffdata;}) 30 3 33
136 {
137 30 shift(@{$diffdata->[$i]});
138 30 push(@{$txd->{data}[$i]}, @{$diffdata->[$i]});
139 }
140 }
141 else
142 {
143 4 $txd->{data} = $diffdata;
144 }
145 7 $txd->write_data($self->{out});
146 }
147  
148 # Perhaps a general helper in TextData for validation of input.
149 sub valid_column
150 {
151 14 ▶ my ($cols, $max) = @_;
Subroutine valid_column: called CC=5 SCAR=19.1
Pod undocumented
152 14 ▶ return 0 if (grep {$_ != int($_) or $_ < 1} @{$cols});
Branch: 2 branches
BranchTrueFalseTotal
if grep {$_ != int $_ or $_ < 1;} @{$cols;} 0 14 14
$_ != int $_ or $_ < 1 15 0 15
153 14 ▶ return 0 if (defined $max and grep {$_ > $max} @{$cols});
Branch: 1 branch
BranchTrueFalseTotal
if defined $max and grep {$_ > $max;} @{$cols;} 0 14 14
Condition33%1/3
defined $max and grep {$_ > $max;} @{$cols;}
!ll&&!rl&&r
1400
MC/DC0%0/2
defined $max and grep {$_ > $max;} @{$cols;}
defined $max grep {$_ > $max;} @{$cols;}
Truth table33%1/3
A and B
A= defined $maxB= grep {$_ > $max;} @{$cols;}
ABresult
0X0
100
111
154 14 return 1;
155 }
156  
157 # local functions, not methods
158  
159 # f'(x) = f(x+h)-f(x-h)/2h + O(h^2)
160 sub central_diff_dualgrid
161 {
162 2 ▶ my ($data, $xi, $yi) = @_;
Subroutine central_diff_dualgrid: called CC=7 SCAR=21.2
Pod undocumented
163 2 my @diff;
164 2 ▶ for(my $i=1; $i<=$#{$data}; ++$i)
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$data;}) 18 2 20
165 {
166 18 my $prev = $data->[$i-1];
167 18 my $next = $data->[$i];
168 # treat empty lines as separations
169 18 ▶ if(not @{$prev} or not @{$next})
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$prev;} and @{$next;}) 0 18 18
Condition33%1/3
@{$prev;} and @{$next;}
!ll&&!rl&&r
0018
MC/DC0%0/2
@{$prev;} and @{$next;}
@{$prev;} @{$next;}
Truth table33%1/3
A and B
A= @{$prev;}B= @{$next;}
ABresult
0X0
100
111
170 {
171 0 push(@diff, []);
172 # Really skip it if nothing follows.
173 0 ▶ ++$i unless not @{$next};
Branch: 1 branch
BranchTrueFalseTotal
if @{$next;} 0 0 0
174 0 next;
175 }
176 18 my $dx = $next->[$xi] - $prev->[$xi];
177 18 ▶ next if not $dx > 1e-12; # some safety precaution, better tunable?
Branch: 1 branch
BranchTrueFalseTotal
unless $dx > "1e-12" 0 18 18
178 18 my $invdx = 1./$dx;
179 18 my @point = ( 0.5*($prev->[$xi]+$next->[$xi]) );
180 18 for(@{$yi})
181 {
182 27 push(@point, $invdx*($next->[$_]-$prev->[$_]));
183 }
184 18 push(@diff, \@point);
185 }
186 2 return \@diff;
187 }
188  
189 # f'(x) = (v^2 f(x+w) - (v^2-w^2) f(x) - w^2 f(x-v)) / (wv^2+vw^2) + O(v^3w^2/(wv^2+vw^2)) + O(w^3v^2/(wv^2+vw^2))
190 # = (f(x+w) - (1-(w/v)^2) f(x) - (w/v)^2 f(x-v)) / w(1+w/v) + O(v^3w^2/(wv^2+vw^2)) + O(w^3v^2/(wv^2+vw^2))
191 # you better have some distance between the points, no precaution here
192 sub central_diff_samegrid
193 {
194 5 ▶ my ($data, $xi, $yi) = @_;
Subroutine central_diff_samegrid: called CC=16 SCAR=27.9
Pod undocumented
195 5 my @diff;
196 # Cannot do anything with a single value ... well, could call it a zero.
197 5 ▶ return \@diff if(@{$data} < 2);
Branch: 1 branch
BranchTrueFalseTotal
if @{$data;} < 2 0 5 5
198  
199 5 ▶ for(my $i=0; $i<=$#{$data}; ++$i)
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$data;}) 50 5 55
200 {
201 50 my $this = $data->[$i];
202 50 ▶ my $prev = $i > 0 ? $data->[$i-1] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$i > 0 ? : 45 5 50
203 50 ▶ my $next = $i < $#{$data} ? $data->[$i+1] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$i < $#{$data;} ? : 45 5 50
204 50 my @point = ( $this->[$xi] );
205 # empty line, treat separate pieces
206 50 for ($prev, $this, $next)
207 {
208 150 ▶ $_ = undef if(defined $_ and not @{$_});
Branch: 1 branch
BranchTrueFalseTotal
if defined $_ and not @{$_;} 0 150 150
Condition66%2/3
defined $_ and not @{$_;}
!ll&&!rl&&r
101400
MC/DC0%0/2
defined $_ and not @{$_;}
defined $_ not @{$_;}
Truth table66%2/3
A and not(B)
A= defined $_B= not @{$_;}
ABresult
0X0
100
111
209 }
210 50 ▶ unless(defined $this)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this) 0 50 50
211 {
212 0 push(@diff, []);
213 0 next;
214 }
215 50 ▶ my $v = defined $prev ? $this->[$xi] - $prev->[$xi] : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $prev ? : 45 5 50
216 50 ▶ my $w = defined $next ? $next->[$xi] - $this->[$xi] : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $next ? : 45 5 50
217  
218 50 ▶ if(not defined $prev)
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $prev) { } 5 45 50
elsif (not defined $next) { } 5 40 45
219 {
220 # f'(x) = (f(x+w)-f(x)) / w
221 5 my $invdx = 1./$w;
222 5 for(@{$yi})
223 {
224 5 push(@point, $invdx*($next->[$_]-$this->[$_]));
225 }
226 }
227 elsif(not defined $next)
228 {
229 # f'(x) = (f(x+w)-f(x)) / w
230 5 my $invdx = 1./$v;
231 5 for(@{$yi})
232 {
233 5 push(@point, $invdx*($this->[$_]-$prev->[$_]));
234 }
235 }
236 else
237 {
238 40 my $wbyv = $w/$v;
239 40 my $invdx = 1./($w*(1+$wbyv));
240 40 my $thisweight = 1-$wbyv**2;
241 40 my $prevweight = $wbyv**2;
242 40 for(@{$yi})
243 {
244 40 push(@point, $invdx*($next->[$_] - $thisweight*$this->[$_] - $prevweight*$prev->[$_]));
245 }
246 }
247 50 push(@diff, \@point);
248 }
249 5 return \@diff;
250 }
251  
252 sub derivtitle
253 {
254 8 ▶ my $ot = shift;
Subroutine derivtitle: called CC=2 SCAR=6.9
Pod undocumented
255 8 my $col = shift;
256 8 ▶ return 'd('.(defined $ot ? $ot : "col$col").')';
Branch: 1 branch
BranchTrueFalseTotal
defined $ot ? : 5 3 8
257 }
« blib/lib/Text/NumericData/App.pm ↑ Summary blib/lib/Text/NumericData/Calc.pm »