binary-tree-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 +0 -84
- package/dist/cjs/index.cjs +1476 -404
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +1473 -401
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +1476 -404
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +1473 -401
- 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/binary-tree-typed.js +1469 -397
- package/dist/umd/binary-tree-typed.js.map +1 -1
- package/dist/umd/binary-tree-typed.min.js +5 -5
- package/dist/umd/binary-tree-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
|
@@ -11,6 +11,89 @@ import { ERR } from '../../common';
|
|
|
11
11
|
/**
|
|
12
12
|
*
|
|
13
13
|
*/
|
|
14
|
+
/**
|
|
15
|
+
* Matrix — a numeric matrix with standard linear algebra operations.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* // Basic matrix arithmetic
|
|
19
|
+
* const a = new Matrix([
|
|
20
|
+
* [1, 2],
|
|
21
|
+
* [3, 4]
|
|
22
|
+
* ]);
|
|
23
|
+
* const b = new Matrix([
|
|
24
|
+
* [5, 6],
|
|
25
|
+
* [7, 8]
|
|
26
|
+
* ]);
|
|
27
|
+
*
|
|
28
|
+
* const sum = a.add(b);
|
|
29
|
+
* console.log(sum?.data); // [
|
|
30
|
+
* // [6, 8],
|
|
31
|
+
* // [10, 12]
|
|
32
|
+
* // ];
|
|
33
|
+
*
|
|
34
|
+
* const diff = b.subtract(a);
|
|
35
|
+
* console.log(diff?.data); // [
|
|
36
|
+
* // [4, 4],
|
|
37
|
+
* // [4, 4]
|
|
38
|
+
* // ];
|
|
39
|
+
* @example
|
|
40
|
+
* // Matrix multiplication for transformations
|
|
41
|
+
* // 2x3 matrix * 3x2 matrix = 2x2 matrix
|
|
42
|
+
* const a = new Matrix([
|
|
43
|
+
* [1, 2, 3],
|
|
44
|
+
* [4, 5, 6]
|
|
45
|
+
* ]);
|
|
46
|
+
* const b = new Matrix([
|
|
47
|
+
* [7, 8],
|
|
48
|
+
* [9, 10],
|
|
49
|
+
* [11, 12]
|
|
50
|
+
* ]);
|
|
51
|
+
*
|
|
52
|
+
* const product = a.multiply(b);
|
|
53
|
+
* console.log(product?.rows); // 2;
|
|
54
|
+
* console.log(product?.cols); // 2;
|
|
55
|
+
* // Row 0: 1*7+2*9+3*11=58, 1*8+2*10+3*12=64
|
|
56
|
+
* // Row 1: 4*7+5*9+6*11=139, 4*8+5*10+6*12=154
|
|
57
|
+
* console.log(product?.data); // [
|
|
58
|
+
* // [58, 64],
|
|
59
|
+
* // [139, 154]
|
|
60
|
+
* // ];
|
|
61
|
+
* @example
|
|
62
|
+
* // Matrix transpose (square matrix)
|
|
63
|
+
* const m = new Matrix([
|
|
64
|
+
* [1, 2, 3],
|
|
65
|
+
* [4, 5, 6],
|
|
66
|
+
* [7, 8, 9]
|
|
67
|
+
* ]);
|
|
68
|
+
*
|
|
69
|
+
* const transposed = m.transpose();
|
|
70
|
+
* console.log(transposed.rows); // 3;
|
|
71
|
+
* console.log(transposed.cols); // 3;
|
|
72
|
+
* console.log(transposed.data); // [
|
|
73
|
+
* // [1, 4, 7],
|
|
74
|
+
* // [2, 5, 8],
|
|
75
|
+
* // [3, 6, 9]
|
|
76
|
+
* // ];
|
|
77
|
+
*
|
|
78
|
+
* // Transpose of transpose = original
|
|
79
|
+
* console.log(transposed.transpose().data); // m.data;
|
|
80
|
+
* @example
|
|
81
|
+
* // Get and set individual cells
|
|
82
|
+
* const m = new Matrix([
|
|
83
|
+
* [0, 0, 0],
|
|
84
|
+
* [0, 0, 0]
|
|
85
|
+
* ]);
|
|
86
|
+
*
|
|
87
|
+
* m.set(0, 1, 42);
|
|
88
|
+
* m.set(1, 2, 99);
|
|
89
|
+
*
|
|
90
|
+
* console.log(m.get(0, 1)); // 42;
|
|
91
|
+
* console.log(m.get(1, 2)); // 99;
|
|
92
|
+
* console.log(m.get(0, 0)); // 0;
|
|
93
|
+
*
|
|
94
|
+
* // Out of bounds returns undefined
|
|
95
|
+
* console.log(m.get(5, 5)); // undefined;
|
|
96
|
+
*/
|
|
14
97
|
export class Matrix {
|
|
15
98
|
/**
|
|
16
99
|
* The constructor function initializes a matrix object with the provided data and options, or with
|
|
@@ -106,6 +189,54 @@ export class Matrix {
|
|
|
106
189
|
* retrieve from the data array.
|
|
107
190
|
* @returns The `get` function returns a number if the provided row and column indices are valid.
|
|
108
191
|
* Otherwise, it returns `undefined`.
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
* @example
|
|
225
|
+
* // Get and set individual cells
|
|
226
|
+
* const m = new Matrix([
|
|
227
|
+
* [0, 0, 0],
|
|
228
|
+
* [0, 0, 0]
|
|
229
|
+
* ]);
|
|
230
|
+
*
|
|
231
|
+
* m.set(0, 1, 42);
|
|
232
|
+
* m.set(1, 2, 99);
|
|
233
|
+
*
|
|
234
|
+
* console.log(m.get(0, 1)); // 42;
|
|
235
|
+
* console.log(m.get(1, 2)); // 99;
|
|
236
|
+
* console.log(m.get(0, 0)); // 0;
|
|
237
|
+
*
|
|
238
|
+
* // Out of bounds returns undefined
|
|
239
|
+
* console.log(m.get(5, 5)); // undefined;
|
|
109
240
|
*/
|
|
110
241
|
get(row: number, col: number): number | undefined {
|
|
111
242
|
if (this.isValidIndex(row, col)) {
|
|
@@ -124,6 +255,45 @@ export class Matrix {
|
|
|
124
255
|
* @returns a boolean value. It returns true if the index (row, col) is valid and the value is
|
|
125
256
|
* successfully set in the data array. It returns false if the index is invalid and the value is not
|
|
126
257
|
* set.
|
|
258
|
+
|
|
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
|
+
* @example
|
|
291
|
+
* // Modify individual cells
|
|
292
|
+
* const m = Matrix.zeros(2, 2);
|
|
293
|
+
* console.log(m.set(0, 0, 5)); // true;
|
|
294
|
+
* console.log(m.set(1, 1, 10)); // true;
|
|
295
|
+
* console.log(m.get(0, 0)); // 5;
|
|
296
|
+
* console.log(m.get(1, 1)); // 10;
|
|
127
297
|
*/
|
|
128
298
|
set(row: number, col: number, value: number): boolean {
|
|
129
299
|
if (this.isValidIndex(row, col)) {
|
|
@@ -148,6 +318,60 @@ export class Matrix {
|
|
|
148
318
|
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
|
|
149
319
|
* @returns The `add` method returns a new `Matrix` object that represents the result of adding the
|
|
150
320
|
* current matrix with the provided `matrix` parameter.
|
|
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
|
+
* // Basic matrix arithmetic
|
|
355
|
+
* const a = new Matrix([
|
|
356
|
+
* [1, 2],
|
|
357
|
+
* [3, 4]
|
|
358
|
+
* ]);
|
|
359
|
+
* const b = new Matrix([
|
|
360
|
+
* [5, 6],
|
|
361
|
+
* [7, 8]
|
|
362
|
+
* ]);
|
|
363
|
+
*
|
|
364
|
+
* const sum = a.add(b);
|
|
365
|
+
* console.log(sum?.data); // [
|
|
366
|
+
* // [6, 8],
|
|
367
|
+
* // [10, 12]
|
|
368
|
+
* // ];
|
|
369
|
+
*
|
|
370
|
+
* const diff = b.subtract(a);
|
|
371
|
+
* console.log(diff?.data); // [
|
|
372
|
+
* // [4, 4],
|
|
373
|
+
* // [4, 4]
|
|
374
|
+
* // ];
|
|
151
375
|
*/
|
|
152
376
|
add(matrix: Matrix): Matrix | undefined {
|
|
153
377
|
if (!this.isMatchForCalculate(matrix)) {
|
|
@@ -161,10 +385,7 @@ export class Matrix {
|
|
|
161
385
|
const a = this.get(i, j),
|
|
162
386
|
b = matrix.get(i, j);
|
|
163
387
|
if (a !== undefined && b !== undefined) {
|
|
164
|
-
|
|
165
|
-
if (added) {
|
|
166
|
-
resultData[i][j] = added;
|
|
167
|
-
}
|
|
388
|
+
resultData[i][j] = this._addFn(a, b) ?? 0;
|
|
168
389
|
}
|
|
169
390
|
}
|
|
170
391
|
}
|
|
@@ -184,6 +405,44 @@ export class Matrix {
|
|
|
184
405
|
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class. It
|
|
185
406
|
* represents the matrix that you want to subtract from the current matrix.
|
|
186
407
|
* @returns a new Matrix object with the result of the subtraction operation.
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
* @example
|
|
441
|
+
* // Element-wise subtraction
|
|
442
|
+
* const a = Matrix.from([[5, 6], [7, 8]]);
|
|
443
|
+
* const b = Matrix.from([[1, 2], [3, 4]]);
|
|
444
|
+
* const result = a.subtract(b);
|
|
445
|
+
* console.log(result?.toArray()); // [[4, 4], [4, 4]];
|
|
187
446
|
*/
|
|
188
447
|
subtract(matrix: Matrix): Matrix | undefined {
|
|
189
448
|
if (!this.isMatchForCalculate(matrix)) {
|
|
@@ -197,10 +456,7 @@ export class Matrix {
|
|
|
197
456
|
const a = this.get(i, j),
|
|
198
457
|
b = matrix.get(i, j);
|
|
199
458
|
if (a !== undefined && b !== undefined) {
|
|
200
|
-
|
|
201
|
-
if (subtracted) {
|
|
202
|
-
resultData[i][j] = subtracted;
|
|
203
|
-
}
|
|
459
|
+
resultData[i][j] = this._subtractFn(a, b) ?? 0;
|
|
204
460
|
}
|
|
205
461
|
}
|
|
206
462
|
}
|
|
@@ -219,6 +475,60 @@ export class Matrix {
|
|
|
219
475
|
* as a new matrix.
|
|
220
476
|
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
|
|
221
477
|
* @returns a new Matrix object.
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
|
|
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
|
+
* @example
|
|
511
|
+
* // Matrix multiplication for transformations
|
|
512
|
+
* // 2x3 matrix * 3x2 matrix = 2x2 matrix
|
|
513
|
+
* const a = new Matrix([
|
|
514
|
+
* [1, 2, 3],
|
|
515
|
+
* [4, 5, 6]
|
|
516
|
+
* ]);
|
|
517
|
+
* const b = new Matrix([
|
|
518
|
+
* [7, 8],
|
|
519
|
+
* [9, 10],
|
|
520
|
+
* [11, 12]
|
|
521
|
+
* ]);
|
|
522
|
+
*
|
|
523
|
+
* const product = a.multiply(b);
|
|
524
|
+
* console.log(product?.rows); // 2;
|
|
525
|
+
* console.log(product?.cols); // 2;
|
|
526
|
+
* // Row 0: 1*7+2*9+3*11=58, 1*8+2*10+3*12=64
|
|
527
|
+
* // Row 1: 4*7+5*9+6*11=139, 4*8+5*10+6*12=154
|
|
528
|
+
* console.log(product?.data); // [
|
|
529
|
+
* // [58, 64],
|
|
530
|
+
* // [139, 154]
|
|
531
|
+
* // ];
|
|
222
532
|
*/
|
|
223
533
|
multiply(matrix: Matrix): Matrix | undefined {
|
|
224
534
|
if (this.cols !== matrix.rows) {
|
|
@@ -257,9 +567,60 @@ export class Matrix {
|
|
|
257
567
|
* The transpose function takes a matrix and returns a new matrix that is the transpose of the
|
|
258
568
|
* original matrix.
|
|
259
569
|
* @returns The transpose() function returns a new Matrix object with the transposed data.
|
|
570
|
+
|
|
571
|
+
|
|
572
|
+
|
|
573
|
+
|
|
574
|
+
|
|
575
|
+
|
|
576
|
+
|
|
577
|
+
|
|
578
|
+
|
|
579
|
+
|
|
580
|
+
|
|
581
|
+
|
|
582
|
+
|
|
583
|
+
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
|
|
587
|
+
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
|
|
591
|
+
|
|
592
|
+
|
|
593
|
+
|
|
594
|
+
|
|
595
|
+
|
|
596
|
+
|
|
597
|
+
|
|
598
|
+
|
|
599
|
+
|
|
600
|
+
|
|
601
|
+
|
|
602
|
+
* @example
|
|
603
|
+
* // Matrix transpose (square matrix)
|
|
604
|
+
* const m = new Matrix([
|
|
605
|
+
* [1, 2, 3],
|
|
606
|
+
* [4, 5, 6],
|
|
607
|
+
* [7, 8, 9]
|
|
608
|
+
* ]);
|
|
609
|
+
*
|
|
610
|
+
* const transposed = m.transpose();
|
|
611
|
+
* console.log(transposed.rows); // 3;
|
|
612
|
+
* console.log(transposed.cols); // 3;
|
|
613
|
+
* console.log(transposed.data); // [
|
|
614
|
+
* // [1, 4, 7],
|
|
615
|
+
* // [2, 5, 8],
|
|
616
|
+
* // [3, 6, 9]
|
|
617
|
+
* // ];
|
|
618
|
+
*
|
|
619
|
+
* // Transpose of transpose = original
|
|
620
|
+
* console.log(transposed.transpose().data); // m.data;
|
|
260
621
|
*/
|
|
261
622
|
transpose(): Matrix {
|
|
262
|
-
if (this.data.some(row => row.length !== this.
|
|
623
|
+
if (this.data.some(row => row.length !== this.cols)) {
|
|
263
624
|
throw new Error(ERR.matrixNotRectangular());
|
|
264
625
|
}
|
|
265
626
|
|
|
@@ -285,6 +646,49 @@ export class Matrix {
|
|
|
285
646
|
/**
|
|
286
647
|
* The `inverse` function calculates the inverse of a square matrix using Gaussian elimination.
|
|
287
648
|
* @returns a Matrix object, which represents the inverse of the original matrix.
|
|
649
|
+
|
|
650
|
+
|
|
651
|
+
|
|
652
|
+
|
|
653
|
+
|
|
654
|
+
|
|
655
|
+
|
|
656
|
+
|
|
657
|
+
|
|
658
|
+
|
|
659
|
+
|
|
660
|
+
|
|
661
|
+
|
|
662
|
+
|
|
663
|
+
|
|
664
|
+
|
|
665
|
+
|
|
666
|
+
|
|
667
|
+
|
|
668
|
+
|
|
669
|
+
|
|
670
|
+
|
|
671
|
+
|
|
672
|
+
|
|
673
|
+
|
|
674
|
+
|
|
675
|
+
|
|
676
|
+
|
|
677
|
+
|
|
678
|
+
|
|
679
|
+
|
|
680
|
+
|
|
681
|
+
* @example
|
|
682
|
+
* // Compute the inverse of a 2x2 matrix
|
|
683
|
+
* const m = Matrix.from([[4, 7], [2, 6]]);
|
|
684
|
+
* const inv = m.inverse();
|
|
685
|
+
* console.log(inv); // defined;
|
|
686
|
+
* // A * A^-1 should ≈ Identity
|
|
687
|
+
* const product = m.multiply(inv!);
|
|
688
|
+
* console.log(product?.get(0, 0)); // toBeCloseTo;
|
|
689
|
+
* console.log(product?.get(0, 1)); // toBeCloseTo;
|
|
690
|
+
* console.log(product?.get(1, 0)); // toBeCloseTo;
|
|
691
|
+
* console.log(product?.get(1, 1)); // toBeCloseTo;
|
|
288
692
|
*/
|
|
289
693
|
inverse(): Matrix | undefined {
|
|
290
694
|
// Check if the matrix is square
|
|
@@ -365,6 +769,44 @@ export class Matrix {
|
|
|
365
769
|
* The dot function calculates the dot product of two matrices and returns a new matrix.
|
|
366
770
|
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
|
|
367
771
|
* @returns a new Matrix object.
|
|
772
|
+
|
|
773
|
+
|
|
774
|
+
|
|
775
|
+
|
|
776
|
+
|
|
777
|
+
|
|
778
|
+
|
|
779
|
+
|
|
780
|
+
|
|
781
|
+
|
|
782
|
+
|
|
783
|
+
|
|
784
|
+
|
|
785
|
+
|
|
786
|
+
|
|
787
|
+
|
|
788
|
+
|
|
789
|
+
|
|
790
|
+
|
|
791
|
+
|
|
792
|
+
|
|
793
|
+
|
|
794
|
+
|
|
795
|
+
|
|
796
|
+
|
|
797
|
+
|
|
798
|
+
|
|
799
|
+
|
|
800
|
+
|
|
801
|
+
|
|
802
|
+
|
|
803
|
+
|
|
804
|
+
* @example
|
|
805
|
+
* // Dot product of two matrices
|
|
806
|
+
* const a = Matrix.from([[1, 2], [3, 4]]);
|
|
807
|
+
* const b = Matrix.from([[5, 6], [7, 8]]);
|
|
808
|
+
* const result = a.dot(b);
|
|
809
|
+
* console.log(result?.toArray()); // [[19, 22], [43, 50]];
|
|
368
810
|
*/
|
|
369
811
|
dot(matrix: Matrix): Matrix | undefined {
|
|
370
812
|
if (this.cols !== matrix.rows) {
|
|
@@ -418,15 +860,145 @@ export class Matrix {
|
|
|
418
860
|
* and properties as the current instance.
|
|
419
861
|
*/
|
|
420
862
|
clone(): Matrix {
|
|
421
|
-
return new Matrix(
|
|
422
|
-
|
|
423
|
-
|
|
863
|
+
return new Matrix(
|
|
864
|
+
this._data.map(row => [...row]),
|
|
865
|
+
{
|
|
866
|
+
rows: this.rows,
|
|
867
|
+
cols: this.cols,
|
|
868
|
+
addFn: this.addFn,
|
|
869
|
+
subtractFn: this.subtractFn,
|
|
870
|
+
multiplyFn: this.multiplyFn
|
|
871
|
+
}
|
|
872
|
+
);
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
// ─── Standard interface ─────────────────────────────────────
|
|
876
|
+
|
|
877
|
+
/**
|
|
878
|
+
* Returns [rows, cols] dimensions tuple.
|
|
879
|
+
*/
|
|
880
|
+
get size(): [number, number] {
|
|
881
|
+
return [this._rows, this._cols];
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
isEmpty(): boolean {
|
|
885
|
+
return this._rows === 0 || this._cols === 0;
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
/**
|
|
889
|
+
* Returns a deep copy of the data as a plain 2D array.
|
|
890
|
+
*/
|
|
891
|
+
toArray(): number[][] {
|
|
892
|
+
return this._data.map(row => [...row]);
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
/**
|
|
896
|
+
* Returns a flat row-major array.
|
|
897
|
+
*/
|
|
898
|
+
flatten(): number[] {
|
|
899
|
+
const result: number[] = [];
|
|
900
|
+
for (const row of this._data) {
|
|
901
|
+
for (const v of row) result.push(v);
|
|
902
|
+
}
|
|
903
|
+
return result;
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
/**
|
|
907
|
+
* Iterates over rows.
|
|
908
|
+
*/
|
|
909
|
+
[Symbol.iterator](): IterableIterator<number[]> {
|
|
910
|
+
const data = this._data;
|
|
911
|
+
let i = 0;
|
|
912
|
+
return {
|
|
913
|
+
[Symbol.iterator]() {
|
|
914
|
+
return this;
|
|
915
|
+
},
|
|
916
|
+
next(): IteratorResult<number[]> {
|
|
917
|
+
if (i < data.length) {
|
|
918
|
+
return { value: [...data[i++]], done: false };
|
|
919
|
+
}
|
|
920
|
+
return { value: undefined, done: true } as IteratorResult<number[]>;
|
|
921
|
+
}
|
|
922
|
+
};
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
/**
|
|
926
|
+
* Visits each element with its row and column index.
|
|
927
|
+
*/
|
|
928
|
+
forEach(callback: (value: number, row: number, col: number) => void): void {
|
|
929
|
+
for (let i = 0; i < this._rows; i++) {
|
|
930
|
+
for (let j = 0; j < this._cols; j++) {
|
|
931
|
+
callback(this._data[i][j], i, j);
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
/**
|
|
937
|
+
* Maps each element (number → number) and returns a new Matrix.
|
|
938
|
+
*/
|
|
939
|
+
map(callback: (value: number, row: number, col: number) => number): Matrix {
|
|
940
|
+
const resultData: number[][] = [];
|
|
941
|
+
for (let i = 0; i < this._rows; i++) {
|
|
942
|
+
resultData[i] = [];
|
|
943
|
+
for (let j = 0; j < this._cols; j++) {
|
|
944
|
+
resultData[i][j] = callback(this._data[i][j], i, j);
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
return new Matrix(resultData, {
|
|
948
|
+
rows: this._rows,
|
|
949
|
+
cols: this._cols,
|
|
424
950
|
addFn: this.addFn,
|
|
425
951
|
subtractFn: this.subtractFn,
|
|
426
952
|
multiplyFn: this.multiplyFn
|
|
427
953
|
});
|
|
428
954
|
}
|
|
429
955
|
|
|
956
|
+
print(): void {
|
|
957
|
+
for (const row of this._data) {
|
|
958
|
+
console.log(row.join('\t'));
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
// ─── Factory methods ────────────────────────────────────────
|
|
963
|
+
|
|
964
|
+
/**
|
|
965
|
+
* Creates a rows×cols zero matrix.
|
|
966
|
+
* @example
|
|
967
|
+
* ```ts
|
|
968
|
+
* const z = Matrix.zeros(2, 3); // [[0,0,0],[0,0,0]]
|
|
969
|
+
* ```
|
|
970
|
+
*/
|
|
971
|
+
static zeros(rows: number, cols: number): Matrix {
|
|
972
|
+
const data: number[][] = Array.from({ length: rows }, () => new Array(cols).fill(0));
|
|
973
|
+
return new Matrix(data);
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
/**
|
|
977
|
+
* Creates an n×n identity matrix.
|
|
978
|
+
* @example
|
|
979
|
+
* ```ts
|
|
980
|
+
* const I = Matrix.identity(3); // [[1,0,0],[0,1,0],[0,0,1]]
|
|
981
|
+
* ```
|
|
982
|
+
*/
|
|
983
|
+
static identity(n: number): Matrix {
|
|
984
|
+
const data: number[][] = Array.from({ length: n }, (_, i) =>
|
|
985
|
+
Array.from({ length: n }, (_, j) => (i === j ? 1 : 0))
|
|
986
|
+
);
|
|
987
|
+
return new Matrix(data);
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
/**
|
|
991
|
+
* Creates a Matrix from a plain 2D array (deep copy).
|
|
992
|
+
* @example
|
|
993
|
+
* ```ts
|
|
994
|
+
* const m = Matrix.from([[1, 2], [3, 4]]);
|
|
995
|
+
* m.get(0, 1); // 2
|
|
996
|
+
* ```
|
|
997
|
+
*/
|
|
998
|
+
static from(data: number[][]): Matrix {
|
|
999
|
+
return new Matrix(data.map(row => [...row]));
|
|
1000
|
+
}
|
|
1001
|
+
|
|
430
1002
|
protected _addFn(a: number | undefined, b: number): number | undefined {
|
|
431
1003
|
if (a === undefined) return b;
|
|
432
1004
|
return a + b;
|