data-structure-typed 1.49.4 → 1.49.5

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 (154) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +1 -3
  3. package/README_zh-CN.md +1 -1
  4. package/dist/cjs/data-structures/base/iterable-base.d.ts +1 -1
  5. package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
  6. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  7. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +1 -13
  8. package/dist/cjs/data-structures/binary-tree/binary-tree.js +19 -49
  9. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  10. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  11. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  12. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +0 -16
  13. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +1 -43
  14. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  15. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +1 -1
  16. package/dist/cjs/data-structures/graph/abstract-graph.js +3 -2
  17. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  18. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  19. package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
  20. package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
  21. package/dist/cjs/data-structures/hash/hash-map.d.ts +1 -1
  22. package/dist/cjs/data-structures/hash/hash-map.js +2 -2
  23. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  24. package/dist/cjs/data-structures/heap/heap.js +2 -3
  25. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  26. package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
  27. package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
  28. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  29. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  30. package/dist/cjs/data-structures/matrix/index.d.ts +0 -2
  31. package/dist/cjs/data-structures/matrix/index.js +0 -2
  32. package/dist/cjs/data-structures/matrix/index.js.map +1 -1
  33. package/dist/cjs/data-structures/matrix/matrix.d.ts +128 -10
  34. package/dist/cjs/data-structures/matrix/matrix.js +400 -15
  35. package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
  36. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  37. package/dist/cjs/data-structures/queue/deque.d.ts +2 -2
  38. package/dist/cjs/data-structures/queue/deque.js +5 -7
  39. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  40. package/dist/cjs/data-structures/queue/queue.d.ts +1 -1
  41. package/dist/cjs/types/data-structures/base/base.d.ts +1 -1
  42. package/dist/cjs/types/data-structures/heap/heap.d.ts +1 -1
  43. package/dist/cjs/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
  44. package/dist/cjs/utils/utils.d.ts +1 -0
  45. package/dist/cjs/utils/utils.js +6 -1
  46. package/dist/cjs/utils/utils.js.map +1 -1
  47. package/dist/mjs/data-structures/base/iterable-base.d.ts +1 -1
  48. package/dist/mjs/data-structures/binary-tree/avl-tree.js +2 -1
  49. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +1 -13
  50. package/dist/mjs/data-structures/binary-tree/binary-tree.js +20 -50
  51. package/dist/mjs/data-structures/binary-tree/bst.js +2 -1
  52. package/dist/mjs/data-structures/binary-tree/rb-tree.js +2 -1
  53. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +0 -16
  54. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +3 -44
  55. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +1 -1
  56. package/dist/mjs/data-structures/graph/abstract-graph.js +4 -3
  57. package/dist/mjs/data-structures/hash/hash-map.d.ts +1 -1
  58. package/dist/mjs/data-structures/hash/hash-map.js +2 -2
  59. package/dist/mjs/data-structures/heap/heap.js +2 -3
  60. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  61. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +1 -1
  62. package/dist/mjs/data-structures/matrix/index.d.ts +0 -2
  63. package/dist/mjs/data-structures/matrix/index.js +0 -2
  64. package/dist/mjs/data-structures/matrix/matrix.d.ts +128 -10
  65. package/dist/mjs/data-structures/matrix/matrix.js +399 -16
  66. package/dist/mjs/data-structures/queue/deque.d.ts +2 -2
  67. package/dist/mjs/data-structures/queue/deque.js +7 -9
  68. package/dist/mjs/data-structures/queue/queue.d.ts +1 -1
  69. package/dist/mjs/data-structures/queue/queue.js +1 -1
  70. package/dist/mjs/types/data-structures/base/base.d.ts +1 -1
  71. package/dist/mjs/types/data-structures/heap/heap.d.ts +1 -1
  72. package/dist/mjs/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
  73. package/dist/mjs/utils/utils.d.ts +1 -0
  74. package/dist/mjs/utils/utils.js +4 -0
  75. package/dist/umd/data-structure-typed.js +356 -524
  76. package/dist/umd/data-structure-typed.min.js +2 -2
  77. package/dist/umd/data-structure-typed.min.js.map +1 -1
  78. package/package.json +4 -3
  79. package/src/data-structures/base/index.ts +1 -1
  80. package/src/data-structures/base/iterable-base.ts +7 -10
  81. package/src/data-structures/binary-tree/avl-tree.ts +15 -8
  82. package/src/data-structures/binary-tree/binary-tree.ts +57 -74
  83. package/src/data-structures/binary-tree/bst.ts +16 -13
  84. package/src/data-structures/binary-tree/rb-tree.ts +16 -10
  85. package/src/data-structures/binary-tree/tree-multimap.ts +11 -48
  86. package/src/data-structures/graph/abstract-graph.ts +13 -11
  87. package/src/data-structures/graph/directed-graph.ts +1 -3
  88. package/src/data-structures/graph/map-graph.ts +6 -1
  89. package/src/data-structures/graph/undirected-graph.ts +3 -6
  90. package/src/data-structures/hash/hash-map.ts +18 -16
  91. package/src/data-structures/heap/heap.ts +7 -10
  92. package/src/data-structures/heap/max-heap.ts +2 -1
  93. package/src/data-structures/heap/min-heap.ts +2 -1
  94. package/src/data-structures/linked-list/singly-linked-list.ts +2 -3
  95. package/src/data-structures/matrix/index.ts +0 -2
  96. package/src/data-structures/matrix/matrix.ts +442 -13
  97. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -10
  98. package/src/data-structures/queue/deque.ts +18 -39
  99. package/src/data-structures/queue/queue.ts +1 -1
  100. package/src/interfaces/binary-tree.ts +7 -2
  101. package/src/types/common.ts +4 -4
  102. package/src/types/data-structures/base/base.ts +14 -3
  103. package/src/types/data-structures/base/index.ts +1 -1
  104. package/src/types/data-structures/graph/abstract-graph.ts +4 -2
  105. package/src/types/data-structures/hash/hash-map.ts +3 -3
  106. package/src/types/data-structures/heap/heap.ts +2 -2
  107. package/src/types/data-structures/priority-queue/priority-queue.ts +2 -2
  108. package/src/utils/utils.ts +7 -1
  109. package/test/integration/avl-tree.test.ts +18 -1
  110. package/test/integration/bst.test.ts +2 -2
  111. package/test/performance/data-structures/binary-tree/rb-tree.test.ts +1 -1
  112. package/test/performance/data-structures/comparison/comparison.test.ts +6 -11
  113. package/test/performance/data-structures/hash/hash-map.test.ts +3 -4
  114. package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +0 -1
  115. package/test/performance/data-structures/queue/deque.test.ts +6 -7
  116. package/test/performance/data-structures/queue/queue.test.ts +10 -9
  117. package/test/performance/reportor.ts +14 -15
  118. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +14 -3
  119. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +59 -24
  120. package/test/unit/data-structures/binary-tree/bst.test.ts +52 -26
  121. package/test/unit/data-structures/binary-tree/overall.test.ts +16 -18
  122. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +17 -6
  123. package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +37 -21
  124. package/test/unit/data-structures/graph/directed-graph.test.ts +104 -45
  125. package/test/unit/data-structures/graph/undirected-graph.test.ts +57 -40
  126. package/test/unit/data-structures/hash/hash-map.test.ts +48 -28
  127. package/test/unit/data-structures/hash/hash-table.test.ts +0 -4
  128. package/test/unit/data-structures/heap/heap.test.ts +0 -1
  129. package/test/unit/data-structures/heap/min-heap.test.ts +6 -5
  130. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +10 -11
  131. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +3 -4
  132. package/test/unit/data-structures/matrix/matrix.test.ts +345 -52
  133. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +1 -1
  134. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +0 -1
  135. package/test/unit/data-structures/queue/deque.test.ts +0 -6
  136. package/test/unit/data-structures/queue/queue.test.ts +1 -1
  137. package/test/unit/data-structures/stack/stack.test.ts +3 -4
  138. package/test/unit/unrestricted-interconversion.test.ts +50 -44
  139. package/test/utils/performanc.ts +1 -1
  140. package/typedoc.json +30 -0
  141. package/dist/cjs/data-structures/matrix/matrix2d.d.ts +0 -107
  142. package/dist/cjs/data-structures/matrix/matrix2d.js +0 -200
  143. package/dist/cjs/data-structures/matrix/matrix2d.js.map +0 -1
  144. package/dist/cjs/data-structures/matrix/vector2d.d.ts +0 -200
  145. package/dist/cjs/data-structures/matrix/vector2d.js +0 -291
  146. package/dist/cjs/data-structures/matrix/vector2d.js.map +0 -1
  147. package/dist/mjs/data-structures/matrix/matrix2d.d.ts +0 -107
  148. package/dist/mjs/data-structures/matrix/matrix2d.js +0 -196
  149. package/dist/mjs/data-structures/matrix/vector2d.d.ts +0 -200
  150. package/dist/mjs/data-structures/matrix/vector2d.js +0 -289
  151. package/src/data-structures/matrix/matrix2d.ts +0 -211
  152. package/src/data-structures/matrix/vector2d.ts +0 -315
  153. package/test/unit/data-structures/matrix/matrix2d.test.ts +0 -345
  154. package/test/unit/data-structures/matrix/vector2d.test.ts +0 -171
@@ -135,8 +135,7 @@ var dataStructureTyped = (() => {
135
135
  MapEdge: () => MapEdge,
136
136
  MapGraph: () => MapGraph,
137
137
  MapVertex: () => MapVertex,
138
- Matrix2D: () => Matrix2D,
139
- MatrixNTI2D: () => MatrixNTI2D,
138
+ Matrix: () => Matrix,
140
139
  MaxHeap: () => MaxHeap,
141
140
  MaxPriorityQueue: () => MaxPriorityQueue,
142
141
  MinHeap: () => MinHeap,
@@ -163,13 +162,13 @@ var dataStructureTyped = (() => {
163
162
  UndirectedEdge: () => UndirectedEdge,
164
163
  UndirectedGraph: () => UndirectedGraph,
165
164
  UndirectedVertex: () => UndirectedVertex,
166
- Vector2D: () => Vector2D,
167
165
  arrayRemove: () => arrayRemove,
168
166
  calcMinUnitsRequired: () => calcMinUnitsRequired,
169
167
  getMSB: () => getMSB,
170
168
  isThunk: () => isThunk,
171
169
  isWeakKey: () => isWeakKey,
172
170
  rangeCheck: () => rangeCheck,
171
+ roundFixed: () => roundFixed,
173
172
  throwRangeError: () => throwRangeError,
174
173
  toThunk: () => toThunk,
175
174
  trampoline: () => trampoline,
@@ -847,6 +846,10 @@ var dataStructureTyped = (() => {
847
846
  return inputType === "object" && input !== null || inputType === "function";
848
847
  };
849
848
  var calcMinUnitsRequired = (totalQuantity, unitSize) => Math.floor((totalQuantity + unitSize - 1) / unitSize);
849
+ var roundFixed = (num, digit = 10) => {
850
+ const multiplier = Math.pow(10, digit);
851
+ return Math.round(num * multiplier) / multiplier;
852
+ };
850
853
 
851
854
  // src/data-structures/hash/hash-map.ts
852
855
  var HashMap = class _HashMap extends IterableEntryBase {
@@ -3859,10 +3862,7 @@ var dataStructureTyped = (() => {
3859
3862
  */
3860
3863
  getAt(pos) {
3861
3864
  rangeCheck(pos, 0, this.size - 1);
3862
- const {
3863
- bucketIndex,
3864
- indexInBucket
3865
- } = this._getBucketAndPosition(pos);
3865
+ const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
3866
3866
  return this._buckets[bucketIndex][indexInBucket];
3867
3867
  }
3868
3868
  /**
@@ -3881,10 +3881,7 @@ var dataStructureTyped = (() => {
3881
3881
  */
3882
3882
  setAt(pos, element) {
3883
3883
  rangeCheck(pos, 0, this.size - 1);
3884
- const {
3885
- bucketIndex,
3886
- indexInBucket
3887
- } = this._getBucketAndPosition(pos);
3884
+ const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
3888
3885
  this._buckets[bucketIndex][indexInBucket] = element;
3889
3886
  return true;
3890
3887
  }
@@ -3948,10 +3945,7 @@ var dataStructureTyped = (() => {
3948
3945
  this.clear();
3949
3946
  return 0;
3950
3947
  }
3951
- const {
3952
- bucketIndex,
3953
- indexInBucket
3954
- } = this._getBucketAndPosition(pos);
3948
+ const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
3955
3949
  this._bucketLast = bucketIndex;
3956
3950
  this._lastInBucket = indexInBucket;
3957
3951
  this._size = pos + 1;
@@ -3980,15 +3974,9 @@ var dataStructureTyped = (() => {
3980
3974
  this.pop();
3981
3975
  else {
3982
3976
  const length = this.size - 1;
3983
- let {
3984
- bucketIndex: curBucket,
3985
- indexInBucket: curPointer
3986
- } = this._getBucketAndPosition(pos);
3977
+ let { bucketIndex: curBucket, indexInBucket: curPointer } = this._getBucketAndPosition(pos);
3987
3978
  for (let i = pos; i < length; ++i) {
3988
- const {
3989
- bucketIndex: nextBucket,
3990
- indexInBucket: nextPointer
3991
- } = this._getBucketAndPosition(pos + 1);
3979
+ const { bucketIndex: nextBucket, indexInBucket: nextPointer } = this._getBucketAndPosition(pos + 1);
3992
3980
  this._buckets[curBucket][curPointer] = this._buckets[nextBucket][nextPointer];
3993
3981
  curBucket = nextBucket;
3994
3982
  curPointer = nextPointer;
@@ -6170,9 +6158,7 @@ var dataStructureTyped = (() => {
6170
6158
  uniqueCycles.set(sorted, cycle);
6171
6159
  }
6172
6160
  }
6173
- return [...uniqueCycles].map(
6174
- (cycleString) => cycleString[1]
6175
- );
6161
+ return [...uniqueCycles].map((cycleString) => cycleString[1]);
6176
6162
  }
6177
6163
  /**
6178
6164
  * Time Complexity: O(n)
@@ -8787,7 +8773,12 @@ var dataStructureTyped = (() => {
8787
8773
  return emptyDisplayLayout;
8788
8774
  } else if (node !== null && node !== void 0) {
8789
8775
  const key = node.key, line = isNaN(this.extractor(key)) ? "S" : this.extractor(key).toString(), width = line.length;
8790
- return _buildNodeDisplay(line, width, this._displayAux(node.left, options), this._displayAux(node.right, options));
8776
+ return _buildNodeDisplay(
8777
+ line,
8778
+ width,
8779
+ this._displayAux(node.left, options),
8780
+ this._displayAux(node.right, options)
8781
+ );
8791
8782
  } else {
8792
8783
  const line = node === void 0 ? "U" : "N", width = line.length;
8793
8784
  return _buildNodeDisplay(line, width, [[""], 1, 0, 0], [[""], 1, 0, 0]);
@@ -8803,7 +8794,12 @@ var dataStructureTyped = (() => {
8803
8794
  const rightLine = i < rightHeight ? rightLines[i] : " ".repeat(rightWidth);
8804
8795
  mergedLines.push(leftLine + " ".repeat(width) + rightLine);
8805
8796
  }
8806
- return [mergedLines, leftWidth + width + rightWidth, Math.max(leftHeight, rightHeight) + 2, leftWidth + Math.floor(width / 2)];
8797
+ return [
8798
+ mergedLines,
8799
+ leftWidth + width + rightWidth,
8800
+ Math.max(leftHeight, rightHeight) + 2,
8801
+ leftWidth + Math.floor(width / 2)
8802
+ ];
8807
8803
  }
8808
8804
  }
8809
8805
  /**
@@ -8852,40 +8848,6 @@ var dataStructureTyped = (() => {
8852
8848
  }
8853
8849
  return newNode;
8854
8850
  }
8855
- /**
8856
- * The function `_addTo` adds a new node to a binary tree if there is an available position.
8857
- * @param {N | null | undefined} newNode - The `newNode` parameter represents the node that you want to add to
8858
- * the binary tree. It can be either a node object or `null`.
8859
- * @param {N} parent - The `parent` parameter represents the parent node to which the new node will
8860
- * be added as a child.
8861
- * @returns either the left or right child node of the parent node, depending on which child is
8862
- * available for adding the new node. If a new node is added, the function also updates the size of
8863
- * the binary tree. If neither the left nor right child is available, the function returns undefined.
8864
- * If the parent node is null, the function also returns undefined.
8865
- */
8866
- _addTo(newNode, parent) {
8867
- if (this.isNotNodeInstance(parent))
8868
- parent = this.getNode(parent);
8869
- if (parent) {
8870
- if (parent.left === void 0) {
8871
- parent.left = newNode;
8872
- if (newNode) {
8873
- this._size = this.size + 1;
8874
- }
8875
- return parent.left;
8876
- } else if (parent.right === void 0) {
8877
- parent.right = newNode;
8878
- if (newNode) {
8879
- this._size = this.size + 1;
8880
- }
8881
- return parent.right;
8882
- } else {
8883
- return;
8884
- }
8885
- } else {
8886
- return;
8887
- }
8888
- }
8889
8851
  /**
8890
8852
  * The function sets the root property of an object to a given value, and if the value is not null,
8891
8853
  * it also sets the parent property of the value to undefined.
@@ -11255,45 +11217,6 @@ var dataStructureTyped = (() => {
11255
11217
  this.bfs((node) => cloned.add(node.key, node.value, node.count));
11256
11218
  return cloned;
11257
11219
  }
11258
- /**
11259
- * Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
11260
- * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
11261
- *
11262
- * The function adds a new node to a binary tree, either as the left child or the right child of a
11263
- * given parent node.
11264
- * @param {N | undefined} newNode - The `newNode` parameter represents the node that needs to be
11265
- * added to the binary tree. It can be of type `N` (which represents a node in the binary tree) or
11266
- * `undefined` if there is no node to add.
11267
- * @param {K | N | undefined} parent - The `parent` parameter represents the parent node to
11268
- * which the new node will be added as a child. It can be either a node object (`N`) or a key value
11269
- * (`K`).
11270
- * @returns The method `_addTo` returns either the `parent.left` or `parent.right` node that was
11271
- * added, or `undefined` if no node was added.
11272
- */
11273
- _addTo(newNode, parent) {
11274
- parent = this.ensureNode(parent);
11275
- if (parent) {
11276
- if (parent.left === void 0) {
11277
- parent.left = newNode;
11278
- if (newNode !== void 0) {
11279
- this._size = this.size + 1;
11280
- this._count += newNode.count;
11281
- }
11282
- return parent.left;
11283
- } else if (parent.right === void 0) {
11284
- parent.right = newNode;
11285
- if (newNode !== void 0) {
11286
- this._size = this.size + 1;
11287
- this._count += newNode.count;
11288
- }
11289
- return parent.right;
11290
- } else {
11291
- return;
11292
- }
11293
- } else {
11294
- return;
11295
- }
11296
- }
11297
11220
  /**
11298
11221
  * The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
11299
11222
  * @param {K | N | undefined} srcNode - The `srcNode` parameter represents the source node from
@@ -11408,488 +11331,397 @@ var dataStructureTyped = (() => {
11408
11331
  };
11409
11332
 
11410
11333
  // src/data-structures/matrix/matrix.ts
11411
- var MatrixNTI2D = class {
11412
- /**
11413
- * The constructor creates a matrix with the specified number of rows and columns, and initializes all elements to a
11414
- * given initial value or 0 if not provided.
11415
- * @param options - An object containing the following properties:
11416
- */
11417
- constructor(options) {
11418
- __publicField(this, "_matrix");
11419
- const { row, col, initialVal } = options;
11420
- this._matrix = new Array(row).fill(void 0).map(() => new Array(col).fill(initialVal || 0));
11421
- }
11422
- /* The `toArray` method returns the matrix as a two-dimensional array. It converts the internal representation of the
11423
- matrix, which is an array of arrays, into a format that is more commonly used in JavaScript. */
11424
- toArray() {
11425
- return this._matrix;
11426
- }
11427
- };
11428
-
11429
- // src/data-structures/matrix/vector2d.ts
11430
- var Vector2D = class _Vector2D {
11431
- constructor(x = 0, y = 0, w = 1) {
11432
- this.x = x;
11433
- this.y = y;
11434
- this.w = w;
11435
- }
11436
- /**
11437
- * The function checks if the x and y values of a point are both zero.
11438
- * @returns A boolean value indicating whether both the x and y properties of the object are equal to 0.
11439
- */
11440
- get isZero() {
11441
- return this.x === 0 && this.y === 0;
11442
- }
11443
- /**
11444
- * The above function calculates the length of a vector using the Pythagorean theorem.
11445
- * @returns The length of a vector, calculated using the Pythagorean theorem.
11446
- */
11447
- get length() {
11448
- return Math.sqrt(this.x * this.x + this.y * this.y);
11334
+ var Matrix = class _Matrix {
11335
+ /**
11336
+ * The constructor function initializes a matrix object with the provided data and options, or with
11337
+ * default values if no options are provided.
11338
+ * @param {number[][]} data - A 2D array of numbers representing the data for the matrix.
11339
+ * @param [options] - The `options` parameter is an optional object that can contain the following
11340
+ * properties:
11341
+ */
11342
+ constructor(data, options) {
11343
+ __publicField(this, "_rows", 0);
11344
+ __publicField(this, "_cols", 0);
11345
+ __publicField(this, "_data");
11346
+ var _a, _b, _c;
11347
+ if (options) {
11348
+ const { rows, cols, addFn, subtractFn, multiplyFn } = options;
11349
+ if (typeof rows === "number" && rows > 0)
11350
+ this._rows = rows;
11351
+ else
11352
+ this._rows = data.length;
11353
+ if (typeof cols === "number" && cols > 0)
11354
+ this._cols = cols;
11355
+ else
11356
+ this._cols = ((_a = data[0]) == null ? void 0 : _a.length) || 0;
11357
+ if (addFn)
11358
+ this._addFn = addFn;
11359
+ if (subtractFn)
11360
+ this._subtractFn = subtractFn;
11361
+ if (multiplyFn)
11362
+ this._multiplyFn = multiplyFn;
11363
+ } else {
11364
+ this._rows = data.length;
11365
+ this._cols = (_c = (_b = data[0]) == null ? void 0 : _b.length) != null ? _c : 0;
11366
+ }
11367
+ if (data.length > 0) {
11368
+ this._data = data;
11369
+ } else {
11370
+ this._data = [];
11371
+ for (let i = 0; i < this.rows; i++) {
11372
+ this._data[i] = new Array(this.cols).fill(0);
11373
+ }
11374
+ }
11449
11375
  }
11450
- /**
11451
- * The function calculates the square of the length of a vector.
11452
- * @returns The method is returning the sum of the squares of the x and y values.
11453
- */
11454
- get lengthSq() {
11455
- return this.x * this.x + this.y * this.y;
11376
+ get rows() {
11377
+ return this._rows;
11456
11378
  }
11457
- /**
11458
- * The "rounded" function returns a new Vector2D object with the x and y values rounded to the nearest whole number.
11459
- * @returns The method is returning a new instance of the Vector2D class with the x and y values rounded to the nearest
11460
- * whole number.
11461
- */
11462
- get rounded() {
11463
- return new _Vector2D(Math.round(this.x), Math.round(this.y));
11379
+ get cols() {
11380
+ return this._cols;
11464
11381
  }
11465
- /**
11466
- * The function "add" takes two Vector2D objects as parameters and returns a new Vector2D object with the sum of their
11467
- * x and y components.
11468
- * @param {Vector2D} vector1 - The parameter `vector1` is an instance of the `Vector2D` class. It represents a
11469
- * 2-dimensional vector with an `x` and `y` component.
11470
- * @param {Vector2D} vector2 - The parameter "vector2" is of type Vector2D. It represents a 2-dimensional vector with
11471
- * an x and y component.
11472
- * @returns The method is returning a new instance of the Vector2D class with the x and y components of the two input
11473
- * vectors added together.
11474
- */
11475
- static add(vector1, vector2) {
11476
- return new _Vector2D(vector1.x + vector2.x, vector1.y + vector2.y);
11382
+ get data() {
11383
+ return this._data;
11477
11384
  }
11478
- /**
11479
- * The subtract function takes two Vector2D objects as parameters and returns a new Vector2D object with the x and y
11480
- * components subtracted.
11481
- * @param {Vector2D} vector1 - The parameter `vector1` is an instance of the `Vector2D` class, representing a
11482
- * 2-dimensional vector. It has properties `x` and `y` which represent the x and y components of the vector
11483
- * respectively.
11484
- * @param {Vector2D} vector2 - The parameter "vector2" is a Vector2D object. It represents the second vector that you
11485
- * want to subtract from the first vector.
11486
- * @returns The method is returning a new Vector2D object with the x and y components subtracted from vector1 and
11487
- * vector2.
11488
- */
11489
- static subtract(vector1, vector2) {
11490
- return new _Vector2D(vector1.x - vector2.x, vector1.y - vector2.y);
11385
+ get addFn() {
11386
+ return this._addFn;
11491
11387
  }
11492
- /**
11493
- * The function subtracts a given value from the x and y components of a Vector2D object and returns a new Vector2D
11494
- * object.
11495
- * @param {Vector2D} vector - The parameter "vector" is of type Vector2D, which represents a 2-dimensional vector with
11496
- * x and y components.
11497
- * @param {number} value - The "value" parameter is a number that will be subtracted from both the x and y components
11498
- * of the "vector" parameter.
11499
- * @returns A new Vector2D object with the x and y values subtracted by the given value.
11500
- */
11501
- static subtractValue(vector, value) {
11502
- return new _Vector2D(vector.x - value, vector.y - value);
11388
+ get subtractFn() {
11389
+ return this._subtractFn;
11503
11390
  }
11504
- /**
11505
- * The function multiplies a Vector2D object by a given value.
11506
- * @param {Vector2D} vector - The parameter "vector" is of type Vector2D, which represents a 2-dimensional vector with
11507
- * x and y components.
11508
- * @param {number} value - The "value" parameter is a number that represents the value by which the x and y components
11509
- * of the vector will be multiplied.
11510
- * @returns A new Vector2D object with the x and y values multiplied by the given value.
11511
- */
11512
- static multiply(vector, value) {
11513
- return new _Vector2D(vector.x * value, vector.y * value);
11514
- }
11515
- /**
11516
- * The function divides the x and y components of a Vector2D by a given value and returns a new Vector2D.
11517
- * @param {Vector2D} vector - The parameter "vector" is of type Vector2D, which represents a 2-dimensional vector with
11518
- * x and y components.
11519
- * @param {number} value - The value parameter is a number that will be used to divide the x and y components of the
11520
- * vector.
11521
- * @returns A new instance of the Vector2D class with the x and y values divided by the given value.
11522
- */
11523
- static divide(vector, value) {
11524
- return new _Vector2D(vector.x / value, vector.y / value);
11391
+ get multiplyFn() {
11392
+ return this._multiplyFn;
11525
11393
  }
11526
11394
  /**
11527
- * The function checks if two Vector2D objects are equal by comparing their x and y values.
11528
- * @param {Vector2D} vector1 - The parameter `vector1` is of type `Vector2D`, which represents a 2-dimensional vector.
11529
- * It has two properties: `x` and `y`, which represent the x and y components of the vector, respectively.
11530
- * @param {Vector2D} vector2 - The parameter "vector2" is of type Vector2D.
11531
- * @returns a boolean value, which indicates whether the two input vectors are equal or not.
11395
+ * The `get` function returns the value at the specified row and column index if it is a valid index.
11396
+ * @param {number} row - The `row` parameter represents the row index of the element you want to
11397
+ * retrieve from the data array.
11398
+ * @param {number} col - The parameter "col" represents the column number of the element you want to
11399
+ * retrieve from the data array.
11400
+ * @returns The `get` function returns a number if the provided row and column indices are valid.
11401
+ * Otherwise, it returns `undefined`.
11532
11402
  */
11533
- static equals(vector1, vector2) {
11534
- return vector1.x === vector2.x && vector1.y === vector2.y;
11403
+ get(row, col) {
11404
+ if (this.isValidIndex(row, col)) {
11405
+ return this.data[row][col];
11406
+ }
11535
11407
  }
11536
11408
  /**
11537
- * The function checks if two Vector2D objects are equal within a specified rounding factor.
11538
- * @param {Vector2D} vector1 - The first vector to compare.
11539
- * @param {Vector2D} vector2 - The parameter "vector2" is a Vector2D object, which represents a 2-dimensional vector.
11540
- * It is used as one of the inputs for the "equalsRounded" function.
11541
- * @param [roundingFactor=12] - The roundingFactor parameter is used to determine the threshold for considering two
11542
- * vectors as equal. If the absolute difference in the x and y components of the vectors is less than the
11543
- * roundingFactor, the vectors are considered equal.
11544
- * @returns a boolean value.
11409
+ * The set function updates the value at a specified row and column in a two-dimensional array.
11410
+ * @param {number} row - The "row" parameter represents the row index of the element in a
11411
+ * two-dimensional array or matrix. It specifies the row where the value will be set.
11412
+ * @param {number} col - The "col" parameter represents the column index of the element in a
11413
+ * two-dimensional array.
11414
+ * @param {number} value - The value parameter represents the number that you want to set at the
11415
+ * specified row and column in the data array.
11416
+ * @returns a boolean value. It returns true if the index (row, col) is valid and the value is
11417
+ * successfully set in the data array. It returns false if the index is invalid and the value is not
11418
+ * set.
11545
11419
  */
11546
- static equalsRounded(vector1, vector2, roundingFactor = 12) {
11547
- const vector = _Vector2D.abs(_Vector2D.subtract(vector1, vector2));
11548
- if (vector.x < roundingFactor && vector.y < roundingFactor) {
11420
+ set(row, col, value) {
11421
+ if (this.isValidIndex(row, col)) {
11422
+ this.data[row][col] = value;
11549
11423
  return true;
11550
11424
  }
11551
11425
  return false;
11552
11426
  }
11553
11427
  /**
11554
- * The normalize function takes a vector as input and returns a normalized version of the vector.Normalizes the vector if it matches a certain condition
11555
- * @param {Vector2D} vector - The parameter "vector" is of type Vector2D.
11556
- * @returns the normalized vector if its length is greater than a very small value (epsilon), otherwise it returns the
11557
- * original vector.
11428
+ * The function checks if the dimensions of the given matrix match the dimensions of the current
11429
+ * matrix.
11430
+ * @param {Matrix} matrix - The parameter `matrix` is of type `Matrix`.
11431
+ * @returns a boolean value.
11558
11432
  */
11559
- static normalize(vector) {
11560
- const length = vector.length;
11561
- if (length > 2220446049250313e-31) {
11562
- return _Vector2D.divide(vector, length);
11563
- }
11564
- return vector;
11433
+ isMatchForCalculate(matrix) {
11434
+ return this.rows === matrix.rows && this.cols === matrix.cols;
11565
11435
  }
11566
11436
  /**
11567
- * The function truncates a vector to a maximum length if it exceeds that length.Adjusts x and y so that the length of the vector does not exceed max
11568
- * @param {Vector2D} vector - A 2D vector represented by the Vector2D class.
11569
- * @param {number} max - The `max` parameter is a number that represents the maximum length that the `vector` should
11570
- * have.
11571
- * @returns either the original vector or a truncated version of the vector, depending on whether the length of the
11572
- * vector is greater than the maximum value specified.
11437
+ * The `add` function adds two matrices together, returning a new matrix with the result.
11438
+ * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
11439
+ * @returns The `add` method returns a new `Matrix` object that represents the result of adding the
11440
+ * current matrix with the provided `matrix` parameter.
11573
11441
  */
11574
- static truncate(vector, max) {
11575
- if (vector.length > max) {
11576
- return _Vector2D.multiply(_Vector2D.normalize(vector), max);
11442
+ add(matrix) {
11443
+ if (!this.isMatchForCalculate(matrix)) {
11444
+ throw new Error("Matrix dimensions must match for addition.");
11577
11445
  }
11578
- return vector;
11579
- }
11580
- /**
11581
- * The function returns a new Vector2D object that is perpendicular to the input vector.The vector that is perpendicular to this one
11582
- * @param {Vector2D} vector - The parameter "vector" is of type Vector2D.
11583
- * @returns A new Vector2D object is being returned.
11584
- */
11585
- static perp(vector) {
11586
- return new _Vector2D(-vector.y, vector.x);
11587
- }
11588
- /**
11589
- * The reverse function takes a Vector2D object and returns a new Vector2D object with the negated x and y values.
11590
- * @param {Vector2D} vector - The parameter "vector" is of type Vector2D, which represents a 2-dimensional vector. It
11591
- * has two properties: "x" and "y", which represent the x and y components of the vector, respectively.
11592
- * @returns A new Vector2D object with the negated x and y values of the input vector. Returns the vector that is the reverse of this vector
11593
- */
11594
- static reverse(vector) {
11595
- return new _Vector2D(-vector.x, -vector.y);
11596
- }
11597
- /**
11598
- * The function takes a Vector2D object as input and returns a new Vector2D object with the absolute values of its x
11599
- * and y components.
11600
- * @param {Vector2D} vector - The parameter "vector" is of type Vector2D, which represents a 2-dimensional vector. It
11601
- * has two properties: "x" and "y", which represent the x and y components of the vector, respectively.
11602
- * @returns The method is returning a new Vector2D object with the absolute values of the x and y components of the
11603
- * input vector.
11604
- */
11605
- static abs(vector) {
11606
- return new _Vector2D(Math.abs(vector.x), Math.abs(vector.y));
11607
- }
11608
- /**
11609
- * The dot function calculates the dot product of two 2D vectors.The dot product of v1 and v2
11610
- * @param {Vector2D} vector1 - The parameter `vector1` represents a 2D vector with its x and y components.
11611
- * @param {Vector2D} vector2 - The "vector2" parameter is a Vector2D object. It represents a two-dimensional vector
11612
- * with an x and y component.
11613
- * @returns The dot product of the two input vectors.
11614
- */
11615
- static dot(vector1, vector2) {
11616
- return vector1.x * vector2.x + vector1.y * vector2.y;
11617
- }
11618
- // /**
11619
- // * Transform vectors based on the current tranformation matrices: translation, rotation and scale
11620
- // * @param vectors The vectors to transform
11621
- // */
11622
- // static transform(vector: Vector2D, transformation: Matrix2D): Vector2D {
11623
- // return Matrix2D.multiplyByVector(transformation, vector)
11624
- // }
11625
- // /**
11626
- // * Transform vectors based on the current tranformation matrices: translation, rotation and scale
11627
- // * @param vectors The vectors to transform
11628
- // */
11629
- // static transformList(vectors: Vector2D[], transformation: Matrix2D): Vector2D[] {
11630
- // return vectors.map(vector => Matrix2D.multiplyByVector(transformation, vector))
11631
- // }
11632
- /**
11633
- * The function calculates the distance between two points in a two-dimensional space.
11634
- * @param {Vector2D} vector1 - The parameter `vector1` represents the first vector in 2D space, while `vector2`
11635
- * represents the second vector. Each vector has an `x` and `y` component, which represent their respective coordinates
11636
- * in the 2D space.
11637
- * @param {Vector2D} vector2 - The `vector2` parameter represents the second vector in the calculation of distance. It
11638
- * is an instance of the `Vector2D` class, which typically has properties `x` and `y` representing the coordinates of
11639
- * the vector in a 2D space.
11640
- * @returns The distance between vector1 and vector2.
11641
- */
11642
- static distance(vector1, vector2) {
11643
- const ySeparation = vector2.y - vector1.y;
11644
- const xSeparation = vector2.x - vector1.x;
11645
- return Math.sqrt(ySeparation * ySeparation + xSeparation * xSeparation);
11646
- }
11647
- /**
11648
- * The function calculates the squared distance between two 2D vectors.
11649
- * @param {Vector2D} vector1 - The parameter `vector1` represents the first vector, which is an instance of the
11650
- * `Vector2D` class. It contains the x and y coordinates of the vector.
11651
- * @param {Vector2D} vector2 - The `vector2` parameter represents the second vector in a two-dimensional space. It has
11652
- * properties `x` and `y` which represent the coordinates of the vector.
11653
- * @returns the square of the distance between the two input vectors.
11654
- */
11655
- static distanceSq(vector1, vector2) {
11656
- const ySeparation = vector2.y - vector1.y;
11657
- const xSeparation = vector2.x - vector1.x;
11658
- return ySeparation * ySeparation + xSeparation * xSeparation;
11659
- }
11660
- /**
11661
- * The sign function determines the sign of the cross product between two 2D vectors.
11662
- * (assuming the Y axis is pointing down, X axis to right like a Window app)
11663
- * @param {Vector2D} vector1 - The parameter `vector1` is of type `Vector2D`, which represents a 2-dimensional vector.
11664
- * It likely has properties `x` and `y` representing the x and y components of the vector, respectively.
11665
- * @param {Vector2D} vector2 - The above code defines a function called "sign" that takes two parameters: vector1 and
11666
- * vector2. Both vector1 and vector2 are of type Vector2D.
11667
- * @returns either -1 or 1. Returns positive if v2 is clockwise of this vector, negative if counterclockwise
11668
- */
11669
- static sign(vector1, vector2) {
11670
- if (vector1.y * vector2.x > vector1.x * vector2.y) {
11671
- return -1;
11446
+ const resultData = [];
11447
+ for (let i = 0; i < this.rows; i++) {
11448
+ resultData[i] = [];
11449
+ for (let j = 0; j < this.cols; j++) {
11450
+ const a = this.get(i, j), b = matrix.get(i, j);
11451
+ if (a !== void 0 && b !== void 0) {
11452
+ const added = this._addFn(a, b);
11453
+ if (added) {
11454
+ resultData[i][j] = added;
11455
+ }
11456
+ }
11457
+ }
11672
11458
  }
11673
- return 1;
11674
- }
11675
- /**
11676
- * The function calculates the angle between a given vector and the negative y-axis.
11677
- * @param {Vector2D} vector - The "vector" parameter is an instance of the Vector2D class, which represents a
11678
- * 2-dimensional vector. It has two properties: "x" and "y", which represent the x and y components of the vector,
11679
- * respectively.
11680
- * @returns the angle between the given vector and the vector (0, -1) in radians.Returns the angle between origin and the given vector in radians
11681
- */
11682
- static angle(vector) {
11683
- const origin = new _Vector2D(0, -1);
11684
- const radian = Math.acos(_Vector2D.dot(vector, origin) / (vector.length * origin.length));
11685
- return _Vector2D.sign(vector, origin) === 1 ? Math.PI * 2 - radian : radian;
11686
- }
11687
- /**
11688
- * The function "random" generates a random Vector2D object with x and y values within the specified range.
11689
- * @param {number} maxX - The maxX parameter represents the maximum value for the x-coordinate of the random vector.
11690
- * @param {number} maxY - The `maxY` parameter represents the maximum value for the y-coordinate of the generated
11691
- * random vector.
11692
- * @returns a new instance of the Vector2D class with random x and y values.
11693
- */
11694
- static random(maxX, maxY) {
11695
- const randX = Math.floor(Math.random() * maxX - maxX / 2);
11696
- const randY = Math.floor(Math.random() * maxY - maxY / 2);
11697
- return new _Vector2D(randX, randY);
11459
+ return new _Matrix(resultData, {
11460
+ rows: this.rows,
11461
+ cols: this.cols,
11462
+ addFn: this.addFn,
11463
+ subtractFn: this.subtractFn,
11464
+ multiplyFn: this.multiplyFn
11465
+ });
11698
11466
  }
11699
11467
  /**
11700
- * The function sets the values of x and y to zero.
11701
- */
11702
- zero() {
11703
- this.x = 0;
11704
- this.y = 0;
11705
- }
11706
- };
11707
-
11708
- // src/data-structures/matrix/matrix2d.ts
11709
- var Matrix2D = class _Matrix2D {
11710
- /**
11711
- * The constructor function initializes a Matrix2D object with either a default identity matrix, or a provided matrix
11712
- * or Vector2D object.
11713
- * @param {number[][] | Vector2D} [value] - The `value` parameter can be either a 2D array of numbers (`number[][]`) or
11714
- * an instance of the `Vector2D` class.
11715
- */
11716
- constructor(value) {
11717
- __publicField(this, "_matrix");
11718
- if (typeof value === "undefined") {
11719
- this._matrix = _Matrix2D.identity;
11720
- } else if (value instanceof Vector2D) {
11721
- this._matrix = _Matrix2D.identity;
11722
- this._matrix[0][0] = value.x;
11723
- this._matrix[1][0] = value.y;
11724
- this._matrix[2][0] = value.w;
11725
- } else {
11726
- this._matrix = value;
11468
+ * The `subtract` function performs element-wise subtraction between two matrices and returns a new
11469
+ * matrix with the result.
11470
+ * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class. It
11471
+ * represents the matrix that you want to subtract from the current matrix.
11472
+ * @returns a new Matrix object with the result of the subtraction operation.
11473
+ */
11474
+ subtract(matrix) {
11475
+ if (!this.isMatchForCalculate(matrix)) {
11476
+ throw new Error("Matrix dimensions must match for subtraction.");
11477
+ }
11478
+ const resultData = [];
11479
+ for (let i = 0; i < this.rows; i++) {
11480
+ resultData[i] = [];
11481
+ for (let j = 0; j < this.cols; j++) {
11482
+ const a = this.get(i, j), b = matrix.get(i, j);
11483
+ if (a !== void 0 && b !== void 0) {
11484
+ const subtracted = this._subtractFn(a, b);
11485
+ if (subtracted) {
11486
+ resultData[i][j] = subtracted;
11487
+ }
11488
+ }
11489
+ }
11727
11490
  }
11491
+ return new _Matrix(resultData, {
11492
+ rows: this.rows,
11493
+ cols: this.cols,
11494
+ addFn: this.addFn,
11495
+ subtractFn: this.subtractFn,
11496
+ multiplyFn: this.multiplyFn
11497
+ });
11728
11498
  }
11729
11499
  /**
11730
- * The function returns a 2D array with three empty arrays.
11731
- * @returns An empty 2-dimensional array with 3 empty arrays inside.
11732
- */
11733
- static get empty() {
11734
- return [[], [], []];
11735
- }
11736
- /**
11737
- * The above function returns a 3x3 identity matrix.
11738
- * @returns The method is returning a 2-dimensional array of numbers representing the identity matrix.
11739
- */
11740
- static get identity() {
11741
- return [
11742
- [1, 0, 0],
11743
- [0, 1, 0],
11744
- [0, 0, 1]
11745
- ];
11746
- }
11747
- /**
11748
- * The function returns a two-dimensional array of numbers.
11749
- * @returns The getter method is returning the value of the private variable `_matrix`, which is a two-dimensional
11750
- * array of numbers.
11751
- */
11752
- get m() {
11753
- return this._matrix;
11754
- }
11755
- /**
11756
- * The function takes two 2D matrices as input and returns their sum as a new 2D matrix.
11757
- * @param {Matrix2D} matrix1 - Matrix2D - The first matrix to be added.
11758
- * @param {Matrix2D} matrix2 - The parameter `matrix2` is a Matrix2D object.
11759
- * @returns a new instance of the Matrix2D class, which is created using the result array.
11760
- */
11761
- static add(matrix1, matrix2) {
11762
- const result = _Matrix2D.empty;
11763
- for (let i = 0; i < 3; i++) {
11764
- for (let j = 0; j < 3; j++) {
11765
- result[i][j] = matrix1.m[i][j] + matrix2.m[i][j];
11500
+ * The `multiply` function performs matrix multiplication between two matrices and returns the result
11501
+ * as a new matrix.
11502
+ * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
11503
+ * @returns a new Matrix object.
11504
+ */
11505
+ multiply(matrix) {
11506
+ if (this.cols !== matrix.rows) {
11507
+ throw new Error("Matrix dimensions must be compatible for multiplication (A.cols = B.rows).");
11508
+ }
11509
+ const resultData = [];
11510
+ for (let i = 0; i < this.rows; i++) {
11511
+ resultData[i] = [];
11512
+ for (let j = 0; j < matrix.cols; j++) {
11513
+ let sum;
11514
+ for (let k = 0; k < this.cols; k++) {
11515
+ const a = this.get(i, k), b = matrix.get(k, j);
11516
+ if (a !== void 0 && b !== void 0) {
11517
+ const multiplied = this.multiplyFn(a, b);
11518
+ if (multiplied !== void 0) {
11519
+ sum = this.addFn(sum, multiplied);
11520
+ }
11521
+ }
11522
+ }
11523
+ if (sum !== void 0)
11524
+ resultData[i][j] = sum;
11766
11525
  }
11767
11526
  }
11768
- return new _Matrix2D(result);
11527
+ return new _Matrix(resultData, {
11528
+ rows: this.rows,
11529
+ cols: matrix.cols,
11530
+ addFn: this.addFn,
11531
+ subtractFn: this.subtractFn,
11532
+ multiplyFn: this.multiplyFn
11533
+ });
11769
11534
  }
11770
11535
  /**
11771
- * The function subtracts two 2D matrices and returns the result as a new Matrix2D object.
11772
- * @param {Matrix2D} matrix1 - Matrix2D - The first matrix to subtract from.
11773
- * @param {Matrix2D} matrix2 - Matrix2D is a class representing a 2D matrix. It has a property `m` which is a 2D array
11774
- * representing the matrix elements.
11775
- * @returns a new instance of the Matrix2D class, which is created using the result array.
11536
+ * The transpose function takes a matrix and returns a new matrix that is the transpose of the
11537
+ * original matrix.
11538
+ * @returns The transpose() function returns a new Matrix object with the transposed data.
11776
11539
  */
11777
- static subtract(matrix1, matrix2) {
11778
- const result = _Matrix2D.empty;
11779
- for (let i = 0; i < 3; i++) {
11780
- for (let j = 0; j < 3; j++) {
11781
- result[i][j] = matrix1.m[i][j] - matrix2.m[i][j];
11540
+ transpose() {
11541
+ if (this.data.some((row) => row.length !== this.rows)) {
11542
+ throw new Error("Matrix must be rectangular for transposition.");
11543
+ }
11544
+ const resultData = [];
11545
+ for (let j = 0; j < this.cols; j++) {
11546
+ resultData[j] = [];
11547
+ for (let i = 0; i < this.rows; i++) {
11548
+ const trans = this.get(i, j);
11549
+ if (trans !== void 0)
11550
+ resultData[j][i] = trans;
11782
11551
  }
11783
11552
  }
11784
- return new _Matrix2D(result);
11553
+ return new _Matrix(resultData, {
11554
+ rows: this.cols,
11555
+ cols: this.rows,
11556
+ addFn: this.addFn,
11557
+ subtractFn: this.subtractFn,
11558
+ multiplyFn: this.multiplyFn
11559
+ });
11785
11560
  }
11786
11561
  /**
11787
- * The function multiplies two 2D matrices and returns the result as a new Matrix2D object.
11788
- * @param {Matrix2D} matrix1 - A 2D matrix represented by the Matrix2D class.
11789
- * @param {Matrix2D} matrix2 - The parameter `matrix2` is a 2D matrix of size 3x3.
11790
- * @returns a new instance of the Matrix2D class, created using the result array.
11562
+ * The `inverse` function calculates the inverse of a square matrix using Gaussian elimination.
11563
+ * @returns a Matrix object, which represents the inverse of the original matrix.
11791
11564
  */
11792
- static multiply(matrix1, matrix2) {
11793
- const result = _Matrix2D.empty;
11794
- for (let i = 0; i < 3; i++) {
11795
- for (let j = 0; j < 3; j++) {
11796
- result[i][j] = 0;
11797
- for (let k = 0; k < 3; k++) {
11798
- result[i][j] += matrix1.m[i][k] * matrix2.m[k][j];
11565
+ inverse() {
11566
+ var _a;
11567
+ if (this.rows !== this.cols) {
11568
+ throw new Error("Matrix must be square for inversion.");
11569
+ }
11570
+ const augmentedMatrixData = [];
11571
+ for (let i = 0; i < this.rows; i++) {
11572
+ augmentedMatrixData[i] = this.data[i].slice();
11573
+ for (let j = 0; j < this.cols; j++) {
11574
+ augmentedMatrixData[i][this.cols + j] = i === j ? 1 : 0;
11575
+ }
11576
+ }
11577
+ const augmentedMatrix = new _Matrix(augmentedMatrixData, {
11578
+ rows: this.rows,
11579
+ cols: this.cols * 2,
11580
+ addFn: this.addFn,
11581
+ subtractFn: this.subtractFn,
11582
+ multiplyFn: this.multiplyFn
11583
+ });
11584
+ for (let i = 0; i < this.rows; i++) {
11585
+ let pivotRow = i;
11586
+ while (pivotRow < this.rows && augmentedMatrix.get(pivotRow, i) === 0) {
11587
+ pivotRow++;
11588
+ }
11589
+ if (pivotRow === this.rows) {
11590
+ throw new Error("Matrix is singular, and its inverse does not exist.");
11591
+ }
11592
+ augmentedMatrix._swapRows(i, pivotRow);
11593
+ const pivotElement = (_a = augmentedMatrix.get(i, i)) != null ? _a : 1;
11594
+ if (pivotElement === 0) {
11595
+ throw new Error("Matrix is singular, and its inverse does not exist (division by zero).");
11596
+ }
11597
+ augmentedMatrix._scaleRow(i, 1 / pivotElement);
11598
+ for (let j = 0; j < this.rows; j++) {
11599
+ if (j !== i) {
11600
+ let factor = augmentedMatrix.get(j, i);
11601
+ if (factor === void 0)
11602
+ factor = 0;
11603
+ augmentedMatrix._addScaledRow(j, i, -factor);
11799
11604
  }
11800
11605
  }
11801
11606
  }
11802
- return new _Matrix2D(result);
11607
+ const inverseData = [];
11608
+ for (let i = 0; i < this.rows; i++) {
11609
+ inverseData[i] = augmentedMatrix.data[i].slice(this.cols);
11610
+ }
11611
+ return new _Matrix(inverseData, {
11612
+ rows: this.rows,
11613
+ cols: this.cols,
11614
+ addFn: this.addFn,
11615
+ subtractFn: this.subtractFn,
11616
+ multiplyFn: this.multiplyFn
11617
+ });
11803
11618
  }
11804
11619
  /**
11805
- * The function multiplies each element of a 2D matrix by a given value and returns the resulting matrix.
11806
- * @param {Matrix2D} matrix - The `matrix` parameter is an instance of the `Matrix2D` class, which represents a 2D
11807
- * matrix. It contains a property `m` that is a 2D array representing the matrix elements.
11808
- * @param {number} value - The `value` parameter is a number that you want to multiply each element of the `matrix` by.
11809
- * @returns a new instance of the Matrix2D class, which is created using the result array.
11810
- */
11811
- static multiplyByValue(matrix, value) {
11812
- const result = _Matrix2D.empty;
11813
- for (let i = 0; i < 3; i++) {
11814
- for (let j = 0; j < 3; j++) {
11815
- result[i][j] = matrix.m[i][j] * value;
11620
+ * The dot function calculates the dot product of two matrices and returns a new matrix.
11621
+ * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
11622
+ * @returns a new Matrix object.
11623
+ */
11624
+ dot(matrix) {
11625
+ if (this.cols !== matrix.rows) {
11626
+ throw new Error(
11627
+ "Number of columns in the first matrix must be equal to the number of rows in the second matrix for dot product."
11628
+ );
11629
+ }
11630
+ const resultData = [];
11631
+ for (let i = 0; i < this.rows; i++) {
11632
+ resultData[i] = [];
11633
+ for (let j = 0; j < matrix.cols; j++) {
11634
+ let sum;
11635
+ for (let k = 0; k < this.cols; k++) {
11636
+ const a = this.get(i, k), b = matrix.get(k, j);
11637
+ if (a !== void 0 && b !== void 0) {
11638
+ const multiplied = this.multiplyFn(a, b);
11639
+ if (multiplied !== void 0) {
11640
+ sum = this.addFn(sum, multiplied);
11641
+ }
11642
+ }
11643
+ }
11644
+ if (sum !== void 0)
11645
+ resultData[i][j] = sum;
11816
11646
  }
11817
11647
  }
11818
- return new _Matrix2D(result);
11648
+ return new _Matrix(resultData, {
11649
+ rows: this.rows,
11650
+ cols: matrix.cols,
11651
+ addFn: this.addFn,
11652
+ subtractFn: this.subtractFn,
11653
+ multiplyFn: this.multiplyFn
11654
+ });
11819
11655
  }
11820
- /**
11821
- * The function multiplies a 2D matrix by a 2D vector and returns the result as a 2D vector.
11822
- * @param {Matrix2D} matrix - The parameter "matrix" is of type Matrix2D. It represents a 2-dimensional matrix.
11823
- * @param {Vector2D} vector - The "vector" parameter is a 2D vector, represented by an object of type Vector2D.
11824
- * @returns a Vector2D.
11825
- */
11826
- static multiplyByVector(matrix, vector) {
11827
- const resultMatrix = _Matrix2D.multiply(matrix, new _Matrix2D(vector));
11828
- return resultMatrix.toVector();
11656
+ _addFn(a, b) {
11657
+ if (a === void 0)
11658
+ return b;
11659
+ return a + b;
11829
11660
  }
11830
- /**
11831
- * The function returns a 2D matrix that scales and flips a vector around the center of a given width and height.
11832
- * @param {number} width - The width parameter represents the width of the view or the canvas. It is a number that
11833
- * specifies the width in pixels or any other unit of measurement.
11834
- * @param {number} height - The height parameter represents the height of the view or the canvas. It is used to
11835
- * calculate the centerY value, which is the vertical center of the view.
11836
- * @returns a Matrix2D object.
11837
- */
11838
- static view(width, height) {
11839
- const scaleStep = 1;
11840
- const centerX = width / 2;
11841
- const centerY = height / 2;
11842
- const flipX = Math.cos(Math.PI);
11843
- return new _Matrix2D([
11844
- [scaleStep, 0, centerX],
11845
- [0, flipX * scaleStep, centerY],
11846
- [0, 0, 1]
11847
- ]);
11661
+ _subtractFn(a, b) {
11662
+ return a - b;
11663
+ }
11664
+ _multiplyFn(a, b) {
11665
+ return a * b;
11848
11666
  }
11849
11667
  /**
11850
- * The function scales a matrix by a given factor.
11851
- * @param {number} factor - The factor parameter is a number that represents the scaling factor by which the matrix
11852
- * should be scaled.
11853
- * @returns the result of multiplying a new instance of Matrix2D by the given factor.
11668
+ * The function checks if a given row and column index is valid within a specified range.
11669
+ * @param {number} row - The `row` parameter represents the row index of a two-dimensional array or
11670
+ * matrix. It is a number that indicates the specific row in the matrix.
11671
+ * @param {number} col - The "col" parameter represents the column index in a two-dimensional array
11672
+ * or grid. It is used to check if the given column index is valid within the bounds of the grid.
11673
+ * @returns A boolean value is being returned.
11854
11674
  */
11855
- static scale(factor) {
11856
- return _Matrix2D.multiplyByValue(new _Matrix2D(), factor);
11675
+ isValidIndex(row, col) {
11676
+ return row >= 0 && row < this.rows && col >= 0 && col < this.cols;
11857
11677
  }
11858
11678
  /**
11859
- * The function "rotate" takes an angle in radians and returns a 2D transformation matrix for rotating objects.
11860
- * @param {number} radians - The "radians" parameter is the angle in radians by which you want to rotate an object.
11861
- * @returns The code is returning a new instance of a Matrix2D object.
11679
+ * The function `_swapRows` swaps the positions of two rows in an array.
11680
+ * @param {number} row1 - The `row1` parameter is the index of the first row that you want to swap.
11681
+ * @param {number} row2 - The `row2` parameter is the index of the second row that you want to swap
11682
+ * with the first row.
11862
11683
  */
11863
- static rotate(radians) {
11864
- const cos = Math.cos(radians);
11865
- const sin = Math.sin(radians);
11866
- return new _Matrix2D([
11867
- [cos, -sin, 0],
11868
- [sin, cos, 0],
11869
- [0, 0, 1]
11870
- ]);
11684
+ _swapRows(row1, row2) {
11685
+ const temp = this.data[row1];
11686
+ this.data[row1] = this.data[row2];
11687
+ this.data[row2] = temp;
11871
11688
  }
11872
11689
  /**
11873
- * The translate function takes a 2D vector and returns a 2D matrix that represents a translation transformation.
11874
- * @param {Vector2D} vector - The parameter "vector" is of type Vector2D. It represents a 2D vector with components x
11875
- * and y, and an optional w component.
11876
- * @returns The method is returning a new instance of the Matrix2D class.
11690
+ * The function scales a specific row in a matrix by a given scalar value.
11691
+ * @param {number} row - The `row` parameter represents the index of the row in the matrix that you
11692
+ * want to scale. It is a number that indicates the position of the row within the matrix.
11693
+ * @param {number} scalar - The scalar parameter is a number that is used to multiply each element in
11694
+ * a specific row of a matrix.
11877
11695
  */
11878
- static translate(vector) {
11879
- return new _Matrix2D([
11880
- [1, 0, vector.x],
11881
- [0, 1, vector.y],
11882
- [0, 0, vector.w]
11883
- ]);
11696
+ _scaleRow(row, scalar) {
11697
+ for (let j = 0; j < this.cols; j++) {
11698
+ let multiplied = this.multiplyFn(this.data[row][j], scalar);
11699
+ if (multiplied === void 0)
11700
+ multiplied = 0;
11701
+ this.data[row][j] = multiplied;
11702
+ }
11884
11703
  }
11885
11704
  /**
11886
- * The function "toVector" returns a new Vector2D object with the values from the first and second elements of the
11887
- * _matrix array.
11888
- * @returns A new instance of the Vector2D class is being returned. The values of the returned vector are taken from
11889
- * the first column of the matrix.
11705
+ * The function `_addScaledRow` multiplies a row in a matrix by a scalar value and adds it to another
11706
+ * row.
11707
+ * @param {number} targetRow - The targetRow parameter represents the index of the row in which the
11708
+ * scaled values will be added.
11709
+ * @param {number} sourceRow - The sourceRow parameter represents the index of the row from which the
11710
+ * values will be scaled and added to the targetRow.
11711
+ * @param {number} scalar - The scalar parameter is a number that is used to scale the values in the
11712
+ * source row before adding them to the target row.
11890
11713
  */
11891
- toVector() {
11892
- return new Vector2D(this._matrix[0][0], this._matrix[1][0]);
11714
+ _addScaledRow(targetRow, sourceRow, scalar) {
11715
+ for (let j = 0; j < this.cols; j++) {
11716
+ let multiplied = this.multiplyFn(this.data[sourceRow][j], scalar);
11717
+ if (multiplied === void 0)
11718
+ multiplied = 0;
11719
+ const scaledValue = multiplied;
11720
+ let added = this.addFn(this.data[targetRow][j], scaledValue);
11721
+ if (added === void 0)
11722
+ added = 0;
11723
+ this.data[targetRow][j] = added;
11724
+ }
11893
11725
  }
11894
11726
  };
11895
11727