@pawells/math-extended 2.0.0 → 3.0.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/README.md +15 -21
- package/build/clamp.d.ts +5 -5
- package/build/clamp.js +5 -5
- package/build/core.d.ts +23 -0
- package/build/core.d.ts.map +1 -0
- package/build/core.js +25 -0
- package/build/core.js.map +1 -0
- package/build/index.d.ts +1 -4
- package/build/index.d.ts.map +1 -1
- package/build/index.js +1 -6
- package/build/index.js.map +1 -1
- package/build/interpolation.d.ts +158 -171
- package/build/interpolation.d.ts.map +1 -1
- package/build/interpolation.js +162 -181
- package/build/interpolation.js.map +1 -1
- package/build/matrices/arithmetic.d.ts +132 -132
- package/build/matrices/arithmetic.d.ts.map +1 -1
- package/build/matrices/arithmetic.js +194 -226
- package/build/matrices/arithmetic.js.map +1 -1
- package/build/matrices/asserts.d.ts +30 -408
- package/build/matrices/asserts.d.ts.map +1 -1
- package/build/matrices/asserts.js +98 -542
- package/build/matrices/asserts.js.map +1 -1
- package/build/matrices/core.d.ts +117 -46
- package/build/matrices/core.d.ts.map +1 -1
- package/build/matrices/core.js +127 -103
- package/build/matrices/core.js.map +1 -1
- package/build/matrices/decompositions.d.ts +95 -96
- package/build/matrices/decompositions.d.ts.map +1 -1
- package/build/matrices/decompositions.js +119 -160
- package/build/matrices/decompositions.js.map +1 -1
- package/build/matrices/index.d.ts +0 -1
- package/build/matrices/index.d.ts.map +1 -1
- package/build/matrices/index.js +0 -3
- package/build/matrices/index.js.map +1 -1
- package/build/matrices/linear-algebra.d.ts +45 -8
- package/build/matrices/linear-algebra.d.ts.map +1 -1
- package/build/matrices/linear-algebra.js +76 -32
- package/build/matrices/linear-algebra.js.map +1 -1
- package/build/matrices/normalization.d.ts +29 -8
- package/build/matrices/normalization.d.ts.map +1 -1
- package/build/matrices/normalization.js +32 -23
- package/build/matrices/normalization.js.map +1 -1
- package/build/matrices/transformations.d.ts +116 -148
- package/build/matrices/transformations.d.ts.map +1 -1
- package/build/matrices/transformations.js +69 -91
- package/build/matrices/transformations.js.map +1 -1
- package/build/matrices/types.d.ts +32 -60
- package/build/matrices/types.d.ts.map +1 -1
- package/build/matrices/types.js +63 -1
- package/build/matrices/types.js.map +1 -1
- package/build/quaternions/asserts.d.ts +29 -38
- package/build/quaternions/asserts.d.ts.map +1 -1
- package/build/quaternions/asserts.js +61 -77
- package/build/quaternions/asserts.js.map +1 -1
- package/build/quaternions/conversions.d.ts +26 -26
- package/build/quaternions/conversions.d.ts.map +1 -1
- package/build/quaternions/conversions.js +24 -24
- package/build/quaternions/core.d.ts +70 -69
- package/build/quaternions/core.d.ts.map +1 -1
- package/build/quaternions/core.js +71 -71
- package/build/quaternions/core.js.map +1 -1
- package/build/quaternions/index.d.ts +0 -1
- package/build/quaternions/index.d.ts.map +1 -1
- package/build/quaternions/index.js +0 -2
- package/build/quaternions/index.js.map +1 -1
- package/build/quaternions/interpolation.d.ts +16 -16
- package/build/quaternions/interpolation.d.ts.map +1 -1
- package/build/quaternions/interpolation.js +21 -21
- package/build/quaternions/interpolation.js.map +1 -1
- package/build/quaternions/predefined.d.ts +10 -10
- package/build/quaternions/predefined.d.ts.map +1 -1
- package/build/quaternions/predefined.js +9 -9
- package/build/quaternions/types.d.ts +23 -12
- package/build/quaternions/types.d.ts.map +1 -1
- package/build/quaternions/types.js +51 -1
- package/build/quaternions/types.js.map +1 -1
- package/build/random.d.ts +66 -20
- package/build/random.d.ts.map +1 -1
- package/build/random.js +73 -20
- package/build/random.js.map +1 -1
- package/build/vectors/asserts.d.ts +50 -220
- package/build/vectors/asserts.d.ts.map +1 -1
- package/build/vectors/asserts.js +141 -327
- package/build/vectors/asserts.js.map +1 -1
- package/build/vectors/core.d.ts +182 -229
- package/build/vectors/core.d.ts.map +1 -1
- package/build/vectors/core.js +234 -288
- package/build/vectors/core.js.map +1 -1
- package/build/vectors/index.d.ts +0 -1
- package/build/vectors/index.d.ts.map +1 -1
- package/build/vectors/index.js +0 -2
- package/build/vectors/index.js.map +1 -1
- package/build/vectors/interpolation.d.ts +40 -40
- package/build/vectors/interpolation.d.ts.map +1 -1
- package/build/vectors/interpolation.js +75 -86
- package/build/vectors/interpolation.js.map +1 -1
- package/build/vectors/predefined.d.ts +31 -7
- package/build/vectors/predefined.d.ts.map +1 -1
- package/build/vectors/predefined.js +30 -6
- package/build/vectors/predefined.js.map +1 -1
- package/build/vectors/types.d.ts +26 -4
- package/build/vectors/types.d.ts.map +1 -1
- package/build/vectors/types.js +50 -1
- package/build/vectors/types.js.map +1 -1
- package/package.json +3 -2
- package/build/matrices/_exports.d.ts +0 -13
- package/build/matrices/_exports.d.ts.map +0 -1
- package/build/matrices/_exports.js +0 -13
- package/build/matrices/_exports.js.map +0 -1
- package/build/quaternions/_exports.d.ts +0 -11
- package/build/quaternions/_exports.d.ts.map +0 -1
- package/build/quaternions/_exports.js +0 -11
- package/build/quaternions/_exports.js.map +0 -1
- package/build/vectors/_exports.d.ts +0 -10
- package/build/vectors/_exports.d.ts.map +0 -1
- package/build/vectors/_exports.js +0 -10
- package/build/vectors/_exports.js.map +0 -1
|
@@ -4,9 +4,9 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import { AssertArray, AssertNumber } from '@pawells/typescript-common';
|
|
6
6
|
import { AssertNormalizedQuaternion } from './asserts.js';
|
|
7
|
-
import { QuaternionSLERP, QuaternionNormalize, QuaternionMultiply, QuaternionInverse } from './core.js';
|
|
7
|
+
import { QuaternionSLERP, QuaternionNormalize, QuaternionMultiply, QuaternionInverse, QUATERNION_TOLERANCE } from './core.js';
|
|
8
|
+
import { VectorMagnitude } from '../vectors/core.js';
|
|
8
9
|
import { Clamp } from '../clamp.js';
|
|
9
|
-
const QUATERNION_LOG_TOLERANCE = 1e-6;
|
|
10
10
|
/**
|
|
11
11
|
* Performs normalized linear interpolation (NLERP) between two quaternions.
|
|
12
12
|
* NLERP is faster than SLERP but doesn't maintain constant angular velocity.
|
|
@@ -18,11 +18,11 @@ const QUATERNION_LOG_TOLERANCE = 1e-6;
|
|
|
18
18
|
* @returns Interpolated and normalized quaternion
|
|
19
19
|
*
|
|
20
20
|
* @example
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
21
|
+
* ```typescript
|
|
22
|
+
* const q1 = [0, 0, 0, 1];
|
|
23
|
+
* const q2 = [0, 0, 0.707, 0.707];
|
|
24
|
+
* const interpolated = QuaternionNLERP(q1, q2, 0.5);
|
|
25
|
+
* ```
|
|
26
26
|
*/
|
|
27
27
|
export function QuaternionNLERP(a, b, t) {
|
|
28
28
|
AssertNormalizedQuaternion(a);
|
|
@@ -54,11 +54,11 @@ export function QuaternionNLERP(a, b, t) {
|
|
|
54
54
|
* @returns Smoothly interpolated quaternion
|
|
55
55
|
*
|
|
56
56
|
* @example
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
57
|
+
* ```typescript
|
|
58
|
+
* // Create a smooth path through multiple rotations
|
|
59
|
+
* const path = [q0, q1, q2, q3]; // Array of quaternions
|
|
60
|
+
* const smooth = QuaternionSQUAD(path[0], path[1], path[2], path[3], 0.5);
|
|
61
|
+
* ```
|
|
62
62
|
*/
|
|
63
63
|
export function QuaternionSQUAD(q0, q1, q2, q3, t) {
|
|
64
64
|
AssertNormalizedQuaternion(q0);
|
|
@@ -114,8 +114,8 @@ function quaternionSquadControlPoint(q0, q1, q2) {
|
|
|
114
114
|
function quaternionLog(quaternion) {
|
|
115
115
|
AssertNormalizedQuaternion(quaternion);
|
|
116
116
|
const [x, y, z, w] = quaternion;
|
|
117
|
-
const vectorLength =
|
|
118
|
-
if (vectorLength <
|
|
117
|
+
const vectorLength = VectorMagnitude([x, y, z]);
|
|
118
|
+
if (vectorLength < QUATERNION_TOLERANCE)
|
|
119
119
|
return [0, 0, 0, 0];
|
|
120
120
|
const angle = Math.atan2(vectorLength, w);
|
|
121
121
|
const scale = angle / vectorLength;
|
|
@@ -130,8 +130,8 @@ function quaternionLog(quaternion) {
|
|
|
130
130
|
*/
|
|
131
131
|
function quaternionExp(quaternion) {
|
|
132
132
|
const [x, y, z, w] = quaternion;
|
|
133
|
-
const vectorLength =
|
|
134
|
-
if (vectorLength <
|
|
133
|
+
const vectorLength = VectorMagnitude([x, y, z]);
|
|
134
|
+
if (vectorLength < QUATERNION_TOLERANCE)
|
|
135
135
|
return [0, 0, 0, 1];
|
|
136
136
|
const expW = Math.exp(w);
|
|
137
137
|
const cosV = Math.cos(vectorLength);
|
|
@@ -154,11 +154,11 @@ function quaternionExp(quaternion) {
|
|
|
154
154
|
* @returns Function that takes t ∈ [0, 1] and returns interpolated quaternion
|
|
155
155
|
*
|
|
156
156
|
* @example
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
157
|
+
* ```typescript
|
|
158
|
+
* const path = [q1, q2, q3, q4];
|
|
159
|
+
* const interpolator = QuaternionCreatePath(path, 'slerp');
|
|
160
|
+
* const halfway = interpolator(0.5); // Interpolated quaternion at 50% along path
|
|
161
|
+
* ```
|
|
162
162
|
*/
|
|
163
163
|
export function QuaternionCreatePath(quaternions, method = 'slerp') {
|
|
164
164
|
AssertArray(quaternions, { minSize: 2 });
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interpolation.js","sourceRoot":"","sources":["../../src/quaternions/interpolation.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AACvE,OAAO,EAAE,0BAA0B,EAAE,MAAM,cAAc,CAAC;AAE1D,OAAO,EAAE,eAAe,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"interpolation.js","sourceRoot":"","sources":["../../src/quaternions/interpolation.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,WAAW,EAAE,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C;QAElC,mCAAmC;QACnC,IAAI,CAAC,EAAE,IAAI,CAAC,EAAE;YAAE,MAAM,IAAI,KAAK,CAAC,qCAAqC,CAAC,CAAC;QAEvE,QAAQ,MAAM,EAAE,CAAC;YAChB,KAAK,OAAO;gBACX,OAAO,eAAe,CAAC,EAAE,EAAE,EAAE,EAAE,QAAQ,CAAC,CAAC;YAC1C,KAAK,OAAO,CAAC,CAAC,CAAC;gBACd,oEAAoE;gBACpE,MAAM,EAAE,GAAG,WAAW,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,YAAY,GAAG,CAAC,CAAC,CAAC,CAAC;gBACtD,MAAM,EAAE,GAAG,WAAW,CAAC,IAAI,CAAC,GAAG,CAAC,WAAW,CAAC,MAAM,GAAG,CAAC,EAAE,SAAS,GAAG,CAAC,CAAC,CAAC,CAAC;gBAExE,mCAAmC;gBACnC,IAAI,CAAC,EAAE,IAAI,CAAC,EAAE;oBAAE,MAAM,IAAI,KAAK,CAAC,+CAA+C,CAAC,CAAC;gBAEjF,OAAO,eAAe,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,QAAQ,CAAC,CAAC;YAClD,CAAC;YACD,KAAK,OAAO,CAAC;YACb;gBACC,OAAO,eAAe,CAAC,EAAE,EAAE,EAAE,EAAE,QAAQ,CAAC,CAAC;QAC3C,CAAC;IACF,CAAC,CAAC;AACH,CAAC"}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Predefined quaternions for common rotations and orientations.
|
|
3
3
|
* Provides convenient constants for frequently used quaternion values.
|
|
4
4
|
*/
|
|
5
|
-
import { TQuaternion } from './types.js';
|
|
5
|
+
import type { TQuaternion } from './types.js';
|
|
6
6
|
/**
|
|
7
7
|
* Creates a quaternion representing rotation around the X-axis.
|
|
8
8
|
*
|
|
@@ -10,9 +10,9 @@ import { TQuaternion } from './types.js';
|
|
|
10
10
|
* @returns Quaternion representing rotation around X-axis
|
|
11
11
|
*
|
|
12
12
|
* @example
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
* ```typescript
|
|
14
|
+
* const q = QuaternionRotationX(Math.PI / 2); // 90° rotation around X-axis
|
|
15
|
+
* ```
|
|
16
16
|
*/
|
|
17
17
|
export declare function QuaternionRotationX(angle: number): TQuaternion;
|
|
18
18
|
/**
|
|
@@ -22,9 +22,9 @@ export declare function QuaternionRotationX(angle: number): TQuaternion;
|
|
|
22
22
|
* @returns Quaternion representing rotation around Y-axis
|
|
23
23
|
*
|
|
24
24
|
* @example
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
* ```typescript
|
|
26
|
+
* const q = QuaternionRotationY(Math.PI / 4); // 45° rotation around Y-axis
|
|
27
|
+
* ```
|
|
28
28
|
*/
|
|
29
29
|
export declare function QuaternionRotationY(angle: number): TQuaternion;
|
|
30
30
|
/**
|
|
@@ -34,9 +34,9 @@ export declare function QuaternionRotationY(angle: number): TQuaternion;
|
|
|
34
34
|
* @returns Quaternion representing rotation around Z-axis
|
|
35
35
|
*
|
|
36
36
|
* @example
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
37
|
+
* ```typescript
|
|
38
|
+
* const q = QuaternionRotationZ(Math.PI); // 180° rotation around Z-axis
|
|
39
|
+
* ```
|
|
40
40
|
*/
|
|
41
41
|
export declare function QuaternionRotationZ(angle: number): TQuaternion;
|
|
42
42
|
//# sourceMappingURL=predefined.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"predefined.d.ts","sourceRoot":"","sources":["../../src/quaternions/predefined.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"predefined.d.ts","sourceRoot":"","sources":["../../src/quaternions/predefined.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAG9C;;;;;;;;;;GAUG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,MAAM,GAAG,WAAW,CAE9D;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,MAAM,GAAG,WAAW,CAE9D;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,MAAM,GAAG,WAAW,CAE9D"}
|
|
@@ -10,9 +10,9 @@ import { QuaternionFromAxisAngle } from './core.js';
|
|
|
10
10
|
* @returns Quaternion representing rotation around X-axis
|
|
11
11
|
*
|
|
12
12
|
* @example
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
* ```typescript
|
|
14
|
+
* const q = QuaternionRotationX(Math.PI / 2); // 90° rotation around X-axis
|
|
15
|
+
* ```
|
|
16
16
|
*/
|
|
17
17
|
export function QuaternionRotationX(angle) {
|
|
18
18
|
return QuaternionFromAxisAngle([1, 0, 0], angle);
|
|
@@ -24,9 +24,9 @@ export function QuaternionRotationX(angle) {
|
|
|
24
24
|
* @returns Quaternion representing rotation around Y-axis
|
|
25
25
|
*
|
|
26
26
|
* @example
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
27
|
+
* ```typescript
|
|
28
|
+
* const q = QuaternionRotationY(Math.PI / 4); // 45° rotation around Y-axis
|
|
29
|
+
* ```
|
|
30
30
|
*/
|
|
31
31
|
export function QuaternionRotationY(angle) {
|
|
32
32
|
return QuaternionFromAxisAngle([0, 1, 0], angle);
|
|
@@ -38,9 +38,9 @@ export function QuaternionRotationY(angle) {
|
|
|
38
38
|
* @returns Quaternion representing rotation around Z-axis
|
|
39
39
|
*
|
|
40
40
|
* @example
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
41
|
+
* ```typescript
|
|
42
|
+
* const q = QuaternionRotationZ(Math.PI); // 180° rotation around Z-axis
|
|
43
|
+
* ```
|
|
44
44
|
*/
|
|
45
45
|
export function QuaternionRotationZ(angle) {
|
|
46
46
|
return QuaternionFromAxisAngle([0, 0, 1], angle);
|
|
@@ -3,8 +3,7 @@
|
|
|
3
3
|
* Quaternions provide a mathematically robust way to represent rotations in 3D space,
|
|
4
4
|
* avoiding gimbal lock and providing smooth interpolation capabilities.
|
|
5
5
|
*/
|
|
6
|
-
import
|
|
7
|
-
import { TMatrix3 } from '../matrices/types.js';
|
|
6
|
+
import z from 'zod';
|
|
8
7
|
/**
|
|
9
8
|
* Quaternion type representing a rotation in 3D space as [x, y, z, w].
|
|
10
9
|
* The first three components (x, y, z) represent the vector part (rotation axis scaled by sin(θ/2)),
|
|
@@ -13,40 +12,52 @@ import { TMatrix3 } from '../matrices/types.js';
|
|
|
13
12
|
* Quaternions extend IVector4 to leverage vector operations while maintaining semantic meaning.
|
|
14
13
|
*
|
|
15
14
|
* @example
|
|
16
|
-
*
|
|
17
|
-
* const
|
|
18
|
-
* const
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const identity: TQuaternion = [0, 0, 0, 1]; // No rotation
|
|
17
|
+
* const rotationX: TQuaternion = [1, 0, 0, 0]; // 180° rotation around X-axis
|
|
18
|
+
* const rotation90Y: TQuaternion = [0, 0.707, 0, 0.707]; // 90° rotation around Y-axis
|
|
19
|
+
* ```
|
|
19
20
|
*/
|
|
20
|
-
export
|
|
21
|
+
export declare const QUATERNION_SCHEMA: z.ZodArray<z.ZodUnknown>;
|
|
22
|
+
export type TQuaternion = [number, number, number, number];
|
|
21
23
|
/**
|
|
22
24
|
* Euler angles representation as [x, y, z] rotations in radians.
|
|
23
25
|
* Order of rotation is typically Z-Y-X (yaw-pitch-roll) unless specified otherwise.
|
|
24
26
|
*
|
|
25
27
|
* @example
|
|
26
|
-
*
|
|
28
|
+
* ```typescript
|
|
29
|
+
* const euler: TEulerAngles = [0, Math.PI/4, 0]; // 45° pitch rotation
|
|
30
|
+
* ```
|
|
27
31
|
*/
|
|
28
|
-
export
|
|
32
|
+
export declare const EULER_ANGLES_SCHEMA: z.ZodArray<z.ZodUnknown>;
|
|
33
|
+
export type TEulerAngles = [number, number, number];
|
|
29
34
|
/**
|
|
30
35
|
* Axis-angle representation as [axis_x, axis_y, axis_z, angle].
|
|
31
36
|
* The first three components define a normalized rotation axis,
|
|
32
37
|
* and the fourth component is the rotation angle in radians.
|
|
33
38
|
*
|
|
34
39
|
* @example
|
|
35
|
-
*
|
|
40
|
+
* ```typescript
|
|
41
|
+
* const axisAngle: TAxisAngle = [0, 1, 0, Math.PI/2]; // 90° rotation around Y-axis
|
|
42
|
+
* ```
|
|
36
43
|
*/
|
|
37
|
-
export
|
|
44
|
+
export declare const AXIS_ANGLE_SCHEMA: z.ZodArray<z.ZodUnknown>;
|
|
45
|
+
export type TAxisAngle = [number, number, number, number];
|
|
38
46
|
/**
|
|
39
47
|
* Rotation matrix representation as a 3x3 matrix.
|
|
40
48
|
* Uses the standard TMatrix3 type for better integration with matrix operations.
|
|
41
49
|
*
|
|
42
50
|
* @example
|
|
43
|
-
*
|
|
51
|
+
* ```typescript
|
|
52
|
+
* const identity: TRotationMatrix = [
|
|
44
53
|
* [1, 0, 0], // First row
|
|
45
54
|
* [0, 1, 0], // Second row
|
|
46
55
|
* [0, 0, 1] // Third row
|
|
47
56
|
* ];
|
|
57
|
+
* ```
|
|
48
58
|
*/
|
|
49
|
-
export
|
|
59
|
+
export declare const ROTATION_MATRIX_SCHEMA: z.ZodArray<z.ZodArray<z.ZodUnknown>>;
|
|
60
|
+
export type TRotationMatrix = [[number, number, number], [number, number, number], [number, number, number]];
|
|
50
61
|
/**
|
|
51
62
|
* Union type representing any supported rotation representation.
|
|
52
63
|
* Useful for functions that can accept multiple rotation formats.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/quaternions/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/quaternions/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,CAAC,MAAM,KAAK,CAAC;AAEpB;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,iBAAiB,0BAQ7B,CAAC;AACF,MAAM,MAAM,WAAW,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;AAE3D;;;;;;;;GAQG;AACH,eAAO,MAAM,mBAAmB,0BAQ/B,CAAC;AACF,MAAM,MAAM,YAAY,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;AAEpD;;;;;;;;;GASG;AACH,eAAO,MAAM,iBAAiB,0BAQ7B,CAAC;AACF,MAAM,MAAM,UAAU,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;AAE1D;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,sBAAsB,sCAUlC,CAAC;AACF,MAAM,MAAM,eAAe,GAAG,CAAC,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;AAE7G;;;GAGG;AACH,MAAM,MAAM,SAAS,GAAG,WAAW,GAAG,YAAY,GAAG,UAAU,GAAG,eAAe,CAAC"}
|
|
@@ -3,5 +3,55 @@
|
|
|
3
3
|
* Quaternions provide a mathematically robust way to represent rotations in 3D space,
|
|
4
4
|
* avoiding gimbal lock and providing smooth interpolation capabilities.
|
|
5
5
|
*/
|
|
6
|
-
|
|
6
|
+
import z from 'zod';
|
|
7
|
+
/**
|
|
8
|
+
* Quaternion type representing a rotation in 3D space as [x, y, z, w].
|
|
9
|
+
* The first three components (x, y, z) represent the vector part (rotation axis scaled by sin(θ/2)),
|
|
10
|
+
* and the fourth component (w) represents the scalar part (cos(θ/2)).
|
|
11
|
+
*
|
|
12
|
+
* Quaternions extend IVector4 to leverage vector operations while maintaining semantic meaning.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const identity: TQuaternion = [0, 0, 0, 1]; // No rotation
|
|
17
|
+
* const rotationX: TQuaternion = [1, 0, 0, 0]; // 180° rotation around X-axis
|
|
18
|
+
* const rotation90Y: TQuaternion = [0, 0.707, 0, 0.707]; // 90° rotation around Y-axis
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export const QUATERNION_SCHEMA = z.array(z.unknown().refine(val => typeof val === 'number' && !Number.isNaN(val), { message: 'Component must be a number' })).refine(arr => arr.length === 4, { message: 'Quaternion must have exactly 4 components' });
|
|
22
|
+
/**
|
|
23
|
+
* Euler angles representation as [x, y, z] rotations in radians.
|
|
24
|
+
* Order of rotation is typically Z-Y-X (yaw-pitch-roll) unless specified otherwise.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* const euler: TEulerAngles = [0, Math.PI/4, 0]; // 45° pitch rotation
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export const EULER_ANGLES_SCHEMA = z.array(z.unknown().refine(val => typeof val === 'number' && !Number.isNaN(val), { message: 'Component must be a number' })).refine(arr => arr.length === 3, { message: 'Euler angles must have exactly 3 components' });
|
|
32
|
+
/**
|
|
33
|
+
* Axis-angle representation as [axis_x, axis_y, axis_z, angle].
|
|
34
|
+
* The first three components define a normalized rotation axis,
|
|
35
|
+
* and the fourth component is the rotation angle in radians.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```typescript
|
|
39
|
+
* const axisAngle: TAxisAngle = [0, 1, 0, Math.PI/2]; // 90° rotation around Y-axis
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export const AXIS_ANGLE_SCHEMA = z.array(z.unknown().refine(val => typeof val === 'number' && !Number.isNaN(val), { message: 'Component must be a number' })).refine(arr => arr.length === 4, { message: 'Axis-angle must have exactly 4 components' });
|
|
43
|
+
/**
|
|
44
|
+
* Rotation matrix representation as a 3x3 matrix.
|
|
45
|
+
* Uses the standard TMatrix3 type for better integration with matrix operations.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
* const identity: TRotationMatrix = [
|
|
50
|
+
* [1, 0, 0], // First row
|
|
51
|
+
* [0, 1, 0], // Second row
|
|
52
|
+
* [0, 0, 1] // Third row
|
|
53
|
+
* ];
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export const ROTATION_MATRIX_SCHEMA = z.array(z.array(z.unknown().refine(val => typeof val === 'number' && !Number.isNaN(val), { message: 'Component must be a number' }))).refine(arr => arr.length === 3 && arr.every(row => row.length === 3), { message: 'Rotation matrix must be 3x3' });
|
|
7
57
|
//# sourceMappingURL=types.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/quaternions/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG"}
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/quaternions/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,CAAC,MAAM,KAAK,CAAC;AAEpB;;;;;;;;;;;;;GAaG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,CAAC,KAAK,CACvC,CAAC,CAAC,OAAO,EAAE,CAAC,MAAM,CACjB,GAAG,CAAC,EAAE,CAAC,OAAO,GAAG,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,EACpD,EAAE,OAAO,EAAE,4BAA4B,EAAE,CACzC,CACD,CAAC,MAAM,CACP,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,MAAM,KAAK,CAAC,EACvB,EAAE,OAAO,EAAE,2CAA2C,EAAE,CACxD,CAAC;AAGF;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,CAAC,KAAK,CACzC,CAAC,CA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CHANGED
|
@@ -2,11 +2,19 @@
|
|
|
2
2
|
* Generates a random integer within the specified range (inclusive).
|
|
3
3
|
* @param min - Minimum value (inclusive)
|
|
4
4
|
* @param max - Maximum value (inclusive)
|
|
5
|
-
* @returns Random integer between min and max
|
|
6
|
-
* @
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
5
|
+
* @returns Random integer between min and max. Returns Number.NaN when min > max.
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* RandomInt(1, 6) // Returns 1, 2, 3, 4, 5, or 6 (dice roll)
|
|
9
|
+
* ```
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* RandomInt(-5, 5) // Returns any integer from -5 to 5
|
|
13
|
+
* ```
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* RandomInt(10, 5) // Returns Number.NaN (invalid range)
|
|
17
|
+
* ```
|
|
10
18
|
*/
|
|
11
19
|
export declare function RandomInt(min: number, max: number): number;
|
|
12
20
|
/**
|
|
@@ -14,11 +22,19 @@ export declare function RandomInt(min: number, max: number): number;
|
|
|
14
22
|
* Note: Asymmetry with RandomInt — this function returns NaN when min >= max (not just min > max).
|
|
15
23
|
* @param min - Minimum value (inclusive)
|
|
16
24
|
* @param max - Maximum value (exclusive)
|
|
17
|
-
* @returns Random float between min (inclusive) and max (exclusive)
|
|
18
|
-
* @
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
25
|
+
* @returns Random float between min (inclusive) and max (exclusive). Returns Number.NaN when min >= max.
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* RandomFloat(0, 1) // Returns 0.0 to 0.999...
|
|
29
|
+
* ```
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* RandomFloat(-1.5, 1.5) // Returns any float from -1.5 to 1.499...
|
|
33
|
+
* ```
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* RandomFloat(5, 5) // Returns Number.NaN (min equals max)
|
|
37
|
+
* ```
|
|
22
38
|
*/
|
|
23
39
|
export declare function RandomFloat(min: number, max: number): number;
|
|
24
40
|
/**
|
|
@@ -26,8 +42,14 @@ export declare function RandomFloat(min: number, max: number): number;
|
|
|
26
42
|
* @template T - The type of elements in the array
|
|
27
43
|
* @param array - Array to choose from
|
|
28
44
|
* @returns Random element from the array, or undefined if array is empty
|
|
29
|
-
* @example
|
|
30
|
-
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* RandomChoice([1, 2, 3, 4, 5]) // Returns one of the numbers
|
|
48
|
+
* ```
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* RandomChoice(['red', 'green', 'blue']) // Returns one of the colors
|
|
52
|
+
* ```
|
|
31
53
|
*/
|
|
32
54
|
export declare function RandomChoice<T>(array: T[]): T | undefined;
|
|
33
55
|
/**
|
|
@@ -37,8 +59,14 @@ export declare function RandomChoice<T>(array: T[]): T | undefined;
|
|
|
37
59
|
* @param array - Array to choose from
|
|
38
60
|
* @param count - Number of elements to select
|
|
39
61
|
* @returns Array of `count` unique randomly selected elements, or empty array if inputs are invalid
|
|
40
|
-
* @example
|
|
41
|
-
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* RandomSample([1, 2, 3, 4, 5], 3) // Returns 3 unique numbers
|
|
65
|
+
* ```
|
|
66
|
+
* @example
|
|
67
|
+
* ```typescript
|
|
68
|
+
* RandomSample(['a', 'b', 'c'], 2) // Returns 2 unique letters
|
|
69
|
+
* ```
|
|
42
70
|
*/
|
|
43
71
|
export declare function RandomSample<T>(array: T[], count: number): T[];
|
|
44
72
|
/**
|
|
@@ -47,8 +75,14 @@ export declare function RandomSample<T>(array: T[], count: number): T[];
|
|
|
47
75
|
* @param array - Array to shuffle
|
|
48
76
|
* @param clone - If true, returns a shuffled copy; if false/undefined, modifies original array
|
|
49
77
|
* @returns Shuffled array (original reference if clone=false, new array if clone=true)
|
|
50
|
-
* @example
|
|
51
|
-
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```typescript
|
|
80
|
+
* RandomShuffle([1, 2, 3, 4, 5]) // Modifies and returns original array
|
|
81
|
+
* ```
|
|
82
|
+
* @example
|
|
83
|
+
* ```typescript
|
|
84
|
+
* RandomShuffle([1, 2, 3, 4, 5], true) // Returns new shuffled array, original unchanged
|
|
85
|
+
* ```
|
|
52
86
|
*/
|
|
53
87
|
export declare function RandomShuffle<T>(array: T[], clone?: boolean): T[];
|
|
54
88
|
/**
|
|
@@ -56,8 +90,14 @@ export declare function RandomShuffle<T>(array: T[], clone?: boolean): T[];
|
|
|
56
90
|
* @param probability - Probability of returning true (0.0 to 1.0, default: 0.5)
|
|
57
91
|
* @returns Random boolean based on probability
|
|
58
92
|
* @throws {RangeError} If probability is outside the range [0, 1]
|
|
59
|
-
* @example
|
|
60
|
-
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```typescript
|
|
95
|
+
* RandomBool() // 50% chance of true
|
|
96
|
+
* ```
|
|
97
|
+
* @example
|
|
98
|
+
* ```typescript
|
|
99
|
+
* RandomBool(0.8) // 80% chance of true
|
|
100
|
+
* ```
|
|
61
101
|
*/
|
|
62
102
|
export declare function RandomBool(probability?: number): boolean;
|
|
63
103
|
/**
|
|
@@ -67,8 +107,14 @@ export declare function RandomBool(probability?: number): boolean;
|
|
|
67
107
|
* @param mean - Mean of the distribution (default: 0)
|
|
68
108
|
* @param standardDeviation - Standard deviation of the distribution (default: 1)
|
|
69
109
|
* @returns Random number from normal distribution
|
|
70
|
-
* @example
|
|
71
|
-
*
|
|
110
|
+
* @example
|
|
111
|
+
* ```typescript
|
|
112
|
+
* RandomNormal() // Standard normal distribution (mean=0, std=1)
|
|
113
|
+
* ```
|
|
114
|
+
* @example
|
|
115
|
+
* ```typescript
|
|
116
|
+
* RandomNormal(100, 15) // IQ-like distribution (mean=100, std=15)
|
|
117
|
+
* ```
|
|
72
118
|
*/
|
|
73
119
|
export declare function RandomNormal(mean?: number, standardDeviation?: number): number;
|
|
74
120
|
//# sourceMappingURL=random.d.ts.map
|
package/build/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;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAI1D;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAI5D;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,CAAC,GAAG,SAAS,CAKzD;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,KAAK,EAAE,MAAM,GAAG,CAAC,EAAE,CAc9D;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,KAAK,CAAC,EAAE,OAAO,GAAG,CAAC,EAAE,CAWjE;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,UAAU,CAAC,WAAW,GAAE,MAAY,GAAG,OAAO,CAI7D;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,YAAY,CAAC,IAAI,GAAE,MAAU,EAAE,iBAAiB,GAAE,MAAU,GAAG,MAAM,CAiBpF"}
|
package/build/random.js
CHANGED
|
@@ -4,11 +4,19 @@ const BOX_MULLER_MIN_U1 = Number.EPSILON; // Avoid log(0) = -Infinity in Box-Mul
|
|
|
4
4
|
* Generates a random integer within the specified range (inclusive).
|
|
5
5
|
* @param min - Minimum value (inclusive)
|
|
6
6
|
* @param max - Maximum value (inclusive)
|
|
7
|
-
* @returns Random integer between min and max
|
|
8
|
-
* @
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
7
|
+
* @returns Random integer between min and max. Returns Number.NaN when min > max.
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* RandomInt(1, 6) // Returns 1, 2, 3, 4, 5, or 6 (dice roll)
|
|
11
|
+
* ```
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* RandomInt(-5, 5) // Returns any integer from -5 to 5
|
|
15
|
+
* ```
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* RandomInt(10, 5) // Returns Number.NaN (invalid range)
|
|
19
|
+
* ```
|
|
12
20
|
*/
|
|
13
21
|
export function RandomInt(min, max) {
|
|
14
22
|
if (min > max)
|
|
@@ -20,11 +28,19 @@ export function RandomInt(min, max) {
|
|
|
20
28
|
* Note: Asymmetry with RandomInt — this function returns NaN when min >= max (not just min > max).
|
|
21
29
|
* @param min - Minimum value (inclusive)
|
|
22
30
|
* @param max - Maximum value (exclusive)
|
|
23
|
-
* @returns Random float between min (inclusive) and max (exclusive)
|
|
24
|
-
* @
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
31
|
+
* @returns Random float between min (inclusive) and max (exclusive). Returns Number.NaN when min >= max.
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* RandomFloat(0, 1) // Returns 0.0 to 0.999...
|
|
35
|
+
* ```
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* RandomFloat(-1.5, 1.5) // Returns any float from -1.5 to 1.499...
|
|
39
|
+
* ```
|
|
40
|
+
* @example
|
|
41
|
+
* ```typescript
|
|
42
|
+
* RandomFloat(5, 5) // Returns Number.NaN (min equals max)
|
|
43
|
+
* ```
|
|
28
44
|
*/
|
|
29
45
|
export function RandomFloat(min, max) {
|
|
30
46
|
if (min >= max)
|
|
@@ -36,8 +52,14 @@ export function RandomFloat(min, max) {
|
|
|
36
52
|
* @template T - The type of elements in the array
|
|
37
53
|
* @param array - Array to choose from
|
|
38
54
|
* @returns Random element from the array, or undefined if array is empty
|
|
39
|
-
* @example
|
|
40
|
-
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* RandomChoice([1, 2, 3, 4, 5]) // Returns one of the numbers
|
|
58
|
+
* ```
|
|
59
|
+
* @example
|
|
60
|
+
* ```typescript
|
|
61
|
+
* RandomChoice(['red', 'green', 'blue']) // Returns one of the colors
|
|
62
|
+
* ```
|
|
41
63
|
*/
|
|
42
64
|
export function RandomChoice(array) {
|
|
43
65
|
if (array.length === 0)
|
|
@@ -52,8 +74,14 @@ export function RandomChoice(array) {
|
|
|
52
74
|
* @param array - Array to choose from
|
|
53
75
|
* @param count - Number of elements to select
|
|
54
76
|
* @returns Array of `count` unique randomly selected elements, or empty array if inputs are invalid
|
|
55
|
-
* @example
|
|
56
|
-
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```typescript
|
|
79
|
+
* RandomSample([1, 2, 3, 4, 5], 3) // Returns 3 unique numbers
|
|
80
|
+
* ```
|
|
81
|
+
* @example
|
|
82
|
+
* ```typescript
|
|
83
|
+
* RandomSample(['a', 'b', 'c'], 2) // Returns 2 unique letters
|
|
84
|
+
* ```
|
|
57
85
|
*/
|
|
58
86
|
export function RandomSample(array, count) {
|
|
59
87
|
if (array.length === 0 || count <= 0 || count > array.length)
|
|
@@ -74,8 +102,14 @@ export function RandomSample(array, count) {
|
|
|
74
102
|
* @param array - Array to shuffle
|
|
75
103
|
* @param clone - If true, returns a shuffled copy; if false/undefined, modifies original array
|
|
76
104
|
* @returns Shuffled array (original reference if clone=false, new array if clone=true)
|
|
77
|
-
* @example
|
|
78
|
-
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```typescript
|
|
107
|
+
* RandomShuffle([1, 2, 3, 4, 5]) // Modifies and returns original array
|
|
108
|
+
* ```
|
|
109
|
+
* @example
|
|
110
|
+
* ```typescript
|
|
111
|
+
* RandomShuffle([1, 2, 3, 4, 5], true) // Returns new shuffled array, original unchanged
|
|
112
|
+
* ```
|
|
79
113
|
*/
|
|
80
114
|
export function RandomShuffle(array, clone) {
|
|
81
115
|
const targetArray = clone ? [...array] : array;
|
|
@@ -92,8 +126,14 @@ export function RandomShuffle(array, clone) {
|
|
|
92
126
|
* @param probability - Probability of returning true (0.0 to 1.0, default: 0.5)
|
|
93
127
|
* @returns Random boolean based on probability
|
|
94
128
|
* @throws {RangeError} If probability is outside the range [0, 1]
|
|
95
|
-
* @example
|
|
96
|
-
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```typescript
|
|
131
|
+
* RandomBool() // 50% chance of true
|
|
132
|
+
* ```
|
|
133
|
+
* @example
|
|
134
|
+
* ```typescript
|
|
135
|
+
* RandomBool(0.8) // 80% chance of true
|
|
136
|
+
* ```
|
|
97
137
|
*/
|
|
98
138
|
export function RandomBool(probability = 0.5) {
|
|
99
139
|
if (probability < 0 || probability > 1)
|
|
@@ -107,12 +147,25 @@ export function RandomBool(probability = 0.5) {
|
|
|
107
147
|
* @param mean - Mean of the distribution (default: 0)
|
|
108
148
|
* @param standardDeviation - Standard deviation of the distribution (default: 1)
|
|
109
149
|
* @returns Random number from normal distribution
|
|
110
|
-
* @example
|
|
111
|
-
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```typescript
|
|
152
|
+
* RandomNormal() // Standard normal distribution (mean=0, std=1)
|
|
153
|
+
* ```
|
|
154
|
+
* @example
|
|
155
|
+
* ```typescript
|
|
156
|
+
* RandomNormal(100, 15) // IQ-like distribution (mean=100, std=15)
|
|
157
|
+
* ```
|
|
112
158
|
*/
|
|
113
159
|
export function RandomNormal(mean = 0, standardDeviation = 1) {
|
|
114
160
|
// Box-Muller transform — u1 must be > 0 to avoid log(0) = -Infinity
|
|
115
161
|
let u1 = Math.random();
|
|
162
|
+
/**
|
|
163
|
+
* Avoid log(0) → -Infinity by rejecting u1 too close to 0.
|
|
164
|
+
* BOX_MULLER_MIN_U1 = Number.EPSILON ≈ 2.22e-16.
|
|
165
|
+
* Expected retry probability per call: ~2.22e-16 (effectively never executed).
|
|
166
|
+
* Box-Muller with u1 >= Number.EPSILON is statistically sound and produces
|
|
167
|
+
* valid normal-distributed values.
|
|
168
|
+
*/
|
|
116
169
|
while (u1 < BOX_MULLER_MIN_U1) {
|
|
117
170
|
u1 = Math.random();
|
|
118
171
|
}
|