data-structure-typed 2.6.0 → 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/.github/workflows/ci.yml +7 -2
- package/.github/workflows/release-package.yml +9 -2
- 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 +253 -101
- package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +60 -80
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +244 -114
- 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 +119 -64
- package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +84 -14
- package/docs-site-docusaurus/docs/api/classes/LinearBase.md +138 -34
- package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +149 -41
- package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +23 -23
- package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +172 -92
- 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 +117 -62
- package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +120 -70
- package/docs-site-docusaurus/docs/api/classes/MinHeap.md +117 -62
- package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +125 -75
- package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +119 -64
- package/docs-site-docusaurus/docs/api/classes/Queue.md +158 -74
- 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 +172 -94
- 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 +112 -50
- package/docs-site-docusaurus/docs/api/classes/TreeMap.md +104 -129
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +90 -121
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +298 -107
- package/docs-site-docusaurus/docs/api/classes/Trie.md +116 -58
- 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 +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 +299 -3983
- 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
package/dist/cjs/matrix.cjs
CHANGED
|
@@ -115,138 +115,96 @@ var Matrix = class _Matrix {
|
|
|
115
115
|
return this._multiplyFn;
|
|
116
116
|
}
|
|
117
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
|
-
|
|
182
|
-
|
|
183
|
-
|
|
118
|
+
* Returns [rows, cols] dimensions tuple.
|
|
119
|
+
*/
|
|
120
|
+
get size() {
|
|
121
|
+
return [this._rows, this._cols];
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Creates a rows×cols zero matrix.
|
|
125
|
+
* @example
|
|
126
|
+
* ```ts
|
|
127
|
+
* const z = Matrix.zeros(2, 3); // [[0,0,0],[0,0,0]]
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
static zeros(rows, cols) {
|
|
131
|
+
const data = Array.from({ length: rows }, () => new Array(cols).fill(0));
|
|
132
|
+
return new _Matrix(data);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Creates an n×n identity matrix.
|
|
136
|
+
* @example
|
|
137
|
+
* ```ts
|
|
138
|
+
* const I = Matrix.identity(3); // [[1,0,0],[0,1,0],[0,0,1]]
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
static identity(n) {
|
|
142
|
+
const data = Array.from(
|
|
143
|
+
{ length: n },
|
|
144
|
+
(_, i) => Array.from({ length: n }, (_2, j) => i === j ? 1 : 0)
|
|
145
|
+
);
|
|
146
|
+
return new _Matrix(data);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Creates a Matrix from a plain 2D array (deep copy).
|
|
150
|
+
* @example
|
|
151
|
+
* ```ts
|
|
152
|
+
* const m = Matrix.from([[1, 2], [3, 4]]);
|
|
153
|
+
* m.get(0, 1); // 2
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
156
|
+
static from(data) {
|
|
157
|
+
return new _Matrix(data.map((row) => [...row]));
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* The `get` function returns the value at the specified row and column index if it is a valid index.
|
|
161
|
+
* @param {number} row - The `row` parameter represents the row index of the element you want to
|
|
162
|
+
* retrieve from the data array.
|
|
163
|
+
* @param {number} col - The parameter "col" represents the column number of the element you want to
|
|
164
|
+
* retrieve from the data array.
|
|
165
|
+
* @returns The `get` function returns a number if the provided row and column indices are valid.
|
|
166
|
+
* Otherwise, it returns `undefined`.
|
|
167
|
+
* @example
|
|
168
|
+
* // Get and set individual cells
|
|
169
|
+
* const m = new Matrix([
|
|
170
|
+
* [0, 0, 0],
|
|
171
|
+
* [0, 0, 0]
|
|
172
|
+
* ]);
|
|
173
|
+
*
|
|
174
|
+
* m.set(0, 1, 42);
|
|
175
|
+
* m.set(1, 2, 99);
|
|
176
|
+
*
|
|
177
|
+
* console.log(m.get(0, 1)); // 42;
|
|
178
|
+
* console.log(m.get(1, 2)); // 99;
|
|
179
|
+
* console.log(m.get(0, 0)); // 0;
|
|
180
|
+
*
|
|
181
|
+
* // Out of bounds returns undefined
|
|
182
|
+
* console.log(m.get(5, 5)); // undefined;
|
|
183
|
+
*/
|
|
184
184
|
get(row, col) {
|
|
185
185
|
if (this.isValidIndex(row, col)) {
|
|
186
186
|
return this.data[row][col];
|
|
187
187
|
}
|
|
188
188
|
}
|
|
189
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
|
-
|
|
241
|
-
|
|
242
|
-
* @example
|
|
243
|
-
* // Modify individual cells
|
|
244
|
-
* const m = Matrix.zeros(2, 2);
|
|
245
|
-
* console.log(m.set(0, 0, 5)); // true;
|
|
246
|
-
* console.log(m.set(1, 1, 10)); // true;
|
|
247
|
-
* console.log(m.get(0, 0)); // 5;
|
|
248
|
-
* console.log(m.get(1, 1)); // 10;
|
|
249
|
-
*/
|
|
190
|
+
* The set function updates the value at a specified row and column in a two-dimensional array.
|
|
191
|
+
* @param {number} row - The "row" parameter represents the row index of the element in a
|
|
192
|
+
* two-dimensional array or matrix. It specifies the row where the value will be set.
|
|
193
|
+
* @param {number} col - The "col" parameter represents the column index of the element in a
|
|
194
|
+
* two-dimensional array.
|
|
195
|
+
* @param {number} value - The value parameter represents the number that you want to set at the
|
|
196
|
+
* specified row and column in the data array.
|
|
197
|
+
* @returns a boolean value. It returns true if the index (row, col) is valid and the value is
|
|
198
|
+
* successfully set in the data array. It returns false if the index is invalid and the value is not
|
|
199
|
+
* set.
|
|
200
|
+
* @example
|
|
201
|
+
* // Modify individual cells
|
|
202
|
+
* const m = Matrix.zeros(2, 2);
|
|
203
|
+
* console.log(m.set(0, 0, 5)); // true;
|
|
204
|
+
* console.log(m.set(1, 1, 10)); // true;
|
|
205
|
+
* console.log(m.get(0, 0)); // 5;
|
|
206
|
+
* console.log(m.get(1, 1)); // 10;
|
|
207
|
+
*/
|
|
250
208
|
set(row, col, value) {
|
|
251
209
|
if (this.isValidIndex(row, col)) {
|
|
252
210
|
this.data[row][col] = value;
|
|
@@ -264,75 +222,33 @@ var Matrix = class _Matrix {
|
|
|
264
222
|
return this.rows === matrix.rows && this.cols === matrix.cols;
|
|
265
223
|
}
|
|
266
224
|
/**
|
|
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
|
-
|
|
312
|
-
|
|
313
|
-
* @example
|
|
314
|
-
* // Basic matrix arithmetic
|
|
315
|
-
* const a = new Matrix([
|
|
316
|
-
* [1, 2],
|
|
317
|
-
* [3, 4]
|
|
318
|
-
* ]);
|
|
319
|
-
* const b = new Matrix([
|
|
320
|
-
* [5, 6],
|
|
321
|
-
* [7, 8]
|
|
322
|
-
* ]);
|
|
323
|
-
*
|
|
324
|
-
* const sum = a.add(b);
|
|
325
|
-
* console.log(sum?.data); // [
|
|
326
|
-
* // [6, 8],
|
|
327
|
-
* // [10, 12]
|
|
328
|
-
* // ];
|
|
329
|
-
*
|
|
330
|
-
* const diff = b.subtract(a);
|
|
331
|
-
* console.log(diff?.data); // [
|
|
332
|
-
* // [4, 4],
|
|
333
|
-
* // [4, 4]
|
|
334
|
-
* // ];
|
|
335
|
-
*/
|
|
225
|
+
* The `add` function adds two matrices together, returning a new matrix with the result.
|
|
226
|
+
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
|
|
227
|
+
* @returns The `add` method returns a new `Matrix` object that represents the result of adding the
|
|
228
|
+
* current matrix with the provided `matrix` parameter.
|
|
229
|
+
* @example
|
|
230
|
+
* // Basic matrix arithmetic
|
|
231
|
+
* const a = new Matrix([
|
|
232
|
+
* [1, 2],
|
|
233
|
+
* [3, 4]
|
|
234
|
+
* ]);
|
|
235
|
+
* const b = new Matrix([
|
|
236
|
+
* [5, 6],
|
|
237
|
+
* [7, 8]
|
|
238
|
+
* ]);
|
|
239
|
+
*
|
|
240
|
+
* const sum = a.add(b);
|
|
241
|
+
* console.log(sum?.data); // [
|
|
242
|
+
* // [6, 8],
|
|
243
|
+
* // [10, 12]
|
|
244
|
+
* // ];
|
|
245
|
+
*
|
|
246
|
+
* const diff = b.subtract(a);
|
|
247
|
+
* console.log(diff?.data); // [
|
|
248
|
+
* // [4, 4],
|
|
249
|
+
* // [4, 4]
|
|
250
|
+
* // ];
|
|
251
|
+
*/
|
|
336
252
|
add(matrix) {
|
|
337
253
|
if (!this.isMatchForCalculate(matrix)) {
|
|
338
254
|
raise(Error, ERR.matrixDimensionMismatch("addition"));
|
|
@@ -356,60 +272,27 @@ var Matrix = class _Matrix {
|
|
|
356
272
|
});
|
|
357
273
|
}
|
|
358
274
|
/**
|
|
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
|
-
|
|
405
|
-
|
|
406
|
-
* @example
|
|
407
|
-
* // Element-wise subtraction
|
|
408
|
-
* const a = Matrix.from([[5, 6], [7, 8]]);
|
|
409
|
-
* const b = Matrix.from([[1, 2], [3, 4]]);
|
|
410
|
-
* const result = a.subtract(b);
|
|
411
|
-
* console.log(result?.toArray()); // [[4, 4], [4, 4]];
|
|
412
|
-
*/
|
|
275
|
+
* The `subtract` function performs element-wise subtraction between two matrices and returns a new
|
|
276
|
+
* matrix with the result.
|
|
277
|
+
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class. It
|
|
278
|
+
* represents the matrix that you want to subtract from the current matrix.
|
|
279
|
+
* @returns a new Matrix object with the result of the subtraction operation.
|
|
280
|
+
* @example
|
|
281
|
+
* // Element-wise subtraction
|
|
282
|
+
* const a = Matrix.from([
|
|
283
|
+
* [5, 6],
|
|
284
|
+
* [7, 8]
|
|
285
|
+
* ]);
|
|
286
|
+
* const b = Matrix.from([
|
|
287
|
+
* [1, 2],
|
|
288
|
+
* [3, 4]
|
|
289
|
+
* ]);
|
|
290
|
+
* const result = a.subtract(b);
|
|
291
|
+
* console.log(result?.toArray()); // [
|
|
292
|
+
* // [4, 4],
|
|
293
|
+
* // [4, 4]
|
|
294
|
+
* // ];
|
|
295
|
+
*/
|
|
413
296
|
subtract(matrix) {
|
|
414
297
|
if (!this.isMatchForCalculate(matrix)) {
|
|
415
298
|
raise(Error, ERR.matrixDimensionMismatch("subtraction"));
|
|
@@ -433,75 +316,33 @@ var Matrix = class _Matrix {
|
|
|
433
316
|
});
|
|
434
317
|
}
|
|
435
318
|
/**
|
|
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
|
-
|
|
481
|
-
|
|
482
|
-
* @example
|
|
483
|
-
* // Matrix multiplication for transformations
|
|
484
|
-
* // 2x3 matrix * 3x2 matrix = 2x2 matrix
|
|
485
|
-
* const a = new Matrix([
|
|
486
|
-
* [1, 2, 3],
|
|
487
|
-
* [4, 5, 6]
|
|
488
|
-
* ]);
|
|
489
|
-
* const b = new Matrix([
|
|
490
|
-
* [7, 8],
|
|
491
|
-
* [9, 10],
|
|
492
|
-
* [11, 12]
|
|
493
|
-
* ]);
|
|
494
|
-
*
|
|
495
|
-
* const product = a.multiply(b);
|
|
496
|
-
* console.log(product?.rows); // 2;
|
|
497
|
-
* console.log(product?.cols); // 2;
|
|
498
|
-
* // Row 0: 1*7+2*9+3*11=58, 1*8+2*10+3*12=64
|
|
499
|
-
* // Row 1: 4*7+5*9+6*11=139, 4*8+5*10+6*12=154
|
|
500
|
-
* console.log(product?.data); // [
|
|
501
|
-
* // [58, 64],
|
|
502
|
-
* // [139, 154]
|
|
503
|
-
* // ];
|
|
504
|
-
*/
|
|
319
|
+
* The `multiply` function performs matrix multiplication between two matrices and returns the result
|
|
320
|
+
* as a new matrix.
|
|
321
|
+
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
|
|
322
|
+
* @returns a new Matrix object.
|
|
323
|
+
* @example
|
|
324
|
+
* // Matrix multiplication for transformations
|
|
325
|
+
* // 2x3 matrix * 3x2 matrix = 2x2 matrix
|
|
326
|
+
* const a = new Matrix([
|
|
327
|
+
* [1, 2, 3],
|
|
328
|
+
* [4, 5, 6]
|
|
329
|
+
* ]);
|
|
330
|
+
* const b = new Matrix([
|
|
331
|
+
* [7, 8],
|
|
332
|
+
* [9, 10],
|
|
333
|
+
* [11, 12]
|
|
334
|
+
* ]);
|
|
335
|
+
*
|
|
336
|
+
* const product = a.multiply(b);
|
|
337
|
+
* console.log(product?.rows); // 2;
|
|
338
|
+
* console.log(product?.cols); // 2;
|
|
339
|
+
* // Row 0: 1*7+2*9+3*11=58, 1*8+2*10+3*12=64
|
|
340
|
+
* // Row 1: 4*7+5*9+6*11=139, 4*8+5*10+6*12=154
|
|
341
|
+
* console.log(product?.data); // [
|
|
342
|
+
* // [58, 64],
|
|
343
|
+
* // [139, 154]
|
|
344
|
+
* // ];
|
|
345
|
+
*/
|
|
505
346
|
multiply(matrix) {
|
|
506
347
|
if (this.cols !== matrix.rows) {
|
|
507
348
|
raise(Error, ERR.matrixDimensionMismatch("multiplication (A.cols must equal B.rows)"));
|
|
@@ -532,71 +373,29 @@ var Matrix = class _Matrix {
|
|
|
532
373
|
});
|
|
533
374
|
}
|
|
534
375
|
/**
|
|
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
|
-
|
|
579
|
-
|
|
580
|
-
* @example
|
|
581
|
-
* // Matrix transpose (square matrix)
|
|
582
|
-
* const m = new Matrix([
|
|
583
|
-
* [1, 2, 3],
|
|
584
|
-
* [4, 5, 6],
|
|
585
|
-
* [7, 8, 9]
|
|
586
|
-
* ]);
|
|
587
|
-
*
|
|
588
|
-
* const transposed = m.transpose();
|
|
589
|
-
* console.log(transposed.rows); // 3;
|
|
590
|
-
* console.log(transposed.cols); // 3;
|
|
591
|
-
* console.log(transposed.data); // [
|
|
592
|
-
* // [1, 4, 7],
|
|
593
|
-
* // [2, 5, 8],
|
|
594
|
-
* // [3, 6, 9]
|
|
595
|
-
* // ];
|
|
596
|
-
*
|
|
597
|
-
* // Transpose of transpose = original
|
|
598
|
-
* console.log(transposed.transpose().data); // m.data;
|
|
599
|
-
*/
|
|
376
|
+
* The transpose function takes a matrix and returns a new matrix that is the transpose of the
|
|
377
|
+
* original matrix.
|
|
378
|
+
* @returns The transpose() function returns a new Matrix object with the transposed data.
|
|
379
|
+
* @example
|
|
380
|
+
* // Matrix transpose (square matrix)
|
|
381
|
+
* const m = new Matrix([
|
|
382
|
+
* [1, 2, 3],
|
|
383
|
+
* [4, 5, 6],
|
|
384
|
+
* [7, 8, 9]
|
|
385
|
+
* ]);
|
|
386
|
+
*
|
|
387
|
+
* const transposed = m.transpose();
|
|
388
|
+
* console.log(transposed.rows); // 3;
|
|
389
|
+
* console.log(transposed.cols); // 3;
|
|
390
|
+
* console.log(transposed.data); // [
|
|
391
|
+
* // [1, 4, 7],
|
|
392
|
+
* // [2, 5, 8],
|
|
393
|
+
* // [3, 6, 9]
|
|
394
|
+
* // ];
|
|
395
|
+
*
|
|
396
|
+
* // Transpose of transpose = original
|
|
397
|
+
* console.log(transposed.transpose().data); // m.data;
|
|
398
|
+
*/
|
|
600
399
|
transpose() {
|
|
601
400
|
if (this.data.some((row) => row.length !== this.cols)) {
|
|
602
401
|
raise(Error, ERR.matrixNotRectangular());
|
|
@@ -617,63 +416,25 @@ var Matrix = class _Matrix {
|
|
|
617
416
|
multiplyFn: this.multiplyFn
|
|
618
417
|
});
|
|
619
418
|
}
|
|
419
|
+
// ─── Standard interface ─────────────────────────────────────
|
|
620
420
|
/**
|
|
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
|
-
|
|
664
|
-
|
|
665
|
-
* @example
|
|
666
|
-
* // Compute the inverse of a 2x2 matrix
|
|
667
|
-
* const m = Matrix.from([[4, 7], [2, 6]]);
|
|
668
|
-
* const inv = m.inverse();
|
|
669
|
-
* console.log(inv); // defined;
|
|
670
|
-
* // A * A^-1 should ≈ Identity
|
|
671
|
-
* const product = m.multiply(inv!);
|
|
672
|
-
* console.log(product?.get(0, 0)); // toBeCloseTo;
|
|
673
|
-
* console.log(product?.get(0, 1)); // toBeCloseTo;
|
|
674
|
-
* console.log(product?.get(1, 0)); // toBeCloseTo;
|
|
675
|
-
* console.log(product?.get(1, 1)); // toBeCloseTo;
|
|
676
|
-
*/
|
|
421
|
+
* The `inverse` function calculates the inverse of a square matrix using Gaussian elimination.
|
|
422
|
+
* @returns a Matrix object, which represents the inverse of the original matrix.
|
|
423
|
+
* @example
|
|
424
|
+
* // Compute the inverse of a 2x2 matrix
|
|
425
|
+
* const m = Matrix.from([
|
|
426
|
+
* [4, 7],
|
|
427
|
+
* [2, 6]
|
|
428
|
+
* ]);
|
|
429
|
+
* const inv = m.inverse();
|
|
430
|
+
* console.log(inv); // defined;
|
|
431
|
+
* // A * A^-1 should ≈ Identity
|
|
432
|
+
* const product = m.multiply(inv!);
|
|
433
|
+
* console.log(product?.get(0, 0)); // toBeCloseTo;
|
|
434
|
+
* console.log(product?.get(0, 1)); // toBeCloseTo;
|
|
435
|
+
* console.log(product?.get(1, 0)); // toBeCloseTo;
|
|
436
|
+
* console.log(product?.get(1, 1)); // toBeCloseTo;
|
|
437
|
+
*/
|
|
677
438
|
inverse() {
|
|
678
439
|
if (this.rows !== this.cols) {
|
|
679
440
|
raise(Error, ERR.matrixNotSquare());
|
|
@@ -727,58 +488,25 @@ var Matrix = class _Matrix {
|
|
|
727
488
|
});
|
|
728
489
|
}
|
|
729
490
|
/**
|
|
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
|
-
|
|
774
|
-
|
|
775
|
-
* @example
|
|
776
|
-
* // Dot product of two matrices
|
|
777
|
-
* const a = Matrix.from([[1, 2], [3, 4]]);
|
|
778
|
-
* const b = Matrix.from([[5, 6], [7, 8]]);
|
|
779
|
-
* const result = a.dot(b);
|
|
780
|
-
* console.log(result?.toArray()); // [[19, 22], [43, 50]];
|
|
781
|
-
*/
|
|
491
|
+
* The dot function calculates the dot product of two matrices and returns a new matrix.
|
|
492
|
+
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
|
|
493
|
+
* @returns a new Matrix object.
|
|
494
|
+
* @example
|
|
495
|
+
* // Dot product of two matrices
|
|
496
|
+
* const a = Matrix.from([
|
|
497
|
+
* [1, 2],
|
|
498
|
+
* [3, 4]
|
|
499
|
+
* ]);
|
|
500
|
+
* const b = Matrix.from([
|
|
501
|
+
* [5, 6],
|
|
502
|
+
* [7, 8]
|
|
503
|
+
* ]);
|
|
504
|
+
* const result = a.dot(b);
|
|
505
|
+
* console.log(result?.toArray()); // [
|
|
506
|
+
* // [19, 22],
|
|
507
|
+
* // [43, 50]
|
|
508
|
+
* // ];
|
|
509
|
+
*/
|
|
782
510
|
dot(matrix) {
|
|
783
511
|
if (this.cols !== matrix.rows) {
|
|
784
512
|
raise(Error, ERR.matrixDimensionMismatch("dot product (A.cols must equal B.rows)"));
|
|
@@ -837,13 +565,6 @@ var Matrix = class _Matrix {
|
|
|
837
565
|
}
|
|
838
566
|
);
|
|
839
567
|
}
|
|
840
|
-
// ─── Standard interface ─────────────────────────────────────
|
|
841
|
-
/**
|
|
842
|
-
* Returns [rows, cols] dimensions tuple.
|
|
843
|
-
*/
|
|
844
|
-
get size() {
|
|
845
|
-
return [this._rows, this._cols];
|
|
846
|
-
}
|
|
847
568
|
isEmpty() {
|
|
848
569
|
return this._rows === 0 || this._cols === 0;
|
|
849
570
|
}
|
|
@@ -881,6 +602,7 @@ var Matrix = class _Matrix {
|
|
|
881
602
|
}
|
|
882
603
|
};
|
|
883
604
|
}
|
|
605
|
+
// ─── Factory methods ────────────────────────────────────────
|
|
884
606
|
/**
|
|
885
607
|
* Visits each element with its row and column index.
|
|
886
608
|
*/
|
|
@@ -915,43 +637,6 @@ var Matrix = class _Matrix {
|
|
|
915
637
|
console.log(row.join(" "));
|
|
916
638
|
}
|
|
917
639
|
}
|
|
918
|
-
// ─── Factory methods ────────────────────────────────────────
|
|
919
|
-
/**
|
|
920
|
-
* Creates a rows×cols zero matrix.
|
|
921
|
-
* @example
|
|
922
|
-
* ```ts
|
|
923
|
-
* const z = Matrix.zeros(2, 3); // [[0,0,0],[0,0,0]]
|
|
924
|
-
* ```
|
|
925
|
-
*/
|
|
926
|
-
static zeros(rows, cols) {
|
|
927
|
-
const data = Array.from({ length: rows }, () => new Array(cols).fill(0));
|
|
928
|
-
return new _Matrix(data);
|
|
929
|
-
}
|
|
930
|
-
/**
|
|
931
|
-
* Creates an n×n identity matrix.
|
|
932
|
-
* @example
|
|
933
|
-
* ```ts
|
|
934
|
-
* const I = Matrix.identity(3); // [[1,0,0],[0,1,0],[0,0,1]]
|
|
935
|
-
* ```
|
|
936
|
-
*/
|
|
937
|
-
static identity(n) {
|
|
938
|
-
const data = Array.from(
|
|
939
|
-
{ length: n },
|
|
940
|
-
(_, i) => Array.from({ length: n }, (_2, j) => i === j ? 1 : 0)
|
|
941
|
-
);
|
|
942
|
-
return new _Matrix(data);
|
|
943
|
-
}
|
|
944
|
-
/**
|
|
945
|
-
* Creates a Matrix from a plain 2D array (deep copy).
|
|
946
|
-
* @example
|
|
947
|
-
* ```ts
|
|
948
|
-
* const m = Matrix.from([[1, 2], [3, 4]]);
|
|
949
|
-
* m.get(0, 1); // 2
|
|
950
|
-
* ```
|
|
951
|
-
*/
|
|
952
|
-
static from(data) {
|
|
953
|
-
return new _Matrix(data.map((row) => [...row]));
|
|
954
|
-
}
|
|
955
640
|
_addFn(a, b) {
|
|
956
641
|
if (a === void 0) return b;
|
|
957
642
|
return a + b;
|