0
|
1 |
#!/usr/bin/perl
|
|
2 |
#======================================================================
|
|
3 |
# A N T S I N T E G R A T E . P L
|
|
4 |
# doc: Fri Feb 28 22:54:04 1997
|
|
5 |
# dlm: Fri Oct 15 23:14:11 2010
|
|
6 |
# (c) 1997 Andreas Thurnherr
|
|
7 |
# uE-Info: 27 62 NIL 0 0 72 2 2 4 NIL ofnI
|
|
8 |
#======================================================================
|
|
9 |
|
|
10 |
# Integrator Library; used by [integrate] and [transport]
|
|
11 |
# ([transport] is deprecated...)
|
|
12 |
|
|
13 |
# HISTORY:
|
|
14 |
# Nov 14, 2000: - divorced from [integrate]
|
|
15 |
# Nov 16, 2000: - added $opt_i
|
|
16 |
# - BUG: $opt_c forgot to output last record
|
|
17 |
# - BUG: copying of buffer had not worked
|
|
18 |
# Nov 17, 2000: - changed $opt_i to $opt_l
|
|
19 |
# Nov 24, 2000: - aimless changes
|
|
20 |
# Mar 10, 2002: - added $dx output if $dfnr defined
|
|
21 |
# Mar 30, 2002: - cosmetics
|
|
22 |
# May 24, 2002: - added -b)ox to -f
|
|
23 |
# May 25, 2002: - BUG: -c did not output records with missing y values
|
|
24 |
# - changed to dying on missing x values
|
|
25 |
# Dec 20, 2005: - BUG: -cf produced short 1st record (dfnr not set)
|
|
26 |
# Jul 1, 2006: - Version 3.3 [HISTORY]
|
|
27 |
# Oct 15, 2010: - removed warning about integrand set to nan
|
|
28 |
|
|
29 |
# NOTES:
|
|
30 |
#
|
|
31 |
# &integrate() can run in several moments:
|
|
32 |
# 0: integral
|
|
33 |
# 1: integral weighted by (signed) distance from zero
|
|
34 |
# 2: integral weighted by square of distance from zero
|
|
35 |
#
|
|
36 |
# &integrate() returns the single-value sum; without -f @sum is
|
|
37 |
# set as a side-effect
|
|
38 |
#
|
|
39 |
# unless xmin,xmax are given as args, the integral is taken over the
|
|
40 |
# entire data
|
|
41 |
#
|
|
42 |
# records with missing y values have the integrand(s) set to NaN
|
|
43 |
#
|
|
44 |
# because this routine was ones an integral part of [integrate] it
|
|
45 |
# uses a number of global variables (ug-lee!):
|
|
46 |
# $xfnr y = f(x)
|
|
47 |
# $opt_c output sum after each step (cummulative)
|
|
48 |
# $opt_f integrate only given field
|
|
49 |
# $yfnr (if $opt_f) ... this one
|
|
50 |
# $opt_b use box rule
|
|
51 |
# $iScale (if $opt_f) scale integrated values
|
|
52 |
# $opt_l (if $opt_f) output individual summands
|
|
53 |
|
|
54 |
sub integrate ($@) # ret integral, set $m
|
|
55 |
{
|
|
56 |
my($moment,$zero,$xmin,$xmax) = @_; # optional args
|
|
57 |
my($lastx,$cur,$curwt,$sum,$cury);
|
|
58 |
my($dx,$r,$f,@out,@nan,$warned);
|
|
59 |
|
|
60 |
for ($f=0; $f<$antsBufNFields; $f++) { # prep nan output
|
|
61 |
$nan[$f] = nan;
|
|
62 |
}
|
|
63 |
|
|
64 |
for ($r=0; $r<=$#ants_; $r++) {
|
|
65 |
croak("$0: can't handle non-numeric x values\n")
|
|
66 |
unless (numberp($ants_[$r][$xfnr]));
|
|
67 |
|
|
68 |
# 1st, find a valid x value, and a y value on -f (one field only).
|
|
69 |
# On -c, generate output: 0 on open limits, nan (only possible
|
|
70 |
# if -f is set) otherwise
|
|
71 |
|
|
72 |
unless (defined($lastx)) {
|
|
73 |
if (defined($opt_f)) {
|
|
74 |
if ($opt_c) {
|
|
75 |
@out = @{$ants_[$r]};
|
|
76 |
$out[$yfnr] = numberp($ants_[$r][$yfnr]) ? 0 : nan;
|
|
77 |
$out[$dfnr] = nan if (defined($dfnr));
|
|
78 |
&antsOut(@out);
|
|
79 |
}
|
|
80 |
next unless (numberp($ants_[$r][$yfnr]));
|
|
81 |
$lastx = $ants_[$r][$xfnr];
|
|
82 |
croak("$0: lower limit ($xmin) < first valid x-value ($lastx)\n")
|
|
83 |
if (defined($xmin) && $lastx > $xmin);
|
|
84 |
$lasty = $ants_[$r][$yfnr];
|
|
85 |
next;
|
|
86 |
}
|
|
87 |
$lastx = $ants_[$r][$xfnr];
|
|
88 |
if ($opt_c) {
|
|
89 |
for ($f=0; $f<$antsBufNFields; $f++) {
|
|
90 |
$out[$f] = ($f == $xfnr) ? $ants_[$r][$f] :
|
|
91 |
(numberp($ants_[$r][$f]) ? 0 : nan);
|
|
92 |
}
|
|
93 |
&antsOut(@out);
|
|
94 |
}
|
|
95 |
next;
|
|
96 |
}
|
|
97 |
|
|
98 |
# next, update x&y while below lower limit for later interpolation
|
|
99 |
# NB: only possible on -f!
|
|
100 |
|
|
101 |
if (defined($xmin) && $ants_[$r][$xfnr] < $xmin) {
|
|
102 |
if (numberp($ants_[$r][$yfnr])) {
|
|
103 |
$lastx = $ants_[$r][$xfnr];
|
|
104 |
$lasty = $ants_[$r][$yfnr];
|
|
105 |
}
|
|
106 |
if ($opt_c) {
|
|
107 |
$ants_[$r][$yfnr] = nan;
|
|
108 |
&antsOut(@{$ants_[$r]});
|
|
109 |
}
|
|
110 |
next;
|
|
111 |
}
|
|
112 |
|
|
113 |
# we have an x-value > min; is it valid?
|
|
114 |
# NB: xmin is undefined once lower limit is handled
|
|
115 |
|
|
116 |
croak("$0: Error: no data within integration limits\n")
|
|
117 |
if (defined($xmin) && defined($xmax) && $ants_[$r][$xfnr] > $xmax);
|
|
118 |
|
|
119 |
# finally! we have a valid x-value; if it's the first, interpolate
|
|
120 |
# y at xmin if that's defined
|
|
121 |
# undefined xmin at end so this code is not executed again
|
|
122 |
# NB: xmin can only be defined on -f!
|
|
123 |
|
|
124 |
if (defined($xmin)) {
|
|
125 |
unless (numberp($ants_[$r][$yfnr])) {
|
|
126 |
if ($opt_c) {
|
|
127 |
$ants_[$r][$yfnr] = nan;
|
|
128 |
&antsOut(@{$ants_[$r]});
|
|
129 |
}
|
|
130 |
next;
|
|
131 |
}
|
|
132 |
$lasty += ($xmin-$lastx) / ($ants_[$r][$xfnr]-$lastx)
|
|
133 |
* ($ants_[$r][$yfnr]-$lasty);
|
|
134 |
$lastx = $xmin; undef($xmin);
|
|
135 |
}
|
|
136 |
|
|
137 |
# it is also possible (though not on the first time round), that we
|
|
138 |
# have just passed the upper limit (xmax); simulate normal code but
|
|
139 |
# using xmax (and interpolated y value) instead of real data
|
|
140 |
# NB: xmax can only be defined on -f!
|
|
141 |
|
|
142 |
if (defined($xmax) && $ants_[$r][$xfnr] >= $xmax) {
|
|
143 |
unless (numberp($ants_[$r][$yfnr])) {
|
|
144 |
if ($opt_c) {
|
|
145 |
$ants_[$r][$yfnr] = nan;
|
|
146 |
&antsOut(@{$ants_[$r]});
|
|
147 |
}
|
|
148 |
next;
|
|
149 |
}
|
|
150 |
$dx = $xmax - $lastx;
|
|
151 |
$cury = $lasty + $dx / ($ants_[$r][$xfnr]-$lastx)
|
|
152 |
* ($ants_[$r][$yfnr]-$lasty);
|
|
153 |
croak("$0: x-field must be monotonically increasing [xmax=$xmax, lastx=$lastx]\n")
|
|
154 |
if ($dx < 0);
|
|
155 |
$cur = ($cury+$lasty)/2 * $dx;
|
|
156 |
$cur *= (($xmax+$lastx)/2-$zero)**$moment if ($moment);
|
|
157 |
$sum += $cur*$iScale;
|
|
158 |
if ($opt_c) { # cummulative
|
|
159 |
@out = @{$ants_[$r]}; # copy everything
|
|
160 |
$out[$yfnr] = $sum;
|
|
161 |
$out[$dfnr] = $dx if (defined($dfnr));
|
|
162 |
&antsOut(@out);
|
|
163 |
} elsif ($opt_l) { # individual summands
|
|
164 |
@out = @{$ants_[$r]}; # copy everything
|
|
165 |
$out[$yfnr] = $cur*$iScale;
|
|
166 |
$out[$dfnr] = $dx if (defined($dfnr));
|
|
167 |
&antsOut(@out);
|
|
168 |
}
|
|
169 |
$lastx = $ants_[$r][$xfnr];
|
|
170 |
last;
|
|
171 |
}
|
|
172 |
|
|
173 |
# phoar! we are finally handling the normal case
|
|
174 |
|
|
175 |
$dx = $ants_[$r][$xfnr] - $lastx; # calc dx
|
|
176 |
croak("$0: x-field must be monotonically increasing [curx=$ants_[$r][$xfnr], lastx=$lastx]\n")
|
|
177 |
if ($dx < 0);
|
|
178 |
|
|
179 |
if (defined($opt_f)) { # integrate single field
|
|
180 |
unless (numberp($ants_[$r][$yfnr])) {
|
|
181 |
if ($opt_c) {
|
|
182 |
$ants_[$r][$yfnr] = nan;
|
|
183 |
&antsOut(@{$ants_[$r]});
|
|
184 |
}
|
|
185 |
next;
|
|
186 |
}
|
|
187 |
$cur = $opt_b ?
|
|
188 |
$ants_[$r][$yfnr] * $dx : # box rule
|
|
189 |
($ants_[$r][$yfnr]+$lasty)/2 * $dx; # interpolate
|
|
190 |
$cur *= (($ants_[$r][$xfnr]+$lastx)/2-$zero)**$moment
|
|
191 |
if ($moment);
|
|
192 |
$sum += $cur*$iScale;
|
|
193 |
$lasty = $ants_[$r][$yfnr];
|
|
194 |
if ($opt_c) { # cummulative
|
|
195 |
@out = @{$ants_[$r]}; # copy everything
|
|
196 |
$out[$yfnr] = $sum;
|
|
197 |
} elsif ($opt_l) { # individual summands
|
|
198 |
@out = @{$ants_[$r]}; # copy everything
|
|
199 |
$out[$yfnr] = $cur*$iScale;
|
|
200 |
}
|
|
201 |
} else { # integrate all
|
|
202 |
for ($f=0; $f<$antsBufNFields; $f++) {
|
|
203 |
next if ($f == $xfnr); # except x-field
|
|
204 |
if (numberp($ants_[$r][$f])) { # val found
|
|
205 |
$sum[$f] += $ants_[$r][$f] * $dx # box-rule (no interp)
|
|
206 |
unless isnan($sum[$f]); # had missing vals
|
|
207 |
} else { # val missing
|
|
208 |
$sum[$f] = nan; # mark for later
|
|
209 |
# unless ($warned) { # warn user
|
|
210 |
# &antsInfo("Warning: integrand(s) set to nan due to missing vals");
|
|
211 |
# $warned = 1;
|
|
212 |
# }
|
|
213 |
}
|
|
214 |
$out[$f] = $sum[$f] if ($opt_c);
|
|
215 |
}
|
|
216 |
}
|
|
217 |
if ($opt_c || $opt_l) {
|
|
218 |
$out[$xfnr] = $ants_[$r][$xfnr];
|
|
219 |
$out[$dfnr] = $dx if (defined($dfnr));
|
|
220 |
&antsOut(@out);
|
|
221 |
}
|
|
222 |
$lastx = $ants_[$r][$xfnr]; # last good x
|
|
223 |
}
|
|
224 |
croak("$0: empty input!\n") # never initialized
|
|
225 |
unless (defined($lastx));
|
|
226 |
croak("$0: upper limit > last valid x-value!\n")
|
|
227 |
if (defined($xmax) && $xmax>$lastx);
|
|
228 |
|
|
229 |
return $sum;
|
|
230 |
}
|
|
231 |
|
|
232 |
1;
|