data-structure-typed 2.5.3 → 2.6.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.
Files changed (158) hide show
  1. package/.github/workflows/ci.yml +7 -2
  2. package/.github/workflows/release-package.yml +9 -2
  3. package/.husky/pre-commit +3 -0
  4. package/CHANGELOG.md +1 -1
  5. package/MIGRATION.md +48 -0
  6. package/README.md +20 -2
  7. package/README_CN.md +20 -2
  8. package/SPECIFICATION.md +24 -0
  9. package/SPECIFICATION.zh-CN.md +24 -0
  10. package/dist/cjs/binary-tree.cjs +1897 -19
  11. package/dist/cjs/graph.cjs +174 -0
  12. package/dist/cjs/hash.cjs +33 -0
  13. package/dist/cjs/heap.cjs +71 -0
  14. package/dist/cjs/index.cjs +2383 -3
  15. package/dist/cjs/linked-list.cjs +224 -2
  16. package/dist/cjs/matrix.cjs +24 -0
  17. package/dist/cjs/priority-queue.cjs +71 -0
  18. package/dist/cjs/queue.cjs +221 -1
  19. package/dist/cjs/stack.cjs +59 -0
  20. package/dist/cjs/trie.cjs +62 -0
  21. package/dist/cjs-legacy/binary-tree.cjs +1897 -19
  22. package/dist/cjs-legacy/graph.cjs +174 -0
  23. package/dist/cjs-legacy/hash.cjs +33 -0
  24. package/dist/cjs-legacy/heap.cjs +71 -0
  25. package/dist/cjs-legacy/index.cjs +2383 -3
  26. package/dist/cjs-legacy/linked-list.cjs +224 -2
  27. package/dist/cjs-legacy/matrix.cjs +24 -0
  28. package/dist/cjs-legacy/priority-queue.cjs +71 -0
  29. package/dist/cjs-legacy/queue.cjs +221 -1
  30. package/dist/cjs-legacy/stack.cjs +59 -0
  31. package/dist/cjs-legacy/trie.cjs +62 -0
  32. package/dist/esm/binary-tree.mjs +1897 -19
  33. package/dist/esm/graph.mjs +174 -0
  34. package/dist/esm/hash.mjs +33 -0
  35. package/dist/esm/heap.mjs +71 -0
  36. package/dist/esm/index.mjs +2383 -3
  37. package/dist/esm/linked-list.mjs +224 -2
  38. package/dist/esm/matrix.mjs +24 -0
  39. package/dist/esm/priority-queue.mjs +71 -0
  40. package/dist/esm/queue.mjs +221 -1
  41. package/dist/esm/stack.mjs +59 -0
  42. package/dist/esm/trie.mjs +62 -0
  43. package/dist/esm-legacy/binary-tree.mjs +1897 -19
  44. package/dist/esm-legacy/graph.mjs +174 -0
  45. package/dist/esm-legacy/hash.mjs +33 -0
  46. package/dist/esm-legacy/heap.mjs +71 -0
  47. package/dist/esm-legacy/index.mjs +2383 -3
  48. package/dist/esm-legacy/linked-list.mjs +224 -2
  49. package/dist/esm-legacy/matrix.mjs +24 -0
  50. package/dist/esm-legacy/priority-queue.mjs +71 -0
  51. package/dist/esm-legacy/queue.mjs +221 -1
  52. package/dist/esm-legacy/stack.mjs +59 -0
  53. package/dist/esm-legacy/trie.mjs +62 -0
  54. package/dist/types/data-structures/base/iterable-element-base.d.ts +17 -0
  55. package/dist/types/data-structures/base/linear-base.d.ts +6 -0
  56. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +36 -0
  57. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +42 -0
  58. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +75 -0
  59. package/dist/types/data-structures/binary-tree/bst.d.ts +72 -0
  60. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +57 -0
  61. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +18 -0
  62. package/dist/types/data-structures/binary-tree/tree-map.d.ts +375 -0
  63. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +389 -0
  64. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +330 -0
  65. package/dist/types/data-structures/binary-tree/tree-set.d.ts +438 -0
  66. package/dist/types/data-structures/graph/directed-graph.d.ts +30 -0
  67. package/dist/types/data-structures/graph/undirected-graph.d.ts +27 -0
  68. package/dist/types/data-structures/hash/hash-map.d.ts +33 -0
  69. package/dist/types/data-structures/heap/heap.d.ts +42 -0
  70. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +75 -2
  71. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +45 -0
  72. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +54 -0
  73. package/dist/types/data-structures/matrix/matrix.d.ts +24 -0
  74. package/dist/types/data-structures/queue/deque.d.ts +90 -1
  75. package/dist/types/data-structures/queue/queue.d.ts +36 -0
  76. package/dist/types/data-structures/stack/stack.d.ts +30 -0
  77. package/dist/types/data-structures/trie/trie.d.ts +36 -0
  78. package/dist/umd/data-structure-typed.js +2383 -3
  79. package/dist/umd/data-structure-typed.min.js +3 -3
  80. package/docs-site-docusaurus/docs/api/classes/AVLTree.md +108 -108
  81. package/docs-site-docusaurus/docs/api/classes/BST.md +101 -101
  82. package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +13 -13
  83. package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +66 -66
  84. package/docs-site-docusaurus/docs/api/classes/Deque.md +235 -51
  85. package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +21 -21
  86. package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +231 -67
  87. package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +9 -9
  88. package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +1 -1
  89. package/docs-site-docusaurus/docs/api/classes/HashMap.md +14 -14
  90. package/docs-site-docusaurus/docs/api/classes/Heap.md +117 -34
  91. package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +83 -13
  92. package/docs-site-docusaurus/docs/api/classes/LinearBase.md +124 -20
  93. package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +140 -32
  94. package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +30 -26
  95. package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +159 -51
  96. package/docs-site-docusaurus/docs/api/classes/MapGraph.md +20 -20
  97. package/docs-site-docusaurus/docs/api/classes/Matrix.md +23 -23
  98. package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +117 -34
  99. package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +117 -34
  100. package/docs-site-docusaurus/docs/api/classes/MinHeap.md +117 -34
  101. package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +117 -34
  102. package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +117 -34
  103. package/docs-site-docusaurus/docs/api/classes/Queue.md +142 -34
  104. package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +117 -117
  105. package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +8 -8
  106. package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +158 -50
  107. package/docs-site-docusaurus/docs/api/classes/SkipList.md +21 -21
  108. package/docs-site-docusaurus/docs/api/classes/Stack.md +108 -26
  109. package/docs-site-docusaurus/docs/api/classes/TreeMap.md +33 -33
  110. package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +75 -39
  111. package/docs-site-docusaurus/docs/api/classes/TreeSet.md +301 -39
  112. package/docs-site-docusaurus/docs/api/classes/Trie.md +110 -28
  113. package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +20 -20
  114. package/jest.integration.config.js +1 -2
  115. package/package.json +51 -50
  116. package/src/common/error.ts +15 -32
  117. package/src/common/index.ts +0 -3
  118. package/src/data-structures/base/iterable-element-base.ts +32 -3
  119. package/src/data-structures/base/linear-base.ts +13 -36
  120. package/src/data-structures/binary-tree/avl-tree.ts +31 -493
  121. package/src/data-structures/binary-tree/binary-indexed-tree.ts +47 -530
  122. package/src/data-structures/binary-tree/binary-tree.ts +326 -1236
  123. package/src/data-structures/binary-tree/bst.ts +158 -1010
  124. package/src/data-structures/binary-tree/red-black-tree.ts +451 -1233
  125. package/src/data-structures/binary-tree/segment-tree.ts +73 -333
  126. package/src/data-structures/binary-tree/tree-map.ts +462 -4749
  127. package/src/data-structures/binary-tree/tree-multi-map.ts +310 -4530
  128. package/src/data-structures/binary-tree/tree-multi-set.ts +300 -3652
  129. package/src/data-structures/binary-tree/tree-set.ts +437 -4443
  130. package/src/data-structures/graph/abstract-graph.ts +98 -167
  131. package/src/data-structures/graph/directed-graph.ts +137 -532
  132. package/src/data-structures/graph/map-graph.ts +0 -3
  133. package/src/data-structures/graph/undirected-graph.ts +132 -484
  134. package/src/data-structures/hash/hash-map.ts +154 -549
  135. package/src/data-structures/heap/heap.ts +200 -753
  136. package/src/data-structures/linked-list/doubly-linked-list.ts +153 -809
  137. package/src/data-structures/linked-list/singly-linked-list.ts +122 -749
  138. package/src/data-structures/linked-list/skip-linked-list.ts +211 -864
  139. package/src/data-structures/matrix/matrix.ts +179 -494
  140. package/src/data-structures/matrix/navigator.ts +0 -1
  141. package/src/data-structures/priority-queue/max-priority-queue.ts +1 -6
  142. package/src/data-structures/priority-queue/min-priority-queue.ts +6 -11
  143. package/src/data-structures/priority-queue/priority-queue.ts +1 -2
  144. package/src/data-structures/queue/deque.ts +241 -807
  145. package/src/data-structures/queue/queue.ts +102 -589
  146. package/src/data-structures/stack/stack.ts +76 -475
  147. package/src/data-structures/trie/trie.ts +98 -592
  148. package/src/types/common.ts +0 -10
  149. package/src/types/data-structures/binary-tree/bst.ts +0 -7
  150. package/src/types/data-structures/binary-tree/red-black-tree.ts +0 -1
  151. package/src/types/data-structures/graph/abstract-graph.ts +0 -2
  152. package/src/types/data-structures/hash/hash-map.ts +0 -3
  153. package/src/types/data-structures/hash/index.ts +0 -1
  154. package/src/types/data-structures/matrix/navigator.ts +0 -2
  155. package/src/types/utils/utils.ts +0 -7
  156. package/src/types/utils/validate-type.ts +0 -7
  157. package/src/utils/number.ts +0 -2
  158. package/src/utils/utils.ts +0 -5
@@ -116,7 +116,6 @@ export class Matrix {
116
116
  this._rows = data.length;
117
117
  this._cols = data[0]?.length ?? 0;
118
118
  }
119
-
120
119
  if (data.length > 0) {
121
120
  this._data = data;
122
121
  } else {
@@ -181,6 +180,51 @@ export class Matrix {
181
180
  return this._multiplyFn;
182
181
  }
183
182
 
183
+ /**
184
+ * Returns [rows, cols] dimensions tuple.
185
+ */
186
+ get size(): [number, number] {
187
+ return [this._rows, this._cols];
188
+ }
189
+
190
+ /**
191
+ * Creates a rows×cols zero matrix.
192
+ * @example
193
+ * ```ts
194
+ * const z = Matrix.zeros(2, 3); // [[0,0,0],[0,0,0]]
195
+ * ```
196
+ */
197
+ static zeros(rows: number, cols: number): Matrix {
198
+ const data: number[][] = Array.from({ length: rows }, () => new Array(cols).fill(0));
199
+ return new Matrix(data);
200
+ }
201
+
202
+ /**
203
+ * Creates an n×n identity matrix.
204
+ * @example
205
+ * ```ts
206
+ * const I = Matrix.identity(3); // [[1,0,0],[0,1,0],[0,0,1]]
207
+ * ```
208
+ */
209
+ static identity(n: number): Matrix {
210
+ const data: number[][] = Array.from({ length: n }, (_, i) =>
211
+ Array.from({ length: n }, (_, j) => (i === j ? 1 : 0))
212
+ );
213
+ return new Matrix(data);
214
+ }
215
+
216
+ /**
217
+ * Creates a Matrix from a plain 2D array (deep copy).
218
+ * @example
219
+ * ```ts
220
+ * const m = Matrix.from([[1, 2], [3, 4]]);
221
+ * m.get(0, 1); // 2
222
+ * ```
223
+ */
224
+ static from(data: number[][]): Matrix {
225
+ return new Matrix(data.map(row => [...row]));
226
+ }
227
+
184
228
  /**
185
229
  * The `get` function returns the value at the specified row and column index if it is a valid index.
186
230
  * @param {number} row - The `row` parameter represents the row index of the element you want to
@@ -189,61 +233,22 @@ export class Matrix {
189
233
  * retrieve from the data array.
190
234
  * @returns The `get` function returns a number if the provided row and column indices are valid.
191
235
  * 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
-
225
-
226
-
227
-
228
-
229
-
230
-
231
- * @example
232
- * // Get and set individual cells
233
- * const m = new Matrix([
234
- * [0, 0, 0],
235
- * [0, 0, 0]
236
- * ]);
237
- *
238
- * m.set(0, 1, 42);
239
- * m.set(1, 2, 99);
240
- *
241
- * console.log(m.get(0, 1)); // 42;
242
- * console.log(m.get(1, 2)); // 99;
243
- * console.log(m.get(0, 0)); // 0;
244
- *
245
- * // Out of bounds returns undefined
246
- * console.log(m.get(5, 5)); // undefined;
236
+ * @example
237
+ * // Get and set individual cells
238
+ * const m = new Matrix([
239
+ * [0, 0, 0],
240
+ * [0, 0, 0]
241
+ * ]);
242
+ *
243
+ * m.set(0, 1, 42);
244
+ * m.set(1, 2, 99);
245
+ *
246
+ * console.log(m.get(0, 1)); // 42;
247
+ * console.log(m.get(1, 2)); // 99;
248
+ * console.log(m.get(0, 0)); // 0;
249
+ *
250
+ * // Out of bounds returns undefined
251
+ * console.log(m.get(5, 5)); // undefined;
247
252
  */
248
253
  get(row: number, col: number): number | undefined {
249
254
  if (this.isValidIndex(row, col)) {
@@ -262,52 +267,13 @@ export class Matrix {
262
267
  * @returns a boolean value. It returns true if the index (row, col) is valid and the value is
263
268
  * successfully set in the data array. It returns false if the index is invalid and the value is not
264
269
  * set.
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
-
292
-
293
-
294
-
295
-
296
-
297
-
298
-
299
-
300
-
301
-
302
-
303
-
304
- * @example
305
- * // Modify individual cells
306
- * const m = Matrix.zeros(2, 2);
307
- * console.log(m.set(0, 0, 5)); // true;
308
- * console.log(m.set(1, 1, 10)); // true;
309
- * console.log(m.get(0, 0)); // 5;
310
- * console.log(m.get(1, 1)); // 10;
270
+ * @example
271
+ * // Modify individual cells
272
+ * const m = Matrix.zeros(2, 2);
273
+ * console.log(m.set(0, 0, 5)); // true;
274
+ * console.log(m.set(1, 1, 10)); // true;
275
+ * console.log(m.get(0, 0)); // 5;
276
+ * console.log(m.get(1, 1)); // 10;
311
277
  */
312
278
  set(row: number, col: number, value: number): boolean {
313
279
  if (this.isValidIndex(row, col)) {
@@ -332,73 +298,33 @@ export class Matrix {
332
298
  * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
333
299
  * @returns The `add` method returns a new `Matrix` object that represents the result of adding the
334
300
  * current matrix with the provided `matrix` parameter.
335
-
336
-
337
-
338
-
339
-
340
-
341
-
342
-
343
-
344
-
345
-
346
-
347
-
348
-
349
-
350
-
351
-
352
-
353
-
354
-
355
-
356
-
357
-
358
-
359
-
360
-
361
-
362
-
363
-
364
-
365
-
366
-
367
-
368
-
369
-
370
-
371
-
372
-
373
-
374
- * @example
375
- * // Basic matrix arithmetic
376
- * const a = new Matrix([
377
- * [1, 2],
378
- * [3, 4]
379
- * ]);
380
- * const b = new Matrix([
381
- * [5, 6],
382
- * [7, 8]
383
- * ]);
384
- *
385
- * const sum = a.add(b);
386
- * console.log(sum?.data); // [
387
- * // [6, 8],
388
- * // [10, 12]
389
- * // ];
390
- *
391
- * const diff = b.subtract(a);
392
- * console.log(diff?.data); // [
393
- * // [4, 4],
394
- * // [4, 4]
395
- * // ];
301
+ * @example
302
+ * // Basic matrix arithmetic
303
+ * const a = new Matrix([
304
+ * [1, 2],
305
+ * [3, 4]
306
+ * ]);
307
+ * const b = new Matrix([
308
+ * [5, 6],
309
+ * [7, 8]
310
+ * ]);
311
+ *
312
+ * const sum = a.add(b);
313
+ * console.log(sum?.data); // [
314
+ * // [6, 8],
315
+ * // [10, 12]
316
+ * // ];
317
+ *
318
+ * const diff = b.subtract(a);
319
+ * console.log(diff?.data); // [
320
+ * // [4, 4],
321
+ * // [4, 4]
322
+ * // ];
396
323
  */
397
324
  add(matrix: Matrix): Matrix | undefined {
398
325
  if (!this.isMatchForCalculate(matrix)) {
399
326
  raise(Error, ERR.matrixDimensionMismatch('addition'));
400
327
  }
401
-
402
328
  const resultData: number[][] = [];
403
329
  for (let i = 0; i < this.rows; i++) {
404
330
  resultData[i] = [];
@@ -410,7 +336,6 @@ export class Matrix {
410
336
  }
411
337
  }
412
338
  }
413
-
414
339
  return new Matrix(resultData, {
415
340
  rows: this.rows,
416
341
  cols: this.cols,
@@ -426,57 +351,26 @@ export class Matrix {
426
351
  * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class. It
427
352
  * represents the matrix that you want to subtract from the current matrix.
428
353
  * @returns a new Matrix object with the result of the subtraction operation.
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
-
459
-
460
-
461
-
462
-
463
-
464
-
465
-
466
-
467
-
468
- * @example
469
- * // Element-wise subtraction
470
- * const a = Matrix.from([[5, 6], [7, 8]]);
471
- * const b = Matrix.from([[1, 2], [3, 4]]);
472
- * const result = a.subtract(b);
473
- * console.log(result?.toArray()); // [[4, 4], [4, 4]];
354
+ * @example
355
+ * // Element-wise subtraction
356
+ * const a = Matrix.from([
357
+ * [5, 6],
358
+ * [7, 8]
359
+ * ]);
360
+ * const b = Matrix.from([
361
+ * [1, 2],
362
+ * [3, 4]
363
+ * ]);
364
+ * const result = a.subtract(b);
365
+ * console.log(result?.toArray()); // [
366
+ * // [4, 4],
367
+ * // [4, 4]
368
+ * // ];
474
369
  */
475
370
  subtract(matrix: Matrix): Matrix | undefined {
476
371
  if (!this.isMatchForCalculate(matrix)) {
477
372
  raise(Error, ERR.matrixDimensionMismatch('subtraction'));
478
373
  }
479
-
480
374
  const resultData: number[][] = [];
481
375
  for (let i = 0; i < this.rows; i++) {
482
376
  resultData[i] = [];
@@ -488,7 +382,6 @@ export class Matrix {
488
382
  }
489
383
  }
490
384
  }
491
-
492
385
  return new Matrix(resultData, {
493
386
  rows: this.rows,
494
387
  cols: this.cols,
@@ -503,73 +396,33 @@ export class Matrix {
503
396
  * as a new matrix.
504
397
  * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
505
398
  * @returns a new Matrix object.
506
-
507
-
508
-
509
-
510
-
511
-
512
-
513
-
514
-
515
-
516
-
517
-
518
-
519
-
520
-
521
-
522
-
523
-
524
-
525
-
526
-
527
-
528
-
529
-
530
-
531
-
532
-
533
-
534
-
535
-
536
-
537
-
538
-
539
-
540
-
541
-
542
-
543
-
544
-
545
- * @example
546
- * // Matrix multiplication for transformations
547
- * // 2x3 matrix * 3x2 matrix = 2x2 matrix
548
- * const a = new Matrix([
549
- * [1, 2, 3],
550
- * [4, 5, 6]
551
- * ]);
552
- * const b = new Matrix([
553
- * [7, 8],
554
- * [9, 10],
555
- * [11, 12]
556
- * ]);
557
- *
558
- * const product = a.multiply(b);
559
- * console.log(product?.rows); // 2;
560
- * console.log(product?.cols); // 2;
561
- * // Row 0: 1*7+2*9+3*11=58, 1*8+2*10+3*12=64
562
- * // Row 1: 4*7+5*9+6*11=139, 4*8+5*10+6*12=154
563
- * console.log(product?.data); // [
564
- * // [58, 64],
565
- * // [139, 154]
566
- * // ];
399
+ * @example
400
+ * // Matrix multiplication for transformations
401
+ * // 2x3 matrix * 3x2 matrix = 2x2 matrix
402
+ * const a = new Matrix([
403
+ * [1, 2, 3],
404
+ * [4, 5, 6]
405
+ * ]);
406
+ * const b = new Matrix([
407
+ * [7, 8],
408
+ * [9, 10],
409
+ * [11, 12]
410
+ * ]);
411
+ *
412
+ * const product = a.multiply(b);
413
+ * console.log(product?.rows); // 2;
414
+ * console.log(product?.cols); // 2;
415
+ * // Row 0: 1*7+2*9+3*11=58, 1*8+2*10+3*12=64
416
+ * // Row 1: 4*7+5*9+6*11=139, 4*8+5*10+6*12=154
417
+ * console.log(product?.data); // [
418
+ * // [58, 64],
419
+ * // [139, 154]
420
+ * // ];
567
421
  */
568
422
  multiply(matrix: Matrix): Matrix | undefined {
569
423
  if (this.cols !== matrix.rows) {
570
424
  raise(Error, ERR.matrixDimensionMismatch('multiplication (A.cols must equal B.rows)'));
571
425
  }
572
-
573
426
  const resultData: number[][] = [];
574
427
  for (let i = 0; i < this.rows; i++) {
575
428
  resultData[i] = [];
@@ -588,7 +441,6 @@ export class Matrix {
588
441
  if (sum !== undefined) resultData[i][j] = sum;
589
442
  }
590
443
  }
591
-
592
444
  return new Matrix(resultData, {
593
445
  rows: this.rows,
594
446
  cols: matrix.cols,
@@ -602,72 +454,31 @@ export class Matrix {
602
454
  * The transpose function takes a matrix and returns a new matrix that is the transpose of the
603
455
  * original matrix.
604
456
  * @returns The transpose() function returns a new Matrix object with the transposed data.
605
-
606
-
607
-
608
-
609
-
610
-
611
-
612
-
613
-
614
-
615
-
616
-
617
-
618
-
619
-
620
-
621
-
622
-
623
-
624
-
625
-
626
-
627
-
628
-
629
-
630
-
631
-
632
-
633
-
634
-
635
-
636
-
637
-
638
-
639
-
640
-
641
-
642
-
643
-
644
- * @example
645
- * // Matrix transpose (square matrix)
646
- * const m = new Matrix([
647
- * [1, 2, 3],
648
- * [4, 5, 6],
649
- * [7, 8, 9]
650
- * ]);
651
- *
652
- * const transposed = m.transpose();
653
- * console.log(transposed.rows); // 3;
654
- * console.log(transposed.cols); // 3;
655
- * console.log(transposed.data); // [
656
- * // [1, 4, 7],
657
- * // [2, 5, 8],
658
- * // [3, 6, 9]
659
- * // ];
660
- *
661
- * // Transpose of transpose = original
662
- * console.log(transposed.transpose().data); // m.data;
457
+ * @example
458
+ * // Matrix transpose (square matrix)
459
+ * const m = new Matrix([
460
+ * [1, 2, 3],
461
+ * [4, 5, 6],
462
+ * [7, 8, 9]
463
+ * ]);
464
+ *
465
+ * const transposed = m.transpose();
466
+ * console.log(transposed.rows); // 3;
467
+ * console.log(transposed.cols); // 3;
468
+ * console.log(transposed.data); // [
469
+ * // [1, 4, 7],
470
+ * // [2, 5, 8],
471
+ * // [3, 6, 9]
472
+ * // ];
473
+ *
474
+ * // Transpose of transpose = original
475
+ * console.log(transposed.transpose().data); // m.data;
663
476
  */
664
477
  transpose(): Matrix {
665
478
  if (this.data.some(row => row.length !== this.cols)) {
666
479
  raise(Error, ERR.matrixNotRectangular());
667
480
  }
668
-
669
481
  const resultData: number[][] = [];
670
-
671
482
  for (let j = 0; j < this.cols; j++) {
672
483
  resultData[j] = [];
673
484
  for (let i = 0; i < this.rows; i++) {
@@ -675,7 +486,6 @@ export class Matrix {
675
486
  if (trans !== undefined) resultData[j][i] = trans;
676
487
  }
677
488
  }
678
-
679
489
  return new Matrix(resultData, {
680
490
  rows: this.cols,
681
491
  cols: this.rows,
@@ -685,66 +495,31 @@ export class Matrix {
685
495
  });
686
496
  }
687
497
 
498
+ // ─── Standard interface ─────────────────────────────────────
499
+
688
500
  /**
689
501
  * The `inverse` function calculates the inverse of a square matrix using Gaussian elimination.
690
502
  * @returns a Matrix object, which represents the inverse of the original matrix.
691
-
692
-
693
-
694
-
695
-
696
-
697
-
698
-
699
-
700
-
701
-
702
-
703
-
704
-
705
-
706
-
707
-
708
-
709
-
710
-
711
-
712
-
713
-
714
-
715
-
716
-
717
-
718
-
719
-
720
-
721
-
722
-
723
-
724
-
725
-
726
-
727
-
728
-
729
-
730
- * @example
731
- * // Compute the inverse of a 2x2 matrix
732
- * const m = Matrix.from([[4, 7], [2, 6]]);
733
- * const inv = m.inverse();
734
- * console.log(inv); // defined;
735
- * // A * A^-1 should ≈ Identity
736
- * const product = m.multiply(inv!);
737
- * console.log(product?.get(0, 0)); // toBeCloseTo;
738
- * console.log(product?.get(0, 1)); // toBeCloseTo;
739
- * console.log(product?.get(1, 0)); // toBeCloseTo;
740
- * console.log(product?.get(1, 1)); // toBeCloseTo;
503
+ * @example
504
+ * // Compute the inverse of a 2x2 matrix
505
+ * const m = Matrix.from([
506
+ * [4, 7],
507
+ * [2, 6]
508
+ * ]);
509
+ * const inv = m.inverse();
510
+ * console.log(inv); // defined;
511
+ * // A * A^-1 should ≈ Identity
512
+ * const product = m.multiply(inv!);
513
+ * console.log(product?.get(0, 0)); // toBeCloseTo;
514
+ * console.log(product?.get(0, 1)); // toBeCloseTo;
515
+ * console.log(product?.get(1, 0)); // toBeCloseTo;
516
+ * console.log(product?.get(1, 1)); // toBeCloseTo;
741
517
  */
742
518
  inverse(): Matrix | undefined {
743
519
  // Check if the matrix is square
744
520
  if (this.rows !== this.cols) {
745
521
  raise(Error, ERR.matrixNotSquare());
746
522
  }
747
-
748
523
  // Create an augmented matrix [this | I]
749
524
  const augmentedMatrixData: number[][] = [];
750
525
  for (let i = 0; i < this.rows; i++) {
@@ -753,7 +528,6 @@ export class Matrix {
753
528
  augmentedMatrixData[i][this.cols + j] = i === j ? 1 : 0; // Append the identity matrix
754
529
  }
755
530
  }
756
-
757
531
  const augmentedMatrix = new Matrix(augmentedMatrixData, {
758
532
  rows: this.rows,
759
533
  cols: this.cols * 2,
@@ -761,7 +535,6 @@ export class Matrix {
761
535
  subtractFn: this.subtractFn,
762
536
  multiplyFn: this.multiplyFn
763
537
  });
764
-
765
538
  // Apply Gaussian elimination to transform the left half into the identity matrix
766
539
  for (let i = 0; i < this.rows; i++) {
767
540
  // Find pivot
@@ -769,42 +542,33 @@ export class Matrix {
769
542
  while (pivotRow < this.rows && augmentedMatrix.get(pivotRow, i) === 0) {
770
543
  pivotRow++;
771
544
  }
772
-
773
545
  if (pivotRow === this.rows) {
774
546
  // Matrix is singular, and its inverse does not exist
775
547
  raise(Error, ERR.matrixSingular());
776
548
  }
777
-
778
549
  // Swap rows to make the pivot the current row
779
550
  augmentedMatrix._swapRows(i, pivotRow);
780
-
781
551
  // Scale the pivot row to make the pivot element 1
782
552
  const pivotElement = augmentedMatrix.get(i, i) ?? 1;
783
-
784
553
  if (pivotElement === 0) {
785
554
  // Handle division by zero
786
555
  raise(Error, ERR.matrixSingular());
787
556
  }
788
-
789
557
  augmentedMatrix._scaleRow(i, 1 / pivotElement);
790
-
791
558
  // Eliminate other rows to make elements in the current column zero
792
559
  for (let j = 0; j < this.rows; j++) {
793
560
  if (j !== i) {
794
561
  let factor = augmentedMatrix.get(j, i);
795
562
  if (factor === undefined) factor = 0;
796
-
797
563
  augmentedMatrix._addScaledRow(j, i, -factor);
798
564
  }
799
565
  }
800
566
  }
801
-
802
567
  // Extract the right half of the augmented matrix as the inverse
803
568
  const inverseData: number[][] = [];
804
569
  for (let i = 0; i < this.rows; i++) {
805
570
  inverseData[i] = augmentedMatrix.data[i].slice(this.cols);
806
571
  }
807
-
808
572
  return new Matrix(inverseData, {
809
573
  rows: this.rows,
810
574
  cols: this.cols,
@@ -818,57 +582,26 @@ export class Matrix {
818
582
  * The dot function calculates the dot product of two matrices and returns a new matrix.
819
583
  * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
820
584
  * @returns a new Matrix object.
821
-
822
-
823
-
824
-
825
-
826
-
827
-
828
-
829
-
830
-
831
-
832
-
833
-
834
-
835
-
836
-
837
-
838
-
839
-
840
-
841
-
842
-
843
-
844
-
845
-
846
-
847
-
848
-
849
-
850
-
851
-
852
-
853
-
854
-
855
-
856
-
857
-
858
-
859
-
860
- * @example
861
- * // Dot product of two matrices
862
- * const a = Matrix.from([[1, 2], [3, 4]]);
863
- * const b = Matrix.from([[5, 6], [7, 8]]);
864
- * const result = a.dot(b);
865
- * console.log(result?.toArray()); // [[19, 22], [43, 50]];
585
+ * @example
586
+ * // Dot product of two matrices
587
+ * const a = Matrix.from([
588
+ * [1, 2],
589
+ * [3, 4]
590
+ * ]);
591
+ * const b = Matrix.from([
592
+ * [5, 6],
593
+ * [7, 8]
594
+ * ]);
595
+ * const result = a.dot(b);
596
+ * console.log(result?.toArray()); // [
597
+ * // [19, 22],
598
+ * // [43, 50]
599
+ * // ];
866
600
  */
867
601
  dot(matrix: Matrix): Matrix | undefined {
868
602
  if (this.cols !== matrix.rows) {
869
603
  raise(Error, ERR.matrixDimensionMismatch('dot product (A.cols must equal B.rows)'));
870
604
  }
871
-
872
605
  const resultData: number[][] = [];
873
606
  for (let i = 0; i < this.rows; i++) {
874
607
  resultData[i] = [];
@@ -887,7 +620,6 @@ export class Matrix {
887
620
  if (sum !== undefined) resultData[i][j] = sum;
888
621
  }
889
622
  }
890
-
891
623
  return new Matrix(resultData, {
892
624
  rows: this.rows,
893
625
  cols: matrix.cols,
@@ -928,15 +660,6 @@ export class Matrix {
928
660
  );
929
661
  }
930
662
 
931
- // ─── Standard interface ─────────────────────────────────────
932
-
933
- /**
934
- * Returns [rows, cols] dimensions tuple.
935
- */
936
- get size(): [number, number] {
937
- return [this._rows, this._cols];
938
- }
939
-
940
663
  isEmpty(): boolean {
941
664
  return this._rows === 0 || this._cols === 0;
942
665
  }
@@ -978,6 +701,8 @@ export class Matrix {
978
701
  };
979
702
  }
980
703
 
704
+ // ─── Factory methods ────────────────────────────────────────
705
+
981
706
  /**
982
707
  * Visits each element with its row and column index.
983
708
  */
@@ -1015,46 +740,6 @@ export class Matrix {
1015
740
  }
1016
741
  }
1017
742
 
1018
- // ─── Factory methods ────────────────────────────────────────
1019
-
1020
- /**
1021
- * Creates a rows×cols zero matrix.
1022
- * @example
1023
- * ```ts
1024
- * const z = Matrix.zeros(2, 3); // [[0,0,0],[0,0,0]]
1025
- * ```
1026
- */
1027
- static zeros(rows: number, cols: number): Matrix {
1028
- const data: number[][] = Array.from({ length: rows }, () => new Array(cols).fill(0));
1029
- return new Matrix(data);
1030
- }
1031
-
1032
- /**
1033
- * Creates an n×n identity matrix.
1034
- * @example
1035
- * ```ts
1036
- * const I = Matrix.identity(3); // [[1,0,0],[0,1,0],[0,0,1]]
1037
- * ```
1038
- */
1039
- static identity(n: number): Matrix {
1040
- const data: number[][] = Array.from({ length: n }, (_, i) =>
1041
- Array.from({ length: n }, (_, j) => (i === j ? 1 : 0))
1042
- );
1043
- return new Matrix(data);
1044
- }
1045
-
1046
- /**
1047
- * Creates a Matrix from a plain 2D array (deep copy).
1048
- * @example
1049
- * ```ts
1050
- * const m = Matrix.from([[1, 2], [3, 4]]);
1051
- * m.get(0, 1); // 2
1052
- * ```
1053
- */
1054
- static from(data: number[][]): Matrix {
1055
- return new Matrix(data.map(row => [...row]));
1056
- }
1057
-
1058
743
  protected _addFn(a: number | undefined, b: number): number | undefined {
1059
744
  if (a === undefined) return b;
1060
745
  return a + b;