@pawells/math-extended 1.0.1
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/LICENSE +21 -0
- package/README.md +319 -0
- package/build/angles.d.ts +31 -0
- package/build/angles.d.ts.map +1 -0
- package/build/angles.js +85 -0
- package/build/angles.js.map +1 -0
- package/build/angles.spec.d.ts +2 -0
- package/build/angles.spec.d.ts.map +1 -0
- package/build/angles.spec.js +147 -0
- package/build/angles.spec.js.map +1 -0
- package/build/clamp.d.ts +17 -0
- package/build/clamp.d.ts.map +1 -0
- package/build/clamp.js +19 -0
- package/build/clamp.js.map +1 -0
- package/build/clamp.spec.d.ts +2 -0
- package/build/clamp.spec.d.ts.map +1 -0
- package/build/clamp.spec.js +19 -0
- package/build/clamp.spec.js.map +1 -0
- package/build/documentation-validation.spec.d.ts +11 -0
- package/build/documentation-validation.spec.d.ts.map +1 -0
- package/build/documentation-validation.spec.js +401 -0
- package/build/documentation-validation.spec.js.map +1 -0
- package/build/index.d.ts +8 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +8 -0
- package/build/index.js.map +1 -0
- package/build/interpolation.d.ts +175 -0
- package/build/interpolation.d.ts.map +1 -0
- package/build/interpolation.js +369 -0
- package/build/interpolation.js.map +1 -0
- package/build/interpolation.spec.d.ts +2 -0
- package/build/interpolation.spec.d.ts.map +1 -0
- package/build/interpolation.spec.js +480 -0
- package/build/interpolation.spec.js.map +1 -0
- package/build/matrices/arithmetic.d.ts +411 -0
- package/build/matrices/arithmetic.d.ts.map +1 -0
- package/build/matrices/arithmetic.js +954 -0
- package/build/matrices/arithmetic.js.map +1 -0
- package/build/matrices/arithmetic.spec.d.ts +2 -0
- package/build/matrices/arithmetic.spec.d.ts.map +1 -0
- package/build/matrices/arithmetic.spec.js +915 -0
- package/build/matrices/arithmetic.spec.js.map +1 -0
- package/build/matrices/asserts.d.ts +306 -0
- package/build/matrices/asserts.d.ts.map +1 -0
- package/build/matrices/asserts.js +396 -0
- package/build/matrices/asserts.js.map +1 -0
- package/build/matrices/asserts.spec.d.ts +2 -0
- package/build/matrices/asserts.spec.d.ts.map +1 -0
- package/build/matrices/asserts.spec.js +565 -0
- package/build/matrices/asserts.spec.js.map +1 -0
- package/build/matrices/core.d.ts +168 -0
- package/build/matrices/core.d.ts.map +1 -0
- package/build/matrices/core.js +457 -0
- package/build/matrices/core.js.map +1 -0
- package/build/matrices/core.spec.d.ts +2 -0
- package/build/matrices/core.spec.d.ts.map +1 -0
- package/build/matrices/core.spec.js +634 -0
- package/build/matrices/core.spec.js.map +1 -0
- package/build/matrices/decompositions.d.ts +326 -0
- package/build/matrices/decompositions.d.ts.map +1 -0
- package/build/matrices/decompositions.js +816 -0
- package/build/matrices/decompositions.js.map +1 -0
- package/build/matrices/decompositions.spec.d.ts +2 -0
- package/build/matrices/decompositions.spec.d.ts.map +1 -0
- package/build/matrices/decompositions.spec.js +195 -0
- package/build/matrices/decompositions.spec.js.map +1 -0
- package/build/matrices/index.d.ts +9 -0
- package/build/matrices/index.d.ts.map +1 -0
- package/build/matrices/index.js +9 -0
- package/build/matrices/index.js.map +1 -0
- package/build/matrices/linear-algebra.d.ts +64 -0
- package/build/matrices/linear-algebra.d.ts.map +1 -0
- package/build/matrices/linear-algebra.js +253 -0
- package/build/matrices/linear-algebra.js.map +1 -0
- package/build/matrices/linear-algebra.spec.d.ts +2 -0
- package/build/matrices/linear-algebra.spec.d.ts.map +1 -0
- package/build/matrices/linear-algebra.spec.js +355 -0
- package/build/matrices/linear-algebra.spec.js.map +1 -0
- package/build/matrices/normalization.d.ts +62 -0
- package/build/matrices/normalization.d.ts.map +1 -0
- package/build/matrices/normalization.js +167 -0
- package/build/matrices/normalization.js.map +1 -0
- package/build/matrices/normalization.spec.d.ts +2 -0
- package/build/matrices/normalization.spec.d.ts.map +1 -0
- package/build/matrices/normalization.spec.js +335 -0
- package/build/matrices/normalization.spec.js.map +1 -0
- package/build/matrices/transformations.d.ts +484 -0
- package/build/matrices/transformations.d.ts.map +1 -0
- package/build/matrices/transformations.js +592 -0
- package/build/matrices/transformations.js.map +1 -0
- package/build/matrices/transformations.spec.d.ts +2 -0
- package/build/matrices/transformations.spec.d.ts.map +1 -0
- package/build/matrices/transformations.spec.js +755 -0
- package/build/matrices/transformations.spec.js.map +1 -0
- package/build/matrices/types.d.ts +134 -0
- package/build/matrices/types.d.ts.map +1 -0
- package/build/matrices/types.js +6 -0
- package/build/matrices/types.js.map +1 -0
- package/build/quaternions/asserts.d.ts +77 -0
- package/build/quaternions/asserts.d.ts.map +1 -0
- package/build/quaternions/asserts.js +175 -0
- package/build/quaternions/asserts.js.map +1 -0
- package/build/quaternions/asserts.spec.d.ts +2 -0
- package/build/quaternions/asserts.spec.d.ts.map +1 -0
- package/build/quaternions/asserts.spec.js +320 -0
- package/build/quaternions/asserts.spec.js.map +1 -0
- package/build/quaternions/conversions.d.ts +73 -0
- package/build/quaternions/conversions.d.ts.map +1 -0
- package/build/quaternions/conversions.js +179 -0
- package/build/quaternions/conversions.js.map +1 -0
- package/build/quaternions/conversions.spec.d.ts +2 -0
- package/build/quaternions/conversions.spec.d.ts.map +1 -0
- package/build/quaternions/conversions.spec.js +344 -0
- package/build/quaternions/conversions.spec.js.map +1 -0
- package/build/quaternions/core.d.ts +203 -0
- package/build/quaternions/core.d.ts.map +1 -0
- package/build/quaternions/core.js +374 -0
- package/build/quaternions/core.js.map +1 -0
- package/build/quaternions/core.spec.d.ts +2 -0
- package/build/quaternions/core.spec.d.ts.map +1 -0
- package/build/quaternions/core.spec.js +294 -0
- package/build/quaternions/core.spec.js.map +1 -0
- package/build/quaternions/index.d.ts +7 -0
- package/build/quaternions/index.d.ts.map +1 -0
- package/build/quaternions/index.js +7 -0
- package/build/quaternions/index.js.map +1 -0
- package/build/quaternions/interpolation.d.ts +54 -0
- package/build/quaternions/interpolation.d.ts.map +1 -0
- package/build/quaternions/interpolation.js +201 -0
- package/build/quaternions/interpolation.js.map +1 -0
- package/build/quaternions/interpolation.spec.d.ts +2 -0
- package/build/quaternions/interpolation.spec.d.ts.map +1 -0
- package/build/quaternions/interpolation.spec.js +64 -0
- package/build/quaternions/interpolation.spec.js.map +1 -0
- package/build/quaternions/predefined.d.ts +36 -0
- package/build/quaternions/predefined.d.ts.map +1 -0
- package/build/quaternions/predefined.js +42 -0
- package/build/quaternions/predefined.js.map +1 -0
- package/build/quaternions/predefined.spec.d.ts +2 -0
- package/build/quaternions/predefined.spec.d.ts.map +1 -0
- package/build/quaternions/predefined.spec.js +35 -0
- package/build/quaternions/predefined.spec.js.map +1 -0
- package/build/quaternions/types.d.ts +55 -0
- package/build/quaternions/types.d.ts.map +1 -0
- package/build/quaternions/types.js +7 -0
- package/build/quaternions/types.js.map +1 -0
- package/build/random.d.ts +66 -0
- package/build/random.d.ts.map +1 -0
- package/build/random.js +115 -0
- package/build/random.js.map +1 -0
- package/build/random.spec.d.ts +2 -0
- package/build/random.spec.d.ts.map +1 -0
- package/build/random.spec.js +267 -0
- package/build/random.spec.js.map +1 -0
- package/build/vectors/asserts.d.ts +182 -0
- package/build/vectors/asserts.d.ts.map +1 -0
- package/build/vectors/asserts.js +285 -0
- package/build/vectors/asserts.js.map +1 -0
- package/build/vectors/asserts.spec.d.ts +2 -0
- package/build/vectors/asserts.spec.d.ts.map +1 -0
- package/build/vectors/asserts.spec.js +260 -0
- package/build/vectors/asserts.spec.js.map +1 -0
- package/build/vectors/core.d.ts +507 -0
- package/build/vectors/core.d.ts.map +1 -0
- package/build/vectors/core.js +825 -0
- package/build/vectors/core.js.map +1 -0
- package/build/vectors/core.spec.d.ts +2 -0
- package/build/vectors/core.spec.d.ts.map +1 -0
- package/build/vectors/core.spec.js +343 -0
- package/build/vectors/core.spec.js.map +1 -0
- package/build/vectors/index.d.ts +6 -0
- package/build/vectors/index.d.ts.map +1 -0
- package/build/vectors/index.js +6 -0
- package/build/vectors/index.js.map +1 -0
- package/build/vectors/interpolation.d.ts +404 -0
- package/build/vectors/interpolation.d.ts.map +1 -0
- package/build/vectors/interpolation.js +585 -0
- package/build/vectors/interpolation.js.map +1 -0
- package/build/vectors/interpolation.spec.d.ts +2 -0
- package/build/vectors/interpolation.spec.d.ts.map +1 -0
- package/build/vectors/interpolation.spec.js +378 -0
- package/build/vectors/interpolation.spec.js.map +1 -0
- package/build/vectors/predefined.d.ts +191 -0
- package/build/vectors/predefined.d.ts.map +1 -0
- package/build/vectors/predefined.js +191 -0
- package/build/vectors/predefined.js.map +1 -0
- package/build/vectors/predefined.spec.d.ts +2 -0
- package/build/vectors/predefined.spec.d.ts.map +1 -0
- package/build/vectors/predefined.spec.js +333 -0
- package/build/vectors/predefined.spec.js.map +1 -0
- package/build/vectors/types.d.ts +62 -0
- package/build/vectors/types.d.ts.map +1 -0
- package/build/vectors/types.js +6 -0
- package/build/vectors/types.js.map +1 -0
- package/package.json +75 -0
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { RandomInt, RandomFloat, RandomChoice, RandomSample, RandomShuffle, RandomBool, RandomNormal, } from './random.ts';
|
|
2
|
+
describe('Math > Random ', () => {
|
|
3
|
+
describe('RandomInt', () => {
|
|
4
|
+
test('Generate a Random Integer within a Positive Range', () => {
|
|
5
|
+
const min = 1;
|
|
6
|
+
const max = 10;
|
|
7
|
+
const result = RandomInt(min, max);
|
|
8
|
+
expect(result).toBeGreaterThanOrEqual(min);
|
|
9
|
+
expect(result).toBeLessThanOrEqual(max);
|
|
10
|
+
expect(Number.isInteger(result)).toBe(true);
|
|
11
|
+
});
|
|
12
|
+
test('Generate a Random Integer within a Negative Range', () => {
|
|
13
|
+
const min = -10;
|
|
14
|
+
const max = -1;
|
|
15
|
+
const result = RandomInt(min, max);
|
|
16
|
+
expect(result).toBeGreaterThanOrEqual(min);
|
|
17
|
+
expect(result).toBeLessThanOrEqual(max);
|
|
18
|
+
expect(Number.isInteger(result)).toBe(true);
|
|
19
|
+
});
|
|
20
|
+
test('If the minimum value and maximum value are the same, return the minimum value.', () => {
|
|
21
|
+
const min = 5;
|
|
22
|
+
const max = 5;
|
|
23
|
+
const result = RandomInt(min, max);
|
|
24
|
+
expect(result).toBe(min);
|
|
25
|
+
});
|
|
26
|
+
test('If the minimum value is greater than the maximum value, return NaN.', () => {
|
|
27
|
+
const min = 10;
|
|
28
|
+
const max = 5;
|
|
29
|
+
const result = RandomInt(min, max);
|
|
30
|
+
expect(result).toBeNaN();
|
|
31
|
+
});
|
|
32
|
+
test('Should include both min and max values over multiple runs', () => {
|
|
33
|
+
const min = 1;
|
|
34
|
+
const max = 3;
|
|
35
|
+
const results = new Set();
|
|
36
|
+
// Run enough times to likely hit all values
|
|
37
|
+
for (let i = 0; i < 100; i++) {
|
|
38
|
+
results.add(RandomInt(min, max));
|
|
39
|
+
}
|
|
40
|
+
expect(results.has(min)).toBe(true);
|
|
41
|
+
expect(results.has(max)).toBe(true);
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
describe('RandomFloat', () => {
|
|
45
|
+
test('Generate a Random Float within Range', () => {
|
|
46
|
+
const min = 0;
|
|
47
|
+
const max = 1;
|
|
48
|
+
const result = RandomFloat(min, max);
|
|
49
|
+
expect(result).toBeGreaterThanOrEqual(min);
|
|
50
|
+
expect(result).toBeLessThan(max);
|
|
51
|
+
expect(typeof result).toBe('number');
|
|
52
|
+
});
|
|
53
|
+
test('Generate a Random Float within Negative Range', () => {
|
|
54
|
+
const min = -5.5;
|
|
55
|
+
const max = -1.1;
|
|
56
|
+
const result = RandomFloat(min, max);
|
|
57
|
+
expect(result).toBeGreaterThanOrEqual(min);
|
|
58
|
+
expect(result).toBeLessThan(max);
|
|
59
|
+
});
|
|
60
|
+
test('If minimum equals maximum, return NaN', () => {
|
|
61
|
+
const min = 5.5;
|
|
62
|
+
const max = 5.5;
|
|
63
|
+
const result = RandomFloat(min, max);
|
|
64
|
+
expect(result).toBeNaN();
|
|
65
|
+
});
|
|
66
|
+
test('If minimum is greater than maximum, return NaN', () => {
|
|
67
|
+
const min = 10.5;
|
|
68
|
+
const max = 5.5;
|
|
69
|
+
const result = RandomFloat(min, max);
|
|
70
|
+
expect(result).toBeNaN();
|
|
71
|
+
});
|
|
72
|
+
test('Should generate different values over multiple runs', () => {
|
|
73
|
+
const results = new Set();
|
|
74
|
+
for (let i = 0; i < 10; i++) {
|
|
75
|
+
results.add(RandomFloat(0, 1));
|
|
76
|
+
}
|
|
77
|
+
// Should have generated different values (very unlikely to get duplicates)
|
|
78
|
+
expect(results.size).toBeGreaterThan(1);
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
describe('RandomChoice', () => {
|
|
82
|
+
test('Choose from array of numbers', () => {
|
|
83
|
+
const array = [1, 2, 3, 4, 5];
|
|
84
|
+
const result = RandomChoice(array);
|
|
85
|
+
expect(array).toContain(result);
|
|
86
|
+
});
|
|
87
|
+
test('Choose from array of strings', () => {
|
|
88
|
+
const array = ['red', 'green', 'blue'];
|
|
89
|
+
const result = RandomChoice(array);
|
|
90
|
+
expect(array).toContain(result);
|
|
91
|
+
});
|
|
92
|
+
test('Return undefined for empty array', () => {
|
|
93
|
+
const array = [];
|
|
94
|
+
const result = RandomChoice(array);
|
|
95
|
+
expect(result).toBeUndefined();
|
|
96
|
+
});
|
|
97
|
+
test('Return the only element for single-element array', () => {
|
|
98
|
+
const array = [42];
|
|
99
|
+
const result = RandomChoice(array);
|
|
100
|
+
expect(result).toBe(42);
|
|
101
|
+
});
|
|
102
|
+
test('Should eventually choose all elements over multiple runs', () => {
|
|
103
|
+
const array = [1, 2, 3];
|
|
104
|
+
const results = new Set();
|
|
105
|
+
for (let i = 0; i < 50; i++) {
|
|
106
|
+
const choice = RandomChoice(array);
|
|
107
|
+
if (choice !== undefined) {
|
|
108
|
+
results.add(choice);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
expect(results.size).toBe(3);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
describe('RandomSample', () => {
|
|
115
|
+
test('Sample multiple elements without replacement', () => {
|
|
116
|
+
const array = [1, 2, 3, 4, 5];
|
|
117
|
+
const result = RandomSample(array, 3);
|
|
118
|
+
expect(result).toHaveLength(3);
|
|
119
|
+
expect(new Set(result).size).toBe(3); // All unique
|
|
120
|
+
result.forEach((item) => {
|
|
121
|
+
expect(array).toContain(item);
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
test('Sample all elements', () => {
|
|
125
|
+
const array = [1, 2, 3];
|
|
126
|
+
const result = RandomSample(array, 3);
|
|
127
|
+
expect(result).toHaveLength(3);
|
|
128
|
+
expect(new Set(result).size).toBe(3);
|
|
129
|
+
});
|
|
130
|
+
test('Return empty array for invalid inputs', () => {
|
|
131
|
+
const array = [1, 2, 3];
|
|
132
|
+
expect(RandomSample(array, 0)).toEqual([]);
|
|
133
|
+
expect(RandomSample(array, -1)).toEqual([]);
|
|
134
|
+
expect(RandomSample(array, 5)).toEqual([]); // More than available
|
|
135
|
+
expect(RandomSample([], 1)).toEqual([]);
|
|
136
|
+
});
|
|
137
|
+
test('Sample single element', () => {
|
|
138
|
+
const array = [1, 2, 3, 4, 5];
|
|
139
|
+
const result = RandomSample(array, 1);
|
|
140
|
+
expect(result).toHaveLength(1);
|
|
141
|
+
expect(array).toContain(result[0]);
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
describe('RandomShuffle', () => {
|
|
145
|
+
test('Shuffle modifies original array by default', () => {
|
|
146
|
+
const original = [1, 2, 3, 4, 5];
|
|
147
|
+
const copy = [...original];
|
|
148
|
+
const result = RandomShuffle(original);
|
|
149
|
+
expect(result).toBe(original);
|
|
150
|
+
expect(result).toHaveLength(copy.length);
|
|
151
|
+
// Should contain all original elements
|
|
152
|
+
copy.forEach((item) => {
|
|
153
|
+
expect(result).toContain(item);
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
test('Shuffle with clone=true does not modify original array', () => {
|
|
157
|
+
const original = [1, 2, 3, 4, 5];
|
|
158
|
+
const copy = [...original];
|
|
159
|
+
const result = RandomShuffle(original, true);
|
|
160
|
+
expect(result).not.toBe(original);
|
|
161
|
+
expect(original).toEqual(copy);
|
|
162
|
+
expect(result).toHaveLength(original.length);
|
|
163
|
+
// Should contain all original elements
|
|
164
|
+
original.forEach((item) => {
|
|
165
|
+
expect(result).toContain(item);
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
test('Shuffle with clone=false modifies original array', () => {
|
|
169
|
+
const original = [1, 2, 3, 4, 5];
|
|
170
|
+
const copy = [...original];
|
|
171
|
+
const result = RandomShuffle(original, false);
|
|
172
|
+
expect(result).toBe(original);
|
|
173
|
+
expect(result).toHaveLength(copy.length);
|
|
174
|
+
// Should contain all original elements
|
|
175
|
+
copy.forEach((item) => {
|
|
176
|
+
expect(result).toContain(item);
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
test('Shuffle empty array', () => {
|
|
180
|
+
const array = [];
|
|
181
|
+
const result = RandomShuffle(array);
|
|
182
|
+
expect(result).toEqual([]);
|
|
183
|
+
});
|
|
184
|
+
test('Shuffle single element array', () => {
|
|
185
|
+
const array = [42];
|
|
186
|
+
const result = RandomShuffle(array);
|
|
187
|
+
expect(result).toEqual([42]);
|
|
188
|
+
});
|
|
189
|
+
test('Should eventually produce different arrangements', () => {
|
|
190
|
+
const arrangements = new Set();
|
|
191
|
+
for (let i = 0; i < 20; i++) {
|
|
192
|
+
const array = [1, 2, 3];
|
|
193
|
+
RandomShuffle(array);
|
|
194
|
+
arrangements.add(JSON.stringify(array));
|
|
195
|
+
}
|
|
196
|
+
// Should have generated at least 2 different arrangements
|
|
197
|
+
expect(arrangements.size).toBeGreaterThan(1);
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
describe('RandomBool', () => {
|
|
201
|
+
test('Generate random boolean with default probability', () => {
|
|
202
|
+
const result = RandomBool();
|
|
203
|
+
expect(typeof result).toBe('boolean');
|
|
204
|
+
});
|
|
205
|
+
test('Generate random boolean with custom probability', () => {
|
|
206
|
+
// Test with very high probability
|
|
207
|
+
let trueCount = 0;
|
|
208
|
+
for (let i = 0; i < 100; i++) {
|
|
209
|
+
if (RandomBool(0.9)) {
|
|
210
|
+
trueCount++;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
// Should be mostly true (allowing some variance)
|
|
214
|
+
expect(trueCount).toBeGreaterThan(70);
|
|
215
|
+
// Test with very low probability
|
|
216
|
+
let falseCount = 0;
|
|
217
|
+
for (let i = 0; i < 100; i++) {
|
|
218
|
+
if (!RandomBool(0.1)) {
|
|
219
|
+
falseCount++;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
// Should be mostly false (allowing some variance)
|
|
223
|
+
expect(falseCount).toBeGreaterThan(70);
|
|
224
|
+
});
|
|
225
|
+
test('Return false for invalid probabilities', () => {
|
|
226
|
+
expect(RandomBool(-0.1)).toBe(false);
|
|
227
|
+
expect(RandomBool(1.1)).toBe(false);
|
|
228
|
+
});
|
|
229
|
+
test('Return true for probability 1.0', () => {
|
|
230
|
+
for (let i = 0; i < 10; i++) {
|
|
231
|
+
expect(RandomBool(1.0)).toBe(true);
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
test('Return false for probability 0.0', () => {
|
|
235
|
+
for (let i = 0; i < 10; i++) {
|
|
236
|
+
expect(RandomBool(0.0)).toBe(false);
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
});
|
|
240
|
+
describe('RandomNormal', () => {
|
|
241
|
+
test('Generate random normal distribution with default parameters', () => {
|
|
242
|
+
const results = [];
|
|
243
|
+
for (let i = 0; i < 1000; i++) {
|
|
244
|
+
results.push(RandomNormal());
|
|
245
|
+
}
|
|
246
|
+
const mean = results.reduce((sum, val) => sum + val, 0) / results.length;
|
|
247
|
+
const variance = results.reduce((sum, val) => sum + Math.pow(val - mean, 2), 0) / results.length;
|
|
248
|
+
// Should be approximately standard normal (mean ≈ 0, variance ≈ 1)
|
|
249
|
+
expect(Math.abs(mean)).toBeLessThan(0.1);
|
|
250
|
+
expect(Math.abs(variance - 1)).toBeLessThan(0.2);
|
|
251
|
+
});
|
|
252
|
+
test('Generate random normal distribution with custom parameters', () => {
|
|
253
|
+
const targetMean = 100;
|
|
254
|
+
const targetStd = 15;
|
|
255
|
+
const results = [];
|
|
256
|
+
for (let i = 0; i < 1000; i++) {
|
|
257
|
+
results.push(RandomNormal(targetMean, targetStd));
|
|
258
|
+
}
|
|
259
|
+
const mean = results.reduce((sum, val) => sum + val, 0) / results.length;
|
|
260
|
+
const variance = results.reduce((sum, val) => sum + Math.pow(val - mean, 2), 0) / results.length;
|
|
261
|
+
const stdDev = Math.sqrt(variance);
|
|
262
|
+
// Should be approximately the target distribution expect(Math.abs(mean - targetMean)).toBeLessThan(2);
|
|
263
|
+
expect(Math.abs(stdDev - targetStd)).toBeLessThan(2);
|
|
264
|
+
});
|
|
265
|
+
});
|
|
266
|
+
});
|
|
267
|
+
//# sourceMappingURL=random.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"random.spec.js","sourceRoot":"","sources":["../src/random.spec.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,SAAS,EACT,WAAW,EACX,YAAY,EACZ,YAAY,EACZ,aAAa,EACb,UAAU,EACV,YAAY,GACZ,MAAM,aAAa,CAAC;AAErB,QAAQ,CAAC,gBAAgB,EAAE,GAAG,EAAE;IAC/B,QAAQ,CAAC,WAAW,EAAE,GAAG,EAAE;QAC1B,IAAI,CAAC,mDAAmD,EAAE,GAAG,EAAE;YAC9D,MAAM,GAAG,GAAG,CAAC,CAAC;YACd,MAAM,GAAG,GAAG,EAAE,CAAC;YACf,MAAM,MAAM,GAAG,SAAS,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;YACnC,MAAM,CAAC,MAAM,CAAC,CAAC,sBAAsB,CAAC,GAAG,CAAC,CAAC;YAC3C,MAAM,CAAC,MAAM,CAAC,CAAC,mBAAmB,CAAC,GAAG,CAAC,CAAC;YACxC,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,MAAM,CA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
3
|
+
* Provides comprehensive validation functions to ensure vector data integrity
|
|
4
|
+
* and catch errors early in mathematical operations.
|
|
5
|
+
*/
|
|
6
|
+
import { IAssertException, IAssertNumberArgs, IAssertArrayArgs } from '@pawells/typescript-common';
|
|
7
|
+
import { TVector, TVector2, TVector3, TVector4 } from './types.js';
|
|
8
|
+
/**
|
|
9
|
+
* Configuration options for vector assertion functions.
|
|
10
|
+
* Allows flexible validation of vector properties and constraints.
|
|
11
|
+
*/
|
|
12
|
+
export type TAssertVectorArgs = IAssertNumberArgs & IAssertArrayArgs;
|
|
13
|
+
/**
|
|
14
|
+
* Extended configuration for validating multiple vectors.
|
|
15
|
+
* Includes all single vector options plus multi-vector constraints.
|
|
16
|
+
*/
|
|
17
|
+
interface IAssertVectorsArgs extends TAssertVectorArgs {
|
|
18
|
+
/**
|
|
19
|
+
* Minimum vector size allowed.
|
|
20
|
+
* If specified, vectors must have at least this many components.
|
|
21
|
+
*/
|
|
22
|
+
minSize?: number;
|
|
23
|
+
/**
|
|
24
|
+
* Whether all vectors must have the same size.
|
|
25
|
+
* Default: true when using individual arguments, configurable when using array syntax.
|
|
26
|
+
*/
|
|
27
|
+
sameSize?: boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Whether all vector elements must be finite numbers.
|
|
30
|
+
* If true, validates that each element is not Infinity or -Infinity.
|
|
31
|
+
*/
|
|
32
|
+
finite?: boolean;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Extended exception interface for vector-specific error information.
|
|
36
|
+
* Provides additional context about which vector component caused the error.
|
|
37
|
+
*/
|
|
38
|
+
interface IAssertVectorException extends IAssertException {
|
|
39
|
+
/**
|
|
40
|
+
* Index of the vector component that caused the validation failure.
|
|
41
|
+
* Useful for debugging which specific element is invalid.
|
|
42
|
+
*/
|
|
43
|
+
index?: number;
|
|
44
|
+
}
|
|
45
|
+
type TAssertVectorValueArgs = IAssertNumberArgs;
|
|
46
|
+
/**
|
|
47
|
+
* Specialized error class for vector-related operations and validations.
|
|
48
|
+
* Thrown when vector assertions fail or vector operations encounter invalid data.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* try {
|
|
52
|
+
* AssertVector("not a vector");
|
|
53
|
+
* } catch (error) {
|
|
54
|
+
* if (error instanceof VectorError) {
|
|
55
|
+
* console.log("Vector validation failed:", error.message);
|
|
56
|
+
* }
|
|
57
|
+
* }
|
|
58
|
+
*/
|
|
59
|
+
export declare class VectorError extends Error {
|
|
60
|
+
/**
|
|
61
|
+
* Creates a new VectorError instance.
|
|
62
|
+
*
|
|
63
|
+
* @param message - Optional error message describing the validation failure
|
|
64
|
+
*/
|
|
65
|
+
constructor(message?: string);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Validates that an unknown value is a valid vector (array of numbers).
|
|
69
|
+
*
|
|
70
|
+
* Performs comprehensive validation including array structure, size constraints,
|
|
71
|
+
* and element-level number validation for every component.
|
|
72
|
+
*
|
|
73
|
+
* @param vector - The value to validate as a vector
|
|
74
|
+
* @param args - Validation constraints (size, minSize, maxSize, finite, integer, gt, gte, lt, lte, eq)
|
|
75
|
+
* @param exception - Custom exception details if validation fails
|
|
76
|
+
* @throws {VectorError} If the value is not a valid vector meeting all constraints
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```typescript
|
|
80
|
+
* // Basic validation — passes silently for a valid vector
|
|
81
|
+
* AssertVector([1, 2, 3]);
|
|
82
|
+
*
|
|
83
|
+
* // Validate with size constraint
|
|
84
|
+
* AssertVector([1, 2], { size: 2 });
|
|
85
|
+
*
|
|
86
|
+
* // Throws VectorError for non-array input
|
|
87
|
+
* AssertVector("not a vector"); // throws VectorError
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
export declare function AssertVector(vector: unknown, args?: TAssertVectorArgs, exception?: IAssertVectorException): asserts vector is TVector;
|
|
91
|
+
/**
|
|
92
|
+
* Validates that an unknown value is a valid 2D vector (exactly 2 numeric components).
|
|
93
|
+
*
|
|
94
|
+
* @param vector - The value to validate as a 2D vector
|
|
95
|
+
* @param exception - Custom exception details if validation fails
|
|
96
|
+
* @throws {VectorError} If the value is not a 2-component vector
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```typescript
|
|
100
|
+
* AssertVector2([1, 2]); // passes
|
|
101
|
+
* AssertVector2([1, 2, 3]); // throws — too many components
|
|
102
|
+
* AssertVector2("not a vec"); // throws
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
export declare function AssertVector2(vector: unknown, exception?: IAssertVectorException): asserts vector is TVector2;
|
|
106
|
+
/**
|
|
107
|
+
* Validates that an unknown value is a valid 3D vector (exactly 3 numeric components).
|
|
108
|
+
*
|
|
109
|
+
* @param vector - The value to validate as a 3D vector
|
|
110
|
+
* @param exception - Custom exception details if validation fails
|
|
111
|
+
* @throws {VectorError} If the value is not a 3-component vector
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```typescript
|
|
115
|
+
* AssertVector3([1, 2, 3]); // passes
|
|
116
|
+
* AssertVector3([1, 2]); // throws — too few components
|
|
117
|
+
* AssertVector3(null); // throws
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
export declare function AssertVector3(vector: unknown, exception?: IAssertVectorException): asserts vector is TVector3;
|
|
121
|
+
/**
|
|
122
|
+
* Validates that an unknown value is a valid 4D vector (exactly 4 numeric components).
|
|
123
|
+
*
|
|
124
|
+
* @param vector - The value to validate as a 4D vector
|
|
125
|
+
* @param exception - Custom exception details if validation fails
|
|
126
|
+
* @throws {VectorError} If the value is not a 4-component vector
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```typescript
|
|
130
|
+
* AssertVector4([1, 2, 3, 4]); // passes
|
|
131
|
+
* AssertVector4([1, 2, 3]); // throws — too few components
|
|
132
|
+
* AssertVector4(undefined); // throws
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
135
|
+
export declare function AssertVector4(vector: unknown, exception?: IAssertVectorException): asserts vector is TVector4;
|
|
136
|
+
/**
|
|
137
|
+
* Validates that an unknown value is a valid vector element (a finite, non-NaN number).
|
|
138
|
+
*
|
|
139
|
+
* Supports optional numeric constraints such as range bounds, integer enforcement,
|
|
140
|
+
* and finiteness checks. Throws `VectorError` with the component index when available.
|
|
141
|
+
*
|
|
142
|
+
* @param value - The value to validate as a vector element
|
|
143
|
+
* @param args - Numeric constraints (finite, integer, gt, gte, lt, lte, eq)
|
|
144
|
+
* @param exception - Custom exception details, optionally including a component `index`
|
|
145
|
+
* @throws {VectorError} If the value is not a valid number or violates any constraint
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```typescript
|
|
149
|
+
* AssertVectorValue(3.14); // passes
|
|
150
|
+
* AssertVectorValue(3.14, { finite: true }); // passes
|
|
151
|
+
* AssertVectorValue(Infinity, { finite: true }); // throws
|
|
152
|
+
* AssertVectorValue(NaN); // throws
|
|
153
|
+
* AssertVectorValue(5, { gte: 0, lte: 10 }); // passes
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
156
|
+
export declare function AssertVectorValue(value: unknown, args?: TAssertVectorValueArgs, exception?: IAssertVectorException): asserts value is number;
|
|
157
|
+
/**
|
|
158
|
+
* Validates an array of vectors, ensuring each vector is valid and optionally
|
|
159
|
+
* enforcing that all vectors share the same size.
|
|
160
|
+
*
|
|
161
|
+
* @param vectors - The array of vectors to validate (must be non-empty)
|
|
162
|
+
* @param args - Validation constraints applied to each vector; set `sameSize: true`
|
|
163
|
+
* to require all vectors to have identical lengths
|
|
164
|
+
* @param exception - Custom exception details if validation fails
|
|
165
|
+
* @throws {VectorError} If the array is empty, any vector is invalid, or sizes
|
|
166
|
+
* differ when `sameSize` is `true`
|
|
167
|
+
*
|
|
168
|
+
* @example
|
|
169
|
+
* ```typescript
|
|
170
|
+
* // Validate an array of 3D vectors
|
|
171
|
+
* AssertVectors([[1, 2, 3], [4, 5, 6]]);
|
|
172
|
+
*
|
|
173
|
+
* // Require all vectors to share the same length
|
|
174
|
+
* AssertVectors([[1, 2], [3, 4]], { sameSize: true });
|
|
175
|
+
*
|
|
176
|
+
* // Throws because vectors have different sizes
|
|
177
|
+
* AssertVectors([[1, 2], [1, 2, 3]], { sameSize: true }); // throws
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
180
|
+
export declare function AssertVectors(vectors: unknown[], args?: IAssertVectorsArgs, exception?: IAssertVectorException): void;
|
|
181
|
+
export {};
|
|
182
|
+
//# sourceMappingURL=asserts.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"asserts.d.ts","sourceRoot":"","sources":["../../src/vectors/asserts.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,gBAAgB,EAAuC,MAAM,4BAA4B,CAAC;AACxI,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEnE;;;GAGG;AACH,MAAM,MAAM,iBAAiB,GAAG,iBAAiB,GAAG,gBAAgB,CAAC;AAErE;;;GAGG;AACH,UAAU,kBAAmB,SAAQ,iBAAiB;IACrD;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB;;;OAGG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB;;;OAGG;IACH,MAAM,CAAC,EAAE,OAAO,CAAC;CACjB;AACD;;;GAGG;AACH,UAAU,sBAAuB,SAAQ,gBAAgB;IACxD;;;OAGG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;CACf;AACD,KAAK,sBAAsB,GAAG,iBAAiB,CAAC;AAEhD;;;;;;;;;;;;GAYG;AACH,qBAAa,WAAY,SAAQ,KAAK;IACrC;;;;OAIG;gBACS,OAAO,CAAC,EAAE,MAAM;CAK5B;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,O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