1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//! aCalc's DERIV and NDERIV (`calcUtil.c:27-74`).
//!
//! Both are the SAME kernel — `deriv()` is literally `nderiv(..., npts=2, ...)`
//! (`:70-74`) — and that kernel is a **sliding QUADRATIC fit**, not a difference
//! formula and not a linear regression:
//!
//! ```c
//! m = 2*npts+1;
//! e = fitpoly(x, y, m, &c, &b, &a, NULL); /* y = c + b*x + a*x*x */
//! for (j=0; j<m/2+1; j++) d[j] = b + 2*a*x[j]; /* dy/dx = b + 2*a*x */
//! ```
//!
//! The port had a central difference for DERIV and a least-squares LINE for NDERIV,
//! and read NDERIV's argument as a total window width. It is points PER SIDE:
//! `NDERIV(y,1)` fits 3 points, `NDERIV(y,2)` fits 5 — and `NDERIV(y,2)` is exactly
//! `DERIV(y)`.
use cratefitting;
/// C `deriv` (`calcUtil.c:70-74`) — `nderiv` with `npts = 2`, i.e. a five-point
/// sliding quadratic fit. Compiled C confirms the identity: for AA=[0,1,10,3,4,5,6,7,20],
/// `DERIV(AA)` and `NDERIV(AA,2)` are the same nine numbers.
/// C `nderiv(x, y, n, d, npts, lx)` (`calcUtil.c:27-68`). `npts` is the number of
/// points on EACH SIDE, so the fit width is `m = 2*npts+1`.
///
/// Three blocks, each a quadratic fit whose derivative `b + 2*a*x` is evaluated at
/// the points it covers:
///
/// * the first `m/2+1` points share ONE fit over `y[0..m]`;
/// * each middle point gets its own fit over the `m` points centred on it;
/// * the last `m/2+1` points share ONE fit over `y[n-m..n]`.
///
/// `x` is the ELEMENT INDEX in every aCalc caller, which is why C's mixed use of the
/// global `x[]` in the first and last blocks and the local `lx[]` in the middle one
/// makes no difference: a segment of the index array, rebased to its own start, is
/// again `0, 1, 2, ...`.
///
/// `None` is C's `return(e)` — the fit failed, which for `m < 3` is `fitpoly`'s
/// `n < 3` rejection (`calcUtil.c:270`). Compiled C: `NDERIV(AA,0)` is status -1.
///
/// # The clamp, and the one deliberate deviation
///
/// C clamps `npts` to half the window — `j = myMIN((lastEl-firstEl)/2, j)`,
/// `aCalcPerform.c:601` — but only in the NDERIV opcode, not in `nderiv` itself, and
/// `deriv()` passes its fixed `npts = 2` straight through. So C's DERIV on a window
/// of fewer than 5 points fits `m = 5` points out of `n < 5`: it reads past the
/// window, and its last block indexes `x[n-m]` with `n-m` NEGATIVE — before the
/// start of the array. That is C undefined behaviour, and the compiled reference
/// duly answers garbage (`DERIV(AA[0,3])` -> [7.15, -0.23, -3.92, 7.15]).
///
/// The clamp therefore lives HERE, in the kernel, where no caller can bypass it:
/// DERIV of a short window becomes NDERIV of that window with the same clamp, which
/// is the answer C's own NDERIV gives. For windows of 5 points or more — every case
/// C defines — the clamp is a no-op and the two agree exactly.