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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//! The floating-point filter of a predicate.
//!
//! The emitted `filter` evaluates the difference form in `f64` and returns
//! its sign when the error bound derived by [`crate::filter_bound`] proves
//! it. It computes, in this order:
//!
//! - each leaf, `p[i][axis] - p[j][axis]`, once;
//! - each node of the difference form, one statement per node in the order
//! of the graph, with the operation the node names: no reassociation, and
//! a negation as a unary minus, which is exact;
//! - `delta`, the largest magnitude of the leaves, by comparisons, which are
//! exact;
//! - `d = delta + e` and `bound = (EPSILON * d + KAPPA * e) * d * ... * d`,
//! with `DEGREE - 1` multiplications by `d`, from left to right;
//! - the decision `e >= 0`, `D_MIN <= d <= D_MAX` and `|det| > bound`,
//! combined with a non-short-circuiting `&`; the sign is then chosen by a
//! comparison, not a branch on the decision.
//!
//! # Why the decision is sound
//!
//! Let `B = (epsilon D + kappa E) D^(k - 1)` be the exact bound, for the
//! exact `D = delta + E` and the exact coefficients. The value of the form at
//! the exact points differs from `det` by at most `(1 + 2^-32) B` whenever
//! `D` is in `[D_MIN, D_MAX]` (see [`crate::FilterBound::constants`]).
//!
//! The emitted bound is computed in `f64` with round-to-nearest, and each of
//! its operations can round down by a factor `1 - u`, `u = 2^-53`. For a
//! degree `k` the operations are `delta + e`, `EPSILON * d`, `KAPPA * e`,
//! their sum and the `k - 1` products by `d`: `k + 3` in all. The rounding
//! of `d` enters the bound once for each of the `k` times `d` is used, so
//! the computed bound is at least `(1 - u)^(2k + 2)` times its exact value:
//! one factor per operation and `k - 1` more for the uses of `d` beyond the
//! first. The caller's sum that forms `e` adds none, because `e` must be
//! rounded up, so that it is at least `E`. That is within the `2k + 8`
//! factors that the constants allow for: `EPSILON` and `KAPPA` are the
//! exact coefficients times `1 + 2^-30`, rounded up, and
//! `(1 + 2^-30)(1 - u)^(2k + 8) >= 1 + 2^-31` for every degree the
//! generator accepts, so the computed bound is at least `(1 + 2^-31) B`,
//! above the error. The partial results are formed from the largest one
//! down, so for `D >= D_MIN` none of them leaves the normal range on the way
//! (the term `KAPPA * e` may underflow, which loses at most `2^-1075`, a
//! fraction `2^-54` of the sum it joins and within the same allowance).
//!
//! `d` is compared with `D_MIN` and `D_MAX` after rounding to nearest, which
//! can make it larger or smaller than the exact `D`, by a factor `1 + u` at
//! most. That is harmless, because both conditions have slack. A `D` just
//! below `D_MIN` that rounds up to it raises the underflow allowance, at
//! most `2^-32` of the bound at `D_MIN`, by a factor `(1 + u)^k`, far within
//! the margin `2^-31` of the constants, and keeps the bound normal, since
//! at `D_MIN` the term `epsilon D^k` is at least `2^-1021`, twice the
//! smallest normal. A `D` just above
//! `D_MAX` that rounds down to it keeps every computed value below
//! `2^1022 (1 + u)^k`, still below the overflow threshold `2^1024`, since
//! `D_MAX` asks for `2^1022`. With `e = 0`, `d = delta` is exact.
//!
//! # The contract of `e`
//!
//! `e` is the sum of the two largest per-point errors, where the error of a
//! point is an upper bound on the absolute error of each coordinate of its
//! approximation, as an approximation of the exact point. It must be
//! finite, non-negative and rounded upward, so that it is at least the
//! exact sum `E`; it is `0.0` for explicit points. The bound needs
//! `E >= 0`: a negative `e` shrinks it, so the filter only decides when
//! `e >= 0`, a comparison that is also false for a NaN `e`. A NaN or
//! negative `e` therefore never decides, and an infinite one makes `d`
//! infinite, which never decides either.
//! Non-finite inputs never decide: a NaN leaf makes `det` NaN, an infinite
//! leaf makes `delta` and `d` infinite, an infinite `e` makes `d` infinite,
//! and every comparison with NaN is false. A decision implies
//! `|det| > bound > 0`, so the filter never returns zero.
use Write;
use comment;
use ;
use crate;
use crateNodeId;
/// The number of `f64` operations in [`bound_statements`] for a filter of
/// degree `degree`: `d`, `EPSILON * d`, `KAPPA * e`, their sum, and the
/// `degree - 1` products by `d`.
pub
/// The number of factors `1 - u` by which the rounding can shrink the
/// emitted bound of a filter of degree `degree`: one per operation, and one
/// more for each use of `d` beyond the first, since the rounding of `d`
/// enters the bound each time `d` is used.
pub
/// The statements that compute `d` and `bound`.
pub
/// Writes the `filter` function of a difference form with `points` points,
/// and tells whether it uses the helper of [`write_max`]. Its documentation
/// states the contract of `e` given in the module documentation.
pub
/// Writes the helper `max` that the filter uses.
pub