@topolab/tda 0.0.0-stage → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +106 -0
- package/CITATION.cff +72 -0
- package/LICENSE +21 -0
- package/README.md +100 -2
- package/SECURITY.md +34 -0
- package/THIRD_PARTY_LICENSES.md +697 -0
- package/dist/arrangement.d.ts +50 -0
- package/dist/arrangement.d.ts.map +1 -0
- package/dist/arrangement.js +63 -0
- package/dist/arrangement.js.map +1 -0
- package/dist/build_info.json +12 -0
- package/dist/check.d.ts +118 -0
- package/dist/check.d.ts.map +1 -0
- package/dist/check.js +298 -0
- package/dist/check.js.map +1 -0
- package/dist/complex.d.ts +543 -0
- package/dist/complex.d.ts.map +1 -0
- package/dist/complex.js +712 -0
- package/dist/complex.js.map +1 -0
- package/dist/diagram.d.ts +116 -0
- package/dist/diagram.d.ts.map +1 -0
- package/dist/diagram.js +238 -0
- package/dist/diagram.js.map +1 -0
- package/dist/diagram_of.d.ts +17 -0
- package/dist/diagram_of.d.ts.map +1 -0
- package/dist/diagram_of.js +40 -0
- package/dist/diagram_of.js.map +1 -0
- package/dist/experimental.d.ts +18 -0
- package/dist/experimental.d.ts.map +1 -0
- package/dist/experimental.js +16 -0
- package/dist/experimental.js.map +1 -0
- package/dist/flat.d.ts +64 -0
- package/dist/flat.d.ts.map +1 -0
- package/dist/flat.js +158 -0
- package/dist/flat.js.map +1 -0
- package/dist/index.d.ts +42 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +31 -0
- package/dist/index.js.map +1 -0
- package/dist/ladder/tda_ladder.d.ts +223 -0
- package/dist/ladder/tda_ladder.js +1360 -0
- package/dist/ladder/tda_ladder_bg.wasm +0 -0
- package/dist/ladder/tda_ladder_bg.wasm.d.ts +43 -0
- package/dist/ladder.d.ts +306 -0
- package/dist/ladder.d.ts.map +1 -0
- package/dist/ladder.js +607 -0
- package/dist/ladder.js.map +1 -0
- package/dist/load.d.ts +53 -0
- package/dist/load.d.ts.map +1 -0
- package/dist/load.js +173 -0
- package/dist/load.js.map +1 -0
- package/dist/memory.d.ts +58 -0
- package/dist/memory.d.ts.map +1 -0
- package/dist/memory.js +2 -0
- package/dist/memory.js.map +1 -0
- package/dist/persistence.d.ts +349 -0
- package/dist/persistence.d.ts.map +1 -0
- package/dist/persistence.js +478 -0
- package/dist/persistence.js.map +1 -0
- package/dist/pkg/tda_wasm.d.ts +13 -0
- package/dist/pkg/tda_wasm.js +2528 -0
- package/dist/prepared.d.ts +62 -0
- package/dist/prepared.d.ts.map +1 -0
- package/dist/prepared.js +81 -0
- package/dist/prepared.js.map +1 -0
- package/dist/rivet_input.d.ts +93 -0
- package/dist/rivet_input.d.ts.map +1 -0
- package/dist/rivet_input.js +132 -0
- package/dist/rivet_input.js.map +1 -0
- package/dist/source.d.ts +43 -0
- package/dist/source.d.ts.map +1 -0
- package/dist/source.js +99 -0
- package/dist/source.js.map +1 -0
- package/dist/tda.wasm +0 -0
- package/dist/two_parameter.d.ts +243 -0
- package/dist/two_parameter.d.ts.map +1 -0
- package/dist/two_parameter.js +275 -0
- package/dist/two_parameter.js.map +1 -0
- package/dist/types.d.ts +146 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/vineyard.d.ts +278 -0
- package/dist/vineyard.d.ts.map +1 -0
- package/dist/vineyard.js +236 -0
- package/dist/vineyard.js.map +1 -0
- package/package.json +77 -4
- package/src/arrangement.ts +96 -0
- package/src/check.ts +338 -0
- package/src/complex.ts +1179 -0
- package/src/diagram.ts +299 -0
- package/src/diagram_of.ts +40 -0
- package/src/experimental.ts +17 -0
- package/src/flat.ts +204 -0
- package/src/index.ts +181 -0
- package/src/ladder.ts +903 -0
- package/src/load.ts +201 -0
- package/src/memory.ts +58 -0
- package/src/persistence.ts +752 -0
- package/src/prepared.ts +111 -0
- package/src/rivet_input.ts +195 -0
- package/src/source.ts +132 -0
- package/src/two_parameter.ts +480 -0
- package/src/types.ts +163 -0
- package/src/vineyard.ts +463 -0
package/dist/complex.js
ADDED
|
@@ -0,0 +1,712 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The filtered complexes, the shapes some of them are built from, and the
|
|
3
|
+
* persistence of each builder's own complex in one call (internal module;
|
|
4
|
+
* the package root re-exports the API).
|
|
5
|
+
*/
|
|
6
|
+
import { call, module } from './load.js';
|
|
7
|
+
import { allFinite, boolOption, f64, noNaN, finite, int, intOption, isTyped, kind, nonNegative, num, options, points as checkPoints, positive, sized, u32 } from './check.js';
|
|
8
|
+
import { diagramOf } from './diagram_of.js';
|
|
9
|
+
import { coeff as checkCoeff, flatTolerance as checkFlatTolerance, minPersistence as checkMinPersistence } from './check.js';
|
|
10
|
+
/** A builder's typed arrays as a {@link Complex} on `scale`. */
|
|
11
|
+
function complexOf(raw, scale) {
|
|
12
|
+
return { vertices: raw.vertices, offsets: raw.offsets, filtrations: raw.filtrations, scale };
|
|
13
|
+
}
|
|
14
|
+
/** The option keys of {@link BuilderPersistenceOptions}. */
|
|
15
|
+
const FUSED_KEYS = { coeff: true, complex: true, minPersistence: true };
|
|
16
|
+
function fused(o) {
|
|
17
|
+
return {
|
|
18
|
+
coeff: checkCoeff(o.coeff),
|
|
19
|
+
complex: boolOption(o.complex, 'complex', false),
|
|
20
|
+
minPersistence: checkMinPersistence(o.minPersistence)
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* The result of a pipeline call (`{ pairs, simplices?, empty?, … }`) as a
|
|
25
|
+
* diagram with homology up to `top` (without the pairs `minPersistence`
|
|
26
|
+
* leaves out), and the complex (built by `shape`) when asked for.
|
|
27
|
+
*/
|
|
28
|
+
function persistenceOf(raw, scale, f, top, withComplex, shape) {
|
|
29
|
+
const diagram = diagramOf(raw.empty ? new Float64Array(0) : raw.pairs, top, scale, f.coeff, f.minPersistence);
|
|
30
|
+
if (!withComplex)
|
|
31
|
+
return diagram;
|
|
32
|
+
return { ...diagram, complex: shape(Object.assign({}, raw, raw.simplices ?? emptyComplex())) };
|
|
33
|
+
}
|
|
34
|
+
function emptyComplex() {
|
|
35
|
+
return { vertices: new Int32Array(0), offsets: Uint32Array.of(0), filtrations: new Float64Array(0) };
|
|
36
|
+
}
|
|
37
|
+
/** A non-negative integer that caps the simplex dimension, with a default. */
|
|
38
|
+
function maxSimplexDim(v, fallback, max = 0x7fffffff) {
|
|
39
|
+
return intOption(v, 'maxSimplexDim', fallback, 0, max);
|
|
40
|
+
}
|
|
41
|
+
// ---------------------------------------------------------------------------
|
|
42
|
+
// Rips
|
|
43
|
+
// ---------------------------------------------------------------------------
|
|
44
|
+
/** The number of points of a distance matrix, after its checks. */
|
|
45
|
+
export function distanceMatrix(v, name) {
|
|
46
|
+
const { distances } = v;
|
|
47
|
+
if (!isTyped(distances, 'Float64Array')) {
|
|
48
|
+
throw new TypeError(`${name}.distances must be a Float64Array, got ${kind(distances)}`);
|
|
49
|
+
}
|
|
50
|
+
const n = Math.round(Math.sqrt(distances.length));
|
|
51
|
+
if (distances.length === 0 || n * n !== distances.length) {
|
|
52
|
+
throw new RangeError(`${name}.distances must hold n × n entries for a positive integer n, got length ${distances.length}`);
|
|
53
|
+
}
|
|
54
|
+
noNaN(distances, `${name}.distances`);
|
|
55
|
+
// The strict lower triangle is what the builders read; the other entries
|
|
56
|
+
// are checked after it, so that no entry of the matrix is negative.
|
|
57
|
+
const sign = (i, j) => {
|
|
58
|
+
const x = distances[i * n + j];
|
|
59
|
+
if (!(x >= 0)) {
|
|
60
|
+
throw new RangeError(`${name}.distances[${i * n + j}] (row ${i}, column ${j}) must be non-negative or Infinity, got ${x}`);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
for (let i = 1; i < n; i++)
|
|
64
|
+
for (let j = 0; j < i; j++)
|
|
65
|
+
sign(i, j);
|
|
66
|
+
for (let i = 0; i < n; i++)
|
|
67
|
+
for (let j = i; j < n; j++)
|
|
68
|
+
sign(i, j);
|
|
69
|
+
return n;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Whether the input of a Rips function is a distance matrix, after the type
|
|
73
|
+
* check of either form. A key whose value is `undefined` counts as absent.
|
|
74
|
+
*/
|
|
75
|
+
export function isMatrix(v, name) {
|
|
76
|
+
const object = v !== null && typeof v === 'object';
|
|
77
|
+
const coords = object && v.coords !== undefined;
|
|
78
|
+
const distances = object && v.distances !== undefined;
|
|
79
|
+
if (coords && distances) {
|
|
80
|
+
throw new TypeError(`${name} must be a point cloud { coords, d } or a distance matrix { distances }, not both`);
|
|
81
|
+
}
|
|
82
|
+
if (distances)
|
|
83
|
+
return true;
|
|
84
|
+
if (!coords) {
|
|
85
|
+
throw new TypeError(`${name} must be a point cloud { coords: Float64Array, d: number } or a distance matrix { distances: Float64Array }, got ${kind(v)}`);
|
|
86
|
+
}
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
const RIPS_COMPLEX_KEYS = { maxEdgeLength: true, maxSimplexDim: true };
|
|
90
|
+
/**
|
|
91
|
+
* The Vietoris–Rips complex of a point cloud or of a distance matrix: every
|
|
92
|
+
* point a vertex at 0, every pair at distance `<= maxEdgeLength` an edge at
|
|
93
|
+
* that distance, every clique of up to `maxSimplexDim + 1` points a simplex
|
|
94
|
+
* at its longest edge. Built by the Rust core (clique enumeration over a
|
|
95
|
+
* sorted neighbour graph); distances in double precision in GUDHI's order,
|
|
96
|
+
* so the values are GUDHI's `Rips_complex`'s, bit for bit. A distance matrix
|
|
97
|
+
* is read in its strict lower triangle, as GUDHI reads it; none of its
|
|
98
|
+
* entries may be NaN or negative (`Infinity` is an absent edge). A point
|
|
99
|
+
* cloud with two points whose squared distance overflows double precision
|
|
100
|
+
* is refused (a `RangeError`: rescale it), and so is an input with both `coords` and
|
|
101
|
+
* `distances`. Complexes above 2^25 simplices are refused (a `RangeError`
|
|
102
|
+
* whose message starts with `input too large:`). Values on the
|
|
103
|
+
* `'edgeLength'` scale.
|
|
104
|
+
*/
|
|
105
|
+
export function ripsComplex(points, opts) {
|
|
106
|
+
module();
|
|
107
|
+
const o = options(opts, 'ripsComplex', RIPS_COMPLEX_KEYS);
|
|
108
|
+
const matrix = isMatrix(points, 'points');
|
|
109
|
+
const n = matrix ? distanceMatrix(points, 'points') : checkPoints(points, 'points', { squaredDistances: true });
|
|
110
|
+
const maxEdgeLength = positive(o.maxEdgeLength, 'maxEdgeLength');
|
|
111
|
+
const cap = maxSimplexDim(o.maxSimplexDim, 3);
|
|
112
|
+
return call('ripsComplex', (m) => {
|
|
113
|
+
if (matrix) {
|
|
114
|
+
return complexOf(m.ripsComplexFromMatrixFlat(points.distances, n, maxEdgeLength, cap), 'edgeLength');
|
|
115
|
+
}
|
|
116
|
+
const { coords, d } = points;
|
|
117
|
+
return complexOf(m.ripsComplexFlat(coords, d, maxEdgeLength, cap), 'edgeLength');
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
const CECH_COMPLEX_KEYS = { maxRadius: true, maxSimplexDim: true };
|
|
121
|
+
/**
|
|
122
|
+
* The Čech complex of a point cloud (at least 2 points): every vertex set
|
|
123
|
+
* whose smallest enclosing ball has radius `<= maxRadius`, up to
|
|
124
|
+
* `maxSimplexDim`, valued by that radius (exact geometry; scale
|
|
125
|
+
* `'radius'`). It grows combinatorially; {@link cechPersistence} computes
|
|
126
|
+
* its diagram from the far smaller Delaunay–Čech complex for points in
|
|
127
|
+
* dimension 1 to 3. The output is a filtration {@link persistence} accepts:
|
|
128
|
+
* where the input's ties are not exact in double precision, rounding can put
|
|
129
|
+
* a face a few ulps above a coface, and the builder raises such a coface to
|
|
130
|
+
* its faces' values (a face is never lowered).
|
|
131
|
+
*
|
|
132
|
+
* Complexes above 2^25 simplices are refused while they are built (a
|
|
133
|
+
* `RangeError` whose message starts with `input too large:`). The builder
|
|
134
|
+
* keeps the smallest enclosing ball of every simplex below `maxSimplexDim`
|
|
135
|
+
* for its cofaces, so memory can run out first (`TOO_LARGE`).
|
|
136
|
+
*/
|
|
137
|
+
export function cechComplex(points, opts) {
|
|
138
|
+
module();
|
|
139
|
+
const o = options(opts, 'cechComplex', CECH_COMPLEX_KEYS);
|
|
140
|
+
checkPoints(points, 'points', { min: 2 });
|
|
141
|
+
const maxRadius = positive(o.maxRadius, 'maxRadius');
|
|
142
|
+
const cap = maxSimplexDim(o.maxSimplexDim, 3);
|
|
143
|
+
const { coords, d } = points;
|
|
144
|
+
return call('cechComplex', (m) => complexOf(m.cechComplexFlat(coords, d, maxRadius, cap), 'radius'));
|
|
145
|
+
}
|
|
146
|
+
const CECH_PERSISTENCE_KEYS = { ...CECH_COMPLEX_KEYS, ...FUSED_KEYS };
|
|
147
|
+
/**
|
|
148
|
+
* The persistence of the Čech complex of a point cloud ({@link cechComplex}),
|
|
149
|
+
* built and reduced in one call. For points in dimension 1 to 3 it is
|
|
150
|
+
* computed on the Delaunay–Čech complex, whose diagram is the Čech
|
|
151
|
+
* complex's ([reference](https://topolab.ai/tda/docs/references/#bauer-and-edelsbrunner-2017)), and `complex: true`
|
|
152
|
+
* returns that complex; above dimension 3 on the full Čech complex, whose
|
|
153
|
+
* values are monotone as built (see {@link cechComplex}).
|
|
154
|
+
* Homology up to `maxSimplexDim`; in that top dimension a truncated full
|
|
155
|
+
* Čech complex has classes only the truncation creates, the Delaunay–Čech
|
|
156
|
+
* complex none once `maxSimplexDim` reaches the point dimension.
|
|
157
|
+
*/
|
|
158
|
+
export function cechPersistence(points, opts) {
|
|
159
|
+
module();
|
|
160
|
+
const o = options(opts, 'cechPersistence', CECH_PERSISTENCE_KEYS);
|
|
161
|
+
checkPoints(points, 'points', { min: 2 });
|
|
162
|
+
const maxRadius = positive(o.maxRadius, 'maxRadius');
|
|
163
|
+
const cap = maxSimplexDim(o.maxSimplexDim, 3);
|
|
164
|
+
const f = fused(o);
|
|
165
|
+
const { coords, d } = points;
|
|
166
|
+
return call('cechPersistence', (m) => persistenceOf(m.cechPipeline(coords, d, maxRadius, cap, f.coeff, f.complex), 'radius', f, cap, f.complex, (raw) => complexOf(raw, 'radius')));
|
|
167
|
+
}
|
|
168
|
+
/** The engine's `flattenedRank` / `flattenedMaxDistance`, or nothing. */
|
|
169
|
+
function flattenedOf(raw) {
|
|
170
|
+
return raw.flattenedRank === undefined ? {} : { flattened: { rank: raw.flattenedRank, maxDistance: raw.flattenedMaxDistance } };
|
|
171
|
+
}
|
|
172
|
+
/** The engine's "off" for `flatTolerance` (NaN). */
|
|
173
|
+
function tolerance(v) {
|
|
174
|
+
return v === undefined ? NaN : v;
|
|
175
|
+
}
|
|
176
|
+
const DELAUNAY_CECH_KEYS = { maxRadius: true, maxSimplexDim: true, flatTolerance: true };
|
|
177
|
+
/**
|
|
178
|
+
* The Delaunay–Čech complex of points in dimension 1, 2 or 3: every face of
|
|
179
|
+
* the Delaunay triangulation, valued by the radius of the smallest ball
|
|
180
|
+
* enclosing its vertices (for a simplex of both complexes the value
|
|
181
|
+
* {@link cechComplex} gives it, bit for bit; scale `'radius'`). Its diagram
|
|
182
|
+
* is the Čech complex's, from a far smaller complex. Repeated points keep
|
|
183
|
+
* their first copy. Cospherical points are triangulated by exact predicates
|
|
184
|
+
* with a symbolic perturbation of fixed priority, so the complex is a pure
|
|
185
|
+
* function of the input.
|
|
186
|
+
*/
|
|
187
|
+
export function delaunayCechComplex(points, opts) {
|
|
188
|
+
module();
|
|
189
|
+
const o = options(opts, 'delaunayCechComplex', DELAUNAY_CECH_KEYS);
|
|
190
|
+
checkPoints(points, 'points', { dims: [1, 2, 3] });
|
|
191
|
+
const maxRadius = positive(o.maxRadius, 'maxRadius');
|
|
192
|
+
const cap = maxSimplexDim(o.maxSimplexDim, 3);
|
|
193
|
+
const flat = tolerance(checkFlatTolerance(o.flatTolerance));
|
|
194
|
+
const { coords, d } = points;
|
|
195
|
+
return call('delaunayCechComplex', (m) => {
|
|
196
|
+
const raw = m.delaunayCechComplexFlat(coords, d, maxRadius, cap, flat);
|
|
197
|
+
return { ...complexOf(raw, 'radius'), ...flattenedOf(raw) };
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
const ALPHA_COMPLEX_KEYS = { weights: true, maxSquaredRadius: true, flatTolerance: true };
|
|
201
|
+
/** `[x, y, z, weight] * n` for the engine, after the checks of the alpha functions. */
|
|
202
|
+
function alphaArgs(points, o) {
|
|
203
|
+
const n = checkPoints(points, 'points', { dims: [3] });
|
|
204
|
+
const weights = o.weights === undefined ? null : f64(o.weights, 'weights');
|
|
205
|
+
if (weights !== null) {
|
|
206
|
+
sized(weights.length, n, 'weights', 'one weight per point');
|
|
207
|
+
for (let i = 0; i < n; i++) {
|
|
208
|
+
const w = weights[i];
|
|
209
|
+
if (!(w >= 0) || !Number.isFinite(w))
|
|
210
|
+
throw new RangeError(`weights[${i}] must be finite and non-negative, got ${w}`);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
// Non-positive is the engine's "no cutoff": an omitted or infinite one.
|
|
214
|
+
const max = o.maxSquaredRadius === undefined ? Infinity : positive(o.maxSquaredRadius, 'maxSquaredRadius');
|
|
215
|
+
const flat = tolerance(checkFlatTolerance(o.flatTolerance));
|
|
216
|
+
const coords = new Float64Array(4 * n);
|
|
217
|
+
for (let i = 0; i < n; i++) {
|
|
218
|
+
coords[4 * i] = points.coords[3 * i];
|
|
219
|
+
coords[4 * i + 1] = points.coords[3 * i + 1];
|
|
220
|
+
coords[4 * i + 2] = points.coords[3 * i + 2];
|
|
221
|
+
coords[4 * i + 3] = weights === null ? 0 : weights[i];
|
|
222
|
+
}
|
|
223
|
+
return { coords, cutoff: Number.isFinite(max) ? max : 0, flat };
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* The (weighted) alpha complex of points in three dimensions, over their
|
|
227
|
+
* exact regular triangulation: values are squared radii (scale `'squaredRadius'`),
|
|
228
|
+
* a vertex at minus its weight. Of the points at one position the heaviest
|
|
229
|
+
* is the vertex (the smallest index on a tie); the others are hidden, as are
|
|
230
|
+
* points whose power cell is empty. Without a cutoff, near-degenerate input can
|
|
231
|
+
* end with astronomically large values for the last tetrahedra, genuine
|
|
232
|
+
* values but rarely wanted; the filtration is monotone, so a cutoff leaves a
|
|
233
|
+
* closed complex.
|
|
234
|
+
*/
|
|
235
|
+
export function alphaComplex(points, opts) {
|
|
236
|
+
module();
|
|
237
|
+
const { coords, cutoff, flat } = alphaArgs(points, options(opts, 'alphaComplex', ALPHA_COMPLEX_KEYS));
|
|
238
|
+
return call('alphaComplex', (m) => {
|
|
239
|
+
const raw = m.weightedAlphaComplexFlat(coords, cutoff, flat);
|
|
240
|
+
return { ...complexOf(raw, 'squaredRadius'), ...flattenedOf(raw) };
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
const ALPHA_PERSISTENCE_KEYS = { ...ALPHA_COMPLEX_KEYS, ...FUSED_KEYS };
|
|
244
|
+
/** The persistence of {@link alphaComplex}, built and reduced in one call: homology up to dimension 3. */
|
|
245
|
+
export function alphaPersistence(points, opts) {
|
|
246
|
+
module();
|
|
247
|
+
const o = options(opts, 'alphaPersistence', ALPHA_PERSISTENCE_KEYS);
|
|
248
|
+
const { coords, cutoff, flat } = alphaArgs(points, o);
|
|
249
|
+
const f = fused(o);
|
|
250
|
+
return call('alphaPersistence', (m) => {
|
|
251
|
+
const raw = m.weightedAlphaPipeline(coords, cutoff, f.coeff, f.complex, flat);
|
|
252
|
+
const flattened = flattenedOf(raw);
|
|
253
|
+
const p = persistenceOf(raw, 'squaredRadius', f, 3, f.complex, (r) => ({ ...complexOf(r, 'squaredRadius'), ...flattened }));
|
|
254
|
+
return { ...p, ...flattened };
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
const COUPLED_ALPHA_KEYS = { colors: true, maxSquaredRadius: true, maxSimplexDim: true };
|
|
258
|
+
/**
|
|
259
|
+
* The coupled alpha complex of a planar two-colour point cloud: the nerve of
|
|
260
|
+
* the sets `B(p, r) ∩ Vor(p, points of p's colour)` (closed balls cut by
|
|
261
|
+
* each colour's own Voronoi cells) ([reference](https://topolab.ai/tda/docs/references/#reani-and-bobrowski-2021), Def. 3.1). A simplex enters at the smallest squared
|
|
262
|
+
* radius at which its vertices' sets meet (scale `'squaredRadius'`). The
|
|
263
|
+
* simplices whose vertices all have colour 0 form the alpha complex of the
|
|
264
|
+
* survivors, with that complex's values up to rounding, and the whole
|
|
265
|
+
* complex has, at every radius, the homotopy type of the union of all the
|
|
266
|
+
* balls, so the two rows of a commutative ladder come from one call (see
|
|
267
|
+
* https://topolab.ai/tda/docs/guides/coupled-alpha/). Vertex ids are input indices, coincident points
|
|
268
|
+
* included. No flag completion and no perturbation.
|
|
269
|
+
*
|
|
270
|
+
* Bounded by design: at most 24 points and simplices up to dimension 3,
|
|
271
|
+
* each birth found by enumerating every candidate minimizer of a planar
|
|
272
|
+
* convex problem in IEEE binary128 (quadruple precision, in software).
|
|
273
|
+
* Dimension 3 holds the whole nerve for generic input; dimension 2
|
|
274
|
+
* suffices for H₀, H₁ and their inclusion maps.
|
|
275
|
+
* Cell membership uses a relative tolerance of 1e-12 after the input is
|
|
276
|
+
* translated and scaled to unit size.
|
|
277
|
+
*
|
|
278
|
+
* There is no `coupledAlphaPersistence`: the whole complex has the
|
|
279
|
+
* homotopy type of the union of all the balls (the diagram of the planar
|
|
280
|
+
* alpha complex of all the points), and its use is the inclusion of the
|
|
281
|
+
* survivors' subcomplex, which {@link persistence} of either complex does
|
|
282
|
+
* not give.
|
|
283
|
+
*/
|
|
284
|
+
export function coupledAlphaComplex(points, opts) {
|
|
285
|
+
module();
|
|
286
|
+
const o = options(opts, 'coupledAlphaComplex', COUPLED_ALPHA_KEYS);
|
|
287
|
+
const n = checkPoints(points, 'points', { dims: [2] });
|
|
288
|
+
if (n > 24)
|
|
289
|
+
throw new RangeError(`points must hold at most 24 points, got ${n}`);
|
|
290
|
+
const colors = o.colors;
|
|
291
|
+
if (colors === null || typeof colors !== 'object' || typeof colors.length !== 'number') {
|
|
292
|
+
throw new TypeError(`colors must be an array of 0s and 1s, got ${kind(colors)}`);
|
|
293
|
+
}
|
|
294
|
+
sized(colors.length, n, 'colors', 'one colour per point');
|
|
295
|
+
const c = new Float64Array(n);
|
|
296
|
+
for (let i = 0; i < n; i++) {
|
|
297
|
+
const x = colors[i];
|
|
298
|
+
if (typeof x !== 'number')
|
|
299
|
+
throw new TypeError(`colors[${i}] must be a number, got ${kind(x)}`);
|
|
300
|
+
if (x !== 0 && x !== 1)
|
|
301
|
+
throw new RangeError(`colors[${i}] must be 0 or 1, got ${x}`);
|
|
302
|
+
c[i] = x;
|
|
303
|
+
}
|
|
304
|
+
const cutoff = o.maxSquaredRadius === undefined ? Infinity : nonNegative(o.maxSquaredRadius, 'maxSquaredRadius');
|
|
305
|
+
const cap = maxSimplexDim(o.maxSimplexDim, 3, 3);
|
|
306
|
+
return call('coupledAlphaComplex', (m) => complexOf(m.coupledAlphaComplex2DFlat(points.coords, c, cutoff, cap), 'squaredRadius'));
|
|
307
|
+
}
|
|
308
|
+
const WITNESS_KEYS = { landmarks: true, maxSquaredRelaxation: true, maxSimplexDim: true };
|
|
309
|
+
/**
|
|
310
|
+
* The Euclidean (relaxed weak) witness complex of a point cloud ([reference](https://topolab.ai/tda/docs/references/#de-silva-and-carlsson-2004)), relaxed as in ([reference](https://topolab.ai/tda/docs/references/#boissonnat-guibas-and-oudot-2009)), on
|
|
311
|
+
* squared distances: a set of landmarks is a simplex at the smallest
|
|
312
|
+
* `a <= maxSquaredRelaxation` at which it and its faces are a-witnessed, a
|
|
313
|
+
* witness w a-witnessing it when its squared distance to the farthest of
|
|
314
|
+
* them is at most `a` more than its squared distance to the nearest other
|
|
315
|
+
* landmark (scale `'squaredRelaxation'`). Landmarks are chosen by
|
|
316
|
+
* deterministic farthest-point sampling; vertex ids index the landmarks in
|
|
317
|
+
* selection order, not the input points. The strong witness complex is not
|
|
318
|
+
* implemented. A cloud with two points whose squared distance overflows
|
|
319
|
+
* double precision is refused (a `RangeError`: rescale it).
|
|
320
|
+
*/
|
|
321
|
+
export function witnessComplex(points, opts) {
|
|
322
|
+
module();
|
|
323
|
+
const o = options(opts, 'witnessComplex', WITNESS_KEYS);
|
|
324
|
+
checkPoints(points, 'points', { squaredDistances: true });
|
|
325
|
+
const landmarks = int(o.landmarks, 'landmarks', 1);
|
|
326
|
+
const relaxation = nonNegative(o.maxSquaredRelaxation, 'maxSquaredRelaxation');
|
|
327
|
+
const cap = maxSimplexDim(o.maxSimplexDim, 3);
|
|
328
|
+
const { coords, d } = points;
|
|
329
|
+
return call('witnessComplex', (m) => complexOf(m.euclideanWitnessComplexFlat(coords, d, landmarks, relaxation, cap), 'squaredRelaxation'));
|
|
330
|
+
}
|
|
331
|
+
const KFOLD_COMPLEX_KEYS = { k: true, maxSquaredRadius: true, maxSimplexDim: true };
|
|
332
|
+
function kFoldArgs(points, o) {
|
|
333
|
+
const n = checkPoints(points, 'points', { dims: [3] });
|
|
334
|
+
const k = int(o.k, 'k', 1);
|
|
335
|
+
if (n < k + 3)
|
|
336
|
+
throw new RangeError(`points must hold at least k + 3 points (k = ${k}), got ${n}`);
|
|
337
|
+
const max = o.maxSquaredRadius === undefined ? Infinity : positive(o.maxSquaredRadius, 'maxSquaredRadius');
|
|
338
|
+
return { k, cutoff: Number.isFinite(max) ? max : 0, cap: maxSimplexDim(o.maxSimplexDim, 3) };
|
|
339
|
+
}
|
|
340
|
+
function kFoldOf(raw) {
|
|
341
|
+
const flat = raw.mosaicVertices;
|
|
342
|
+
const width = raw.mosaicWidth;
|
|
343
|
+
const count = raw.mosaicCount;
|
|
344
|
+
const offsets = raw.mosaicOffsets;
|
|
345
|
+
const mosaicVertices = [];
|
|
346
|
+
for (let i = 0; i < count; i++) {
|
|
347
|
+
const a = offsets ? offsets[i] : i * width;
|
|
348
|
+
const b = offsets ? offsets[i + 1] : a + width;
|
|
349
|
+
mosaicVertices.push(Array.from(flat.subarray(a, b)));
|
|
350
|
+
}
|
|
351
|
+
return {
|
|
352
|
+
...complexOf(raw, 'squaredRadius'),
|
|
353
|
+
mosaicVertices,
|
|
354
|
+
diagnostics: {
|
|
355
|
+
mosaicCells: raw.numMosaicCells,
|
|
356
|
+
octahedra: raw.numOctahedra,
|
|
357
|
+
topRhomboids: raw.numTopRhomboids,
|
|
358
|
+
clampedCells: raw.numClampedCells,
|
|
359
|
+
exactSignFallbacks: raw.numExactSignFallbacks,
|
|
360
|
+
intervalAnomalies: raw.numIntervalAnomalies,
|
|
361
|
+
usedExactFallback: raw.usedExactFallback,
|
|
362
|
+
squaredRadiusBound: raw.squaredRadiusBound
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* The k-fold cover complex of unweighted points in three dimensions: the
|
|
368
|
+
* order-k Delaunay mosaic filtered by the squared radius function (scale
|
|
369
|
+
* `'squaredRadius'`), the exact nerve of the k-fold cover, so its
|
|
370
|
+
* persistence is the k-fold cover's ([reference](https://topolab.ai/tda/docs/references/#edelsbrunner-and-osang-2018)). `k = 1` is the
|
|
371
|
+
* alpha complex. The input must be genuinely three-dimensional and must not
|
|
372
|
+
* repeat a point; exactly cospherical input that makes two order-k sites
|
|
373
|
+
* coincide (an integer grid from `k = 2`) is refused: perturb it. The mosaic
|
|
374
|
+
* is a pure function of the input.
|
|
375
|
+
*/
|
|
376
|
+
export function kFoldCoverComplex(points, opts) {
|
|
377
|
+
module();
|
|
378
|
+
const { k, cutoff, cap } = kFoldArgs(points, options(opts, 'kFoldCoverComplex', KFOLD_COMPLEX_KEYS));
|
|
379
|
+
return call('kFoldCoverComplex', (m) => kFoldOf(m.kFoldCoverComplexFlat(points.coords, k, cutoff, cap)));
|
|
380
|
+
}
|
|
381
|
+
const KFOLD_PERSISTENCE_KEYS = { ...KFOLD_COMPLEX_KEYS, ...FUSED_KEYS };
|
|
382
|
+
/** The persistence of {@link kFoldCoverComplex}, built and reduced in one call: homology up to `maxSimplexDim`. */
|
|
383
|
+
export function kFoldCoverPersistence(points, opts) {
|
|
384
|
+
module();
|
|
385
|
+
const o = options(opts, 'kFoldCoverPersistence', KFOLD_PERSISTENCE_KEYS);
|
|
386
|
+
const { k, cutoff, cap } = kFoldArgs(points, o);
|
|
387
|
+
const f = fused(o);
|
|
388
|
+
return call('kFoldCoverPersistence', (m) => persistenceOf(m.kFoldCoverPipeline(points.coords, k, cutoff, cap, f.coeff, f.complex), 'squaredRadius', f, cap, f.complex, kFoldOf));
|
|
389
|
+
}
|
|
390
|
+
const ELLIPSOID_KEYS = { neighbors: true, axes: true };
|
|
391
|
+
/** The engine's axes ratio (`0` selects `'pca'`), after the checks of the ellipsoid functions. */
|
|
392
|
+
function ellipsoidArgs(points, o) {
|
|
393
|
+
checkPoints(points, 'points', { dims: [2, 3], min: 2, squaredDistances: true });
|
|
394
|
+
const neighbors = int(o.neighbors, 'neighbors', 2);
|
|
395
|
+
if (o.axes === 'pca')
|
|
396
|
+
return { neighbors, ratio: 0 };
|
|
397
|
+
if (typeof o.axes !== 'number')
|
|
398
|
+
throw new TypeError(`axes must be a number or 'pca', got ${kind(o.axes)}`);
|
|
399
|
+
if (!Number.isFinite(o.axes) || o.axes < 1)
|
|
400
|
+
throw new RangeError(`axes must be a finite ratio >= 1 or 'pca', got ${o.axes}`);
|
|
401
|
+
return { neighbors, ratio: o.axes };
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* The ellipsoids the two ellipsoid complexes fit at every point (the same
|
|
405
|
+
* fitting, so a drawing matches the complex), for renderers that draw them.
|
|
406
|
+
*/
|
|
407
|
+
export function ellipsoids(points, opts) {
|
|
408
|
+
module();
|
|
409
|
+
const { neighbors, ratio } = ellipsoidArgs(points, options(opts, 'ellipsoids', ELLIPSOID_KEYS));
|
|
410
|
+
const { coords, d } = points;
|
|
411
|
+
const flat = call('ellipsoids', (m) => m.computeEllipsoidGeometry(coords, d, neighbors, ratio));
|
|
412
|
+
const stride = 2 * d + d * d;
|
|
413
|
+
const out = [];
|
|
414
|
+
for (let i = 0; i < flat.length / stride; i++) {
|
|
415
|
+
let at = i * stride;
|
|
416
|
+
const center = [];
|
|
417
|
+
for (let c = 0; c < d; c++)
|
|
418
|
+
center.push(flat[at++]);
|
|
419
|
+
const frame = [];
|
|
420
|
+
for (let a = 0; a < d; a++) {
|
|
421
|
+
const row = [];
|
|
422
|
+
for (let c = 0; c < d; c++)
|
|
423
|
+
row.push(flat[at++]);
|
|
424
|
+
frame.push(row);
|
|
425
|
+
}
|
|
426
|
+
const semiAxes = [];
|
|
427
|
+
for (let c = 0; c < d; c++)
|
|
428
|
+
semiAxes.push(flat[at++]);
|
|
429
|
+
out.push({ center, frame, semiAxes });
|
|
430
|
+
}
|
|
431
|
+
return out;
|
|
432
|
+
}
|
|
433
|
+
const ELLIPSOID_RIPS_COMPLEX_KEYS = { ...ELLIPSOID_KEYS, maxEdgeLength: true, maxSimplexDim: true };
|
|
434
|
+
/**
|
|
435
|
+
* The Rips-type ellipsoid complex of points in two or three dimensions
|
|
436
|
+
* ([reference](https://topolab.ai/tda/docs/references/#canova-et-al)): each point
|
|
437
|
+
* gets an ellipsoid from a local PCA frame, an edge appears when two
|
|
438
|
+
* ellipsoids first touch, and higher simplices come from flag expansion. A
|
|
439
|
+
* flag complex, as the paper builds it (Definition 2.2), not a nerve. Values
|
|
440
|
+
* are twice the birth radius, on the Rips scale (`'edgeLength'`), so
|
|
441
|
+
* `axes: 1` (balls) reproduces {@link ripsComplex}.
|
|
442
|
+
*
|
|
443
|
+
* Complexes above 2^25 simplices are refused with a `RangeError` whose
|
|
444
|
+
* message starts with `input too large:`. Without `maxEdgeLength`, or with
|
|
445
|
+
* one at which the centroid of the points lies in every ellipsoid (every
|
|
446
|
+
* pair is then an edge), the count of every set of at most
|
|
447
|
+
* `maxSimplexDim + 1` points is checked before any pair is solved.
|
|
448
|
+
* Otherwise, when the vertices and edges alone exceed the limit, the pairs
|
|
449
|
+
* stop at the first edge past it; when only the higher cliques do, they
|
|
450
|
+
* are refused while they are enumerated.
|
|
451
|
+
*/
|
|
452
|
+
export function ellipsoidRipsComplex(points, opts) {
|
|
453
|
+
module();
|
|
454
|
+
const o = options(opts, 'ellipsoidRipsComplex', ELLIPSOID_RIPS_COMPLEX_KEYS);
|
|
455
|
+
const { neighbors, ratio } = ellipsoidArgs(points, o);
|
|
456
|
+
const cutoff = o.maxEdgeLength === undefined ? 0 : positive(o.maxEdgeLength, 'maxEdgeLength');
|
|
457
|
+
const cap = maxSimplexDim(o.maxSimplexDim, 2);
|
|
458
|
+
const { coords, d } = points;
|
|
459
|
+
return call('ellipsoidRipsComplex', (m) => complexOf(m.ellipsoidRipsComplexFlat(coords, d, neighbors, ratio, cap, cutoff), 'edgeLength'));
|
|
460
|
+
}
|
|
461
|
+
const ELLIPSOID_RIPS_PERSISTENCE_KEYS = { ...ELLIPSOID_RIPS_COMPLEX_KEYS, ...FUSED_KEYS };
|
|
462
|
+
/** The persistence of {@link ellipsoidRipsComplex}, built and reduced in one call: homology up to `maxSimplexDim`. */
|
|
463
|
+
export function ellipsoidRipsPersistence(points, opts) {
|
|
464
|
+
module();
|
|
465
|
+
const o = options(opts, 'ellipsoidRipsPersistence', ELLIPSOID_RIPS_PERSISTENCE_KEYS);
|
|
466
|
+
const { neighbors, ratio } = ellipsoidArgs(points, o);
|
|
467
|
+
const cutoff = o.maxEdgeLength === undefined ? 0 : positive(o.maxEdgeLength, 'maxEdgeLength');
|
|
468
|
+
const cap = maxSimplexDim(o.maxSimplexDim, 2);
|
|
469
|
+
const f = fused(o);
|
|
470
|
+
const { coords, d } = points;
|
|
471
|
+
return call('ellipsoidRipsPersistence', (m) => persistenceOf(m.ellipsoidRipsPipeline(coords, d, neighbors, ratio, cap, cutoff, f.coeff, f.complex), 'edgeLength', f, cap, f.complex, (raw) => complexOf(raw, 'edgeLength')));
|
|
472
|
+
}
|
|
473
|
+
const ELLIPSOID_CECH_COMPLEX_KEYS = { ...ELLIPSOID_KEYS, maxRadius: true, maxSimplexDim: true };
|
|
474
|
+
/**
|
|
475
|
+
* The ellipsoidal Čech complex, a nerve ([reference](https://topolab.ai/tda/docs/references/#giunti-hill-and-ye)): the ellipsoids of {@link ellipsoidRipsComplex}, but a simplex enters
|
|
476
|
+
* only when the ellipsoids of all its vertices share a point, at the
|
|
477
|
+
* smallest radius at which they do (scale `'radius'`). The ellipsoids are
|
|
478
|
+
* convex, so the cover is good and the nerve lemma applies: its persistence
|
|
479
|
+
* is the persistence of the growing union of ellipsoids. It is a subcomplex
|
|
480
|
+
* of {@link ellipsoidRipsComplex} on the same input, each shared simplex
|
|
481
|
+
* entering no earlier here (at half the value, on the radius scale), and
|
|
482
|
+
* `axes: 1` reproduces {@link cechComplex}, simplex for simplex, with
|
|
483
|
+
* values equal to within a few ulps (measured to 1.8e-16 in 2D and 3.0e-16
|
|
484
|
+
* in 3D).
|
|
485
|
+
*
|
|
486
|
+
* Complexes above 2^25 simplices are refused while they are built (a
|
|
487
|
+
* `RangeError` whose message starts with `input too large:`). Without
|
|
488
|
+
* `maxRadius`, or with one at which the centroid of the points lies in
|
|
489
|
+
* every ellipsoid, the complex is every set of at most `maxSimplexDim + 1`
|
|
490
|
+
* points, and that count is checked before anything is solved.
|
|
491
|
+
*/
|
|
492
|
+
export function ellipsoidCechComplex(points, opts) {
|
|
493
|
+
module();
|
|
494
|
+
const o = options(opts, 'ellipsoidCechComplex', ELLIPSOID_CECH_COMPLEX_KEYS);
|
|
495
|
+
const { neighbors, ratio } = ellipsoidArgs(points, o);
|
|
496
|
+
const cutoff = o.maxRadius === undefined ? 0 : positive(o.maxRadius, 'maxRadius');
|
|
497
|
+
const cap = maxSimplexDim(o.maxSimplexDim, 2);
|
|
498
|
+
const { coords, d } = points;
|
|
499
|
+
return call('ellipsoidCechComplex', (m) => complexOf(m.ellipsoidCechComplexFlat(coords, d, neighbors, ratio, cap, cutoff), 'radius'));
|
|
500
|
+
}
|
|
501
|
+
const ELLIPSOID_CECH_PERSISTENCE_KEYS = { ...ELLIPSOID_CECH_COMPLEX_KEYS, ...FUSED_KEYS };
|
|
502
|
+
/** The persistence of {@link ellipsoidCechComplex}, built and reduced in one call: homology up to `maxSimplexDim`. */
|
|
503
|
+
export function ellipsoidCechPersistence(points, opts) {
|
|
504
|
+
module();
|
|
505
|
+
const o = options(opts, 'ellipsoidCechPersistence', ELLIPSOID_CECH_PERSISTENCE_KEYS);
|
|
506
|
+
const { neighbors, ratio } = ellipsoidArgs(points, o);
|
|
507
|
+
const cutoff = o.maxRadius === undefined ? 0 : positive(o.maxRadius, 'maxRadius');
|
|
508
|
+
const cap = maxSimplexDim(o.maxSimplexDim, 2);
|
|
509
|
+
const f = fused(o);
|
|
510
|
+
const { coords, d } = points;
|
|
511
|
+
return call('ellipsoidCechPersistence', (m) => persistenceOf(m.ellipsoidCechPipeline(coords, d, neighbors, ratio, cap, cutoff, f.coeff, f.complex), 'radius', f, cap, f.complex, (raw) => complexOf(raw, 'radius')));
|
|
512
|
+
}
|
|
513
|
+
const WING_KEYS = { q: true, theta: true, normals: true };
|
|
514
|
+
/** The engine's `normals` (`[k]` or the 2n normals), after the checks of the wing functions. */
|
|
515
|
+
function wingArgs(points, o) {
|
|
516
|
+
const n = checkPoints(points, 'points', { dims: [2], min: 2 });
|
|
517
|
+
const q = num(o.q, 'q');
|
|
518
|
+
if (!(q >= 0 && q <= 1))
|
|
519
|
+
throw new RangeError(`q must lie in [0, 1], got ${q}`);
|
|
520
|
+
const theta = num(o.theta, 'theta');
|
|
521
|
+
if (!(theta > 0 && theta <= Math.PI / 2))
|
|
522
|
+
throw new RangeError(`theta must lie in (0, π/2], got ${theta}`);
|
|
523
|
+
if (typeof o.normals === 'number')
|
|
524
|
+
return { q, theta, normals: Float64Array.of(int(o.normals, 'normals', 2)) };
|
|
525
|
+
if (o.normals === undefined)
|
|
526
|
+
throw new TypeError('normals must be a neighbour count or the unit normals, got undefined');
|
|
527
|
+
const normals = f64(o.normals, 'normals');
|
|
528
|
+
sized(normals.length, 2 * n, 'normals', 'two coordinates per point');
|
|
529
|
+
allFinite(normals, 'normals');
|
|
530
|
+
return { q, theta, normals };
|
|
531
|
+
}
|
|
532
|
+
const WING_NORMAL_SOURCES = ['supplied', 'osculating', 'tangent', 'isolated'];
|
|
533
|
+
/** The wings {@link wingComplex} builds, for renderers that draw them. */
|
|
534
|
+
export function wings(points, opts) {
|
|
535
|
+
module();
|
|
536
|
+
const { q, theta, normals } = wingArgs(points, options(opts, 'wings', WING_KEYS));
|
|
537
|
+
const coords = points.coords;
|
|
538
|
+
const flat = call('wings', (m) => m.computeWingGeometry(coords, q, theta, normals));
|
|
539
|
+
const stride = 19;
|
|
540
|
+
const out = [];
|
|
541
|
+
for (let i = 0; i < flat.length / stride; i++) {
|
|
542
|
+
let at = i * stride;
|
|
543
|
+
const point = [flat[at++], flat[at++]];
|
|
544
|
+
const normal = [flat[at++], flat[at++]];
|
|
545
|
+
const code = flat[at++];
|
|
546
|
+
const normalSource = WING_NORMAL_SOURCES[code];
|
|
547
|
+
if (normalSource === undefined)
|
|
548
|
+
throw new Error(`unknown normal provenance code ${code}`);
|
|
549
|
+
const curvatureRadius = flat[at++];
|
|
550
|
+
const neighborhoodScale = flat[at++];
|
|
551
|
+
const outline = [];
|
|
552
|
+
for (let v = 0; v < 6; v++)
|
|
553
|
+
outline.push([flat[at++], flat[at++]]);
|
|
554
|
+
out.push({ point, normal, normalSource, curvatureRadius, neighborhoodScale, outline });
|
|
555
|
+
}
|
|
556
|
+
return out;
|
|
557
|
+
}
|
|
558
|
+
const WING_COMPLEX_KEYS = { ...WING_KEYS, maxEpsilon: true, maxSimplexDim: true };
|
|
559
|
+
/**
|
|
560
|
+
* The curvature-adaptive wing complex of a planar point cloud, a flag
|
|
561
|
+
* complex (not a nerve) ([reference](https://topolab.ai/tda/docs/references/#weng-and-zhao-2026)). Pair
|
|
562
|
+
* births are analytic; the 1-skeleton is expanded to a flag complex by the
|
|
563
|
+
* Rust core. Values on the wing scale ε (`'epsilon'`).
|
|
564
|
+
*/
|
|
565
|
+
export function wingComplex(points, opts) {
|
|
566
|
+
module();
|
|
567
|
+
const o = options(opts, 'wingComplex', WING_COMPLEX_KEYS);
|
|
568
|
+
const { q, theta, normals } = wingArgs(points, o);
|
|
569
|
+
const maxEpsilon = o.maxEpsilon === undefined ? Infinity : positive(o.maxEpsilon, 'maxEpsilon');
|
|
570
|
+
const cap = maxSimplexDim(o.maxSimplexDim, 3);
|
|
571
|
+
return call('wingComplex', (m) => complexOf(m.wingComplexFlat(points.coords, q, theta, normals, cap, maxEpsilon), 'epsilon'));
|
|
572
|
+
}
|
|
573
|
+
const WING_PERSISTENCE_KEYS = { ...WING_COMPLEX_KEYS, ...FUSED_KEYS };
|
|
574
|
+
/** The persistence of {@link wingComplex}, built and reduced in one call: homology up to `maxSimplexDim`. */
|
|
575
|
+
export function wingPersistence(points, opts) {
|
|
576
|
+
module();
|
|
577
|
+
const o = options(opts, 'wingPersistence', WING_PERSISTENCE_KEYS);
|
|
578
|
+
const { q, theta, normals } = wingArgs(points, o);
|
|
579
|
+
const maxEpsilon = o.maxEpsilon === undefined ? Infinity : positive(o.maxEpsilon, 'maxEpsilon');
|
|
580
|
+
const cap = maxSimplexDim(o.maxSimplexDim, 3);
|
|
581
|
+
const f = fused(o);
|
|
582
|
+
return call('wingPersistence', (m) => persistenceOf(m.wingPipeline(points.coords, q, theta, normals, cap, maxEpsilon, f.coeff, f.complex), 'epsilon', f, cap, f.complex, (raw) => complexOf(raw, 'epsilon')));
|
|
583
|
+
}
|
|
584
|
+
const BOX_KEYS = { stepSize: true, growth: true, maxSteps: true, maxSimplexDim: true, boxes: true };
|
|
585
|
+
function boxArgs(points, o) {
|
|
586
|
+
checkPoints(points, 'points', { dims: [2, 3] });
|
|
587
|
+
const stepSize = finite(o.stepSize, 'stepSize');
|
|
588
|
+
if (!(stepSize > 0))
|
|
589
|
+
throw new RangeError(`stepSize must be positive, got ${stepSize}`);
|
|
590
|
+
const growth = num(o.growth, 'growth');
|
|
591
|
+
if (!(growth >= 0 && growth <= 1))
|
|
592
|
+
throw new RangeError(`growth must lie in [0, 1], got ${growth}`);
|
|
593
|
+
// 0 is the engine's "the paper's m"
|
|
594
|
+
const maxSteps = o.maxSteps === undefined ? 0 : int(o.maxSteps, 'maxSteps', 1);
|
|
595
|
+
return { stepSize, growth, maxSteps, cap: maxSimplexDim(o.maxSimplexDim, 3), boxes: boolOption(o.boxes, 'boxes', false) };
|
|
596
|
+
}
|
|
597
|
+
function boxOf(d, withBoxes) {
|
|
598
|
+
return (raw) => {
|
|
599
|
+
let boxes;
|
|
600
|
+
if (withBoxes) {
|
|
601
|
+
const extents = raw.boxExtents;
|
|
602
|
+
boxes = [];
|
|
603
|
+
let at = 0;
|
|
604
|
+
for (let step = 0; step <= raw.numSteps; step++) {
|
|
605
|
+
const perStep = [];
|
|
606
|
+
for (let box = 0; box < raw.numBoxes; box++) {
|
|
607
|
+
const lower = Array.from(extents.subarray(at, at + d));
|
|
608
|
+
const upper = Array.from(extents.subarray(at + d, at + 2 * d));
|
|
609
|
+
at += 2 * d;
|
|
610
|
+
perStep.push({ lower, upper });
|
|
611
|
+
}
|
|
612
|
+
boxes.push(perStep);
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
return {
|
|
616
|
+
...complexOf(raw, 'growthRadius'),
|
|
617
|
+
numSteps: raw.numSteps,
|
|
618
|
+
stepSize: raw.stepSize,
|
|
619
|
+
growth: raw.alpha,
|
|
620
|
+
maxBirthStep: raw.maxBirthStep,
|
|
621
|
+
numEdges: raw.numEdges,
|
|
622
|
+
...(boxes !== undefined ? { boxes } : {}),
|
|
623
|
+
diagnostics: {
|
|
624
|
+
expansions: raw.numExpansions,
|
|
625
|
+
descentSteps: raw.numDescentSteps,
|
|
626
|
+
iterationCapHits: raw.numIterationCapHits
|
|
627
|
+
}
|
|
628
|
+
};
|
|
629
|
+
};
|
|
630
|
+
}
|
|
631
|
+
/**
|
|
632
|
+
* The box filtration of points in two or three dimensions, a nerve
|
|
633
|
+
* ([reference](https://topolab.ai/tda/docs/references/#alvarado-gupta-and-krishnamoorthy)): every point starts
|
|
634
|
+
* as a degenerate box and grows, a step at a time, inside the ℓ∞ ball of
|
|
635
|
+
* radius `j · stepSize` around itself (scale `'growthRadius'`), following
|
|
636
|
+
* the paper's rule: growth is worth taking when it covers other points (each
|
|
637
|
+
* worth `growth`) for less than it costs in added width (each unit worth
|
|
638
|
+
* `1 − growth`). Simplices enter when their boxes first intersect; boxes
|
|
639
|
+
* have the Helly property, so the flag complex of the intersection graph is
|
|
640
|
+
* the nerve and the nerve theorem applies.
|
|
641
|
+
*
|
|
642
|
+
* Every value is a multiple of `stepSize`: compare diagrams only at equal
|
|
643
|
+
* `stepSize`. Deterministic, bit for bit: the steps are integer and the
|
|
644
|
+
* intersection tests exact. Duplicate, collinear and coincident points are
|
|
645
|
+
* accepted.
|
|
646
|
+
*
|
|
647
|
+
* The build holds every box at every step, whether or not `boxes` returns
|
|
648
|
+
* them: `(numSteps + 1) · n · 2d` box coordinates of 2^28 or more are
|
|
649
|
+
* refused, with `boxes` `true` or `false`, by a `RangeError` whose message
|
|
650
|
+
* starts with `input too large:` (a larger `stepSize` or a smaller
|
|
651
|
+
* `maxSteps` takes fewer steps).
|
|
652
|
+
*/
|
|
653
|
+
export function boxFiltration(points, opts) {
|
|
654
|
+
module();
|
|
655
|
+
const a = boxArgs(points, options(opts, 'boxFiltration', BOX_KEYS));
|
|
656
|
+
const { coords, d } = points;
|
|
657
|
+
return call('boxFiltration', (m) => boxOf(d, a.boxes)(m.boxFiltrationFlat(coords, d, a.stepSize, a.growth, a.maxSteps, a.cap, a.boxes)));
|
|
658
|
+
}
|
|
659
|
+
const BOX_PERSISTENCE_KEYS = { ...BOX_KEYS, ...FUSED_KEYS };
|
|
660
|
+
/**
|
|
661
|
+
* The persistence of {@link boxFiltration}, built and reduced in one call:
|
|
662
|
+
* homology up to `maxSimplexDim`. The step limit of {@link boxFiltration}
|
|
663
|
+
* applies, with `boxes` `true` or `false`.
|
|
664
|
+
*/
|
|
665
|
+
export function boxFiltrationPersistence(points, opts) {
|
|
666
|
+
module();
|
|
667
|
+
const o = options(opts, 'boxFiltrationPersistence', BOX_PERSISTENCE_KEYS);
|
|
668
|
+
const a = boxArgs(points, o);
|
|
669
|
+
const f = fused(o);
|
|
670
|
+
const { coords, d } = points;
|
|
671
|
+
return call('boxFiltrationPersistence', (m) => persistenceOf(m.boxFiltrationPipeline(coords, d, a.stepSize, a.growth, a.maxSteps, a.cap, a.boxes, f.coeff, f.complex), 'growthRadius', f, a.cap, f.complex, boxOf(d, a.boxes)));
|
|
672
|
+
}
|
|
673
|
+
// ---------------------------------------------------------------------------
|
|
674
|
+
// Cubical
|
|
675
|
+
// ---------------------------------------------------------------------------
|
|
676
|
+
/** The grid's extents, fastest axis first (the engine's order), after its checks. */
|
|
677
|
+
export function gridArgs(v, name) {
|
|
678
|
+
if (v === null || typeof v !== 'object') {
|
|
679
|
+
throw new TypeError(`${name} must be { values: Float64Array, shape: number[] }, got ${kind(v)}`);
|
|
680
|
+
}
|
|
681
|
+
const { values, shape } = v;
|
|
682
|
+
if (!isTyped(values, 'Float64Array'))
|
|
683
|
+
throw new TypeError(`${name}.values must be a Float64Array, got ${kind(values)}`);
|
|
684
|
+
if (!Array.isArray(shape))
|
|
685
|
+
throw new TypeError(`${name}.shape must be an array of positive integers, got ${kind(shape)}`);
|
|
686
|
+
if (shape.length === 0 || shape.length > 16) {
|
|
687
|
+
throw new RangeError(`${name}.shape must have from 1 to 16 axes, got ${shape.length}`);
|
|
688
|
+
}
|
|
689
|
+
let size = 1;
|
|
690
|
+
for (let i = 0; i < shape.length; i++) {
|
|
691
|
+
u32(shape[i], `${name}.shape[${i}]`, 1);
|
|
692
|
+
size *= shape[i];
|
|
693
|
+
}
|
|
694
|
+
sized(values.length, size, `${name}.values`, 'one value per cell of the shape');
|
|
695
|
+
allFinite(values, `${name}.values`);
|
|
696
|
+
return [...shape].reverse();
|
|
697
|
+
}
|
|
698
|
+
/**
|
|
699
|
+
* Every cell of the cubical complex of an image or a volume, T-construction
|
|
700
|
+
* (each value on a top-dimensional cell, every lower cell at the smallest
|
|
701
|
+
* value of the top cells containing it), by the Rust core: the cells, values
|
|
702
|
+
* and order of GUDHI's `Bitmap_cubical_complex`.
|
|
703
|
+
*/
|
|
704
|
+
export function cubicalComplex(image) {
|
|
705
|
+
module();
|
|
706
|
+
const extents = gridArgs(image, 'image');
|
|
707
|
+
return call('cubicalComplex', (m) => {
|
|
708
|
+
const raw = m.cubicalComplexFlat(extents, image.values);
|
|
709
|
+
return { shape: [...image.shape], cells: raw.cells, dims: raw.dimensions, filtrations: raw.filtrations, scale: 'value' };
|
|
710
|
+
});
|
|
711
|
+
}
|
|
712
|
+
//# sourceMappingURL=complex.js.map
|