0
|
1 |
#======================================================================
|
|
2 |
# P O L I N T . P L
|
|
3 |
# doc: Thu Nov 23 20:38:46 2000
|
|
4 |
# dlm: Tue Aug 5 14:06:31 2008
|
|
5 |
# (c) 2000 A.M. Thurnherr
|
|
6 |
# uE-Info: 17 0 NIL 0 0 72 2 2 4 NIL ofnI
|
|
7 |
#======================================================================
|
|
8 |
|
|
9 |
# 2nd edition NR polint.c adapted to ANTS
|
|
10 |
|
|
11 |
# HISTORY:
|
|
12 |
# Nov 23, 2000: - created for [.interp.poly]
|
|
13 |
# Jan 12, 2006: - BUG: higher-order polynomials could not be used
|
|
14 |
# to interpolate linear function
|
|
15 |
# Jul 1, 2006: - Version 3.3 [HISTORY]
|
|
16 |
# Jul 28, 2006: - cosmetics
|
|
17 |
# Aug 5, 2008: - BUG: [.interp.poly] takes data from ref, not @ants_
|
|
18 |
|
|
19 |
# NOTES:
|
|
20 |
# - &vector()-allocated arrays are numbered from 1
|
|
21 |
# - (nan,nan) is returned on non-numeric required @ants_ values
|
|
22 |
# - in contrast to the NR routine, the error value returned is +ve
|
|
23 |
|
|
24 |
require "$ANTS/nrutil.pl";
|
|
25 |
|
|
26 |
sub polint($$$$$$) # ($y,$dy) = &polint(...)
|
|
27 |
{
|
|
28 |
my($dR,$xf,$xv,$ti,$n,$yf) = @_;
|
|
29 |
my($y,$dy);
|
|
30 |
|
|
31 |
my($i,$m); my($ns) = 1;
|
|
32 |
my($den,$dif,$dift,$ho,$hp,$w);
|
|
33 |
my(@c,@d);
|
|
34 |
|
|
35 |
for ($i=0; $i<$n; $i++) { # check for nans
|
|
36 |
return (nan,nan)
|
|
37 |
unless (numberp($dR->[$ti+$i][$xf]) &&
|
|
38 |
numberp($dR->[$ti+$i][$yf]));
|
|
39 |
}
|
|
40 |
|
|
41 |
$dif = abs($xv - $dR->[$ti][$xf]);
|
|
42 |
&vector(\@c,1,$n);
|
|
43 |
&vector(\@d,1,$n);
|
|
44 |
for ($i=1; $i<=$n; $i++) {
|
|
45 |
$dift = abs($xv - $dR->[$ti+$i-1][$xf]);
|
|
46 |
if ($dift < $dif) {
|
|
47 |
$ns = $i;
|
|
48 |
$dif = $dift;
|
|
49 |
}
|
|
50 |
$c[$i] = $dR->[$ti+$i-1][$yf];
|
|
51 |
$d[$i] = $dR->[$ti+$i-1][$yf];
|
|
52 |
}
|
|
53 |
$y = $dR->[$ti+$ns---1][$yf]; # WHAT A CONSTRUCT :-)
|
|
54 |
for ($m=1; $m<$n; $m++) {
|
|
55 |
for ($i=1; $i<=$n-$m; $i++) {
|
|
56 |
$ho = $dR->[$ti+$i-1][$xf] - $xv;
|
|
57 |
$hp = $dR->[$ti+$i+$m-1][$xf] - $xv;
|
|
58 |
$w = $c[$i+1] - $d[$i];
|
|
59 |
$den = $ho - $hp;
|
|
60 |
### The following two lines of code are the original, which makes polint
|
|
61 |
### fail when interpolating a linear function with a higher-order polynomial,
|
|
62 |
### as is done in [ubtest/resample.TF].
|
|
63 |
### croak("$0 (polint.pl): ERROR!") if ($den == 0);
|
|
64 |
### $den = $w / $den;
|
|
65 |
### The following line of code is the replacement that solves the bug.
|
|
66 |
$den = $w / $den unless ($den == 0);
|
|
67 |
$d[$i] = $hp * $den;
|
|
68 |
$c[$i] = $ho * $den;
|
|
69 |
}
|
|
70 |
$dy = (2*$ns < ($n-$m)) ? $c[$ns+1] : $d[$ns--];
|
|
71 |
$y += $dy;
|
|
72 |
}
|
|
73 |
return ($y,abs($dy));
|
|
74 |
}
|
|
75 |
|
|
76 |
1;
|