data-structure-typed 2.6.0 → 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.
- package/.github/workflows/ci.yml +7 -2
- package/.github/workflows/release-package.yml +9 -2
- package/docs-site-docusaurus/docs/api/classes/AVLTree.md +108 -108
- package/docs-site-docusaurus/docs/api/classes/BST.md +101 -101
- package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +13 -13
- package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +66 -66
- package/docs-site-docusaurus/docs/api/classes/Deque.md +235 -51
- package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +21 -21
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +231 -67
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +9 -9
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +1 -1
- package/docs-site-docusaurus/docs/api/classes/HashMap.md +14 -14
- package/docs-site-docusaurus/docs/api/classes/Heap.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +83 -13
- package/docs-site-docusaurus/docs/api/classes/LinearBase.md +124 -20
- package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +140 -32
- package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +23 -23
- package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +159 -51
- package/docs-site-docusaurus/docs/api/classes/MapGraph.md +20 -20
- package/docs-site-docusaurus/docs/api/classes/Matrix.md +23 -23
- package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/MinHeap.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/Queue.md +142 -34
- package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +117 -117
- package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +8 -8
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +158 -50
- package/docs-site-docusaurus/docs/api/classes/SkipList.md +21 -21
- package/docs-site-docusaurus/docs/api/classes/Stack.md +108 -26
- package/docs-site-docusaurus/docs/api/classes/TreeMap.md +33 -33
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +75 -39
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +301 -39
- package/docs-site-docusaurus/docs/api/classes/Trie.md +110 -28
- package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +20 -20
- package/package.json +45 -46
- package/src/common/error.ts +15 -32
- package/src/common/index.ts +0 -3
- package/src/data-structures/base/iterable-element-base.ts +0 -3
- package/src/data-structures/base/linear-base.ts +2 -36
- package/src/data-structures/binary-tree/avl-tree.ts +31 -529
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +47 -572
- package/src/data-structures/binary-tree/binary-tree.ts +326 -1311
- package/src/data-structures/binary-tree/bst.ts +158 -1082
- package/src/data-structures/binary-tree/red-black-tree.ts +451 -1290
- package/src/data-structures/binary-tree/segment-tree.ts +73 -351
- package/src/data-structures/binary-tree/tree-map.ts +462 -5124
- package/src/data-structures/binary-tree/tree-multi-map.ts +302 -4914
- package/src/data-structures/binary-tree/tree-multi-set.ts +284 -3972
- package/src/data-structures/binary-tree/tree-set.ts +338 -4836
- package/src/data-structures/graph/abstract-graph.ts +98 -167
- package/src/data-structures/graph/directed-graph.ts +137 -562
- package/src/data-structures/graph/map-graph.ts +0 -3
- package/src/data-structures/graph/undirected-graph.ts +132 -511
- package/src/data-structures/hash/hash-map.ts +154 -582
- package/src/data-structures/heap/heap.ts +200 -795
- package/src/data-structures/linked-list/doubly-linked-list.ts +121 -865
- package/src/data-structures/linked-list/singly-linked-list.ts +122 -794
- package/src/data-structures/linked-list/skip-linked-list.ts +211 -918
- package/src/data-structures/matrix/matrix.ts +179 -518
- package/src/data-structures/matrix/navigator.ts +0 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -6
- package/src/data-structures/priority-queue/min-priority-queue.ts +6 -11
- package/src/data-structures/priority-queue/priority-queue.ts +1 -2
- package/src/data-structures/queue/deque.ts +214 -882
- package/src/data-structures/queue/queue.ts +102 -625
- package/src/data-structures/stack/stack.ts +76 -505
- package/src/data-structures/trie/trie.ts +98 -628
- package/src/types/common.ts +0 -10
- package/src/types/data-structures/binary-tree/bst.ts +0 -7
- package/src/types/data-structures/binary-tree/red-black-tree.ts +0 -1
- package/src/types/data-structures/graph/abstract-graph.ts +0 -2
- package/src/types/data-structures/hash/hash-map.ts +0 -3
- package/src/types/data-structures/hash/index.ts +0 -1
- package/src/types/data-structures/matrix/navigator.ts +0 -2
- package/src/types/utils/utils.ts +0 -7
- package/src/types/utils/validate-type.ts +0 -7
- package/src/utils/number.ts +0 -2
- 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,64 +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
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
* @example
|
|
235
|
-
* // Get and set individual cells
|
|
236
|
-
* const m = new Matrix([
|
|
237
|
-
* [0, 0, 0],
|
|
238
|
-
* [0, 0, 0]
|
|
239
|
-
* ]);
|
|
240
|
-
*
|
|
241
|
-
* m.set(0, 1, 42);
|
|
242
|
-
* m.set(1, 2, 99);
|
|
243
|
-
*
|
|
244
|
-
* console.log(m.get(0, 1)); // 42;
|
|
245
|
-
* console.log(m.get(1, 2)); // 99;
|
|
246
|
-
* console.log(m.get(0, 0)); // 0;
|
|
247
|
-
*
|
|
248
|
-
* // Out of bounds returns undefined
|
|
249
|
-
* 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;
|
|
250
252
|
*/
|
|
251
253
|
get(row: number, col: number): number | undefined {
|
|
252
254
|
if (this.isValidIndex(row, col)) {
|
|
@@ -265,55 +267,13 @@ export class Matrix {
|
|
|
265
267
|
* @returns a boolean value. It returns true if the index (row, col) is valid and the value is
|
|
266
268
|
* successfully set in the data array. It returns false if the index is invalid and the value is not
|
|
267
269
|
* set.
|
|
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
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
* @example
|
|
311
|
-
* // Modify individual cells
|
|
312
|
-
* const m = Matrix.zeros(2, 2);
|
|
313
|
-
* console.log(m.set(0, 0, 5)); // true;
|
|
314
|
-
* console.log(m.set(1, 1, 10)); // true;
|
|
315
|
-
* console.log(m.get(0, 0)); // 5;
|
|
316
|
-
* 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;
|
|
317
277
|
*/
|
|
318
278
|
set(row: number, col: number, value: number): boolean {
|
|
319
279
|
if (this.isValidIndex(row, col)) {
|
|
@@ -338,76 +298,33 @@ export class Matrix {
|
|
|
338
298
|
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
|
|
339
299
|
* @returns The `add` method returns a new `Matrix` object that represents the result of adding the
|
|
340
300
|
* current matrix with the provided `matrix` parameter.
|
|
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
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
* @example
|
|
384
|
-
* // Basic matrix arithmetic
|
|
385
|
-
* const a = new Matrix([
|
|
386
|
-
* [1, 2],
|
|
387
|
-
* [3, 4]
|
|
388
|
-
* ]);
|
|
389
|
-
* const b = new Matrix([
|
|
390
|
-
* [5, 6],
|
|
391
|
-
* [7, 8]
|
|
392
|
-
* ]);
|
|
393
|
-
*
|
|
394
|
-
* const sum = a.add(b);
|
|
395
|
-
* console.log(sum?.data); // [
|
|
396
|
-
* // [6, 8],
|
|
397
|
-
* // [10, 12]
|
|
398
|
-
* // ];
|
|
399
|
-
*
|
|
400
|
-
* const diff = b.subtract(a);
|
|
401
|
-
* console.log(diff?.data); // [
|
|
402
|
-
* // [4, 4],
|
|
403
|
-
* // [4, 4]
|
|
404
|
-
* // ];
|
|
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
|
+
* // ];
|
|
405
323
|
*/
|
|
406
324
|
add(matrix: Matrix): Matrix | undefined {
|
|
407
325
|
if (!this.isMatchForCalculate(matrix)) {
|
|
408
326
|
raise(Error, ERR.matrixDimensionMismatch('addition'));
|
|
409
327
|
}
|
|
410
|
-
|
|
411
328
|
const resultData: number[][] = [];
|
|
412
329
|
for (let i = 0; i < this.rows; i++) {
|
|
413
330
|
resultData[i] = [];
|
|
@@ -419,7 +336,6 @@ export class Matrix {
|
|
|
419
336
|
}
|
|
420
337
|
}
|
|
421
338
|
}
|
|
422
|
-
|
|
423
339
|
return new Matrix(resultData, {
|
|
424
340
|
rows: this.rows,
|
|
425
341
|
cols: this.cols,
|
|
@@ -435,60 +351,26 @@ export class Matrix {
|
|
|
435
351
|
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class. It
|
|
436
352
|
* represents the matrix that you want to subtract from the current matrix.
|
|
437
353
|
* @returns a new Matrix object with the result of the subtraction operation.
|
|
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
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
* @example
|
|
481
|
-
* // Element-wise subtraction
|
|
482
|
-
* const a = Matrix.from([[5, 6], [7, 8]]);
|
|
483
|
-
* const b = Matrix.from([[1, 2], [3, 4]]);
|
|
484
|
-
* const result = a.subtract(b);
|
|
485
|
-
* 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
|
+
* // ];
|
|
486
369
|
*/
|
|
487
370
|
subtract(matrix: Matrix): Matrix | undefined {
|
|
488
371
|
if (!this.isMatchForCalculate(matrix)) {
|
|
489
372
|
raise(Error, ERR.matrixDimensionMismatch('subtraction'));
|
|
490
373
|
}
|
|
491
|
-
|
|
492
374
|
const resultData: number[][] = [];
|
|
493
375
|
for (let i = 0; i < this.rows; i++) {
|
|
494
376
|
resultData[i] = [];
|
|
@@ -500,7 +382,6 @@ export class Matrix {
|
|
|
500
382
|
}
|
|
501
383
|
}
|
|
502
384
|
}
|
|
503
|
-
|
|
504
385
|
return new Matrix(resultData, {
|
|
505
386
|
rows: this.rows,
|
|
506
387
|
cols: this.cols,
|
|
@@ -515,76 +396,33 @@ export class Matrix {
|
|
|
515
396
|
* as a new matrix.
|
|
516
397
|
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
|
|
517
398
|
* @returns a new Matrix object.
|
|
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
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
* @example
|
|
561
|
-
* // Matrix multiplication for transformations
|
|
562
|
-
* // 2x3 matrix * 3x2 matrix = 2x2 matrix
|
|
563
|
-
* const a = new Matrix([
|
|
564
|
-
* [1, 2, 3],
|
|
565
|
-
* [4, 5, 6]
|
|
566
|
-
* ]);
|
|
567
|
-
* const b = new Matrix([
|
|
568
|
-
* [7, 8],
|
|
569
|
-
* [9, 10],
|
|
570
|
-
* [11, 12]
|
|
571
|
-
* ]);
|
|
572
|
-
*
|
|
573
|
-
* const product = a.multiply(b);
|
|
574
|
-
* console.log(product?.rows); // 2;
|
|
575
|
-
* console.log(product?.cols); // 2;
|
|
576
|
-
* // Row 0: 1*7+2*9+3*11=58, 1*8+2*10+3*12=64
|
|
577
|
-
* // Row 1: 4*7+5*9+6*11=139, 4*8+5*10+6*12=154
|
|
578
|
-
* console.log(product?.data); // [
|
|
579
|
-
* // [58, 64],
|
|
580
|
-
* // [139, 154]
|
|
581
|
-
* // ];
|
|
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
|
+
* // ];
|
|
582
421
|
*/
|
|
583
422
|
multiply(matrix: Matrix): Matrix | undefined {
|
|
584
423
|
if (this.cols !== matrix.rows) {
|
|
585
424
|
raise(Error, ERR.matrixDimensionMismatch('multiplication (A.cols must equal B.rows)'));
|
|
586
425
|
}
|
|
587
|
-
|
|
588
426
|
const resultData: number[][] = [];
|
|
589
427
|
for (let i = 0; i < this.rows; i++) {
|
|
590
428
|
resultData[i] = [];
|
|
@@ -603,7 +441,6 @@ export class Matrix {
|
|
|
603
441
|
if (sum !== undefined) resultData[i][j] = sum;
|
|
604
442
|
}
|
|
605
443
|
}
|
|
606
|
-
|
|
607
444
|
return new Matrix(resultData, {
|
|
608
445
|
rows: this.rows,
|
|
609
446
|
cols: matrix.cols,
|
|
@@ -617,75 +454,31 @@ export class Matrix {
|
|
|
617
454
|
* The transpose function takes a matrix and returns a new matrix that is the transpose of the
|
|
618
455
|
* original matrix.
|
|
619
456
|
* @returns The transpose() function returns a new Matrix object with the transposed data.
|
|
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
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
* @example
|
|
663
|
-
* // Matrix transpose (square matrix)
|
|
664
|
-
* const m = new Matrix([
|
|
665
|
-
* [1, 2, 3],
|
|
666
|
-
* [4, 5, 6],
|
|
667
|
-
* [7, 8, 9]
|
|
668
|
-
* ]);
|
|
669
|
-
*
|
|
670
|
-
* const transposed = m.transpose();
|
|
671
|
-
* console.log(transposed.rows); // 3;
|
|
672
|
-
* console.log(transposed.cols); // 3;
|
|
673
|
-
* console.log(transposed.data); // [
|
|
674
|
-
* // [1, 4, 7],
|
|
675
|
-
* // [2, 5, 8],
|
|
676
|
-
* // [3, 6, 9]
|
|
677
|
-
* // ];
|
|
678
|
-
*
|
|
679
|
-
* // Transpose of transpose = original
|
|
680
|
-
* 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;
|
|
681
476
|
*/
|
|
682
477
|
transpose(): Matrix {
|
|
683
478
|
if (this.data.some(row => row.length !== this.cols)) {
|
|
684
479
|
raise(Error, ERR.matrixNotRectangular());
|
|
685
480
|
}
|
|
686
|
-
|
|
687
481
|
const resultData: number[][] = [];
|
|
688
|
-
|
|
689
482
|
for (let j = 0; j < this.cols; j++) {
|
|
690
483
|
resultData[j] = [];
|
|
691
484
|
for (let i = 0; i < this.rows; i++) {
|
|
@@ -693,7 +486,6 @@ export class Matrix {
|
|
|
693
486
|
if (trans !== undefined) resultData[j][i] = trans;
|
|
694
487
|
}
|
|
695
488
|
}
|
|
696
|
-
|
|
697
489
|
return new Matrix(resultData, {
|
|
698
490
|
rows: this.cols,
|
|
699
491
|
cols: this.rows,
|
|
@@ -703,69 +495,31 @@ export class Matrix {
|
|
|
703
495
|
});
|
|
704
496
|
}
|
|
705
497
|
|
|
498
|
+
// ─── Standard interface ─────────────────────────────────────
|
|
499
|
+
|
|
706
500
|
/**
|
|
707
501
|
* The `inverse` function calculates the inverse of a square matrix using Gaussian elimination.
|
|
708
502
|
* @returns a Matrix object, which represents the inverse of the original matrix.
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
* @example
|
|
752
|
-
* // Compute the inverse of a 2x2 matrix
|
|
753
|
-
* const m = Matrix.from([[4, 7], [2, 6]]);
|
|
754
|
-
* const inv = m.inverse();
|
|
755
|
-
* console.log(inv); // defined;
|
|
756
|
-
* // A * A^-1 should ≈ Identity
|
|
757
|
-
* const product = m.multiply(inv!);
|
|
758
|
-
* console.log(product?.get(0, 0)); // toBeCloseTo;
|
|
759
|
-
* console.log(product?.get(0, 1)); // toBeCloseTo;
|
|
760
|
-
* console.log(product?.get(1, 0)); // toBeCloseTo;
|
|
761
|
-
* 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;
|
|
762
517
|
*/
|
|
763
518
|
inverse(): Matrix | undefined {
|
|
764
519
|
// Check if the matrix is square
|
|
765
520
|
if (this.rows !== this.cols) {
|
|
766
521
|
raise(Error, ERR.matrixNotSquare());
|
|
767
522
|
}
|
|
768
|
-
|
|
769
523
|
// Create an augmented matrix [this | I]
|
|
770
524
|
const augmentedMatrixData: number[][] = [];
|
|
771
525
|
for (let i = 0; i < this.rows; i++) {
|
|
@@ -774,7 +528,6 @@ export class Matrix {
|
|
|
774
528
|
augmentedMatrixData[i][this.cols + j] = i === j ? 1 : 0; // Append the identity matrix
|
|
775
529
|
}
|
|
776
530
|
}
|
|
777
|
-
|
|
778
531
|
const augmentedMatrix = new Matrix(augmentedMatrixData, {
|
|
779
532
|
rows: this.rows,
|
|
780
533
|
cols: this.cols * 2,
|
|
@@ -782,7 +535,6 @@ export class Matrix {
|
|
|
782
535
|
subtractFn: this.subtractFn,
|
|
783
536
|
multiplyFn: this.multiplyFn
|
|
784
537
|
});
|
|
785
|
-
|
|
786
538
|
// Apply Gaussian elimination to transform the left half into the identity matrix
|
|
787
539
|
for (let i = 0; i < this.rows; i++) {
|
|
788
540
|
// Find pivot
|
|
@@ -790,42 +542,33 @@ export class Matrix {
|
|
|
790
542
|
while (pivotRow < this.rows && augmentedMatrix.get(pivotRow, i) === 0) {
|
|
791
543
|
pivotRow++;
|
|
792
544
|
}
|
|
793
|
-
|
|
794
545
|
if (pivotRow === this.rows) {
|
|
795
546
|
// Matrix is singular, and its inverse does not exist
|
|
796
547
|
raise(Error, ERR.matrixSingular());
|
|
797
548
|
}
|
|
798
|
-
|
|
799
549
|
// Swap rows to make the pivot the current row
|
|
800
550
|
augmentedMatrix._swapRows(i, pivotRow);
|
|
801
|
-
|
|
802
551
|
// Scale the pivot row to make the pivot element 1
|
|
803
552
|
const pivotElement = augmentedMatrix.get(i, i) ?? 1;
|
|
804
|
-
|
|
805
553
|
if (pivotElement === 0) {
|
|
806
554
|
// Handle division by zero
|
|
807
555
|
raise(Error, ERR.matrixSingular());
|
|
808
556
|
}
|
|
809
|
-
|
|
810
557
|
augmentedMatrix._scaleRow(i, 1 / pivotElement);
|
|
811
|
-
|
|
812
558
|
// Eliminate other rows to make elements in the current column zero
|
|
813
559
|
for (let j = 0; j < this.rows; j++) {
|
|
814
560
|
if (j !== i) {
|
|
815
561
|
let factor = augmentedMatrix.get(j, i);
|
|
816
562
|
if (factor === undefined) factor = 0;
|
|
817
|
-
|
|
818
563
|
augmentedMatrix._addScaledRow(j, i, -factor);
|
|
819
564
|
}
|
|
820
565
|
}
|
|
821
566
|
}
|
|
822
|
-
|
|
823
567
|
// Extract the right half of the augmented matrix as the inverse
|
|
824
568
|
const inverseData: number[][] = [];
|
|
825
569
|
for (let i = 0; i < this.rows; i++) {
|
|
826
570
|
inverseData[i] = augmentedMatrix.data[i].slice(this.cols);
|
|
827
571
|
}
|
|
828
|
-
|
|
829
572
|
return new Matrix(inverseData, {
|
|
830
573
|
rows: this.rows,
|
|
831
574
|
cols: this.cols,
|
|
@@ -839,60 +582,26 @@ export class Matrix {
|
|
|
839
582
|
* The dot function calculates the dot product of two matrices and returns a new matrix.
|
|
840
583
|
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
|
|
841
584
|
* @returns a new Matrix object.
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
* @example
|
|
885
|
-
* // Dot product of two matrices
|
|
886
|
-
* const a = Matrix.from([[1, 2], [3, 4]]);
|
|
887
|
-
* const b = Matrix.from([[5, 6], [7, 8]]);
|
|
888
|
-
* const result = a.dot(b);
|
|
889
|
-
* 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
|
+
* // ];
|
|
890
600
|
*/
|
|
891
601
|
dot(matrix: Matrix): Matrix | undefined {
|
|
892
602
|
if (this.cols !== matrix.rows) {
|
|
893
603
|
raise(Error, ERR.matrixDimensionMismatch('dot product (A.cols must equal B.rows)'));
|
|
894
604
|
}
|
|
895
|
-
|
|
896
605
|
const resultData: number[][] = [];
|
|
897
606
|
for (let i = 0; i < this.rows; i++) {
|
|
898
607
|
resultData[i] = [];
|
|
@@ -911,7 +620,6 @@ export class Matrix {
|
|
|
911
620
|
if (sum !== undefined) resultData[i][j] = sum;
|
|
912
621
|
}
|
|
913
622
|
}
|
|
914
|
-
|
|
915
623
|
return new Matrix(resultData, {
|
|
916
624
|
rows: this.rows,
|
|
917
625
|
cols: matrix.cols,
|
|
@@ -952,15 +660,6 @@ export class Matrix {
|
|
|
952
660
|
);
|
|
953
661
|
}
|
|
954
662
|
|
|
955
|
-
// ─── Standard interface ─────────────────────────────────────
|
|
956
|
-
|
|
957
|
-
/**
|
|
958
|
-
* Returns [rows, cols] dimensions tuple.
|
|
959
|
-
*/
|
|
960
|
-
get size(): [number, number] {
|
|
961
|
-
return [this._rows, this._cols];
|
|
962
|
-
}
|
|
963
|
-
|
|
964
663
|
isEmpty(): boolean {
|
|
965
664
|
return this._rows === 0 || this._cols === 0;
|
|
966
665
|
}
|
|
@@ -1002,6 +701,8 @@ export class Matrix {
|
|
|
1002
701
|
};
|
|
1003
702
|
}
|
|
1004
703
|
|
|
704
|
+
// ─── Factory methods ────────────────────────────────────────
|
|
705
|
+
|
|
1005
706
|
/**
|
|
1006
707
|
* Visits each element with its row and column index.
|
|
1007
708
|
*/
|
|
@@ -1039,46 +740,6 @@ export class Matrix {
|
|
|
1039
740
|
}
|
|
1040
741
|
}
|
|
1041
742
|
|
|
1042
|
-
// ─── Factory methods ────────────────────────────────────────
|
|
1043
|
-
|
|
1044
|
-
/**
|
|
1045
|
-
* Creates a rows×cols zero matrix.
|
|
1046
|
-
* @example
|
|
1047
|
-
* ```ts
|
|
1048
|
-
* const z = Matrix.zeros(2, 3); // [[0,0,0],[0,0,0]]
|
|
1049
|
-
* ```
|
|
1050
|
-
*/
|
|
1051
|
-
static zeros(rows: number, cols: number): Matrix {
|
|
1052
|
-
const data: number[][] = Array.from({ length: rows }, () => new Array(cols).fill(0));
|
|
1053
|
-
return new Matrix(data);
|
|
1054
|
-
}
|
|
1055
|
-
|
|
1056
|
-
/**
|
|
1057
|
-
* Creates an n×n identity matrix.
|
|
1058
|
-
* @example
|
|
1059
|
-
* ```ts
|
|
1060
|
-
* const I = Matrix.identity(3); // [[1,0,0],[0,1,0],[0,0,1]]
|
|
1061
|
-
* ```
|
|
1062
|
-
*/
|
|
1063
|
-
static identity(n: number): Matrix {
|
|
1064
|
-
const data: number[][] = Array.from({ length: n }, (_, i) =>
|
|
1065
|
-
Array.from({ length: n }, (_, j) => (i === j ? 1 : 0))
|
|
1066
|
-
);
|
|
1067
|
-
return new Matrix(data);
|
|
1068
|
-
}
|
|
1069
|
-
|
|
1070
|
-
/**
|
|
1071
|
-
* Creates a Matrix from a plain 2D array (deep copy).
|
|
1072
|
-
* @example
|
|
1073
|
-
* ```ts
|
|
1074
|
-
* const m = Matrix.from([[1, 2], [3, 4]]);
|
|
1075
|
-
* m.get(0, 1); // 2
|
|
1076
|
-
* ```
|
|
1077
|
-
*/
|
|
1078
|
-
static from(data: number[][]): Matrix {
|
|
1079
|
-
return new Matrix(data.map(row => [...row]));
|
|
1080
|
-
}
|
|
1081
|
-
|
|
1082
743
|
protected _addFn(a: number | undefined, b: number): number | undefined {
|
|
1083
744
|
if (a === undefined) return b;
|
|
1084
745
|
return a + b;
|