@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/src/check.ts
ADDED
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Argument checks shared by the API (internal). A value of the wrong type is
|
|
3
|
+
* a `TypeError`, a value out of range a `RangeError`; both messages name the
|
|
4
|
+
* parameter as the caller wrote it (`maxEdgeLength`, `points.d`). Typed
|
|
5
|
+
* arrays are recognised by their kind, not by `instanceof`, so arrays made
|
|
6
|
+
* in another realm (an iframe, a `vm` context, a test runner's sandbox) are
|
|
7
|
+
* accepted.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** Weights, matrices and values: a `Float64Array`, or an array of numbers (converted). */
|
|
11
|
+
export type Vec = Float64Array | number[];
|
|
12
|
+
|
|
13
|
+
/** The kinds of typed array the API reads. */
|
|
14
|
+
type TypedArrayName = 'Float64Array' | 'Float32Array' | 'Int32Array' | 'Uint32Array' | 'Uint8Array';
|
|
15
|
+
interface TypedArrays {
|
|
16
|
+
Float64Array: Float64Array;
|
|
17
|
+
Float32Array: Float32Array;
|
|
18
|
+
Int32Array: Int32Array;
|
|
19
|
+
Uint32Array: Uint32Array;
|
|
20
|
+
Uint8Array: Uint8Array;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** The kind of a typed array or `DataView` (`'Float64Array'`, …), from any realm; `undefined` for anything else. */
|
|
24
|
+
export function viewKind(v: unknown): string | undefined {
|
|
25
|
+
if (!ArrayBuffer.isView(v)) return undefined;
|
|
26
|
+
// The tag of a typed array is read from its internal slot, so it is the
|
|
27
|
+
// same in every realm and cannot be forged by a plain object.
|
|
28
|
+
return Object.prototype.toString.call(v).slice(8, -1);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Whether `v` is a typed array of kind `name`, from this realm or another. */
|
|
32
|
+
export function isTyped<K extends TypedArrayName>(v: unknown, name: K): v is TypedArrays[K] {
|
|
33
|
+
return viewKind(v) === name;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** How a wrong value is described in a message. */
|
|
37
|
+
export function kind(v: unknown): string {
|
|
38
|
+
if (v === null || v === undefined) return String(v);
|
|
39
|
+
if (Array.isArray(v)) return 'an array';
|
|
40
|
+
const view = viewKind(v);
|
|
41
|
+
if (view !== undefined) return `a ${view}`;
|
|
42
|
+
return typeof v === 'object' ? 'an object' : `${typeof v} ${String(v)}`;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* A `Float64Array` as it is, an array of numbers converted; an entry that is
|
|
47
|
+
* not a number (a string, `null`, `undefined`, a boolean, a hole) is a
|
|
48
|
+
* `TypeError` naming its index, and anything else is a `TypeError`.
|
|
49
|
+
*/
|
|
50
|
+
export function f64(v: unknown, name: string): Float64Array {
|
|
51
|
+
if (isTyped(v, 'Float64Array')) return v;
|
|
52
|
+
if (Array.isArray(v)) {
|
|
53
|
+
for (let i = 0; i < v.length; i++) {
|
|
54
|
+
if (typeof v[i] !== 'number') throw new TypeError(`${name}[${i}] must be a number, got ${kind(v[i])}`);
|
|
55
|
+
}
|
|
56
|
+
return Float64Array.from(v);
|
|
57
|
+
}
|
|
58
|
+
throw new TypeError(`${name} must be a Float64Array or an array of numbers, got ${kind(v)}`);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Every key of an options type, for {@link options}: listing them as an
|
|
63
|
+
* object of this type makes the compiler check that none is missing or
|
|
64
|
+
* extra.
|
|
65
|
+
*/
|
|
66
|
+
export type Keys<T> = { readonly [K in keyof T]-?: true };
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The `TypeError` for an option `key` that `known` does not list: `fn: unknown
|
|
70
|
+
* option "key"`, with the known key it differs from only in case when there
|
|
71
|
+
* is one.
|
|
72
|
+
*/
|
|
73
|
+
function unknownKey(fn: string, key: string, known: object, prefix: string, what: string): TypeError {
|
|
74
|
+
const lower = key.toLowerCase();
|
|
75
|
+
const near = Object.keys(known).find((k) => k.toLowerCase() === lower);
|
|
76
|
+
const hint = near === undefined ? '' : ` (did you mean "${prefix}${near}"?)`;
|
|
77
|
+
return new TypeError(`${fn}: unknown ${what} "${prefix}${key}"${hint}`);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* An options object (`undefined` reads as `{}`) of function `fn`: every own
|
|
82
|
+
* key must be one of `known` (a key set to `undefined` is allowed), or the
|
|
83
|
+
* call is refused with a `TypeError` naming the function and the key. `name`
|
|
84
|
+
* is the parameter's name in messages, and `prefix` what goes before a key in
|
|
85
|
+
* them (`'representatives.'` for an options object nested in another).
|
|
86
|
+
*/
|
|
87
|
+
export function options<T extends object>(
|
|
88
|
+
v: unknown,
|
|
89
|
+
fn: string,
|
|
90
|
+
known: Keys<T>,
|
|
91
|
+
name = 'opts',
|
|
92
|
+
prefix = '',
|
|
93
|
+
what = 'option'
|
|
94
|
+
): Partial<T> {
|
|
95
|
+
if (v === undefined) return {};
|
|
96
|
+
if (v === null || typeof v !== 'object' || Array.isArray(v)) {
|
|
97
|
+
throw new TypeError(`${name} must be an object, got ${kind(v)}`);
|
|
98
|
+
}
|
|
99
|
+
for (const key of Object.keys(v)) {
|
|
100
|
+
if (!Object.prototype.hasOwnProperty.call(known, key)) throw unknownKey(fn, key, known, prefix, what);
|
|
101
|
+
}
|
|
102
|
+
return v as Partial<T>;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** A number (any value; ranges are checked where they are known). */
|
|
106
|
+
export function num(v: unknown, name: string): number {
|
|
107
|
+
if (typeof v !== 'number') throw new TypeError(`${name} must be a number, got ${kind(v)}`);
|
|
108
|
+
return v;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Largest value of an integer control: the wasm boundary takes u32. */
|
|
112
|
+
export const U32_MAX = 0xffffffff;
|
|
113
|
+
|
|
114
|
+
/** An integer control in `[min, 2³² − 1]`: the wasm boundary would truncate a fraction and wrap anything else. */
|
|
115
|
+
export function u32(v: unknown, name: string, min: number, max = U32_MAX): number {
|
|
116
|
+
num(v, name);
|
|
117
|
+
if (!Number.isSafeInteger(v) || (v as number) < min || (v as number) > max)
|
|
118
|
+
throw new RangeError(`${name} must be an integer from ${min} to ${max}, got ${v}`);
|
|
119
|
+
return v as number;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** A boolean. */
|
|
123
|
+
export function bool(v: unknown, name: string): boolean {
|
|
124
|
+
if (typeof v !== 'boolean') throw new TypeError(`${name} must be a boolean, got ${kind(v)}`);
|
|
125
|
+
return v;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** One of `allowed`. */
|
|
129
|
+
export function oneOf<T extends string>(v: unknown, name: string, allowed: readonly T[]): T {
|
|
130
|
+
if (typeof v !== 'string') throw new TypeError(`${name} must be a string, got ${kind(v)}`);
|
|
131
|
+
if (!allowed.includes(v as T)) {
|
|
132
|
+
throw new RangeError(`${name} must be ${allowed.map((a) => `"${a}"`).join(' or ')}, got "${v}"`);
|
|
133
|
+
}
|
|
134
|
+
return v as T;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** A point cloud: `n` points of `d` coordinates, row-major (`coords[i·d + c]`). */
|
|
138
|
+
export interface PointCloud {
|
|
139
|
+
/** The coordinates, point after point (length `n · d`). */
|
|
140
|
+
coords: Float64Array;
|
|
141
|
+
/** Coordinates per point (a positive integer). */
|
|
142
|
+
d: number;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* A point cloud with at least one point; returns its number of points. When
|
|
147
|
+
* `coords` is given but is not a `Float64Array`, the message says so, and
|
|
148
|
+
* for a plain array names `pointCloud`, which builds a cloud from rows.
|
|
149
|
+
*/
|
|
150
|
+
export function cloud(v: unknown, name: string): number {
|
|
151
|
+
if (v === null || typeof v !== 'object')
|
|
152
|
+
throw new TypeError(`${name} must be a point cloud { coords: Float64Array, d: number }, got ${kind(v)}`);
|
|
153
|
+
const given = (v as { coords?: unknown }).coords;
|
|
154
|
+
if (!isTyped(given, 'Float64Array')) {
|
|
155
|
+
if (given === undefined)
|
|
156
|
+
throw new TypeError(`${name} must be a point cloud { coords: Float64Array, d: number }, got ${kind(v)}`);
|
|
157
|
+
const hint = Array.isArray(given) ? '; pointCloud(rows) builds a point cloud from one array of numbers per point' : '';
|
|
158
|
+
throw new TypeError(`${name}.coords must be a Float64Array, got ${kind(given)}${hint}`);
|
|
159
|
+
}
|
|
160
|
+
const { coords, d } = v as PointCloud;
|
|
161
|
+
u32(d, `${name}.d`, 1);
|
|
162
|
+
if (coords.length === 0 || coords.length % d !== 0)
|
|
163
|
+
throw new RangeError(`${name}.coords must hold a positive whole number of points of ${d} coordinates, got length ${coords.length}`);
|
|
164
|
+
return coords.length / d;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** `length` must be `expected`. */
|
|
168
|
+
export function sized(length: number, expected: number, name: string, what: string): void {
|
|
169
|
+
if (length !== expected) throw new RangeError(`${name} must have ${what} (${expected}), got length ${length}`);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** A number that is not NaN. */
|
|
173
|
+
export function notNaN(v: unknown, name: string): number {
|
|
174
|
+
num(v, name);
|
|
175
|
+
if (Number.isNaN(v)) throw new RangeError(`${name} must not be NaN`);
|
|
176
|
+
return v as number;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** A positive number or `Infinity` (a cutoff). */
|
|
180
|
+
export function positive(v: unknown, name: string): number {
|
|
181
|
+
num(v, name);
|
|
182
|
+
if (!((v as number) > 0)) throw new RangeError(`${name} must be positive, got ${v}`);
|
|
183
|
+
return v as number;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** A non-negative number or `Infinity` (a cutoff that may be 0). */
|
|
187
|
+
export function nonNegative(v: unknown, name: string): number {
|
|
188
|
+
num(v, name);
|
|
189
|
+
if (!((v as number) >= 0)) throw new RangeError(`${name} must be non-negative, got ${v}`);
|
|
190
|
+
return v as number;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** A finite number. */
|
|
194
|
+
export function finite(v: unknown, name: string): number {
|
|
195
|
+
num(v, name);
|
|
196
|
+
if (!Number.isFinite(v)) throw new RangeError(`${name} must be finite, got ${v}`);
|
|
197
|
+
return v as number;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** Every entry of `values` finite, naming the first that is not. */
|
|
201
|
+
export function allFinite(values: ArrayLike<number>, name: string): void {
|
|
202
|
+
for (let i = 0; i < values.length; i++) {
|
|
203
|
+
if (!Number.isFinite(values[i])) throw new RangeError(`${name}[${i}] must be finite, got ${values[i]}`);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** No entry of `values` NaN, naming the first that is. */
|
|
208
|
+
export function noNaN(values: ArrayLike<number>, name: string): void {
|
|
209
|
+
for (let i = 0; i < values.length; i++) {
|
|
210
|
+
if (Number.isNaN(values[i])) throw new RangeError(`${name}[${i}] must not be NaN`);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* The largest coefficient field: `2^31 - 1`, a prime. The engines are exact
|
|
216
|
+
* up to it: sums of two elements stay below `2^32`, products are taken in 64
|
|
217
|
+
* bits, inverses come from a table below `2^16` and from Fermat's
|
|
218
|
+
* `a^(p - 2)` above, and the field reaches the engines as a 32-bit `int`.
|
|
219
|
+
*/
|
|
220
|
+
export const MAX_COEFF = 2147483647;
|
|
221
|
+
|
|
222
|
+
/** Trial division up to the square root (at most 46,340 divisions). */
|
|
223
|
+
function isPrime(n: number): boolean {
|
|
224
|
+
if (n < 2) return false;
|
|
225
|
+
if (n % 2 === 0) return n === 2;
|
|
226
|
+
for (let d = 3; d * d <= n; d += 2) if (n % d === 0) return false;
|
|
227
|
+
return true;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** A prime coefficient field `coeff` in `[2, MAX_COEFF]`, default 2. */
|
|
231
|
+
export function coeff(v: unknown): number {
|
|
232
|
+
if (v === undefined) return 2;
|
|
233
|
+
num(v, 'coeff');
|
|
234
|
+
if (!Number.isSafeInteger(v) || (v as number) < 2 || (v as number) > MAX_COEFF || !isPrime(v as number)) {
|
|
235
|
+
throw new RangeError(`coeff must be a prime from 2 to ${MAX_COEFF}, got ${v}`);
|
|
236
|
+
}
|
|
237
|
+
return v as number;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** The `minPersistence` option: a finite non-negative number, default 0. */
|
|
241
|
+
export function minPersistence(v: unknown): number {
|
|
242
|
+
if (v === undefined) return 0;
|
|
243
|
+
num(v, 'minPersistence');
|
|
244
|
+
if (!Number.isFinite(v) || !((v as number) >= 0)) {
|
|
245
|
+
throw new RangeError(`minPersistence must be a finite non-negative number, got ${v}`);
|
|
246
|
+
}
|
|
247
|
+
return v as number;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** The `flatTolerance` option: a finite non-negative number, or `undefined` (off). */
|
|
251
|
+
export function flatTolerance(v: unknown): number | undefined {
|
|
252
|
+
if (v === undefined) return undefined;
|
|
253
|
+
num(v, 'flatTolerance');
|
|
254
|
+
if (!Number.isFinite(v) || !((v as number) >= 0)) {
|
|
255
|
+
throw new RangeError(`flatTolerance must be a finite non-negative number, got ${v}`);
|
|
256
|
+
}
|
|
257
|
+
return v as number;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** An integer in `[min, max]` (a count or a dimension; `max` at most `2^31 - 1`, the module's 32-bit `int` arguments). */
|
|
261
|
+
export function int(v: unknown, name: string, min: number, max = 0x7fffffff): number {
|
|
262
|
+
return u32(v, name, min, max);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* A point cloud (`cloud`) whose coordinates are finite, with `d` among
|
|
267
|
+
* `dims` when given and at least `min` points; returns its number of points.
|
|
268
|
+
* With `squaredDistances`, for the builders that compute distances from
|
|
269
|
+
* squared coordinate differences in double precision, no pair of points may
|
|
270
|
+
* have a squared distance that overflows (computed as those builders compute
|
|
271
|
+
* it), or the call is a `RangeError` that says to rescale.
|
|
272
|
+
*/
|
|
273
|
+
export function points(
|
|
274
|
+
v: unknown,
|
|
275
|
+
name: string,
|
|
276
|
+
opts: { min?: number; dims?: readonly number[]; squaredDistances?: boolean } = {}
|
|
277
|
+
): number {
|
|
278
|
+
const n = cloud(v, name);
|
|
279
|
+
const { coords, d } = v as PointCloud;
|
|
280
|
+
if (opts.dims !== undefined && !opts.dims.includes(d)) {
|
|
281
|
+
throw new RangeError(`${name}.d must be ${opts.dims.join(' or ')}, got ${d}`);
|
|
282
|
+
}
|
|
283
|
+
if (opts.min !== undefined && n < opts.min) {
|
|
284
|
+
throw new RangeError(`${name} must hold at least ${opts.min} points, got ${n}`);
|
|
285
|
+
}
|
|
286
|
+
allFinite(coords, `${name}.coords`);
|
|
287
|
+
if (opts.squaredDistances) squaredDistances(coords, d, name);
|
|
288
|
+
return n;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Refuses a cloud with a pair of points whose squared distance overflows
|
|
293
|
+
* double precision, computed as the builders compute it: the squares of the
|
|
294
|
+
* coordinate differences added in coordinate order. Rounding is monotone, so
|
|
295
|
+
* every pair's sum is at most the same sum over the extents of the bounding
|
|
296
|
+
* box; only when that one overflows are the pairs themselves checked (in
|
|
297
|
+
* time quadratic in the number of points, as the builders' own distances).
|
|
298
|
+
*/
|
|
299
|
+
function squaredDistances(coords: Float64Array, d: number, name: string): void {
|
|
300
|
+
let bound = 0;
|
|
301
|
+
for (let c = 0; c < d; c++) {
|
|
302
|
+
let lo = Infinity;
|
|
303
|
+
let hi = -Infinity;
|
|
304
|
+
for (let i = c; i < coords.length; i += d) {
|
|
305
|
+
const x = coords[i]!;
|
|
306
|
+
if (x < lo) lo = x;
|
|
307
|
+
if (x > hi) hi = x;
|
|
308
|
+
}
|
|
309
|
+
const extent = hi - lo;
|
|
310
|
+
bound += extent * extent;
|
|
311
|
+
}
|
|
312
|
+
if (bound !== Infinity) return;
|
|
313
|
+
const n = coords.length / d;
|
|
314
|
+
for (let i = 0; i < n; i++) {
|
|
315
|
+
for (let j = i + 1; j < n; j++) {
|
|
316
|
+
let sum = 0;
|
|
317
|
+
for (let c = 0; c < d; c++) {
|
|
318
|
+
const diff = coords[i * d + c]! - coords[j * d + c]!;
|
|
319
|
+
sum += diff * diff;
|
|
320
|
+
}
|
|
321
|
+
if (sum === Infinity) {
|
|
322
|
+
throw new RangeError(
|
|
323
|
+
`${name}.coords: the squared distance between points ${i} and ${j} overflows double precision; rescale the points`
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/** An integer option with a default. */
|
|
331
|
+
export function intOption(v: unknown, name: string, fallback: number, min: number, max?: number): number {
|
|
332
|
+
return v === undefined ? fallback : int(v, name, min, max);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/** A boolean option with a default. */
|
|
336
|
+
export function boolOption(v: unknown, name: string, fallback: boolean): boolean {
|
|
337
|
+
return v === undefined ? fallback : bool(v, name);
|
|
338
|
+
}
|