@flighthq/math 0.5.1-next.3308.79ae053 → 0.5.1-next.4305.c5b7dc0
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/README.md +2 -2
- package/dist/contract.d.ts +16 -15
- package/dist/contract.d.ts.map +1 -1
- package/dist/contract.js +15 -15
- package/dist/contract.js.map +1 -1
- package/dist/hash.js +7 -7
- package/dist/hash.js.map +1 -1
- package/dist/nextPowerOfTwo.js +4 -4
- package/dist/nextPowerOfTwo.js.map +1 -1
- package/dist/random.d.ts +1 -2
- package/dist/random.d.ts.map +1 -1
- package/dist/random.js +1 -1
- package/dist/random.js.map +1 -1
- package/dist/randomDistributions.d.ts +13 -12
- package/dist/randomDistributions.d.ts.map +1 -1
- package/dist/randomDistributions.js +7 -5
- package/dist/randomDistributions.js.map +1 -1
- package/dist/randomRange.d.ts +5 -4
- package/dist/randomRange.d.ts.map +1 -1
- package/dist/randomRange.js +1 -1
- package/dist/randomRange.js.map +1 -1
- package/dist/scalar.js +2 -2
- package/dist/scalar.js.map +1 -1
- package/package.json +2 -4
- package/src/interpolation.test.ts +2 -2
- package/src/interpolationAdvanced.test.ts +2 -2
- package/src/random.test.ts +6 -6
- package/src/randomDistributions.test.ts +87 -87
- package/src/randomRange.test.ts +19 -19
- package/src/statistics.test.ts +4 -4
package/README.md
CHANGED
|
@@ -13,5 +13,5 @@ Import the supported application-facing API from `@flighthq/math`. The `@flighth
|
|
|
13
13
|
This package is part of the locked-version Flight SDK graph. Applications may instead install and import `@flighthq/sdk` when package-level tree shaking is sufficient.
|
|
14
14
|
|
|
15
15
|
- [Flight project](https://github.com/flighthq/flight)
|
|
16
|
-
- [Source for @flighthq/math@0.5.1-next.
|
|
17
|
-
- [License](https://github.com/flighthq/flight/blob/
|
|
16
|
+
- [Source for @flighthq/math@0.5.1-next.4305.c5b7dc0](https://github.com/flighthq/flight/tree/c5b7dc0276d23e5772eb5aa05cdff1a91777141f/packages/math)
|
|
17
|
+
- [License](https://github.com/flighthq/flight/blob/c5b7dc0276d23e5772eb5aa05cdff1a91777141f/LICENSE.md)
|
package/dist/contract.d.ts
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
export
|
|
4
|
-
export
|
|
5
|
-
export
|
|
6
|
-
export
|
|
7
|
-
export
|
|
8
|
-
export
|
|
9
|
-
export
|
|
10
|
-
export
|
|
11
|
-
export
|
|
12
|
-
export
|
|
13
|
-
export
|
|
14
|
-
export
|
|
15
|
-
export
|
|
1
|
+
export { degToRad, deltaAngle, normalizeAngle, radToDeg } from './angle.ts';
|
|
2
|
+
export { clamp, inRange, saturate } from './clamp.ts';
|
|
3
|
+
export { approxEqual, approxEqualRelative, approxZero } from './comparison.ts';
|
|
4
|
+
export { CIRCLE_KAPPA, DEG_TO_RAD, EPSILON, HALF_PI, RAD_TO_DEG, TAU } from './constants.ts';
|
|
5
|
+
export { createRandomSourceFromHash, hash2D, hash3D, hashCombine, hashUint32 } from './hash.ts';
|
|
6
|
+
export { inverseLerp, lerp, remap, smoothStep, step } from './interpolation.ts';
|
|
7
|
+
export { damp, lerpAngle, moveTowards, pingPong, repeat, smootherStep } from './interpolationAdvanced.ts';
|
|
8
|
+
export { isPowerOfTwo, nextMultipleOf, nextPowerOfTwo, previousPowerOfTwo } from './nextPowerOfTwo.ts';
|
|
9
|
+
export { factorial, gcd, hypot2, isEven, isOdd, lcm } from './numberTheory.ts';
|
|
10
|
+
export { createRandomSource } from './random.ts';
|
|
11
|
+
export type { RandomSource } from './random.ts';
|
|
12
|
+
export { pick, randomExponential, randomGaussian, randomGaussianPair, randomInsideUnitDisc, randomInsideUnitSphere, randomOnUnitCircle, randomOnUnitSphere, randomPoisson, randomWeighted, shuffle, shuffleInPlace, } from './randomDistributions.ts';
|
|
13
|
+
export { randomBool, randomInt, randomRange, randomSign } from './randomRange.ts';
|
|
14
|
+
export { ceilTo, euclideanMod, floorTo, fract, roundTo } from './rounding.ts';
|
|
15
|
+
export { ceilPowerOfTwo, floorPowerOfTwo, quantize, sign } from './scalar.ts';
|
|
16
|
+
export { mean, median, standardDeviation, variance, weightedAverage } from './statistics.ts';
|
|
16
17
|
//# sourceMappingURL=contract.d.ts.map
|
package/dist/contract.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"contract.d.ts","sourceRoot":"","sources":["../src/contract.ts"],"names":[],"mappings":"AAGA,cAAc,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"contract.d.ts","sourceRoot":"","sources":["../src/contract.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAC5E,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtD,OAAO,EAAE,WAAW,EAAE,mBAAmB,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC/E,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AAC7F,OAAO,EAAE,0BAA0B,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAChG,OAAO,EAAE,WAAW,EAAE,IAAI,EAAE,KAAK,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAChF,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAC1G,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AACvG,OAAO,EAAE,SAAS,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAC;AAC/E,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACjD,YAAY,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,EACL,IAAI,EACJ,iBAAiB,EACjB,cAAc,EACd,kBAAkB,EAClB,oBAAoB,EACpB,sBAAsB,EACtB,kBAAkB,EAClB,kBAAkB,EAClB,aAAa,EACb,cAAc,EACd,OAAO,EACP,cAAc,GACf,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAClF,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAC9E,OAAO,EAAE,cAAc,EAAE,eAAe,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAC9E,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,iBAAiB,EAAE,QAAQ,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC"}
|
package/dist/contract.js
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
// Generated from crates/flight-math/src/lib.rs by flight-compiler.
|
|
2
2
|
// Rust is authoritative; run `npm run generate` after changing it.
|
|
3
|
-
export
|
|
4
|
-
export
|
|
5
|
-
export
|
|
6
|
-
export
|
|
7
|
-
export
|
|
8
|
-
export
|
|
9
|
-
export
|
|
10
|
-
export
|
|
11
|
-
export
|
|
12
|
-
export
|
|
13
|
-
export
|
|
14
|
-
export
|
|
15
|
-
export
|
|
16
|
-
export
|
|
17
|
-
export
|
|
3
|
+
export { degToRad, deltaAngle, normalizeAngle, radToDeg } from "./angle.js";
|
|
4
|
+
export { clamp, inRange, saturate } from "./clamp.js";
|
|
5
|
+
export { approxEqual, approxEqualRelative, approxZero } from "./comparison.js";
|
|
6
|
+
export { CIRCLE_KAPPA, DEG_TO_RAD, EPSILON, HALF_PI, RAD_TO_DEG, TAU } from "./constants.js";
|
|
7
|
+
export { createRandomSourceFromHash, hash2D, hash3D, hashCombine, hashUint32 } from "./hash.js";
|
|
8
|
+
export { inverseLerp, lerp, remap, smoothStep, step } from "./interpolation.js";
|
|
9
|
+
export { damp, lerpAngle, moveTowards, pingPong, repeat, smootherStep } from "./interpolationAdvanced.js";
|
|
10
|
+
export { isPowerOfTwo, nextMultipleOf, nextPowerOfTwo, previousPowerOfTwo } from "./nextPowerOfTwo.js";
|
|
11
|
+
export { factorial, gcd, hypot2, isEven, isOdd, lcm } from "./numberTheory.js";
|
|
12
|
+
export { createRandomSource } from "./random.js";
|
|
13
|
+
export { pick, randomExponential, randomGaussian, randomGaussianPair, randomInsideUnitDisc, randomInsideUnitSphere, randomOnUnitCircle, randomOnUnitSphere, randomPoisson, randomWeighted, shuffle, shuffleInPlace, } from "./randomDistributions.js";
|
|
14
|
+
export { randomBool, randomInt, randomRange, randomSign } from "./randomRange.js";
|
|
15
|
+
export { ceilTo, euclideanMod, floorTo, fract, roundTo } from "./rounding.js";
|
|
16
|
+
export { ceilPowerOfTwo, floorPowerOfTwo, quantize, sign } from "./scalar.js";
|
|
17
|
+
export { mean, median, standardDeviation, variance, weightedAverage } from "./statistics.js";
|
|
18
18
|
//# sourceMappingURL=contract.js.map
|
package/dist/contract.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"contract.js","sourceRoot":"","sources":["../src/contract.ts"],"names":[],"mappings":"AAAA,mEAAmE;AACnE,mEAAmE;AAEnE,cAAc,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"contract.js","sourceRoot":"","sources":["../src/contract.ts"],"names":[],"mappings":"AAAA,mEAAmE;AACnE,mEAAmE;AAEnE,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAC5E,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM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,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,iBAAiB,EAAE,QAAQ,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC"}
|
package/dist/hash.js
CHANGED
|
@@ -17,13 +17,13 @@ export function createRandomSourceFromHash(x, y) {
|
|
|
17
17
|
* tile/cell content and gradient noise seed generation.
|
|
18
18
|
*/
|
|
19
19
|
export function hash2D(x, y) {
|
|
20
|
-
return hashCombine(hashUint32(x >>> 0), y >>> 0);
|
|
20
|
+
return hashCombine(hashUint32(x >>> 0), y >>> 0) >>> 0;
|
|
21
21
|
}
|
|
22
22
|
/**
|
|
23
23
|
* Stateless position-seeded hash over a 3D integer grid.
|
|
24
24
|
*/
|
|
25
25
|
export function hash3D(x, y, z) {
|
|
26
|
-
return hashCombine(hash2D(x, y), z >>> 0);
|
|
26
|
+
return hashCombine(hash2D(x, y), z >>> 0) >>> 0;
|
|
27
27
|
}
|
|
28
28
|
/**
|
|
29
29
|
* Combine a running hash `seed` with a new `value` using a Murmur3-inspired mix.
|
|
@@ -32,8 +32,8 @@ export function hash3D(x, y, z) {
|
|
|
32
32
|
*/
|
|
33
33
|
export function hashCombine(seed, value) {
|
|
34
34
|
const seedI = seed | 0;
|
|
35
|
-
const inner = (((((((value +
|
|
36
|
-
return hashUint32((seed ^ inner) >>> 0);
|
|
35
|
+
const inner = (((((((value + 0x9e3779b9) >>> 0) + (seedI << 6)) >>> 0) >>> 0) + (seedI >> 2)) >>> 0) >>> 0;
|
|
36
|
+
return hashUint32((seed ^ inner) >>> 0) >>> 0;
|
|
37
37
|
}
|
|
38
38
|
/**
|
|
39
39
|
* Finalise an arbitrary 32-bit integer through the fmix32 finalizer (Murmur3).
|
|
@@ -44,10 +44,10 @@ export function hashCombine(seed, value) {
|
|
|
44
44
|
export function hashUint32(value) {
|
|
45
45
|
let h = value;
|
|
46
46
|
h = (h ^ (h >>> 16)) >>> 0;
|
|
47
|
-
h = Math.imul(h,
|
|
47
|
+
h = Math.imul(h, 0x85ebca6b) >>> 0;
|
|
48
48
|
h = (h ^ (h >>> 13)) >>> 0;
|
|
49
|
-
h = Math.imul(h,
|
|
49
|
+
h = Math.imul(h, 0xc2b2ae35) >>> 0;
|
|
50
50
|
h = (h ^ (h >>> 16)) >>> 0;
|
|
51
|
-
return h;
|
|
51
|
+
return h >>> 0;
|
|
52
52
|
}
|
|
53
53
|
//# sourceMappingURL=hash.js.map
|
package/dist/hash.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hash.js","sourceRoot":"","sources":["../src/hash.ts"],"names":[],"mappings":"AAAA,oEAAoE;AACpE,mEAAmE;AAGnE,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAEjD;;;;;GAKG;AACH,MAAM,UAAU,0BAA0B,CAAC,CAAS,EAAE,CAAS;IAC7D,OAAO,kBAAkB,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AAC1C,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,MAAM,CAAC,CAAS,EAAE,CAAS;IACzC,OAAO,WAAW,CAAC,UAAU,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"hash.js","sourceRoot":"","sources":["../src/hash.ts"],"names":[],"mappings":"AAAA,oEAAoE;AACpE,mEAAmE;AAGnE,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAEjD;;;;;GAKG;AACH,MAAM,UAAU,0BAA0B,CAAC,CAAS,EAAE,CAAS;IAC7D,OAAO,kBAAkB,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AAC1C,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,MAAM,CAAC,CAAS,EAAE,CAAS;IACzC,OAAO,WAAW,CAAC,UAAU,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,KAAK,CAA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AC,KAAK,CAAC,CAAC;AACjB,CAAC"}
|
package/dist/nextPowerOfTwo.js
CHANGED
|
@@ -28,7 +28,7 @@ export function nextMultipleOf(value, multiple) {
|
|
|
28
28
|
*/
|
|
29
29
|
export function nextPowerOfTwo(n) {
|
|
30
30
|
if (n <= 1) {
|
|
31
|
-
return 1;
|
|
31
|
+
return 1 >>> 0;
|
|
32
32
|
}
|
|
33
33
|
let v = (n - 1) >>> 0;
|
|
34
34
|
v |= v >>> 1;
|
|
@@ -36,7 +36,7 @@ export function nextPowerOfTwo(n) {
|
|
|
36
36
|
v |= v >>> 4;
|
|
37
37
|
v |= v >>> 8;
|
|
38
38
|
v |= v >>> 16;
|
|
39
|
-
return (v + 1) >>> 0;
|
|
39
|
+
return ((v + 1) >>> 0) >>> 0;
|
|
40
40
|
}
|
|
41
41
|
/**
|
|
42
42
|
* Round `n` down to the largest power of two that is `<= n`.
|
|
@@ -45,7 +45,7 @@ export function nextPowerOfTwo(n) {
|
|
|
45
45
|
*/
|
|
46
46
|
export function previousPowerOfTwo(n) {
|
|
47
47
|
if (n <= 1) {
|
|
48
|
-
return 1;
|
|
48
|
+
return 1 >>> 0;
|
|
49
49
|
}
|
|
50
50
|
let v = n;
|
|
51
51
|
v |= v >> 1;
|
|
@@ -53,6 +53,6 @@ export function previousPowerOfTwo(n) {
|
|
|
53
53
|
v |= v >> 4;
|
|
54
54
|
v |= v >> 8;
|
|
55
55
|
v |= v >> 16;
|
|
56
|
-
return (((v >>> 0) + 1) >>> 0) >>> 1;
|
|
56
|
+
return ((((v >>> 0) + 1) >>> 0) >>> 1) >>> 0;
|
|
57
57
|
}
|
|
58
58
|
//# sourceMappingURL=nextPowerOfTwo.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nextPowerOfTwo.js","sourceRoot":"","sources":["../src/nextPowerOfTwo.ts"],"names":[],"mappings":"AAAA,iFAAiF;AACjF,mEAAmE;AAEnE;;;;GAIG;AACH,MAAM,UAAU,YAA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C,IAAI,CAAC,EAAE,CAAC;QACX,OAAO,CAAC,KAAK,CAAC,CAAC;IACjB,CAAC;IACD,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC;IACtB,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACb,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACb,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACb,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACb,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;IACd,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC;AAC/B,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CAAC,CAAS;IAC1C,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;QACX,OAAO,CAAC,KAAK,CAAC,CAAC;IACjB,CAAC;IACD,IAAI,CAAC,GAAG,CAAC,CAAC;IACV,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACZ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACZ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACZ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACZ,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;IACb,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC;AAC/C,CAAC"}
|
package/dist/random.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import type { RandomSource } from '@flighthq/types/contract';
|
|
2
1
|
/**
|
|
3
2
|
* A callable random source. Cloning it shares the stream, as a TS function reference does.
|
|
4
3
|
*/
|
|
5
|
-
export type
|
|
4
|
+
export type RandomSource = () => number;
|
|
6
5
|
/**
|
|
7
6
|
* Create a fast, deterministic pseudo-random generator seeded by an integer.
|
|
8
7
|
*
|
package/dist/random.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"random.d.ts","sourceRoot":"","sources":["../src/random.ts"],"names":[],"mappings":"AAGA
|
|
1
|
+
{"version":3,"file":"random.d.ts","sourceRoot":"","sources":["../src/random.ts"],"names":[],"mappings":"AAGA;;GAEG;AACH,MAAM,MAAM,YAAY,GAAG,MAAM,MAAM,CAAC;AAExC;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,CAS7D"}
|
package/dist/random.js
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
export function createRandomSource(seed) {
|
|
11
11
|
const state = { value: seed >>> 0 };
|
|
12
12
|
return () => {
|
|
13
|
-
const a = (state.value = (state.value +
|
|
13
|
+
const a = (state.value = (state.value + 0x6d2b79f5) >>> 0);
|
|
14
14
|
let t = Math.imul((a ^ (a >>> 15)) >>> 0, 1 | a) >>> 0;
|
|
15
15
|
t = ((((t + Math.imul((t ^ (t >>> 7)) >>> 0, 61 | t)) >>> 0) >>> 0) ^ t) >>> 0;
|
|
16
16
|
const sample = ((t ^ (t >>> 14)) >>> 0) / 4294967296.0;
|
package/dist/random.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"random.js","sourceRoot":"","sources":["../src/random.ts"],"names":[],"mappings":"AAAA,sEAAsE;AACtE,mEAAmE;
|
|
1
|
+
{"version":3,"file":"random.js","sourceRoot":"","sources":["../src/random.ts"],"names":[],"mappings":"AAAA,sEAAsE;AACtE,mEAAmE;AAOnE;;;;;;GAMG;AACH,MAAM,UAAU,kBAAkB,CAAC,IAAY;IAC7C,MAAM,KAAK,GAAG,EAAE,KAAK,EAAE,IAAI,KAAK,CAAC,EAAE,CAAC;IACpC,OAAO,GAAG,EAAE;QACV,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,GAAG,CAAC,KAAK,CAAC,KAAK,GAAG,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC;QAC3D,IAAI,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC;QACvD,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,EAAE,EAAE,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC;QAC/E,MAAM,MAAM,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,YAAY,CAAC;QACvD,OAAO,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC,CAAC;IACxD,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
import type { RandomSource } from './random.ts';
|
|
1
2
|
/**
|
|
2
3
|
* Pick a random element from `items`, returning `undefined` when `items` is empty.
|
|
3
4
|
*
|
|
4
5
|
* Does not mutate `items`. Deterministic: same random sequence -> same picks.
|
|
5
6
|
*/
|
|
6
|
-
export declare function pick<T>(random:
|
|
7
|
+
export declare function pick<T>(random: RandomSource, items: Readonly<T[]>): T | undefined;
|
|
7
8
|
/**
|
|
8
9
|
* Return an exponentially-distributed random number with the given `rate`
|
|
9
10
|
* (lambda > 0, mean = 1/lambda).
|
|
@@ -12,14 +13,14 @@ export declare function pick<T>(random: () => number, items: Readonly<T[]>): T |
|
|
|
12
13
|
* The result is always non-negative. Throws if `rate` is not finite or not
|
|
13
14
|
* positive.
|
|
14
15
|
*/
|
|
15
|
-
export declare function randomExponential(random:
|
|
16
|
+
export declare function randomExponential(random: RandomSource, rate?: number): number;
|
|
16
17
|
/**
|
|
17
18
|
* Return a normally-distributed random number with the given `mean` and
|
|
18
19
|
* `standardDeviation`, using the Box-Muller transform.
|
|
19
20
|
*
|
|
20
21
|
* Each call consumes two values from `random` and discards one.
|
|
21
22
|
*/
|
|
22
|
-
export declare function randomGaussian(random:
|
|
23
|
+
export declare function randomGaussian(random: RandomSource, mean?: number, standardDeviation?: number): number;
|
|
23
24
|
/**
|
|
24
25
|
* Return two independent normally-distributed random numbers as `(z0, z1)`
|
|
25
26
|
* sharing the same Box-Muller pair.
|
|
@@ -27,7 +28,7 @@ export declare function randomGaussian(random: () => number, mean?: number, stan
|
|
|
27
28
|
* Prefer this over calling `randomGaussian` twice when both samples are
|
|
28
29
|
* needed — it consumes exactly 2 values from `random` instead of 4.
|
|
29
30
|
*/
|
|
30
|
-
export declare function randomGaussianPair(random:
|
|
31
|
+
export declare function randomGaussianPair(random: RandomSource, mean?: number, standardDeviation?: number): readonly [number, number];
|
|
31
32
|
/**
|
|
32
33
|
* Sample a uniformly-distributed point inside the unit disc (including the
|
|
33
34
|
* boundary) and write it into `out[0]` / `out[1]`.
|
|
@@ -35,49 +36,49 @@ export declare function randomGaussianPair(random: () => number, mean?: number,
|
|
|
35
36
|
* Uses rejection sampling to avoid the over-dense centre that polar-radius
|
|
36
37
|
* sampling without a square-root correction produces.
|
|
37
38
|
*/
|
|
38
|
-
export declare function randomInsideUnitDisc(out:
|
|
39
|
+
export declare function randomInsideUnitDisc(out: Float32Array, random: RandomSource): void;
|
|
39
40
|
/**
|
|
40
41
|
* Sample a uniformly-distributed point inside the unit sphere (radius <= 1)
|
|
41
42
|
* and write it into `out[0]` / `out[1]` / `out[2]`.
|
|
42
43
|
*
|
|
43
44
|
* Uses 3D box rejection sampling.
|
|
44
45
|
*/
|
|
45
|
-
export declare function randomInsideUnitSphere(out:
|
|
46
|
+
export declare function randomInsideUnitSphere(out: Float32Array, random: RandomSource): void;
|
|
46
47
|
/**
|
|
47
48
|
* Sample a uniformly-distributed point on the unit circle and write it into
|
|
48
49
|
* `out[0]` / `out[1]`. The result has unit length.
|
|
49
50
|
*/
|
|
50
|
-
export declare function randomOnUnitCircle(out:
|
|
51
|
+
export declare function randomOnUnitCircle(out: Float32Array, random: RandomSource): void;
|
|
51
52
|
/**
|
|
52
53
|
* Sample a uniformly-distributed point on the surface of the unit sphere
|
|
53
54
|
* and write it into `out[0]` / `out[1]` / `out[2]`.
|
|
54
55
|
*
|
|
55
56
|
* Uses the Marsaglia (1972) method.
|
|
56
57
|
*/
|
|
57
|
-
export declare function randomOnUnitSphere(out:
|
|
58
|
+
export declare function randomOnUnitSphere(out: Float32Array, random: RandomSource): void;
|
|
58
59
|
/**
|
|
59
60
|
* Return a Poisson-distributed random integer with mean `lambda` (lambda > 0).
|
|
60
61
|
*
|
|
61
62
|
* Uses Knuth's multiplicative algorithm. Accurate and simple for small
|
|
62
63
|
* lambda (lambda <= ~30). Throws if `lambda` is not finite or not positive.
|
|
63
64
|
*/
|
|
64
|
-
export declare function randomPoisson(random:
|
|
65
|
+
export declare function randomPoisson(random: RandomSource, lambda?: number): number;
|
|
65
66
|
/**
|
|
66
67
|
* Return a random index from a weight array, selected proportionally to
|
|
67
68
|
* each weight. Weights do not need to sum to 1.
|
|
68
69
|
*
|
|
69
70
|
* Returns `-1` for an empty or all-zero weight array.
|
|
70
71
|
*/
|
|
71
|
-
export declare function randomWeighted(random:
|
|
72
|
+
export declare function randomWeighted(random: RandomSource, weights: Readonly<number[]>): number;
|
|
72
73
|
/**
|
|
73
74
|
* Clear and fill `out` with the elements of `items` shuffled in a random order
|
|
74
75
|
* (Fisher-Yates / Knuth shuffle). Reuses the caller's allocation and does not mutate `items`.
|
|
75
76
|
*/
|
|
76
|
-
export declare function shuffle<T>(out: T[], random:
|
|
77
|
+
export declare function shuffle<T>(out: T[], random: RandomSource, items: Readonly<T[]>): void;
|
|
77
78
|
/**
|
|
78
79
|
* Shuffle `items` in place using the Fisher-Yates algorithm.
|
|
79
80
|
*
|
|
80
81
|
* Mutates `items`. Deterministic: same random state -> same shuffled order.
|
|
81
82
|
*/
|
|
82
|
-
export declare function shuffleInPlace<T>(random:
|
|
83
|
+
export declare function shuffleInPlace<T>(random: RandomSource, items: T[]): void;
|
|
83
84
|
//# sourceMappingURL=randomDistributions.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"randomDistributions.d.ts","sourceRoot":"","sources":["../src/randomDistributions.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"randomDistributions.d.ts","sourceRoot":"","sources":["../src/randomDistributions.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAIhD;;;;GAIG;AACH,wBAAgB,IAAI,CAAC,CAAC,EAAE,MAAM,EAAE,YAAY,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,GAAG,SAAS,CAMjF;AAED;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,YAAY,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,CAQ7E;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,YAAY,EAAE,IAAI,CAAC,EAAE,MAAM,EAAE,iBAAiB,CAAC,EAAE,MAAM,GAAG,MAAM,CAStG;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,YAAY,EACpB,IAAI,CAAC,EAAE,MAAM,EACb,iBAAiB,CAAC,EAAE,MAAM,GACzB,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC,CAW3B;AAED;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,YAAY,EAAE,MAAM,EAAE,YAAY,GAAG,IAAI,CAUlF;AAED;;;;;GAKG;AACH,wBAAgB,sBAAsB,CAAC,GAAG,EAAE,YAAY,EAAE,MAAM,EAAE,YAAY,GAAG,IAAI,CAYpF;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,YAAY,EAAE,MAAM,EAAE,YAAY,GAAG,IAAI,CAMhF;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,YAAY,EAAE,MAAM,EAAE,YAAY,GAAG,IAAI,CAmBhF;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,YAAY,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,CAa3E;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,YAAY,EAAE,OAAO,EAAE,QAAQ,CAAC,MAAM,EAAE,CAAC,GAAG,MAAM,CAoBxF;AAED;;;GAGG;AACH,wBAAgB,OAAO,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,EAAE,MAAM,EAAE,YAAY,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC,EAAE,CAAC,GAAG,IAAI,CAMrF;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAAE,MAAM,EAAE,YAAY,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,IAAI,CAWxE"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Generated from crates/flight-math/src/random_distributions.rs by flight-compiler.
|
|
2
2
|
// Rust is authoritative; run `npm run generate` after changing it.
|
|
3
|
-
const ZERO_SAMPLE_EPSILON = 1.0 /
|
|
3
|
+
const ZERO_SAMPLE_EPSILON = 1.0 / 4_503_599_627_370_496.0;
|
|
4
4
|
/**
|
|
5
5
|
* Pick a random element from `items`, returning `undefined` when `items` is empty.
|
|
6
6
|
*
|
|
@@ -10,7 +10,7 @@ export function pick(random, items) {
|
|
|
10
10
|
if (items.length === 0) {
|
|
11
11
|
return undefined;
|
|
12
12
|
}
|
|
13
|
-
const index = Math.floor(random() * items.length);
|
|
13
|
+
const index = Math.floor(random() * items.length) >>> 0;
|
|
14
14
|
return items[index];
|
|
15
15
|
}
|
|
16
16
|
/**
|
|
@@ -107,7 +107,7 @@ export function randomInsideUnitSphere(out, random) {
|
|
|
107
107
|
* `out[0]` / `out[1]`. The result has unit length.
|
|
108
108
|
*/
|
|
109
109
|
export function randomOnUnitCircle(out, random) {
|
|
110
|
-
const angle = random() * Math.PI * 2;
|
|
110
|
+
const angle = random() * (Math.PI * 2);
|
|
111
111
|
const x = Math.cos(angle);
|
|
112
112
|
const y = Math.sin(angle);
|
|
113
113
|
out[0] = x;
|
|
@@ -192,7 +192,9 @@ export function randomWeighted(random, weights) {
|
|
|
192
192
|
*/
|
|
193
193
|
export function shuffle(out, random, items) {
|
|
194
194
|
out.length = 0;
|
|
195
|
-
|
|
195
|
+
for (let index = 0, count = items.length; index < count; index++) {
|
|
196
|
+
out.push(items[index]);
|
|
197
|
+
}
|
|
196
198
|
shuffleInPlace(random, out);
|
|
197
199
|
}
|
|
198
200
|
/**
|
|
@@ -207,7 +209,7 @@ export function shuffleInPlace(random, items) {
|
|
|
207
209
|
}
|
|
208
210
|
let i = len - 1;
|
|
209
211
|
while (i > 0) {
|
|
210
|
-
const j = Math.floor(random() * (i + 1));
|
|
212
|
+
const j = Math.floor(random() * (i + 1)) >>> 0;
|
|
211
213
|
[items[i], items[j]] = [items[j], items[i]];
|
|
212
214
|
i -= 1;
|
|
213
215
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"randomDistributions.js","sourceRoot":"","sources":["../src/randomDistributions.ts"],"names":[],"mappings":"AAAA,oFAAoF;AACpF,mEAAmE;
|
|
1
|
+
{"version":3,"file":"randomDistributions.js","sourceRoot":"","sources":["../src/randomDistributions.ts"],"names":[],"mappings":"AAAA,oFAAoF;AACpF,mEAAmE;AAInE,MAAM,mBAAmB,GAAW,GAAG,GAAG,uBAAuB,CAAC;AAElE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,GAAG,CAAC,CAAC;AAC9B,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAAI,MAAoB,EAAE,KAAU;IAChE,MAAM,GAAG,GAAG,KAAK,CAAC,MAAM,CAAC;IACzB,IAAI,GAAG,IAAI,CAAC,EAAE,CAAC;QACb,OAAO;IACT,CAAC;IACD,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC,CAAC;IAChB,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;QACb,MAAM,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;QAC/C,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;QAC5C,CAAC,IAAI,CAAC,CAAC;IACT,CAAC;AACH,CAAC"}
|
package/dist/randomRange.d.ts
CHANGED
|
@@ -1,21 +1,22 @@
|
|
|
1
|
+
import type { RandomSource } from './random.ts';
|
|
1
2
|
/**
|
|
2
3
|
* Return a random boolean with the given `probability` of being `true`.
|
|
3
4
|
*
|
|
4
5
|
* Values outside `[0, 1]` behave like the nearest endpoint; `0.5` is a fair coin flip.
|
|
5
6
|
*/
|
|
6
|
-
export declare function randomBool(random:
|
|
7
|
+
export declare function randomBool(random: RandomSource, probability?: number): boolean;
|
|
7
8
|
/**
|
|
8
9
|
* Return a random integer in the inclusive range `[min, max]`.
|
|
9
10
|
*
|
|
10
11
|
* Throws if `min` is greater than `max`.
|
|
11
12
|
*/
|
|
12
|
-
export declare function randomInt(random:
|
|
13
|
+
export declare function randomInt(random: RandomSource, min: number, max: number): number;
|
|
13
14
|
/**
|
|
14
15
|
* Return a random float in the half-open range `[min, max)`.
|
|
15
16
|
*/
|
|
16
|
-
export declare function randomRange(random:
|
|
17
|
+
export declare function randomRange(random: RandomSource, min: number, max: number): number;
|
|
17
18
|
/**
|
|
18
19
|
* Return a random sign: either `1` or `-1` with equal probability.
|
|
19
20
|
*/
|
|
20
|
-
export declare function randomSign(random:
|
|
21
|
+
export declare function randomSign(random: RandomSource): number;
|
|
21
22
|
//# sourceMappingURL=randomRange.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"randomRange.d.ts","sourceRoot":"","sources":["../src/randomRange.ts"],"names":[],"mappings":"AAGA;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,
|
|
1
|
+
{"version":3,"file":"randomRange.d.ts","sourceRoot":"","sources":["../src/randomRange.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAEhD;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,YAAY,EAAE,WAAW,CAAC,EAAE,MAAM,GAAG,OAAO,CAG9E;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,MAAM,EAAE,YAAY,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAKhF;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,YAAY,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAElF;AAED;;GAEG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,YAAY,GAAG,MAAM,CAEvD"}
|
package/dist/randomRange.js
CHANGED
|
@@ -18,7 +18,7 @@ export function randomInt(random, min, max) {
|
|
|
18
18
|
if (min > max) {
|
|
19
19
|
throw new RangeError('randomInt: min must be <= max');
|
|
20
20
|
}
|
|
21
|
-
return
|
|
21
|
+
return min + (Math.floor(random() * (max - min + 1)) | 0);
|
|
22
22
|
}
|
|
23
23
|
/**
|
|
24
24
|
* Return a random float in the half-open range `[min, max)`.
|
package/dist/randomRange.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"randomRange.js","sourceRoot":"","sources":["../src/randomRange.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,mEAAmE;
|
|
1
|
+
{"version":3,"file":"randomRange.js","sourceRoot":"","sources":["../src/randomRange.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,mEAAmE;AAInE;;;;GAIG;AACH,MAAM,UAAU,UAAU,CAAC,MAAoB,EAAE,WAAoB;IACnE,WAAW,GAAG,WAAW,IAAI,GAAG,CAAC;IACjC,OAAO,MAAM,EAAE,GAAG,WAAW,CAAC;AAChC,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,SAAS,CAAC,MAAoB,EAAE,GAAW,EAAE,GAAW;IACtE,IAAI,GAAG,GAAG,GAAG,EAAE,CAAC;QACd,MAAM,IAAI,UAAU,CAAC,+BAA+B,CAAC,CAAC;IACxD,CAAC;IACD,OAAO,GAAG,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,GAAG,GAAG,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;AAC5D,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,WAAW,CAAC,MAAoB,EAAE,GAAW,EAAE,GAAW;IACxE,OAAO,GAAG,GAAG,MAAM,EAAE,GAAG,CAAC,GAAG,GAAG,GAAG,CAAC,CAAC;AACtC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,UAAU,CAAC,MAAoB;IAC7C,OAAO,MAAM,EAAE,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC;AACrC,CAAC"}
|
package/dist/scalar.js
CHANGED
|
@@ -8,7 +8,7 @@ import { nextPowerOfTwo, previousPowerOfTwo } from "./nextPowerOfTwo.js";
|
|
|
8
8
|
* sites where the direction is important.
|
|
9
9
|
*/
|
|
10
10
|
export function ceilPowerOfTwo(n) {
|
|
11
|
-
return nextPowerOfTwo(n);
|
|
11
|
+
return nextPowerOfTwo(n) >>> 0;
|
|
12
12
|
}
|
|
13
13
|
/**
|
|
14
14
|
* Round `n` down to the previous power of two.
|
|
@@ -17,7 +17,7 @@ export function ceilPowerOfTwo(n) {
|
|
|
17
17
|
* call sites where the direction is important.
|
|
18
18
|
*/
|
|
19
19
|
export function floorPowerOfTwo(n) {
|
|
20
|
-
return previousPowerOfTwo(n);
|
|
20
|
+
return previousPowerOfTwo(n) >>> 0;
|
|
21
21
|
}
|
|
22
22
|
/**
|
|
23
23
|
* Quantize `value` into `steps` equal buckets over `[min, max]`.
|
package/dist/scalar.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scalar.js","sourceRoot":"","sources":["../src/scalar.ts"],"names":[],"mappings":"AAAA,sEAAsE;AACtE,mEAAmE;AAEnE,OAAO,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAEzE;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAAC,CAAS;IACtC,OAAO,cAAc,CAAC,CAAC,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"scalar.js","sourceRoot":"","sources":["../src/scalar.ts"],"names":[],"mappings":"AAAA,sEAAsE;AACtE,mEAAmE;AAEnE,OAAO,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAEzE;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAAC,CAAS;IACtC,OAAO,cAAc,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;AACjC,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,eAAe,CAAC,CAAS;IACvC,OAAO,kBAAkB,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;AACrC,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,QAAQ,CAAC,KAAa,EAAE,KAAa,EAAE,GAAW,EAAE,GAAW;IAC7E,IAAI,KAAK,IAAI,GAAG,IAAI,GAAG,KAAK,GAAG,EAAE,CAAC;QAChC,OAAO,GAAG,CAAC;IACb,CAAC;IACD,MAAM,CAAC,GAAG,CAAC,KAAK,GAAG,GAAG,CAAC,GAAG,CAAC,GAAG,GAAG,GAAG,CAAC,CAAC;IACtC,MAAM,OAAO,GAAG,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;IAClD,OAAO,GAAG,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,GAAG,KAAK,CAAC,GAAG,KAAK,CAAC,GAAG,CAAC,GAAG,GAAG,GAAG,CAAC,CAAC;AACnE,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,IAAI,CAAC,KAAa;IAChC,OAAO,KAAK,GAAG,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC;AACtD,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flighthq/math",
|
|
3
|
-
"version": "0.5.1-next.
|
|
3
|
+
"version": "0.5.1-next.4305.c5b7dc0",
|
|
4
4
|
"author": "Joshua Granick and other contributors",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -33,14 +33,12 @@
|
|
|
33
33
|
"build": "tsc -b",
|
|
34
34
|
"clean": "tsc -b --clean",
|
|
35
35
|
"generate": "tsx ../../scripts/generate-rust-package.ts --crate flight-math --package math",
|
|
36
|
+
"generate:check": "tsx ../../scripts/generate-rust-package.ts --crate flight-math --package math --check",
|
|
36
37
|
"test": "vitest run --config vitest.config.ts",
|
|
37
38
|
"test:watch": "vitest --watch --config vitest.config.ts",
|
|
38
39
|
"prepack": "npm run clean && npm run clean:dist && npm run build",
|
|
39
40
|
"clean:dist": "tsx ../../scripts/clean-package-dist.ts"
|
|
40
41
|
},
|
|
41
|
-
"dependencies": {
|
|
42
|
-
"@flighthq/types": "0.5.1-next.3308.79ae053"
|
|
43
|
-
},
|
|
44
42
|
"devDependencies": {
|
|
45
43
|
"typescript": "^5.3.0"
|
|
46
44
|
},
|
|
@@ -18,7 +18,7 @@ describe('inverseLerp', () => {
|
|
|
18
18
|
it('returns 1 when value equals b', () => {
|
|
19
19
|
expect(inverseLerp(0.0, 10.0, 10.0)).toEqual(1.0);
|
|
20
20
|
});
|
|
21
|
-
it('returns 0
|
|
21
|
+
it('returns 0.5 for the midpoint', () => {
|
|
22
22
|
expect(inverseLerp(0.0, 10.0, 5.0)).toEqual(0.5);
|
|
23
23
|
});
|
|
24
24
|
it('returns 0 when a equals b to avoid division by zero', () => {
|
|
@@ -73,7 +73,7 @@ describe('smoothStep', () => {
|
|
|
73
73
|
it('returns 1 at the upper edge', () => {
|
|
74
74
|
expect(smoothStep(0.0, 1.0, 1.0)).toEqual(1.0);
|
|
75
75
|
});
|
|
76
|
-
it('returns 0
|
|
76
|
+
it('returns 0.5 at the midpoint', () => {
|
|
77
77
|
expect(smoothStep(0.0, 1.0, 0.5)).toEqual(0.5);
|
|
78
78
|
});
|
|
79
79
|
it('returns 0 for x below the lower edge', () => {
|
|
@@ -84,7 +84,7 @@ describe('pingPong', () => {
|
|
|
84
84
|
it('reaches length at t equals length', () => {
|
|
85
85
|
expect(pingPong(1.0, 1.0)).toEqual(1.0);
|
|
86
86
|
});
|
|
87
|
-
it('bounces back at t 1
|
|
87
|
+
it('bounces back at t 1.5 times length', () => {
|
|
88
88
|
assertClose(pingPong(1.5, 1.0), 0.5, 10);
|
|
89
89
|
});
|
|
90
90
|
it('propagates nan when length is 0', () => {
|
|
@@ -119,7 +119,7 @@ describe('smootherStep', () => {
|
|
|
119
119
|
it('returns 1 at the upper edge', () => {
|
|
120
120
|
expect(smootherStep(0.0, 1.0, 1.0)).toEqual(1.0);
|
|
121
121
|
});
|
|
122
|
-
it('returns 0
|
|
122
|
+
it('returns 0.5 at the midpoint', () => {
|
|
123
123
|
expect(smootherStep(0.0, 1.0, 0.5)).toEqual(0.5);
|
|
124
124
|
});
|
|
125
125
|
it('returns 0 for x below the lower edge', () => {
|
package/src/random.test.ts
CHANGED
|
@@ -22,8 +22,8 @@ describe('createRandomSource', () => {
|
|
|
22
22
|
}
|
|
23
23
|
});
|
|
24
24
|
it('is deterministic same seed yields the same sequence', () => {
|
|
25
|
-
const a = createRandomSource(
|
|
26
|
-
const b = createRandomSource(
|
|
25
|
+
const a = createRandomSource(0xc0ffee);
|
|
26
|
+
const b = createRandomSource(0xc0ffee);
|
|
27
27
|
for (let _ = 0; _ < 100; _ += 1) {
|
|
28
28
|
expect(a()).toEqual(b());
|
|
29
29
|
}
|
|
@@ -40,10 +40,10 @@ describe('createRandomSource', () => {
|
|
|
40
40
|
expect(differs).toBe(true);
|
|
41
41
|
});
|
|
42
42
|
it('pins the mulberry32 transform with a known answer sequence', () => {
|
|
43
|
-
const random = createRandomSource(
|
|
44
|
-
expect([random(), random(), random(), random()]).toEqual(
|
|
45
|
-
0.021141508361324668, 0.6661099966149777, 0.7799714196007699, 0.7395844468846917,
|
|
46
|
-
|
|
43
|
+
const random = createRandomSource(0xc0ffee);
|
|
44
|
+
expect(Array.from([random(), random(), random(), random()])).toEqual(
|
|
45
|
+
Array.from([0.021141508361324668, 0.6661099966149777, 0.7799714196007699, 0.7395844468846917]),
|
|
46
|
+
);
|
|
47
47
|
});
|
|
48
48
|
it('repeats the sequence when constructed with the same seed', () => {
|
|
49
49
|
const rng = createRandomSource(42);
|
|
@@ -34,11 +34,11 @@ describe('pick', () => {
|
|
|
34
34
|
let items = [0, 1, 2, 3];
|
|
35
35
|
shuffleInPlace(random, items);
|
|
36
36
|
items.sort((a, b) => a - b);
|
|
37
|
-
expect(items).toEqual([0, 1, 2, 3]);
|
|
37
|
+
expect(Array.from(items)).toEqual([0, 1, 2, 3]);
|
|
38
38
|
});
|
|
39
39
|
it('returns an element from the slice', () => {
|
|
40
40
|
const items = [1, 2, 3, 4, 5];
|
|
41
|
-
const random = createRandomSource(
|
|
41
|
+
const random = createRandomSource(0xabcdef);
|
|
42
42
|
for (let _ = 0; _ < 50; _ += 1) {
|
|
43
43
|
const result = pick(random, items);
|
|
44
44
|
expect(result !== undefined).toBe(true);
|
|
@@ -47,18 +47,18 @@ describe('pick', () => {
|
|
|
47
47
|
});
|
|
48
48
|
it('returns none for an empty slice', () => {
|
|
49
49
|
const empty: Readonly<number[]> = [];
|
|
50
|
-
expect(pick(createRandomSource(
|
|
50
|
+
expect(pick(createRandomSource(0xabcdef), empty) === undefined).toBe(true);
|
|
51
51
|
});
|
|
52
52
|
it('always returns the only element in a single item slice', () => {
|
|
53
|
-
const random = createRandomSource(
|
|
53
|
+
const random = createRandomSource(0xabcdef);
|
|
54
54
|
for (let _ = 0; _ < 20; _ += 1) {
|
|
55
55
|
expect(pick(random, [42])).toEqual(42);
|
|
56
56
|
}
|
|
57
57
|
});
|
|
58
58
|
it('is deterministic for the same seed', () => {
|
|
59
59
|
const items = ['a', 'b', 'c', 'd'];
|
|
60
|
-
const a = createRandomSource(
|
|
61
|
-
const b = createRandomSource(
|
|
60
|
+
const a = createRandomSource(0xabcdef);
|
|
61
|
+
const b = createRandomSource(0xabcdef);
|
|
62
62
|
for (let _ = 0; _ < 20; _ += 1) {
|
|
63
63
|
expect(pick(a, items)).toEqual(pick(b, items));
|
|
64
64
|
}
|
|
@@ -70,13 +70,13 @@ describe('pick', () => {
|
|
|
70
70
|
|
|
71
71
|
describe('randomExponential', () => {
|
|
72
72
|
it('returns a non negative value', () => {
|
|
73
|
-
const random = createRandomSource(
|
|
73
|
+
const random = createRandomSource(0xabcdef);
|
|
74
74
|
for (let _ = 0; _ < 200; _ += 1) {
|
|
75
75
|
expect(randomExponential(random, undefined) >= 0.0).toBe(true);
|
|
76
76
|
}
|
|
77
77
|
});
|
|
78
78
|
it('mean approximates 1 over rate for rate 1', () => {
|
|
79
|
-
const random = createRandomSource(
|
|
79
|
+
const random = createRandomSource(0xabcdef);
|
|
80
80
|
let sum = 0.0;
|
|
81
81
|
const n = 5000;
|
|
82
82
|
for (let _ = 0; _ < n; _ += 1) {
|
|
@@ -86,7 +86,7 @@ describe('randomExponential', () => {
|
|
|
86
86
|
expect(Math.abs(avg - 1.0) < 0.2).toBe(true);
|
|
87
87
|
});
|
|
88
88
|
it('mean approximates 1 over rate for rate 2', () => {
|
|
89
|
-
const random = createRandomSource(
|
|
89
|
+
const random = createRandomSource(0xabcdef);
|
|
90
90
|
let sum = 0.0;
|
|
91
91
|
const n = 5000;
|
|
92
92
|
for (let _ = 0; _ < n; _ += 1) {
|
|
@@ -97,27 +97,27 @@ describe('randomExponential', () => {
|
|
|
97
97
|
});
|
|
98
98
|
it('throws for rate zero', () => {
|
|
99
99
|
crate.assertRangeError(() => {
|
|
100
|
-
randomExponential(createRandomSource(
|
|
100
|
+
randomExponential(createRandomSource(0xabcdef), 0.0);
|
|
101
101
|
}, 'randomExponential: rate must be finite and > 0');
|
|
102
102
|
});
|
|
103
103
|
it('throws for negative rate', () => {
|
|
104
104
|
crate.assertRangeError(() => {
|
|
105
|
-
randomExponential(createRandomSource(
|
|
105
|
+
randomExponential(createRandomSource(0xabcdef), -1.0);
|
|
106
106
|
}, 'randomExponential: rate must be finite and > 0');
|
|
107
107
|
});
|
|
108
108
|
it('rejects nan rate', () => {
|
|
109
109
|
crate.assertRangeError(() => {
|
|
110
|
-
randomExponential(createRandomSource(
|
|
110
|
+
randomExponential(createRandomSource(0xabcdef), Number.NaN);
|
|
111
111
|
}, 'randomExponential: rate must be finite and > 0');
|
|
112
112
|
});
|
|
113
113
|
it('rejects infinite rate', () => {
|
|
114
114
|
crate.assertRangeError(() => {
|
|
115
|
-
randomExponential(createRandomSource(
|
|
115
|
+
randomExponential(createRandomSource(0xabcdef), Number.POSITIVE_INFINITY);
|
|
116
116
|
}, 'randomExponential: rate must be finite and > 0');
|
|
117
117
|
});
|
|
118
118
|
it('is deterministic for the same seed', () => {
|
|
119
|
-
const a = createRandomSource(
|
|
120
|
-
const b = createRandomSource(
|
|
119
|
+
const a = createRandomSource(0xabcdef);
|
|
120
|
+
const b = createRandomSource(0xabcdef);
|
|
121
121
|
for (let _ = 0; _ < 20; _ += 1) {
|
|
122
122
|
expect(randomExponential(a, undefined)).toEqual(randomExponential(b, undefined));
|
|
123
123
|
}
|
|
@@ -129,13 +129,13 @@ describe('randomExponential', () => {
|
|
|
129
129
|
|
|
130
130
|
describe('randomGaussian', () => {
|
|
131
131
|
it('produces finite values', () => {
|
|
132
|
-
const random = createRandomSource(
|
|
132
|
+
const random = createRandomSource(0xabcdef);
|
|
133
133
|
for (let _ = 0; _ < 100; _ += 1) {
|
|
134
134
|
expect(Number.isFinite(randomGaussian(random, undefined, undefined))).toBe(true);
|
|
135
135
|
}
|
|
136
136
|
});
|
|
137
137
|
it('centers around the mean', () => {
|
|
138
|
-
const random = createRandomSource(
|
|
138
|
+
const random = createRandomSource(0xabcdef);
|
|
139
139
|
let sum = 0.0;
|
|
140
140
|
const n = 2000;
|
|
141
141
|
for (let _ = 0; _ < n; _ += 1) {
|
|
@@ -145,8 +145,8 @@ describe('randomGaussian', () => {
|
|
|
145
145
|
expect(Math.abs(avg - 5.0) < 0.5).toBe(true);
|
|
146
146
|
});
|
|
147
147
|
it('is deterministic for the same seed', () => {
|
|
148
|
-
const a = createRandomSource(
|
|
149
|
-
const b = createRandomSource(
|
|
148
|
+
const a = createRandomSource(0xabcdef);
|
|
149
|
+
const b = createRandomSource(0xabcdef);
|
|
150
150
|
for (let _ = 0; _ < 20; _ += 1) {
|
|
151
151
|
expect(randomGaussian(a, undefined, undefined)).toEqual(randomGaussian(b, undefined, undefined));
|
|
152
152
|
}
|
|
@@ -167,13 +167,13 @@ describe('randomGaussian', () => {
|
|
|
167
167
|
|
|
168
168
|
describe('randomGaussianPair', () => {
|
|
169
169
|
it('returns a tuple of two finite values', () => {
|
|
170
|
-
const random = createRandomSource(
|
|
170
|
+
const random = createRandomSource(0xabcdef);
|
|
171
171
|
const [z0, z1] = randomGaussianPair(random, undefined, undefined);
|
|
172
172
|
expect(Number.isFinite(z0)).toBe(true);
|
|
173
173
|
expect(Number.isFinite(z1)).toBe(true);
|
|
174
174
|
});
|
|
175
175
|
it('produces values matching the requested mean', () => {
|
|
176
|
-
const random = createRandomSource(
|
|
176
|
+
const random = createRandomSource(0xabcdef);
|
|
177
177
|
let sum = 0.0;
|
|
178
178
|
const n = 2000;
|
|
179
179
|
for (let _ = 0; _ < n; _ += 1) {
|
|
@@ -200,18 +200,18 @@ describe('randomGaussianPair', () => {
|
|
|
200
200
|
|
|
201
201
|
describe('randomInsideUnitDisc', () => {
|
|
202
202
|
it('produces points inside the unit disc', () => {
|
|
203
|
-
const random = createRandomSource(
|
|
204
|
-
let out = new
|
|
203
|
+
const random = createRandomSource(0xabcdef);
|
|
204
|
+
let out = new Float32Array(2).fill(0.0);
|
|
205
205
|
for (let _ = 0; _ < 200; _ += 1) {
|
|
206
206
|
randomInsideUnitDisc(out, random);
|
|
207
207
|
expect(out[0] * out[0] + out[1] * out[1] <= 1.0 + 1e-9).toBe(true);
|
|
208
208
|
}
|
|
209
209
|
});
|
|
210
210
|
it('is deterministic for the same seed', () => {
|
|
211
|
-
const a = createRandomSource(
|
|
212
|
-
const b = createRandomSource(
|
|
213
|
-
let outA = new
|
|
214
|
-
let outB = new
|
|
211
|
+
const a = createRandomSource(0xabcdef);
|
|
212
|
+
const b = createRandomSource(0xabcdef);
|
|
213
|
+
let outA = new Float32Array(2).fill(0.0);
|
|
214
|
+
let outB = new Float32Array(2).fill(0.0);
|
|
215
215
|
for (let _ = 0; _ < 20; _ += 1) {
|
|
216
216
|
randomInsideUnitDisc(outA, a);
|
|
217
217
|
randomInsideUnitDisc(outB, b);
|
|
@@ -220,34 +220,34 @@ describe('randomInsideUnitDisc', () => {
|
|
|
220
220
|
}
|
|
221
221
|
});
|
|
222
222
|
it('is alias safe when out is the same object as input', () => {
|
|
223
|
-
const random = createRandomSource(
|
|
224
|
-
let out = new
|
|
223
|
+
const random = createRandomSource(0xabcdef);
|
|
224
|
+
let out = new Float32Array(2).fill(0.0);
|
|
225
225
|
randomInsideUnitDisc(out, random);
|
|
226
226
|
expect(Number.isFinite(out[0])).toBe(true);
|
|
227
227
|
expect(Number.isFinite(out[1])).toBe(true);
|
|
228
228
|
});
|
|
229
229
|
it('accepts and preserves a deterministic boundary point', () => {
|
|
230
230
|
const random = crate.sequenceRandom([1.0, 0.5, 0.5, 0.5]);
|
|
231
|
-
let out = new
|
|
231
|
+
let out = new Float32Array(2).fill(0.0);
|
|
232
232
|
randomInsideUnitDisc(out, random);
|
|
233
|
-
expect(out).toEqual([1.0, 0.0]);
|
|
233
|
+
expect(Array.from(out)).toEqual([1.0, 0.0]);
|
|
234
234
|
});
|
|
235
235
|
});
|
|
236
236
|
|
|
237
237
|
describe('randomInsideUnitSphere', () => {
|
|
238
238
|
it('produces points with radius less than or equal to 1', () => {
|
|
239
|
-
const random = createRandomSource(
|
|
240
|
-
let out = new
|
|
239
|
+
const random = createRandomSource(0xabcdef);
|
|
240
|
+
let out = new Float32Array(3).fill(0.0);
|
|
241
241
|
for (let _ = 0; _ < 200; _ += 1) {
|
|
242
242
|
randomInsideUnitSphere(out, random);
|
|
243
243
|
expect(out[0] * out[0] + out[1] * out[1] + out[2] * out[2] <= 1.0 + 1e-9).toBe(true);
|
|
244
244
|
}
|
|
245
245
|
});
|
|
246
246
|
it('is deterministic for the same seed', () => {
|
|
247
|
-
const a = createRandomSource(
|
|
248
|
-
const b = createRandomSource(
|
|
249
|
-
let outA = new
|
|
250
|
-
let outB = new
|
|
247
|
+
const a = createRandomSource(0xabcdef);
|
|
248
|
+
const b = createRandomSource(0xabcdef);
|
|
249
|
+
let outA = new Float32Array(3).fill(0.0);
|
|
250
|
+
let outB = new Float32Array(3).fill(0.0);
|
|
251
251
|
for (let _ = 0; _ < 20; _ += 1) {
|
|
252
252
|
randomInsideUnitSphere(outA, a);
|
|
253
253
|
randomInsideUnitSphere(outB, b);
|
|
@@ -257,8 +257,8 @@ describe('randomInsideUnitSphere', () => {
|
|
|
257
257
|
}
|
|
258
258
|
});
|
|
259
259
|
it('is alias safe when out is the same object as input', () => {
|
|
260
|
-
const random = createRandomSource(
|
|
261
|
-
let out = new
|
|
260
|
+
const random = createRandomSource(0xabcdef);
|
|
261
|
+
let out = new Float32Array(3).fill(0.0);
|
|
262
262
|
randomInsideUnitSphere(out, random);
|
|
263
263
|
expect(Number.isFinite(out[0])).toBe(true);
|
|
264
264
|
expect(Number.isFinite(out[1])).toBe(true);
|
|
@@ -266,26 +266,26 @@ describe('randomInsideUnitSphere', () => {
|
|
|
266
266
|
});
|
|
267
267
|
it('accepts and preserves a deterministic boundary point', () => {
|
|
268
268
|
const random = crate.sequenceRandom([1.0, 0.5, 0.5, 0.5, 0.5, 0.5]);
|
|
269
|
-
let out = new
|
|
269
|
+
let out = new Float32Array(3).fill(0.0);
|
|
270
270
|
randomInsideUnitSphere(out, random);
|
|
271
|
-
expect(out).toEqual([1.0, 0.0, 0.0]);
|
|
271
|
+
expect(Array.from(out)).toEqual([1.0, 0.0, 0.0]);
|
|
272
272
|
});
|
|
273
273
|
});
|
|
274
274
|
|
|
275
275
|
describe('randomOnUnitCircle', () => {
|
|
276
276
|
it('produces points on the unit circle', () => {
|
|
277
|
-
const random = createRandomSource(
|
|
278
|
-
let out = new
|
|
277
|
+
const random = createRandomSource(0xabcdef);
|
|
278
|
+
let out = new Float32Array(2).fill(0.0);
|
|
279
279
|
for (let _ = 0; _ < 100; _ += 1) {
|
|
280
280
|
randomOnUnitCircle(out, random);
|
|
281
281
|
assertApprox(out[0] * out[0] + out[1] * out[1], 1.0);
|
|
282
282
|
}
|
|
283
283
|
});
|
|
284
284
|
it('is deterministic for the same seed', () => {
|
|
285
|
-
const a = createRandomSource(
|
|
286
|
-
const b = createRandomSource(
|
|
287
|
-
let outA = new
|
|
288
|
-
let outB = new
|
|
285
|
+
const a = createRandomSource(0xabcdef);
|
|
286
|
+
const b = createRandomSource(0xabcdef);
|
|
287
|
+
let outA = new Float32Array(2).fill(0.0);
|
|
288
|
+
let outB = new Float32Array(2).fill(0.0);
|
|
289
289
|
for (let _ = 0; _ < 20; _ += 1) {
|
|
290
290
|
randomOnUnitCircle(outA, a);
|
|
291
291
|
randomOnUnitCircle(outB, b);
|
|
@@ -294,14 +294,14 @@ describe('randomOnUnitCircle', () => {
|
|
|
294
294
|
}
|
|
295
295
|
});
|
|
296
296
|
it('is alias safe when out is the same object as input', () => {
|
|
297
|
-
const random = createRandomSource(
|
|
298
|
-
let out = new
|
|
297
|
+
const random = createRandomSource(0xabcdef);
|
|
298
|
+
let out = new Float32Array(2).fill(0.0);
|
|
299
299
|
randomOnUnitCircle(out, random);
|
|
300
300
|
expect(Number.isFinite(out[0])).toBe(true);
|
|
301
301
|
expect(Number.isFinite(out[1])).toBe(true);
|
|
302
302
|
});
|
|
303
303
|
it('maps a quarter turn sample to the top of the circle', () => {
|
|
304
|
-
let out = new
|
|
304
|
+
let out = new Float32Array(2).fill(0.0);
|
|
305
305
|
randomOnUnitCircle(out, () => 0.25);
|
|
306
306
|
expect(Math.abs(out[0]) < 5e-13, `TS quarter-turn x should be close to 0 at 12 digits, got ${out[0]}`).toBe(true);
|
|
307
307
|
expect(Math.abs(out[1] - 1.0) < 5e-13).toBe(true);
|
|
@@ -310,8 +310,8 @@ describe('randomOnUnitCircle', () => {
|
|
|
310
310
|
|
|
311
311
|
describe('randomOnUnitSphere', () => {
|
|
312
312
|
it('produces points on the unit sphere', () => {
|
|
313
|
-
const random = createRandomSource(
|
|
314
|
-
let out = new
|
|
313
|
+
const random = createRandomSource(0xabcdef);
|
|
314
|
+
let out = new Float32Array(3).fill(0.0);
|
|
315
315
|
for (let _ = 0; _ < 100; _ += 1) {
|
|
316
316
|
randomOnUnitSphere(out, random);
|
|
317
317
|
const len2 = out[0] * out[0] + out[1] * out[1] + out[2] * out[2];
|
|
@@ -319,10 +319,10 @@ describe('randomOnUnitSphere', () => {
|
|
|
319
319
|
}
|
|
320
320
|
});
|
|
321
321
|
it('is deterministic for the same seed', () => {
|
|
322
|
-
const a = createRandomSource(
|
|
323
|
-
const b = createRandomSource(
|
|
324
|
-
let outA = new
|
|
325
|
-
let outB = new
|
|
322
|
+
const a = createRandomSource(0xabcdef);
|
|
323
|
+
const b = createRandomSource(0xabcdef);
|
|
324
|
+
let outA = new Float32Array(3).fill(0.0);
|
|
325
|
+
let outB = new Float32Array(3).fill(0.0);
|
|
326
326
|
for (let _ = 0; _ < 20; _ += 1) {
|
|
327
327
|
randomOnUnitSphere(outA, a);
|
|
328
328
|
randomOnUnitSphere(outB, b);
|
|
@@ -332,39 +332,39 @@ describe('randomOnUnitSphere', () => {
|
|
|
332
332
|
}
|
|
333
333
|
});
|
|
334
334
|
it('is alias safe when out is the same object as input', () => {
|
|
335
|
-
const random = createRandomSource(
|
|
336
|
-
let out = new
|
|
335
|
+
const random = createRandomSource(0xabcdef);
|
|
336
|
+
let out = new Float32Array(3).fill(0.0);
|
|
337
337
|
randomOnUnitSphere(out, random);
|
|
338
338
|
expect(Number.isFinite(out[0])).toBe(true);
|
|
339
339
|
expect(Number.isFinite(out[1])).toBe(true);
|
|
340
340
|
expect(Number.isFinite(out[2])).toBe(true);
|
|
341
341
|
});
|
|
342
342
|
it('projects a deterministic disc sample onto the sphere', () => {
|
|
343
|
-
let out = new
|
|
343
|
+
let out = new Float32Array(3).fill(0.0);
|
|
344
344
|
const random = crate.sequenceRandom([0.75, 0.5]);
|
|
345
345
|
randomOnUnitSphere(out, random);
|
|
346
|
-
expect(Math.abs(out[0] - Math.sqrt(3.0) / 2.0) < 5e-13).toBe(true);
|
|
346
|
+
expect(Math.abs(out[0] - Math.fround(Math.sqrt(3.0) / 2.0)) < 5e-13).toBe(true);
|
|
347
347
|
expect(out[1]).toEqual(0.0);
|
|
348
348
|
expect(out[2]).toEqual(0.5);
|
|
349
349
|
});
|
|
350
350
|
it('rejects a disc boundary sample before projecting', () => {
|
|
351
|
-
let out = new
|
|
351
|
+
let out = new Float32Array(3).fill(0.0);
|
|
352
352
|
const random = crate.sequenceRandom([1.0, 0.5, 0.5, 0.5]);
|
|
353
353
|
randomOnUnitSphere(out, random);
|
|
354
|
-
expect(out).toEqual([0.0, 0.0, 1.0]);
|
|
354
|
+
expect(Array.from(out)).toEqual([0.0, 0.0, 1.0]);
|
|
355
355
|
});
|
|
356
356
|
});
|
|
357
357
|
|
|
358
358
|
describe('randomPoisson', () => {
|
|
359
359
|
it('returns a non negative integer', () => {
|
|
360
|
-
const random = createRandomSource(
|
|
360
|
+
const random = createRandomSource(0xabcdef);
|
|
361
361
|
for (let _ = 0; _ < 200; _ += 1) {
|
|
362
362
|
const k = randomPoisson(random, 3.0);
|
|
363
363
|
expect(k >= 0).toBe(true);
|
|
364
364
|
}
|
|
365
365
|
});
|
|
366
366
|
it('mean approximates lambda 1', () => {
|
|
367
|
-
const random = createRandomSource(
|
|
367
|
+
const random = createRandomSource(0xabcdef);
|
|
368
368
|
let sum = 0;
|
|
369
369
|
const n = 5000;
|
|
370
370
|
for (let _ = 0; _ < n; _ += 1) {
|
|
@@ -374,7 +374,7 @@ describe('randomPoisson', () => {
|
|
|
374
374
|
expect(Math.abs(avg - 1.0) < 0.2).toBe(true);
|
|
375
375
|
});
|
|
376
376
|
it('mean approximates lambda 5', () => {
|
|
377
|
-
const random = createRandomSource(
|
|
377
|
+
const random = createRandomSource(0xabcdef);
|
|
378
378
|
let sum = 0;
|
|
379
379
|
const n = 5000;
|
|
380
380
|
for (let _ = 0; _ < n; _ += 1) {
|
|
@@ -385,27 +385,27 @@ describe('randomPoisson', () => {
|
|
|
385
385
|
});
|
|
386
386
|
it('throws for lambda zero', () => {
|
|
387
387
|
crate.assertRangeError(() => {
|
|
388
|
-
randomPoisson(createRandomSource(
|
|
388
|
+
randomPoisson(createRandomSource(0xabcdef), 0.0);
|
|
389
389
|
}, 'randomPoisson: lambda must be finite and > 0');
|
|
390
390
|
});
|
|
391
391
|
it('throws for negative lambda', () => {
|
|
392
392
|
crate.assertRangeError(() => {
|
|
393
|
-
randomPoisson(createRandomSource(
|
|
393
|
+
randomPoisson(createRandomSource(0xabcdef), -1.0);
|
|
394
394
|
}, 'randomPoisson: lambda must be finite and > 0');
|
|
395
395
|
});
|
|
396
396
|
it('rejects nan lambda', () => {
|
|
397
397
|
crate.assertRangeError(() => {
|
|
398
|
-
randomPoisson(createRandomSource(
|
|
398
|
+
randomPoisson(createRandomSource(0xabcdef), Number.NaN);
|
|
399
399
|
}, 'randomPoisson: lambda must be finite and > 0');
|
|
400
400
|
});
|
|
401
401
|
it('rejects infinite lambda', () => {
|
|
402
402
|
crate.assertRangeError(() => {
|
|
403
|
-
randomPoisson(createRandomSource(
|
|
403
|
+
randomPoisson(createRandomSource(0xabcdef), Number.POSITIVE_INFINITY);
|
|
404
404
|
}, 'randomPoisson: lambda must be finite and > 0');
|
|
405
405
|
});
|
|
406
406
|
it('is deterministic for the same seed', () => {
|
|
407
|
-
const a = createRandomSource(
|
|
408
|
-
const b = createRandomSource(
|
|
407
|
+
const a = createRandomSource(0xabcdef);
|
|
408
|
+
const b = createRandomSource(0xabcdef);
|
|
409
409
|
for (let _ = 0; _ < 20; _ += 1) {
|
|
410
410
|
expect(randomPoisson(a, 3.0)).toEqual(randomPoisson(b, 3.0));
|
|
411
411
|
}
|
|
@@ -418,29 +418,29 @@ describe('randomPoisson', () => {
|
|
|
418
418
|
|
|
419
419
|
describe('randomWeighted', () => {
|
|
420
420
|
it('returns indices within the weights slice', () => {
|
|
421
|
-
const random = createRandomSource(
|
|
421
|
+
const random = createRandomSource(0xabcdef);
|
|
422
422
|
const weights = [1.0, 2.0, 3.0];
|
|
423
423
|
for (let _ = 0; _ < 100; _ += 1) {
|
|
424
424
|
const idx = randomWeighted(random, weights);
|
|
425
425
|
expect(idx >= 0).toBe(true);
|
|
426
|
-
expect(idx < weights.length).toBe(true);
|
|
426
|
+
expect(idx >>> 0 < weights.length).toBe(true);
|
|
427
427
|
}
|
|
428
428
|
});
|
|
429
429
|
it('returns negative 1 for an empty slice', () => {
|
|
430
|
-
expect(randomWeighted(createRandomSource(
|
|
430
|
+
expect(randomWeighted(createRandomSource(0xabcdef), [])).toEqual(-1);
|
|
431
431
|
});
|
|
432
432
|
it('returns negative 1 for an all zero weight slice', () => {
|
|
433
|
-
expect(randomWeighted(createRandomSource(
|
|
433
|
+
expect(randomWeighted(createRandomSource(0xabcdef), [0.0, 0.0, 0.0])).toEqual(-1);
|
|
434
434
|
});
|
|
435
435
|
it('always returns the only non zero index when all others are zero', () => {
|
|
436
|
-
const random = createRandomSource(
|
|
436
|
+
const random = createRandomSource(0xabcdef);
|
|
437
437
|
for (let _ = 0; _ < 20; _ += 1) {
|
|
438
438
|
expect(randomWeighted(random, [0.0, 10.0, 0.0])).toEqual(1);
|
|
439
439
|
}
|
|
440
440
|
});
|
|
441
441
|
it('is deterministic for the same seed', () => {
|
|
442
|
-
const a = createRandomSource(
|
|
443
|
-
const b = createRandomSource(
|
|
442
|
+
const a = createRandomSource(0xabcdef);
|
|
443
|
+
const b = createRandomSource(0xabcdef);
|
|
444
444
|
const weights = [1.0, 2.0, 3.0, 4.0];
|
|
445
445
|
for (let _ = 0; _ < 20; _ += 1) {
|
|
446
446
|
expect(randomWeighted(a, weights)).toEqual(randomWeighted(b, weights));
|
|
@@ -459,7 +459,7 @@ describe('shuffle', () => {
|
|
|
459
459
|
it('clears stale output and fills the same length', () => {
|
|
460
460
|
const items = [1, 2, 3, 4, 5];
|
|
461
461
|
let out = [99, 98, 97, 96, 95, 94];
|
|
462
|
-
shuffle(out, createRandomSource(
|
|
462
|
+
shuffle(out, createRandomSource(0xabcdef), items);
|
|
463
463
|
expect(out.length).toEqual(items.length);
|
|
464
464
|
expect(!out.includes(99)).toBe(true);
|
|
465
465
|
});
|
|
@@ -467,14 +467,14 @@ describe('shuffle', () => {
|
|
|
467
467
|
const items = [1, 2, 3, 4, 5, 6, 7, 8];
|
|
468
468
|
let a = [] as never[];
|
|
469
469
|
let b = [] as never[];
|
|
470
|
-
shuffle(a, createRandomSource(
|
|
471
|
-
shuffle(b, createRandomSource(
|
|
470
|
+
shuffle(a, createRandomSource(0xabcdef), items);
|
|
471
|
+
shuffle(b, createRandomSource(0xabcdef), items);
|
|
472
472
|
expect(a).toEqual(b);
|
|
473
473
|
});
|
|
474
474
|
it('does not mutate the original array', () => {
|
|
475
475
|
const items = [1, 2, 3, 4, 5];
|
|
476
476
|
const original = items;
|
|
477
|
-
shuffle([] as never[], createRandomSource(
|
|
477
|
+
shuffle([] as never[], createRandomSource(0xabcdef), items);
|
|
478
478
|
expect(items).toEqual(original);
|
|
479
479
|
});
|
|
480
480
|
});
|
|
@@ -484,21 +484,21 @@ describe('shuffleInPlace', () => {
|
|
|
484
484
|
let items = [1, 2, 3, 4, 5];
|
|
485
485
|
let expected = [...items];
|
|
486
486
|
expected.sort((a, b) => a - b);
|
|
487
|
-
shuffleInPlace(createRandomSource(
|
|
487
|
+
shuffleInPlace(createRandomSource(0xabcdef), items);
|
|
488
488
|
items.sort((a, b) => a - b);
|
|
489
489
|
expect(items).toEqual(expected);
|
|
490
490
|
});
|
|
491
491
|
it('is deterministic for the same seed', () => {
|
|
492
492
|
let a = [1, 2, 3, 4, 5, 6, 7, 8];
|
|
493
493
|
let b = [1, 2, 3, 4, 5, 6, 7, 8];
|
|
494
|
-
shuffleInPlace(createRandomSource(
|
|
495
|
-
shuffleInPlace(createRandomSource(
|
|
494
|
+
shuffleInPlace(createRandomSource(0xabcdef), a);
|
|
495
|
+
shuffleInPlace(createRandomSource(0xabcdef), b);
|
|
496
496
|
expect(a).toEqual(b);
|
|
497
497
|
});
|
|
498
498
|
it('mutates the original array', () => {
|
|
499
499
|
let items = [1, 2, 3, 4, 5, 6, 7, 8];
|
|
500
500
|
const copy = [...items];
|
|
501
|
-
shuffleInPlace(createRandomSource(
|
|
501
|
+
shuffleInPlace(createRandomSource(0xabcdef), items);
|
|
502
502
|
expect(items.length).toEqual(copy.length);
|
|
503
503
|
});
|
|
504
504
|
it('pins the swaps and consumes exactly one sample per fisher yates step', () => {
|
|
@@ -511,7 +511,7 @@ describe('shuffleInPlace', () => {
|
|
|
511
511
|
};
|
|
512
512
|
let items = ['a', 'b', 'c', 'd'];
|
|
513
513
|
shuffleInPlace(random, items);
|
|
514
|
-
expect(items).toEqual(['c', 'a', 'b', 'd']);
|
|
514
|
+
expect(Array.from(items)).toEqual(['c', 'a', 'b', 'd']);
|
|
515
515
|
expect(calls.value).toEqual(3);
|
|
516
516
|
});
|
|
517
517
|
});
|
package/src/randomRange.test.ts
CHANGED
|
@@ -8,26 +8,26 @@ import { rustTestSupport as crate } from './rustTestSupport.ts';
|
|
|
8
8
|
|
|
9
9
|
describe('randomBool', () => {
|
|
10
10
|
it('returns only booleans', () => {
|
|
11
|
-
const random = createRandomSource(
|
|
11
|
+
const random = createRandomSource(0xabcdef);
|
|
12
12
|
for (let _ = 0; _ < 100; _ += 1) {
|
|
13
|
-
const
|
|
13
|
+
const _v: boolean = randomBool(random, undefined);
|
|
14
14
|
}
|
|
15
15
|
});
|
|
16
16
|
it('returns true with probability 1', () => {
|
|
17
|
-
const random = createRandomSource(
|
|
17
|
+
const random = createRandomSource(0xabcdef);
|
|
18
18
|
for (let _ = 0; _ < 10; _ += 1) {
|
|
19
19
|
expect(randomBool(random, 1.0)).toBe(true);
|
|
20
20
|
}
|
|
21
21
|
});
|
|
22
22
|
it('returns false with probability 0', () => {
|
|
23
|
-
const random = createRandomSource(
|
|
23
|
+
const random = createRandomSource(0xabcdef);
|
|
24
24
|
for (let _ = 0; _ < 10; _ += 1) {
|
|
25
25
|
expect(!randomBool(random, 0.0)).toBe(true);
|
|
26
26
|
}
|
|
27
27
|
});
|
|
28
28
|
it('is deterministic for the same seed', () => {
|
|
29
|
-
const a = createRandomSource(
|
|
30
|
-
const b = createRandomSource(
|
|
29
|
+
const a = createRandomSource(0xabcdef);
|
|
30
|
+
const b = createRandomSource(0xabcdef);
|
|
31
31
|
for (let _ = 0; _ < 20; _ += 1) {
|
|
32
32
|
expect(randomBool(a, undefined)).toEqual(randomBool(b, undefined));
|
|
33
33
|
}
|
|
@@ -39,7 +39,7 @@ describe('randomBool', () => {
|
|
|
39
39
|
|
|
40
40
|
describe('randomInt', () => {
|
|
41
41
|
it('returns integers within min max', () => {
|
|
42
|
-
const random = createRandomSource(
|
|
42
|
+
const random = createRandomSource(0xabcdef);
|
|
43
43
|
for (let _ = 0; _ < 200; _ += 1) {
|
|
44
44
|
const v = randomInt(random, 0, 9);
|
|
45
45
|
expect(v >= 0).toBe(true);
|
|
@@ -47,18 +47,18 @@ describe('randomInt', () => {
|
|
|
47
47
|
}
|
|
48
48
|
});
|
|
49
49
|
it('returns min when min equals max', () => {
|
|
50
|
-
const random = createRandomSource(
|
|
50
|
+
const random = createRandomSource(0xabcdef);
|
|
51
51
|
expect(randomInt(random, 5, 5)).toEqual(5);
|
|
52
52
|
});
|
|
53
53
|
it('throws when min greater than max', () => {
|
|
54
54
|
crate.assertRangeError(() => {
|
|
55
|
-
const random = createRandomSource(
|
|
55
|
+
const random = createRandomSource(0xabcdef);
|
|
56
56
|
randomInt(random, 10, 5);
|
|
57
57
|
}, 'randomInt: min must be <= max');
|
|
58
58
|
});
|
|
59
59
|
it('is deterministic for the same seed', () => {
|
|
60
|
-
const a = createRandomSource(
|
|
61
|
-
const b = createRandomSource(
|
|
60
|
+
const a = createRandomSource(0xabcdef);
|
|
61
|
+
const b = createRandomSource(0xabcdef);
|
|
62
62
|
for (let _ = 0; _ < 20; _ += 1) {
|
|
63
63
|
expect(randomInt(a, 0, 100)).toEqual(randomInt(b, 0, 100));
|
|
64
64
|
}
|
|
@@ -70,7 +70,7 @@ describe('randomInt', () => {
|
|
|
70
70
|
|
|
71
71
|
describe('randomRange', () => {
|
|
72
72
|
it('returns values within min max', () => {
|
|
73
|
-
const random = createRandomSource(
|
|
73
|
+
const random = createRandomSource(0xabcdef);
|
|
74
74
|
for (let _ = 0; _ < 200; _ += 1) {
|
|
75
75
|
const v = randomRange(random, 5.0, 10.0);
|
|
76
76
|
expect(v >= 5.0).toBe(true);
|
|
@@ -78,12 +78,12 @@ describe('randomRange', () => {
|
|
|
78
78
|
}
|
|
79
79
|
});
|
|
80
80
|
it('returns min when min equals max', () => {
|
|
81
|
-
const random = createRandomSource(
|
|
81
|
+
const random = createRandomSource(0xabcdef);
|
|
82
82
|
expect(randomRange(random, 3.0, 3.0)).toEqual(3.0);
|
|
83
83
|
});
|
|
84
84
|
it('is deterministic for the same seed', () => {
|
|
85
|
-
const a = createRandomSource(
|
|
86
|
-
const b = createRandomSource(
|
|
85
|
+
const a = createRandomSource(0xabcdef);
|
|
86
|
+
const b = createRandomSource(0xabcdef);
|
|
87
87
|
for (let _ = 0; _ < 20; _ += 1) {
|
|
88
88
|
expect(randomRange(a, 0.0, 100.0)).toEqual(randomRange(b, 0.0, 100.0));
|
|
89
89
|
}
|
|
@@ -92,14 +92,14 @@ describe('randomRange', () => {
|
|
|
92
92
|
|
|
93
93
|
describe('randomSign', () => {
|
|
94
94
|
it('returns only 1 or negative 1', () => {
|
|
95
|
-
const random = createRandomSource(
|
|
95
|
+
const random = createRandomSource(0xabcdef);
|
|
96
96
|
for (let _ = 0; _ < 100; _ += 1) {
|
|
97
97
|
const v = randomSign(random);
|
|
98
98
|
expect(v === 1.0 || v === -1.0).toBe(true);
|
|
99
99
|
}
|
|
100
100
|
});
|
|
101
101
|
it('produces both signs', () => {
|
|
102
|
-
const random = createRandomSource(
|
|
102
|
+
const random = createRandomSource(0xabcdef);
|
|
103
103
|
let sawPositive = false;
|
|
104
104
|
let sawNegative = false;
|
|
105
105
|
for (let _ = 0; _ < 100; _ += 1) {
|
|
@@ -115,8 +115,8 @@ describe('randomSign', () => {
|
|
|
115
115
|
expect(sawNegative).toBe(true);
|
|
116
116
|
});
|
|
117
117
|
it('is deterministic for the same seed', () => {
|
|
118
|
-
const a = createRandomSource(
|
|
119
|
-
const b = createRandomSource(
|
|
118
|
+
const a = createRandomSource(0xabcdef);
|
|
119
|
+
const b = createRandomSource(0xabcdef);
|
|
120
120
|
for (let _ = 0; _ < 20; _ += 1) {
|
|
121
121
|
expect(randomSign(a)).toEqual(randomSign(b));
|
|
122
122
|
}
|
package/src/statistics.test.ts
CHANGED
|
@@ -102,7 +102,7 @@ describe('variance', () => {
|
|
|
102
102
|
expect(variance([Number.MAX_VALUE, Number.MAX_VALUE])).toEqual(0.0);
|
|
103
103
|
});
|
|
104
104
|
it('compensates the sum of squared deviations', () => {
|
|
105
|
-
expect(variance([-1.0,
|
|
105
|
+
expect(variance([-1.0, 1_000_000_000_000.0, 1_000_000_000_000.0])).toEqual(2.2222222222266665e23);
|
|
106
106
|
});
|
|
107
107
|
it('handles zero and preserves non finite propagation', () => {
|
|
108
108
|
expect(variance([0.0, 0.0])).toEqual(0.0);
|
|
@@ -133,9 +133,9 @@ describe('weightedAverage', () => {
|
|
|
133
133
|
expect(weightedAverage([2.0, 10.0], [1.0, 3.0])).toEqual(8.0);
|
|
134
134
|
});
|
|
135
135
|
it('retains small weighted terms across cancellation', () => {
|
|
136
|
-
expect(
|
|
137
|
-
|
|
138
|
-
);
|
|
136
|
+
expect(
|
|
137
|
+
weightedAverage([10_000_000_000_000_000.0, -1_000_000_000_000_000.0, -10_000_000_000_000_000.0], [2.0, 1.0, 1.0]),
|
|
138
|
+
).toEqual(2_250_000_000_000_000.0);
|
|
139
139
|
});
|
|
140
140
|
it('handles zero values and cancelling non zero weights', () => {
|
|
141
141
|
expect(weightedAverage([0.0, 0.0], [1.0, 1.0])).toEqual(0.0);
|