data-structure-typed 2.6.1 → 2.6.2
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/CHANGELOG.md +9 -1
- package/dist/cjs/binary-tree.cjs +2927 -23688
- package/dist/cjs/graph.cjs +845 -2634
- package/dist/cjs/hash.cjs +185 -616
- package/dist/cjs/heap.cjs +266 -818
- package/dist/cjs/index.cjs +5116 -31358
- package/dist/cjs/linked-list.cjs +644 -2615
- package/dist/cjs/matrix.cjs +220 -535
- package/dist/cjs/priority-queue.cjs +266 -818
- package/dist/cjs/queue.cjs +637 -2343
- package/dist/cjs/stack.cjs +124 -530
- package/dist/cjs/trie.cjs +145 -592
- package/dist/cjs-legacy/binary-tree.cjs +4352 -25113
- package/dist/cjs-legacy/graph.cjs +847 -2636
- package/dist/cjs-legacy/hash.cjs +181 -612
- package/dist/cjs-legacy/heap.cjs +266 -818
- package/dist/cjs-legacy/index.cjs +6657 -32899
- package/dist/cjs-legacy/linked-list.cjs +640 -2611
- package/dist/cjs-legacy/matrix.cjs +220 -535
- package/dist/cjs-legacy/priority-queue.cjs +266 -818
- package/dist/cjs-legacy/queue.cjs +634 -2340
- package/dist/cjs-legacy/stack.cjs +124 -530
- package/dist/cjs-legacy/trie.cjs +145 -592
- package/dist/esm/binary-tree.mjs +2927 -23688
- package/dist/esm/graph.mjs +845 -2634
- package/dist/esm/hash.mjs +185 -616
- package/dist/esm/heap.mjs +266 -818
- package/dist/esm/index.mjs +5116 -31358
- package/dist/esm/linked-list.mjs +644 -2615
- package/dist/esm/matrix.mjs +220 -535
- package/dist/esm/priority-queue.mjs +266 -818
- package/dist/esm/queue.mjs +637 -2343
- package/dist/esm/stack.mjs +124 -530
- package/dist/esm/trie.mjs +145 -592
- package/dist/esm-legacy/binary-tree.mjs +4352 -25113
- package/dist/esm-legacy/graph.mjs +847 -2636
- package/dist/esm-legacy/hash.mjs +181 -612
- package/dist/esm-legacy/heap.mjs +266 -818
- package/dist/esm-legacy/index.mjs +6657 -32899
- package/dist/esm-legacy/linked-list.mjs +640 -2611
- package/dist/esm-legacy/matrix.mjs +220 -535
- package/dist/esm-legacy/priority-queue.mjs +266 -818
- package/dist/esm-legacy/queue.mjs +634 -2340
- package/dist/esm-legacy/stack.mjs +124 -530
- package/dist/esm-legacy/trie.mjs +145 -592
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +29 -500
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +38 -563
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +230 -1212
- package/dist/types/data-structures/binary-tree/bst.d.ts +124 -1063
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +99 -854
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +62 -314
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +366 -5057
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +277 -4885
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +253 -3929
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +261 -4782
- package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -44
- package/dist/types/data-structures/graph/directed-graph.d.ts +130 -527
- package/dist/types/data-structures/graph/undirected-graph.d.ts +125 -482
- package/dist/types/data-structures/hash/hash-map.d.ts +132 -562
- package/dist/types/data-structures/heap/heap.d.ts +194 -746
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +120 -809
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +121 -733
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +193 -863
- package/dist/types/data-structures/matrix/matrix.d.ts +159 -474
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -6
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +6 -11
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/types/data-structures/queue/deque.d.ts +196 -804
- package/dist/types/data-structures/queue/queue.d.ts +99 -585
- package/dist/types/data-structures/stack/stack.d.ts +75 -481
- package/dist/types/data-structures/trie/trie.d.ts +98 -584
- package/dist/umd/data-structure-typed.js +6580 -32822
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/docs-site-docusaurus/docs/api/classes/AVLTree.md +162 -215
- package/docs-site-docusaurus/docs/api/classes/AVLTreeNode.md +9 -9
- package/docs-site-docusaurus/docs/api/classes/AbstractGraph.md +47 -47
- package/docs-site-docusaurus/docs/api/classes/BST.md +155 -208
- package/docs-site-docusaurus/docs/api/classes/BSTNode.md +7 -7
- package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +13 -29
- package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +104 -158
- package/docs-site-docusaurus/docs/api/classes/BinaryTreeNode.md +13 -13
- package/docs-site-docusaurus/docs/api/classes/Deque.md +72 -104
- package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +60 -80
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +67 -101
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedListNode.md +6 -6
- 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 +52 -48
- package/docs-site-docusaurus/docs/api/classes/Heap.md +40 -68
- package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +17 -17
- package/docs-site-docusaurus/docs/api/classes/LinearBase.md +38 -38
- package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +45 -45
- package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +23 -23
- package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +68 -96
- package/docs-site-docusaurus/docs/api/classes/MapGraph.md +60 -80
- package/docs-site-docusaurus/docs/api/classes/Matrix.md +57 -52
- package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +38 -66
- package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +41 -74
- package/docs-site-docusaurus/docs/api/classes/MinHeap.md +38 -66
- package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +46 -79
- package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +40 -68
- package/docs-site-docusaurus/docs/api/classes/Queue.md +54 -78
- package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +171 -225
- package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +10 -22
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +68 -98
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedListNode.md +4 -4
- package/docs-site-docusaurus/docs/api/classes/SkipList.md +98 -75
- package/docs-site-docusaurus/docs/api/classes/Stack.md +33 -53
- package/docs-site-docusaurus/docs/api/classes/TreeMap.md +104 -129
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +57 -124
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +45 -116
- package/docs-site-docusaurus/docs/api/classes/Trie.md +37 -61
- package/docs-site-docusaurus/docs/api/classes/TrieNode.md +8 -8
- package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +59 -77
- package/package.json +1 -1
- package/src/data-structures/binary-tree/tree-multi-set.ts +15 -11
package/dist/esm/matrix.mjs
CHANGED
|
@@ -113,138 +113,96 @@ var Matrix = class _Matrix {
|
|
|
113
113
|
return this._multiplyFn;
|
|
114
114
|
}
|
|
115
115
|
/**
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
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
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
116
|
+
* Returns [rows, cols] dimensions tuple.
|
|
117
|
+
*/
|
|
118
|
+
get size() {
|
|
119
|
+
return [this._rows, this._cols];
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Creates a rows×cols zero matrix.
|
|
123
|
+
* @example
|
|
124
|
+
* ```ts
|
|
125
|
+
* const z = Matrix.zeros(2, 3); // [[0,0,0],[0,0,0]]
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
static zeros(rows, cols) {
|
|
129
|
+
const data = Array.from({ length: rows }, () => new Array(cols).fill(0));
|
|
130
|
+
return new _Matrix(data);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Creates an n×n identity matrix.
|
|
134
|
+
* @example
|
|
135
|
+
* ```ts
|
|
136
|
+
* const I = Matrix.identity(3); // [[1,0,0],[0,1,0],[0,0,1]]
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
static identity(n) {
|
|
140
|
+
const data = Array.from(
|
|
141
|
+
{ length: n },
|
|
142
|
+
(_, i) => Array.from({ length: n }, (_2, j) => i === j ? 1 : 0)
|
|
143
|
+
);
|
|
144
|
+
return new _Matrix(data);
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Creates a Matrix from a plain 2D array (deep copy).
|
|
148
|
+
* @example
|
|
149
|
+
* ```ts
|
|
150
|
+
* const m = Matrix.from([[1, 2], [3, 4]]);
|
|
151
|
+
* m.get(0, 1); // 2
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
static from(data) {
|
|
155
|
+
return new _Matrix(data.map((row) => [...row]));
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* The `get` function returns the value at the specified row and column index if it is a valid index.
|
|
159
|
+
* @param {number} row - The `row` parameter represents the row index of the element you want to
|
|
160
|
+
* retrieve from the data array.
|
|
161
|
+
* @param {number} col - The parameter "col" represents the column number of the element you want to
|
|
162
|
+
* retrieve from the data array.
|
|
163
|
+
* @returns The `get` function returns a number if the provided row and column indices are valid.
|
|
164
|
+
* Otherwise, it returns `undefined`.
|
|
165
|
+
* @example
|
|
166
|
+
* // Get and set individual cells
|
|
167
|
+
* const m = new Matrix([
|
|
168
|
+
* [0, 0, 0],
|
|
169
|
+
* [0, 0, 0]
|
|
170
|
+
* ]);
|
|
171
|
+
*
|
|
172
|
+
* m.set(0, 1, 42);
|
|
173
|
+
* m.set(1, 2, 99);
|
|
174
|
+
*
|
|
175
|
+
* console.log(m.get(0, 1)); // 42;
|
|
176
|
+
* console.log(m.get(1, 2)); // 99;
|
|
177
|
+
* console.log(m.get(0, 0)); // 0;
|
|
178
|
+
*
|
|
179
|
+
* // Out of bounds returns undefined
|
|
180
|
+
* console.log(m.get(5, 5)); // undefined;
|
|
181
|
+
*/
|
|
182
182
|
get(row, col) {
|
|
183
183
|
if (this.isValidIndex(row, col)) {
|
|
184
184
|
return this.data[row][col];
|
|
185
185
|
}
|
|
186
186
|
}
|
|
187
187
|
/**
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
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
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
* @example
|
|
241
|
-
* // Modify individual cells
|
|
242
|
-
* const m = Matrix.zeros(2, 2);
|
|
243
|
-
* console.log(m.set(0, 0, 5)); // true;
|
|
244
|
-
* console.log(m.set(1, 1, 10)); // true;
|
|
245
|
-
* console.log(m.get(0, 0)); // 5;
|
|
246
|
-
* console.log(m.get(1, 1)); // 10;
|
|
247
|
-
*/
|
|
188
|
+
* The set function updates the value at a specified row and column in a two-dimensional array.
|
|
189
|
+
* @param {number} row - The "row" parameter represents the row index of the element in a
|
|
190
|
+
* two-dimensional array or matrix. It specifies the row where the value will be set.
|
|
191
|
+
* @param {number} col - The "col" parameter represents the column index of the element in a
|
|
192
|
+
* two-dimensional array.
|
|
193
|
+
* @param {number} value - The value parameter represents the number that you want to set at the
|
|
194
|
+
* specified row and column in the data array.
|
|
195
|
+
* @returns a boolean value. It returns true if the index (row, col) is valid and the value is
|
|
196
|
+
* successfully set in the data array. It returns false if the index is invalid and the value is not
|
|
197
|
+
* set.
|
|
198
|
+
* @example
|
|
199
|
+
* // Modify individual cells
|
|
200
|
+
* const m = Matrix.zeros(2, 2);
|
|
201
|
+
* console.log(m.set(0, 0, 5)); // true;
|
|
202
|
+
* console.log(m.set(1, 1, 10)); // true;
|
|
203
|
+
* console.log(m.get(0, 0)); // 5;
|
|
204
|
+
* console.log(m.get(1, 1)); // 10;
|
|
205
|
+
*/
|
|
248
206
|
set(row, col, value) {
|
|
249
207
|
if (this.isValidIndex(row, col)) {
|
|
250
208
|
this.data[row][col] = value;
|
|
@@ -262,75 +220,33 @@ var Matrix = class _Matrix {
|
|
|
262
220
|
return this.rows === matrix.rows && this.cols === matrix.cols;
|
|
263
221
|
}
|
|
264
222
|
/**
|
|
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
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
* @example
|
|
312
|
-
* // Basic matrix arithmetic
|
|
313
|
-
* const a = new Matrix([
|
|
314
|
-
* [1, 2],
|
|
315
|
-
* [3, 4]
|
|
316
|
-
* ]);
|
|
317
|
-
* const b = new Matrix([
|
|
318
|
-
* [5, 6],
|
|
319
|
-
* [7, 8]
|
|
320
|
-
* ]);
|
|
321
|
-
*
|
|
322
|
-
* const sum = a.add(b);
|
|
323
|
-
* console.log(sum?.data); // [
|
|
324
|
-
* // [6, 8],
|
|
325
|
-
* // [10, 12]
|
|
326
|
-
* // ];
|
|
327
|
-
*
|
|
328
|
-
* const diff = b.subtract(a);
|
|
329
|
-
* console.log(diff?.data); // [
|
|
330
|
-
* // [4, 4],
|
|
331
|
-
* // [4, 4]
|
|
332
|
-
* // ];
|
|
333
|
-
*/
|
|
223
|
+
* The `add` function adds two matrices together, returning a new matrix with the result.
|
|
224
|
+
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
|
|
225
|
+
* @returns The `add` method returns a new `Matrix` object that represents the result of adding the
|
|
226
|
+
* current matrix with the provided `matrix` parameter.
|
|
227
|
+
* @example
|
|
228
|
+
* // Basic matrix arithmetic
|
|
229
|
+
* const a = new Matrix([
|
|
230
|
+
* [1, 2],
|
|
231
|
+
* [3, 4]
|
|
232
|
+
* ]);
|
|
233
|
+
* const b = new Matrix([
|
|
234
|
+
* [5, 6],
|
|
235
|
+
* [7, 8]
|
|
236
|
+
* ]);
|
|
237
|
+
*
|
|
238
|
+
* const sum = a.add(b);
|
|
239
|
+
* console.log(sum?.data); // [
|
|
240
|
+
* // [6, 8],
|
|
241
|
+
* // [10, 12]
|
|
242
|
+
* // ];
|
|
243
|
+
*
|
|
244
|
+
* const diff = b.subtract(a);
|
|
245
|
+
* console.log(diff?.data); // [
|
|
246
|
+
* // [4, 4],
|
|
247
|
+
* // [4, 4]
|
|
248
|
+
* // ];
|
|
249
|
+
*/
|
|
334
250
|
add(matrix) {
|
|
335
251
|
if (!this.isMatchForCalculate(matrix)) {
|
|
336
252
|
raise(Error, ERR.matrixDimensionMismatch("addition"));
|
|
@@ -354,60 +270,27 @@ var Matrix = class _Matrix {
|
|
|
354
270
|
});
|
|
355
271
|
}
|
|
356
272
|
/**
|
|
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
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
* @example
|
|
405
|
-
* // Element-wise subtraction
|
|
406
|
-
* const a = Matrix.from([[5, 6], [7, 8]]);
|
|
407
|
-
* const b = Matrix.from([[1, 2], [3, 4]]);
|
|
408
|
-
* const result = a.subtract(b);
|
|
409
|
-
* console.log(result?.toArray()); // [[4, 4], [4, 4]];
|
|
410
|
-
*/
|
|
273
|
+
* The `subtract` function performs element-wise subtraction between two matrices and returns a new
|
|
274
|
+
* matrix with the result.
|
|
275
|
+
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class. It
|
|
276
|
+
* represents the matrix that you want to subtract from the current matrix.
|
|
277
|
+
* @returns a new Matrix object with the result of the subtraction operation.
|
|
278
|
+
* @example
|
|
279
|
+
* // Element-wise subtraction
|
|
280
|
+
* const a = Matrix.from([
|
|
281
|
+
* [5, 6],
|
|
282
|
+
* [7, 8]
|
|
283
|
+
* ]);
|
|
284
|
+
* const b = Matrix.from([
|
|
285
|
+
* [1, 2],
|
|
286
|
+
* [3, 4]
|
|
287
|
+
* ]);
|
|
288
|
+
* const result = a.subtract(b);
|
|
289
|
+
* console.log(result?.toArray()); // [
|
|
290
|
+
* // [4, 4],
|
|
291
|
+
* // [4, 4]
|
|
292
|
+
* // ];
|
|
293
|
+
*/
|
|
411
294
|
subtract(matrix) {
|
|
412
295
|
if (!this.isMatchForCalculate(matrix)) {
|
|
413
296
|
raise(Error, ERR.matrixDimensionMismatch("subtraction"));
|
|
@@ -431,75 +314,33 @@ var Matrix = class _Matrix {
|
|
|
431
314
|
});
|
|
432
315
|
}
|
|
433
316
|
/**
|
|
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
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
* @example
|
|
481
|
-
* // Matrix multiplication for transformations
|
|
482
|
-
* // 2x3 matrix * 3x2 matrix = 2x2 matrix
|
|
483
|
-
* const a = new Matrix([
|
|
484
|
-
* [1, 2, 3],
|
|
485
|
-
* [4, 5, 6]
|
|
486
|
-
* ]);
|
|
487
|
-
* const b = new Matrix([
|
|
488
|
-
* [7, 8],
|
|
489
|
-
* [9, 10],
|
|
490
|
-
* [11, 12]
|
|
491
|
-
* ]);
|
|
492
|
-
*
|
|
493
|
-
* const product = a.multiply(b);
|
|
494
|
-
* console.log(product?.rows); // 2;
|
|
495
|
-
* console.log(product?.cols); // 2;
|
|
496
|
-
* // Row 0: 1*7+2*9+3*11=58, 1*8+2*10+3*12=64
|
|
497
|
-
* // Row 1: 4*7+5*9+6*11=139, 4*8+5*10+6*12=154
|
|
498
|
-
* console.log(product?.data); // [
|
|
499
|
-
* // [58, 64],
|
|
500
|
-
* // [139, 154]
|
|
501
|
-
* // ];
|
|
502
|
-
*/
|
|
317
|
+
* The `multiply` function performs matrix multiplication between two matrices and returns the result
|
|
318
|
+
* as a new matrix.
|
|
319
|
+
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
|
|
320
|
+
* @returns a new Matrix object.
|
|
321
|
+
* @example
|
|
322
|
+
* // Matrix multiplication for transformations
|
|
323
|
+
* // 2x3 matrix * 3x2 matrix = 2x2 matrix
|
|
324
|
+
* const a = new Matrix([
|
|
325
|
+
* [1, 2, 3],
|
|
326
|
+
* [4, 5, 6]
|
|
327
|
+
* ]);
|
|
328
|
+
* const b = new Matrix([
|
|
329
|
+
* [7, 8],
|
|
330
|
+
* [9, 10],
|
|
331
|
+
* [11, 12]
|
|
332
|
+
* ]);
|
|
333
|
+
*
|
|
334
|
+
* const product = a.multiply(b);
|
|
335
|
+
* console.log(product?.rows); // 2;
|
|
336
|
+
* console.log(product?.cols); // 2;
|
|
337
|
+
* // Row 0: 1*7+2*9+3*11=58, 1*8+2*10+3*12=64
|
|
338
|
+
* // Row 1: 4*7+5*9+6*11=139, 4*8+5*10+6*12=154
|
|
339
|
+
* console.log(product?.data); // [
|
|
340
|
+
* // [58, 64],
|
|
341
|
+
* // [139, 154]
|
|
342
|
+
* // ];
|
|
343
|
+
*/
|
|
503
344
|
multiply(matrix) {
|
|
504
345
|
if (this.cols !== matrix.rows) {
|
|
505
346
|
raise(Error, ERR.matrixDimensionMismatch("multiplication (A.cols must equal B.rows)"));
|
|
@@ -530,71 +371,29 @@ var Matrix = class _Matrix {
|
|
|
530
371
|
});
|
|
531
372
|
}
|
|
532
373
|
/**
|
|
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
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
* @example
|
|
579
|
-
* // Matrix transpose (square matrix)
|
|
580
|
-
* const m = new Matrix([
|
|
581
|
-
* [1, 2, 3],
|
|
582
|
-
* [4, 5, 6],
|
|
583
|
-
* [7, 8, 9]
|
|
584
|
-
* ]);
|
|
585
|
-
*
|
|
586
|
-
* const transposed = m.transpose();
|
|
587
|
-
* console.log(transposed.rows); // 3;
|
|
588
|
-
* console.log(transposed.cols); // 3;
|
|
589
|
-
* console.log(transposed.data); // [
|
|
590
|
-
* // [1, 4, 7],
|
|
591
|
-
* // [2, 5, 8],
|
|
592
|
-
* // [3, 6, 9]
|
|
593
|
-
* // ];
|
|
594
|
-
*
|
|
595
|
-
* // Transpose of transpose = original
|
|
596
|
-
* console.log(transposed.transpose().data); // m.data;
|
|
597
|
-
*/
|
|
374
|
+
* The transpose function takes a matrix and returns a new matrix that is the transpose of the
|
|
375
|
+
* original matrix.
|
|
376
|
+
* @returns The transpose() function returns a new Matrix object with the transposed data.
|
|
377
|
+
* @example
|
|
378
|
+
* // Matrix transpose (square matrix)
|
|
379
|
+
* const m = new Matrix([
|
|
380
|
+
* [1, 2, 3],
|
|
381
|
+
* [4, 5, 6],
|
|
382
|
+
* [7, 8, 9]
|
|
383
|
+
* ]);
|
|
384
|
+
*
|
|
385
|
+
* const transposed = m.transpose();
|
|
386
|
+
* console.log(transposed.rows); // 3;
|
|
387
|
+
* console.log(transposed.cols); // 3;
|
|
388
|
+
* console.log(transposed.data); // [
|
|
389
|
+
* // [1, 4, 7],
|
|
390
|
+
* // [2, 5, 8],
|
|
391
|
+
* // [3, 6, 9]
|
|
392
|
+
* // ];
|
|
393
|
+
*
|
|
394
|
+
* // Transpose of transpose = original
|
|
395
|
+
* console.log(transposed.transpose().data); // m.data;
|
|
396
|
+
*/
|
|
598
397
|
transpose() {
|
|
599
398
|
if (this.data.some((row) => row.length !== this.cols)) {
|
|
600
399
|
raise(Error, ERR.matrixNotRectangular());
|
|
@@ -615,63 +414,25 @@ var Matrix = class _Matrix {
|
|
|
615
414
|
multiplyFn: this.multiplyFn
|
|
616
415
|
});
|
|
617
416
|
}
|
|
417
|
+
// ─── Standard interface ─────────────────────────────────────
|
|
618
418
|
/**
|
|
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
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
* @example
|
|
664
|
-
* // Compute the inverse of a 2x2 matrix
|
|
665
|
-
* const m = Matrix.from([[4, 7], [2, 6]]);
|
|
666
|
-
* const inv = m.inverse();
|
|
667
|
-
* console.log(inv); // defined;
|
|
668
|
-
* // A * A^-1 should ≈ Identity
|
|
669
|
-
* const product = m.multiply(inv!);
|
|
670
|
-
* console.log(product?.get(0, 0)); // toBeCloseTo;
|
|
671
|
-
* console.log(product?.get(0, 1)); // toBeCloseTo;
|
|
672
|
-
* console.log(product?.get(1, 0)); // toBeCloseTo;
|
|
673
|
-
* console.log(product?.get(1, 1)); // toBeCloseTo;
|
|
674
|
-
*/
|
|
419
|
+
* The `inverse` function calculates the inverse of a square matrix using Gaussian elimination.
|
|
420
|
+
* @returns a Matrix object, which represents the inverse of the original matrix.
|
|
421
|
+
* @example
|
|
422
|
+
* // Compute the inverse of a 2x2 matrix
|
|
423
|
+
* const m = Matrix.from([
|
|
424
|
+
* [4, 7],
|
|
425
|
+
* [2, 6]
|
|
426
|
+
* ]);
|
|
427
|
+
* const inv = m.inverse();
|
|
428
|
+
* console.log(inv); // defined;
|
|
429
|
+
* // A * A^-1 should ≈ Identity
|
|
430
|
+
* const product = m.multiply(inv!);
|
|
431
|
+
* console.log(product?.get(0, 0)); // toBeCloseTo;
|
|
432
|
+
* console.log(product?.get(0, 1)); // toBeCloseTo;
|
|
433
|
+
* console.log(product?.get(1, 0)); // toBeCloseTo;
|
|
434
|
+
* console.log(product?.get(1, 1)); // toBeCloseTo;
|
|
435
|
+
*/
|
|
675
436
|
inverse() {
|
|
676
437
|
if (this.rows !== this.cols) {
|
|
677
438
|
raise(Error, ERR.matrixNotSquare());
|
|
@@ -725,58 +486,25 @@ var Matrix = class _Matrix {
|
|
|
725
486
|
});
|
|
726
487
|
}
|
|
727
488
|
/**
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
* @example
|
|
774
|
-
* // Dot product of two matrices
|
|
775
|
-
* const a = Matrix.from([[1, 2], [3, 4]]);
|
|
776
|
-
* const b = Matrix.from([[5, 6], [7, 8]]);
|
|
777
|
-
* const result = a.dot(b);
|
|
778
|
-
* console.log(result?.toArray()); // [[19, 22], [43, 50]];
|
|
779
|
-
*/
|
|
489
|
+
* The dot function calculates the dot product of two matrices and returns a new matrix.
|
|
490
|
+
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
|
|
491
|
+
* @returns a new Matrix object.
|
|
492
|
+
* @example
|
|
493
|
+
* // Dot product of two matrices
|
|
494
|
+
* const a = Matrix.from([
|
|
495
|
+
* [1, 2],
|
|
496
|
+
* [3, 4]
|
|
497
|
+
* ]);
|
|
498
|
+
* const b = Matrix.from([
|
|
499
|
+
* [5, 6],
|
|
500
|
+
* [7, 8]
|
|
501
|
+
* ]);
|
|
502
|
+
* const result = a.dot(b);
|
|
503
|
+
* console.log(result?.toArray()); // [
|
|
504
|
+
* // [19, 22],
|
|
505
|
+
* // [43, 50]
|
|
506
|
+
* // ];
|
|
507
|
+
*/
|
|
780
508
|
dot(matrix) {
|
|
781
509
|
if (this.cols !== matrix.rows) {
|
|
782
510
|
raise(Error, ERR.matrixDimensionMismatch("dot product (A.cols must equal B.rows)"));
|
|
@@ -835,13 +563,6 @@ var Matrix = class _Matrix {
|
|
|
835
563
|
}
|
|
836
564
|
);
|
|
837
565
|
}
|
|
838
|
-
// ─── Standard interface ─────────────────────────────────────
|
|
839
|
-
/**
|
|
840
|
-
* Returns [rows, cols] dimensions tuple.
|
|
841
|
-
*/
|
|
842
|
-
get size() {
|
|
843
|
-
return [this._rows, this._cols];
|
|
844
|
-
}
|
|
845
566
|
isEmpty() {
|
|
846
567
|
return this._rows === 0 || this._cols === 0;
|
|
847
568
|
}
|
|
@@ -879,6 +600,7 @@ var Matrix = class _Matrix {
|
|
|
879
600
|
}
|
|
880
601
|
};
|
|
881
602
|
}
|
|
603
|
+
// ─── Factory methods ────────────────────────────────────────
|
|
882
604
|
/**
|
|
883
605
|
* Visits each element with its row and column index.
|
|
884
606
|
*/
|
|
@@ -913,43 +635,6 @@ var Matrix = class _Matrix {
|
|
|
913
635
|
console.log(row.join(" "));
|
|
914
636
|
}
|
|
915
637
|
}
|
|
916
|
-
// ─── Factory methods ────────────────────────────────────────
|
|
917
|
-
/**
|
|
918
|
-
* Creates a rows×cols zero matrix.
|
|
919
|
-
* @example
|
|
920
|
-
* ```ts
|
|
921
|
-
* const z = Matrix.zeros(2, 3); // [[0,0,0],[0,0,0]]
|
|
922
|
-
* ```
|
|
923
|
-
*/
|
|
924
|
-
static zeros(rows, cols) {
|
|
925
|
-
const data = Array.from({ length: rows }, () => new Array(cols).fill(0));
|
|
926
|
-
return new _Matrix(data);
|
|
927
|
-
}
|
|
928
|
-
/**
|
|
929
|
-
* Creates an n×n identity matrix.
|
|
930
|
-
* @example
|
|
931
|
-
* ```ts
|
|
932
|
-
* const I = Matrix.identity(3); // [[1,0,0],[0,1,0],[0,0,1]]
|
|
933
|
-
* ```
|
|
934
|
-
*/
|
|
935
|
-
static identity(n) {
|
|
936
|
-
const data = Array.from(
|
|
937
|
-
{ length: n },
|
|
938
|
-
(_, i) => Array.from({ length: n }, (_2, j) => i === j ? 1 : 0)
|
|
939
|
-
);
|
|
940
|
-
return new _Matrix(data);
|
|
941
|
-
}
|
|
942
|
-
/**
|
|
943
|
-
* Creates a Matrix from a plain 2D array (deep copy).
|
|
944
|
-
* @example
|
|
945
|
-
* ```ts
|
|
946
|
-
* const m = Matrix.from([[1, 2], [3, 4]]);
|
|
947
|
-
* m.get(0, 1); // 2
|
|
948
|
-
* ```
|
|
949
|
-
*/
|
|
950
|
-
static from(data) {
|
|
951
|
-
return new _Matrix(data.map((row) => [...row]));
|
|
952
|
-
}
|
|
953
638
|
_addFn(a, b) {
|
|
954
639
|
if (a === void 0) return b;
|
|
955
640
|
return a + b;
|