max-priority-queue-typed 2.4.5 → 2.5.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/README.md +63 -0
- package/dist/cjs/index.cjs +694 -119
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +693 -118
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +694 -119
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +693 -118
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
- package/dist/types/data-structures/base/linear-base.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +380 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +487 -147
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +956 -80
- package/dist/types/data-structures/binary-tree/bst.d.ts +816 -29
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +610 -31
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +326 -135
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +3781 -6
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3607 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2874 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +3528 -6
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
- package/dist/types/data-structures/graph/directed-graph.d.ts +429 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +393 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +473 -89
- package/dist/types/data-structures/heap/heap.d.ts +581 -99
- package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +646 -47
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +596 -68
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +793 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +499 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
- package/dist/types/data-structures/queue/deque.d.ts +593 -71
- package/dist/types/data-structures/queue/queue.d.ts +463 -42
- package/dist/types/data-structures/stack/stack.d.ts +384 -32
- package/dist/types/data-structures/trie/trie.d.ts +470 -48
- package/dist/types/interfaces/graph.d.ts +1 -1
- package/dist/types/types/common.d.ts +2 -2
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
- package/dist/types/types/utils/validate-type.d.ts +4 -4
- package/dist/umd/max-priority-queue-typed.js +691 -116
- package/dist/umd/max-priority-queue-typed.js.map +1 -1
- package/dist/umd/max-priority-queue-typed.min.js +1 -1
- package/dist/umd/max-priority-queue-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-element-base.ts +4 -5
- package/src/data-structures/base/iterable-entry-base.ts +8 -8
- package/src/data-structures/base/linear-base.ts +3 -3
- package/src/data-structures/binary-tree/avl-tree.ts +386 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +596 -247
- package/src/data-structures/binary-tree/binary-tree.ts +956 -81
- package/src/data-structures/binary-tree/bst.ts +840 -35
- package/src/data-structures/binary-tree/red-black-tree.ts +689 -97
- package/src/data-structures/binary-tree/segment-tree.ts +498 -249
- package/src/data-structures/binary-tree/tree-map.ts +3784 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +3614 -211
- package/src/data-structures/binary-tree/tree-multi-set.ts +2874 -65
- package/src/data-structures/binary-tree/tree-set.ts +3531 -10
- package/src/data-structures/graph/abstract-graph.ts +4 -4
- package/src/data-structures/graph/directed-graph.ts +429 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +393 -59
- package/src/data-structures/hash/hash-map.ts +476 -92
- package/src/data-structures/heap/heap.ts +581 -99
- package/src/data-structures/heap/max-heap.ts +46 -0
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +646 -47
- package/src/data-structures/linked-list/singly-linked-list.ts +596 -68
- package/src/data-structures/linked-list/skip-linked-list.ts +1067 -90
- package/src/data-structures/matrix/matrix.ts +584 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
- package/src/data-structures/priority-queue/priority-queue.ts +60 -0
- package/src/data-structures/queue/deque.ts +592 -70
- package/src/data-structures/queue/queue.ts +463 -42
- package/src/data-structures/stack/stack.ts +384 -32
- package/src/data-structures/trie/trie.ts +470 -48
- package/src/interfaces/graph.ts +1 -1
- package/src/types/common.ts +2 -2
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/heap/heap.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
- package/src/types/utils/validate-type.ts +4 -4
|
@@ -9,6 +9,89 @@ import type { MatrixOptions } from '../../types';
|
|
|
9
9
|
/**
|
|
10
10
|
*
|
|
11
11
|
*/
|
|
12
|
+
/**
|
|
13
|
+
* Matrix — a numeric matrix with standard linear algebra operations.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* // Basic matrix arithmetic
|
|
17
|
+
* const a = new Matrix([
|
|
18
|
+
* [1, 2],
|
|
19
|
+
* [3, 4]
|
|
20
|
+
* ]);
|
|
21
|
+
* const b = new Matrix([
|
|
22
|
+
* [5, 6],
|
|
23
|
+
* [7, 8]
|
|
24
|
+
* ]);
|
|
25
|
+
*
|
|
26
|
+
* const sum = a.add(b);
|
|
27
|
+
* console.log(sum?.data); // [
|
|
28
|
+
* // [6, 8],
|
|
29
|
+
* // [10, 12]
|
|
30
|
+
* // ];
|
|
31
|
+
*
|
|
32
|
+
* const diff = b.subtract(a);
|
|
33
|
+
* console.log(diff?.data); // [
|
|
34
|
+
* // [4, 4],
|
|
35
|
+
* // [4, 4]
|
|
36
|
+
* // ];
|
|
37
|
+
* @example
|
|
38
|
+
* // Matrix multiplication for transformations
|
|
39
|
+
* // 2x3 matrix * 3x2 matrix = 2x2 matrix
|
|
40
|
+
* const a = new Matrix([
|
|
41
|
+
* [1, 2, 3],
|
|
42
|
+
* [4, 5, 6]
|
|
43
|
+
* ]);
|
|
44
|
+
* const b = new Matrix([
|
|
45
|
+
* [7, 8],
|
|
46
|
+
* [9, 10],
|
|
47
|
+
* [11, 12]
|
|
48
|
+
* ]);
|
|
49
|
+
*
|
|
50
|
+
* const product = a.multiply(b);
|
|
51
|
+
* console.log(product?.rows); // 2;
|
|
52
|
+
* console.log(product?.cols); // 2;
|
|
53
|
+
* // Row 0: 1*7+2*9+3*11=58, 1*8+2*10+3*12=64
|
|
54
|
+
* // Row 1: 4*7+5*9+6*11=139, 4*8+5*10+6*12=154
|
|
55
|
+
* console.log(product?.data); // [
|
|
56
|
+
* // [58, 64],
|
|
57
|
+
* // [139, 154]
|
|
58
|
+
* // ];
|
|
59
|
+
* @example
|
|
60
|
+
* // Matrix transpose (square matrix)
|
|
61
|
+
* const m = new Matrix([
|
|
62
|
+
* [1, 2, 3],
|
|
63
|
+
* [4, 5, 6],
|
|
64
|
+
* [7, 8, 9]
|
|
65
|
+
* ]);
|
|
66
|
+
*
|
|
67
|
+
* const transposed = m.transpose();
|
|
68
|
+
* console.log(transposed.rows); // 3;
|
|
69
|
+
* console.log(transposed.cols); // 3;
|
|
70
|
+
* console.log(transposed.data); // [
|
|
71
|
+
* // [1, 4, 7],
|
|
72
|
+
* // [2, 5, 8],
|
|
73
|
+
* // [3, 6, 9]
|
|
74
|
+
* // ];
|
|
75
|
+
*
|
|
76
|
+
* // Transpose of transpose = original
|
|
77
|
+
* console.log(transposed.transpose().data); // m.data;
|
|
78
|
+
* @example
|
|
79
|
+
* // Get and set individual cells
|
|
80
|
+
* const m = new Matrix([
|
|
81
|
+
* [0, 0, 0],
|
|
82
|
+
* [0, 0, 0]
|
|
83
|
+
* ]);
|
|
84
|
+
*
|
|
85
|
+
* m.set(0, 1, 42);
|
|
86
|
+
* m.set(1, 2, 99);
|
|
87
|
+
*
|
|
88
|
+
* console.log(m.get(0, 1)); // 42;
|
|
89
|
+
* console.log(m.get(1, 2)); // 99;
|
|
90
|
+
* console.log(m.get(0, 0)); // 0;
|
|
91
|
+
*
|
|
92
|
+
* // Out of bounds returns undefined
|
|
93
|
+
* console.log(m.get(5, 5)); // undefined;
|
|
94
|
+
*/
|
|
12
95
|
export declare class Matrix {
|
|
13
96
|
/**
|
|
14
97
|
* The constructor function initializes a matrix object with the provided data and options, or with
|
|
@@ -59,6 +142,54 @@ export declare class Matrix {
|
|
|
59
142
|
* retrieve from the data array.
|
|
60
143
|
* @returns The `get` function returns a number if the provided row and column indices are valid.
|
|
61
144
|
* Otherwise, it returns `undefined`.
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
* @example
|
|
178
|
+
* // Get and set individual cells
|
|
179
|
+
* const m = new Matrix([
|
|
180
|
+
* [0, 0, 0],
|
|
181
|
+
* [0, 0, 0]
|
|
182
|
+
* ]);
|
|
183
|
+
*
|
|
184
|
+
* m.set(0, 1, 42);
|
|
185
|
+
* m.set(1, 2, 99);
|
|
186
|
+
*
|
|
187
|
+
* console.log(m.get(0, 1)); // 42;
|
|
188
|
+
* console.log(m.get(1, 2)); // 99;
|
|
189
|
+
* console.log(m.get(0, 0)); // 0;
|
|
190
|
+
*
|
|
191
|
+
* // Out of bounds returns undefined
|
|
192
|
+
* console.log(m.get(5, 5)); // undefined;
|
|
62
193
|
*/
|
|
63
194
|
get(row: number, col: number): number | undefined;
|
|
64
195
|
/**
|
|
@@ -72,6 +203,45 @@ export declare class Matrix {
|
|
|
72
203
|
* @returns a boolean value. It returns true if the index (row, col) is valid and the value is
|
|
73
204
|
* successfully set in the data array. It returns false if the index is invalid and the value is not
|
|
74
205
|
* set.
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
* @example
|
|
239
|
+
* // Modify individual cells
|
|
240
|
+
* const m = Matrix.zeros(2, 2);
|
|
241
|
+
* console.log(m.set(0, 0, 5)); // true;
|
|
242
|
+
* console.log(m.set(1, 1, 10)); // true;
|
|
243
|
+
* console.log(m.get(0, 0)); // 5;
|
|
244
|
+
* console.log(m.get(1, 1)); // 10;
|
|
75
245
|
*/
|
|
76
246
|
set(row: number, col: number, value: number): boolean;
|
|
77
247
|
/**
|
|
@@ -86,6 +256,60 @@ export declare class Matrix {
|
|
|
86
256
|
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
|
|
87
257
|
* @returns The `add` method returns a new `Matrix` object that represents the result of adding the
|
|
88
258
|
* current matrix with the provided `matrix` parameter.
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
* @example
|
|
292
|
+
* // Basic matrix arithmetic
|
|
293
|
+
* const a = new Matrix([
|
|
294
|
+
* [1, 2],
|
|
295
|
+
* [3, 4]
|
|
296
|
+
* ]);
|
|
297
|
+
* const b = new Matrix([
|
|
298
|
+
* [5, 6],
|
|
299
|
+
* [7, 8]
|
|
300
|
+
* ]);
|
|
301
|
+
*
|
|
302
|
+
* const sum = a.add(b);
|
|
303
|
+
* console.log(sum?.data); // [
|
|
304
|
+
* // [6, 8],
|
|
305
|
+
* // [10, 12]
|
|
306
|
+
* // ];
|
|
307
|
+
*
|
|
308
|
+
* const diff = b.subtract(a);
|
|
309
|
+
* console.log(diff?.data); // [
|
|
310
|
+
* // [4, 4],
|
|
311
|
+
* // [4, 4]
|
|
312
|
+
* // ];
|
|
89
313
|
*/
|
|
90
314
|
add(matrix: Matrix): Matrix | undefined;
|
|
91
315
|
/**
|
|
@@ -94,6 +318,44 @@ export declare class Matrix {
|
|
|
94
318
|
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class. It
|
|
95
319
|
* represents the matrix that you want to subtract from the current matrix.
|
|
96
320
|
* @returns a new Matrix object with the result of the subtraction operation.
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
* @example
|
|
354
|
+
* // Element-wise subtraction
|
|
355
|
+
* const a = Matrix.from([[5, 6], [7, 8]]);
|
|
356
|
+
* const b = Matrix.from([[1, 2], [3, 4]]);
|
|
357
|
+
* const result = a.subtract(b);
|
|
358
|
+
* console.log(result?.toArray()); // [[4, 4], [4, 4]];
|
|
97
359
|
*/
|
|
98
360
|
subtract(matrix: Matrix): Matrix | undefined;
|
|
99
361
|
/**
|
|
@@ -101,23 +363,209 @@ export declare class Matrix {
|
|
|
101
363
|
* as a new matrix.
|
|
102
364
|
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
|
|
103
365
|
* @returns a new Matrix object.
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
* @example
|
|
399
|
+
* // Matrix multiplication for transformations
|
|
400
|
+
* // 2x3 matrix * 3x2 matrix = 2x2 matrix
|
|
401
|
+
* const a = new Matrix([
|
|
402
|
+
* [1, 2, 3],
|
|
403
|
+
* [4, 5, 6]
|
|
404
|
+
* ]);
|
|
405
|
+
* const b = new Matrix([
|
|
406
|
+
* [7, 8],
|
|
407
|
+
* [9, 10],
|
|
408
|
+
* [11, 12]
|
|
409
|
+
* ]);
|
|
410
|
+
*
|
|
411
|
+
* const product = a.multiply(b);
|
|
412
|
+
* console.log(product?.rows); // 2;
|
|
413
|
+
* console.log(product?.cols); // 2;
|
|
414
|
+
* // Row 0: 1*7+2*9+3*11=58, 1*8+2*10+3*12=64
|
|
415
|
+
* // Row 1: 4*7+5*9+6*11=139, 4*8+5*10+6*12=154
|
|
416
|
+
* console.log(product?.data); // [
|
|
417
|
+
* // [58, 64],
|
|
418
|
+
* // [139, 154]
|
|
419
|
+
* // ];
|
|
104
420
|
*/
|
|
105
421
|
multiply(matrix: Matrix): Matrix | undefined;
|
|
106
422
|
/**
|
|
107
423
|
* The transpose function takes a matrix and returns a new matrix that is the transpose of the
|
|
108
424
|
* original matrix.
|
|
109
425
|
* @returns The transpose() function returns a new Matrix object with the transposed data.
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
* @example
|
|
459
|
+
* // Matrix transpose (square matrix)
|
|
460
|
+
* const m = new Matrix([
|
|
461
|
+
* [1, 2, 3],
|
|
462
|
+
* [4, 5, 6],
|
|
463
|
+
* [7, 8, 9]
|
|
464
|
+
* ]);
|
|
465
|
+
*
|
|
466
|
+
* const transposed = m.transpose();
|
|
467
|
+
* console.log(transposed.rows); // 3;
|
|
468
|
+
* console.log(transposed.cols); // 3;
|
|
469
|
+
* console.log(transposed.data); // [
|
|
470
|
+
* // [1, 4, 7],
|
|
471
|
+
* // [2, 5, 8],
|
|
472
|
+
* // [3, 6, 9]
|
|
473
|
+
* // ];
|
|
474
|
+
*
|
|
475
|
+
* // Transpose of transpose = original
|
|
476
|
+
* console.log(transposed.transpose().data); // m.data;
|
|
110
477
|
*/
|
|
111
478
|
transpose(): Matrix;
|
|
112
479
|
/**
|
|
113
480
|
* The `inverse` function calculates the inverse of a square matrix using Gaussian elimination.
|
|
114
481
|
* @returns a Matrix object, which represents the inverse of the original matrix.
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
|
|
485
|
+
|
|
486
|
+
|
|
487
|
+
|
|
488
|
+
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
|
|
511
|
+
|
|
512
|
+
|
|
513
|
+
|
|
514
|
+
* @example
|
|
515
|
+
* // Compute the inverse of a 2x2 matrix
|
|
516
|
+
* const m = Matrix.from([[4, 7], [2, 6]]);
|
|
517
|
+
* const inv = m.inverse();
|
|
518
|
+
* console.log(inv); // defined;
|
|
519
|
+
* // A * A^-1 should ≈ Identity
|
|
520
|
+
* const product = m.multiply(inv!);
|
|
521
|
+
* console.log(product?.get(0, 0)); // toBeCloseTo;
|
|
522
|
+
* console.log(product?.get(0, 1)); // toBeCloseTo;
|
|
523
|
+
* console.log(product?.get(1, 0)); // toBeCloseTo;
|
|
524
|
+
* console.log(product?.get(1, 1)); // toBeCloseTo;
|
|
115
525
|
*/
|
|
116
526
|
inverse(): Matrix | undefined;
|
|
117
527
|
/**
|
|
118
528
|
* The dot function calculates the dot product of two matrices and returns a new matrix.
|
|
119
529
|
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
|
|
120
530
|
* @returns a new Matrix object.
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
|
|
537
|
+
|
|
538
|
+
|
|
539
|
+
|
|
540
|
+
|
|
541
|
+
|
|
542
|
+
|
|
543
|
+
|
|
544
|
+
|
|
545
|
+
|
|
546
|
+
|
|
547
|
+
|
|
548
|
+
|
|
549
|
+
|
|
550
|
+
|
|
551
|
+
|
|
552
|
+
|
|
553
|
+
|
|
554
|
+
|
|
555
|
+
|
|
556
|
+
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
|
|
560
|
+
|
|
561
|
+
|
|
562
|
+
|
|
563
|
+
* @example
|
|
564
|
+
* // Dot product of two matrices
|
|
565
|
+
* const a = Matrix.from([[1, 2], [3, 4]]);
|
|
566
|
+
* const b = Matrix.from([[5, 6], [7, 8]]);
|
|
567
|
+
* const result = a.dot(b);
|
|
568
|
+
* console.log(result?.toArray()); // [[19, 22], [43, 50]];
|
|
121
569
|
*/
|
|
122
570
|
dot(matrix: Matrix): Matrix | undefined;
|
|
123
571
|
/**
|
|
@@ -136,6 +584,57 @@ export declare class Matrix {
|
|
|
136
584
|
* and properties as the current instance.
|
|
137
585
|
*/
|
|
138
586
|
clone(): Matrix;
|
|
587
|
+
/**
|
|
588
|
+
* Returns [rows, cols] dimensions tuple.
|
|
589
|
+
*/
|
|
590
|
+
get size(): [number, number];
|
|
591
|
+
isEmpty(): boolean;
|
|
592
|
+
/**
|
|
593
|
+
* Returns a deep copy of the data as a plain 2D array.
|
|
594
|
+
*/
|
|
595
|
+
toArray(): number[][];
|
|
596
|
+
/**
|
|
597
|
+
* Returns a flat row-major array.
|
|
598
|
+
*/
|
|
599
|
+
flatten(): number[];
|
|
600
|
+
/**
|
|
601
|
+
* Iterates over rows.
|
|
602
|
+
*/
|
|
603
|
+
[Symbol.iterator](): IterableIterator<number[]>;
|
|
604
|
+
/**
|
|
605
|
+
* Visits each element with its row and column index.
|
|
606
|
+
*/
|
|
607
|
+
forEach(callback: (value: number, row: number, col: number) => void): void;
|
|
608
|
+
/**
|
|
609
|
+
* Maps each element (number → number) and returns a new Matrix.
|
|
610
|
+
*/
|
|
611
|
+
map(callback: (value: number, row: number, col: number) => number): Matrix;
|
|
612
|
+
print(): void;
|
|
613
|
+
/**
|
|
614
|
+
* Creates a rows×cols zero matrix.
|
|
615
|
+
* @example
|
|
616
|
+
* ```ts
|
|
617
|
+
* const z = Matrix.zeros(2, 3); // [[0,0,0],[0,0,0]]
|
|
618
|
+
* ```
|
|
619
|
+
*/
|
|
620
|
+
static zeros(rows: number, cols: number): Matrix;
|
|
621
|
+
/**
|
|
622
|
+
* Creates an n×n identity matrix.
|
|
623
|
+
* @example
|
|
624
|
+
* ```ts
|
|
625
|
+
* const I = Matrix.identity(3); // [[1,0,0],[0,1,0],[0,0,1]]
|
|
626
|
+
* ```
|
|
627
|
+
*/
|
|
628
|
+
static identity(n: number): Matrix;
|
|
629
|
+
/**
|
|
630
|
+
* Creates a Matrix from a plain 2D array (deep copy).
|
|
631
|
+
* @example
|
|
632
|
+
* ```ts
|
|
633
|
+
* const m = Matrix.from([[1, 2], [3, 4]]);
|
|
634
|
+
* m.get(0, 1); // 2
|
|
635
|
+
* ```
|
|
636
|
+
*/
|
|
637
|
+
static from(data: number[][]): Matrix;
|
|
139
638
|
protected _addFn(a: number | undefined, b: number): number | undefined;
|
|
140
639
|
protected _subtractFn(a: number, b: number): number;
|
|
141
640
|
protected _multiplyFn(a: number, b: number): number;
|
|
@@ -14,6 +14,63 @@ import { PriorityQueue } from './priority-queue';
|
|
|
14
14
|
* @template E Element type stored in the queue.
|
|
15
15
|
* @template R Extra record/metadata associated with each element.
|
|
16
16
|
* @example
|
|
17
|
+
* // Job scheduling by priority
|
|
18
|
+
* const jobs = new MaxPriorityQueue<number>();
|
|
19
|
+
*
|
|
20
|
+
* jobs.add(3); // low priority
|
|
21
|
+
* jobs.add(7); // high priority
|
|
22
|
+
* jobs.add(5); // medium priority
|
|
23
|
+
* jobs.add(10); // critical
|
|
24
|
+
*
|
|
25
|
+
* // Highest priority job first
|
|
26
|
+
* console.log(jobs.poll()); // 10;
|
|
27
|
+
* console.log(jobs.poll()); // 7;
|
|
28
|
+
* console.log(jobs.poll()); // 5;
|
|
29
|
+
* console.log(jobs.poll()); // 3;
|
|
30
|
+
* @example
|
|
31
|
+
* // Auction system with highest bid tracking
|
|
32
|
+
* interface Bid {
|
|
33
|
+
* bidder: string;
|
|
34
|
+
* amount: number;
|
|
35
|
+
* }
|
|
36
|
+
*
|
|
37
|
+
* const auction = new MaxPriorityQueue<Bid>([], {
|
|
38
|
+
* comparator: (a, b) => b.amount - a.amount
|
|
39
|
+
* });
|
|
40
|
+
*
|
|
41
|
+
* auction.add({ bidder: 'Alice', amount: 100 });
|
|
42
|
+
* auction.add({ bidder: 'Bob', amount: 250 });
|
|
43
|
+
* auction.add({ bidder: 'Charlie', amount: 175 });
|
|
44
|
+
*
|
|
45
|
+
* // Current highest bid
|
|
46
|
+
* console.log(auction.peek()?.bidder); // 'Bob';
|
|
47
|
+
* console.log(auction.peek()?.amount); // 250;
|
|
48
|
+
*
|
|
49
|
+
* // Process winning bid
|
|
50
|
+
* const winner = auction.poll()!;
|
|
51
|
+
* console.log(winner.bidder); // 'Bob';
|
|
52
|
+
* console.log(auction.peek()?.bidder); // 'Charlie';
|
|
53
|
+
* @example
|
|
54
|
+
* // CPU process scheduling
|
|
55
|
+
* const cpuQueue = new MaxPriorityQueue<[number, string]>([], {
|
|
56
|
+
* comparator: (a, b) => b[0] - a[0]
|
|
57
|
+
* });
|
|
58
|
+
*
|
|
59
|
+
* cpuQueue.add([5, 'System process']);
|
|
60
|
+
* cpuQueue.add([1, 'Background task']);
|
|
61
|
+
* cpuQueue.add([8, 'User interaction']);
|
|
62
|
+
* cpuQueue.add([3, 'Network sync']);
|
|
63
|
+
*
|
|
64
|
+
* const order = [];
|
|
65
|
+
* while (cpuQueue.size > 0) {
|
|
66
|
+
* order.push(cpuQueue.poll()![1]);
|
|
67
|
+
* }
|
|
68
|
+
* console.log(order); // [
|
|
69
|
+
* // 'User interaction',
|
|
70
|
+
* // 'System process',
|
|
71
|
+
* // 'Network sync',
|
|
72
|
+
* // 'Background task'
|
|
73
|
+
* // ];
|
|
17
74
|
*/
|
|
18
75
|
export declare class MaxPriorityQueue<E = any, R = any> extends PriorityQueue<E, R> {
|
|
19
76
|
/**
|
|
@@ -14,6 +14,66 @@ import { PriorityQueue } from './priority-queue';
|
|
|
14
14
|
* @template E Element type stored in the queue.
|
|
15
15
|
* @template R Extra record/metadata associated with each element.
|
|
16
16
|
* @example
|
|
17
|
+
* // Shortest job first scheduling
|
|
18
|
+
* const jobs = new MinPriorityQueue<number>();
|
|
19
|
+
*
|
|
20
|
+
* jobs.add(8); // 8 seconds
|
|
21
|
+
* jobs.add(2); // 2 seconds
|
|
22
|
+
* jobs.add(5); // 5 seconds
|
|
23
|
+
* jobs.add(1); // 1 second
|
|
24
|
+
*
|
|
25
|
+
* // Shortest job first
|
|
26
|
+
* console.log(jobs.poll()); // 1;
|
|
27
|
+
* console.log(jobs.poll()); // 2;
|
|
28
|
+
* console.log(jobs.poll()); // 5;
|
|
29
|
+
* console.log(jobs.poll()); // 8;
|
|
30
|
+
* @example
|
|
31
|
+
* // Event-driven simulation with timestamps
|
|
32
|
+
* interface Event {
|
|
33
|
+
* time: number;
|
|
34
|
+
* action: string;
|
|
35
|
+
* }
|
|
36
|
+
*
|
|
37
|
+
* const timeline = new MinPriorityQueue<Event>([], {
|
|
38
|
+
* comparator: (a, b) => a.time - b.time
|
|
39
|
+
* });
|
|
40
|
+
*
|
|
41
|
+
* timeline.add({ time: 300, action: 'Timeout' });
|
|
42
|
+
* timeline.add({ time: 100, action: 'Request received' });
|
|
43
|
+
* timeline.add({ time: 200, action: 'Processing done' });
|
|
44
|
+
* timeline.add({ time: 150, action: 'Cache hit' });
|
|
45
|
+
*
|
|
46
|
+
* const order = [];
|
|
47
|
+
* while (timeline.size > 0) {
|
|
48
|
+
* order.push(timeline.poll()!.action);
|
|
49
|
+
* }
|
|
50
|
+
* console.log(order); // [
|
|
51
|
+
* // 'Request received',
|
|
52
|
+
* // 'Cache hit',
|
|
53
|
+
* // 'Processing done',
|
|
54
|
+
* // 'Timeout'
|
|
55
|
+
* // ];
|
|
56
|
+
* @example
|
|
57
|
+
* // Huffman coding frequency selection
|
|
58
|
+
* // Character frequencies for Huffman tree building
|
|
59
|
+
* const freq = new MinPriorityQueue<[number, string]>([], {
|
|
60
|
+
* comparator: (a, b) => a[0] - b[0]
|
|
61
|
+
* });
|
|
62
|
+
*
|
|
63
|
+
* freq.add([5, 'a']);
|
|
64
|
+
* freq.add([9, 'b']);
|
|
65
|
+
* freq.add([12, 'c']);
|
|
66
|
+
* freq.add([2, 'd']);
|
|
67
|
+
*
|
|
68
|
+
* // Always pick two lowest frequencies
|
|
69
|
+
* const first = freq.poll()!;
|
|
70
|
+
* const second = freq.poll()!;
|
|
71
|
+
* console.log(first[1]); // 'd'; // freq 2
|
|
72
|
+
* console.log(second[1]); // 'a'; // freq 5
|
|
73
|
+
*
|
|
74
|
+
* // Combined node goes back
|
|
75
|
+
* freq.add([first[0] + second[0], first[1] + second[1]]);
|
|
76
|
+
* console.log(freq.peek()![0]); // 7;
|
|
17
77
|
*/
|
|
18
78
|
export declare class MinPriorityQueue<E = any, R = any> extends PriorityQueue<E, R> {
|
|
19
79
|
/**
|