data-structure-typed 1.49.3 → 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 (162) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +70 -69
  3. package/README_zh-CN.md +44 -49
  4. package/benchmark/report.html +16 -16
  5. package/benchmark/report.json +187 -187
  6. package/dist/cjs/data-structures/base/iterable-base.d.ts +1 -1
  7. package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
  8. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  9. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +2 -14
  10. package/dist/cjs/data-structures/binary-tree/binary-tree.js +19 -49
  11. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  12. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  13. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  14. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +0 -16
  15. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +1 -43
  16. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  17. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +2 -11
  18. package/dist/cjs/data-structures/graph/abstract-graph.js +3 -19
  19. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  20. package/dist/cjs/data-structures/graph/directed-graph.js +4 -0
  21. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  22. package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
  23. package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
  24. package/dist/cjs/data-structures/hash/hash-map.d.ts +1 -1
  25. package/dist/cjs/data-structures/hash/hash-map.js +2 -2
  26. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  27. package/dist/cjs/data-structures/heap/heap.js +2 -3
  28. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  29. package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
  30. package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
  31. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  32. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  33. package/dist/cjs/data-structures/matrix/index.d.ts +0 -2
  34. package/dist/cjs/data-structures/matrix/index.js +0 -2
  35. package/dist/cjs/data-structures/matrix/index.js.map +1 -1
  36. package/dist/cjs/data-structures/matrix/matrix.d.ts +128 -10
  37. package/dist/cjs/data-structures/matrix/matrix.js +400 -15
  38. package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
  39. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  40. package/dist/cjs/data-structures/queue/deque.d.ts +2 -2
  41. package/dist/cjs/data-structures/queue/deque.js +5 -7
  42. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  43. package/dist/cjs/data-structures/queue/queue.d.ts +1 -1
  44. package/dist/cjs/types/data-structures/base/base.d.ts +1 -1
  45. package/dist/cjs/types/data-structures/heap/heap.d.ts +1 -1
  46. package/dist/cjs/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
  47. package/dist/cjs/utils/utils.d.ts +1 -0
  48. package/dist/cjs/utils/utils.js +6 -1
  49. package/dist/cjs/utils/utils.js.map +1 -1
  50. package/dist/mjs/data-structures/base/iterable-base.d.ts +1 -1
  51. package/dist/mjs/data-structures/binary-tree/avl-tree.js +2 -1
  52. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +2 -14
  53. package/dist/mjs/data-structures/binary-tree/binary-tree.js +20 -50
  54. package/dist/mjs/data-structures/binary-tree/bst.js +2 -1
  55. package/dist/mjs/data-structures/binary-tree/rb-tree.js +2 -1
  56. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +0 -16
  57. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +3 -44
  58. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +2 -11
  59. package/dist/mjs/data-structures/graph/abstract-graph.js +4 -20
  60. package/dist/mjs/data-structures/graph/directed-graph.js +4 -0
  61. package/dist/mjs/data-structures/hash/hash-map.d.ts +1 -1
  62. package/dist/mjs/data-structures/hash/hash-map.js +2 -2
  63. package/dist/mjs/data-structures/heap/heap.js +2 -3
  64. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  65. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +1 -1
  66. package/dist/mjs/data-structures/matrix/index.d.ts +0 -2
  67. package/dist/mjs/data-structures/matrix/index.js +0 -2
  68. package/dist/mjs/data-structures/matrix/matrix.d.ts +128 -10
  69. package/dist/mjs/data-structures/matrix/matrix.js +399 -16
  70. package/dist/mjs/data-structures/queue/deque.d.ts +2 -2
  71. package/dist/mjs/data-structures/queue/deque.js +7 -9
  72. package/dist/mjs/data-structures/queue/queue.d.ts +1 -1
  73. package/dist/mjs/data-structures/queue/queue.js +1 -1
  74. package/dist/mjs/types/data-structures/base/base.d.ts +1 -1
  75. package/dist/mjs/types/data-structures/heap/heap.d.ts +1 -1
  76. package/dist/mjs/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
  77. package/dist/mjs/utils/utils.d.ts +1 -0
  78. package/dist/mjs/utils/utils.js +4 -0
  79. package/dist/umd/data-structure-typed.js +361 -542
  80. package/dist/umd/data-structure-typed.min.js +2 -2
  81. package/dist/umd/data-structure-typed.min.js.map +1 -1
  82. package/package.json +4 -3
  83. package/src/data-structures/base/index.ts +1 -1
  84. package/src/data-structures/base/iterable-base.ts +7 -10
  85. package/src/data-structures/binary-tree/avl-tree.ts +15 -8
  86. package/src/data-structures/binary-tree/binary-tree.ts +57 -74
  87. package/src/data-structures/binary-tree/bst.ts +16 -13
  88. package/src/data-structures/binary-tree/rb-tree.ts +16 -10
  89. package/src/data-structures/binary-tree/tree-multimap.ts +11 -48
  90. package/src/data-structures/graph/abstract-graph.ts +14 -24
  91. package/src/data-structures/graph/directed-graph.ts +8 -6
  92. package/src/data-structures/graph/map-graph.ts +6 -1
  93. package/src/data-structures/graph/undirected-graph.ts +4 -7
  94. package/src/data-structures/hash/hash-map.ts +18 -16
  95. package/src/data-structures/heap/heap.ts +7 -10
  96. package/src/data-structures/heap/max-heap.ts +2 -1
  97. package/src/data-structures/heap/min-heap.ts +2 -1
  98. package/src/data-structures/linked-list/singly-linked-list.ts +3 -5
  99. package/src/data-structures/matrix/index.ts +0 -2
  100. package/src/data-structures/matrix/matrix.ts +442 -13
  101. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -10
  102. package/src/data-structures/queue/deque.ts +18 -39
  103. package/src/data-structures/queue/queue.ts +1 -1
  104. package/src/interfaces/binary-tree.ts +7 -2
  105. package/src/types/common.ts +4 -4
  106. package/src/types/data-structures/base/base.ts +14 -3
  107. package/src/types/data-structures/base/index.ts +1 -1
  108. package/src/types/data-structures/graph/abstract-graph.ts +4 -2
  109. package/src/types/data-structures/hash/hash-map.ts +3 -3
  110. package/src/types/data-structures/heap/heap.ts +2 -2
  111. package/src/types/data-structures/priority-queue/priority-queue.ts +2 -2
  112. package/src/utils/utils.ts +7 -1
  113. package/test/integration/avl-tree.test.ts +18 -1
  114. package/test/integration/bst.test.ts +2 -2
  115. package/test/performance/data-structures/binary-tree/rb-tree.test.ts +1 -1
  116. package/test/performance/data-structures/comparison/comparison.test.ts +18 -23
  117. package/test/performance/data-structures/hash/hash-map.test.ts +3 -4
  118. package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +16 -28
  119. package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +4 -12
  120. package/test/performance/data-structures/queue/deque.test.ts +11 -12
  121. package/test/performance/data-structures/queue/queue.test.ts +15 -14
  122. package/test/performance/data-structures/stack/stack.test.ts +11 -11
  123. package/test/performance/reportor.ts +14 -15
  124. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +14 -3
  125. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +65 -15
  126. package/test/unit/data-structures/binary-tree/bst.test.ts +52 -26
  127. package/test/unit/data-structures/binary-tree/overall.test.ts +16 -18
  128. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +17 -6
  129. package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +37 -21
  130. package/test/unit/data-structures/graph/abstract-graph.test.ts +11 -0
  131. package/test/unit/data-structures/graph/directed-graph.test.ts +112 -40
  132. package/test/unit/data-structures/graph/undirected-graph.test.ts +63 -33
  133. package/test/unit/data-structures/hash/hash-map.test.ts +64 -23
  134. package/test/unit/data-structures/hash/hash-table.test.ts +0 -4
  135. package/test/unit/data-structures/heap/heap.test.ts +0 -1
  136. package/test/unit/data-structures/heap/min-heap.test.ts +6 -5
  137. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +30 -4
  138. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +3 -4
  139. package/test/unit/data-structures/matrix/matrix.test.ts +345 -52
  140. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +1 -1
  141. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +0 -1
  142. package/test/unit/data-structures/queue/deque.test.ts +0 -6
  143. package/test/unit/data-structures/queue/queue.test.ts +1 -1
  144. package/test/unit/data-structures/stack/stack.test.ts +3 -4
  145. package/test/unit/unrestricted-interconversion.test.ts +50 -44
  146. package/test/utils/big-o.ts +14 -14
  147. package/test/utils/performanc.ts +1 -1
  148. package/typedoc.json +30 -0
  149. package/dist/cjs/data-structures/matrix/matrix2d.d.ts +0 -107
  150. package/dist/cjs/data-structures/matrix/matrix2d.js +0 -200
  151. package/dist/cjs/data-structures/matrix/matrix2d.js.map +0 -1
  152. package/dist/cjs/data-structures/matrix/vector2d.d.ts +0 -200
  153. package/dist/cjs/data-structures/matrix/vector2d.js +0 -291
  154. package/dist/cjs/data-structures/matrix/vector2d.js.map +0 -1
  155. package/dist/mjs/data-structures/matrix/matrix2d.d.ts +0 -107
  156. package/dist/mjs/data-structures/matrix/matrix2d.js +0 -196
  157. package/dist/mjs/data-structures/matrix/vector2d.d.ts +0 -200
  158. package/dist/mjs/data-structures/matrix/vector2d.js +0 -289
  159. package/src/data-structures/matrix/matrix2d.ts +0 -211
  160. package/src/data-structures/matrix/vector2d.ts +0 -315
  161. package/test/unit/data-structures/matrix/matrix2d.test.ts +0 -345
  162. package/test/unit/data-structures/matrix/vector2d.test.ts +0 -171
@@ -398,11 +398,10 @@ export class Heap extends IterableElementBase {
398
398
  _sinkDown(index, halfLength) {
399
399
  const element = this.elements[index];
400
400
  while (index < halfLength) {
401
- let left = index << 1 | 1;
401
+ let left = (index << 1) | 1;
402
402
  const right = left + 1;
403
403
  let minItem = this.elements[left];
404
- if (right < this.elements.length &&
405
- this.options.comparator(minItem, this.elements[right]) > 0) {
404
+ if (right < this.elements.length && this.options.comparator(minItem, this.elements[right]) > 0) {
406
405
  left = right;
407
406
  minItem = this.elements[right];
408
407
  }
@@ -5,8 +5,8 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { ElementCallback } from "../../types";
9
- import { IterableElementBase } from "../base";
8
+ import type { ElementCallback } from '../../types';
9
+ import { IterableElementBase } from '../base';
10
10
  export declare class SinglyLinkedListNode<E = any> {
11
11
  value: E;
12
12
  next: SinglyLinkedListNode<E> | undefined;
@@ -1,4 +1,4 @@
1
- import { IterableElementBase } from "../base";
1
+ import { IterableElementBase } from '../base';
2
2
  export class SinglyLinkedListNode {
3
3
  value;
4
4
  next;
@@ -1,4 +1,2 @@
1
1
  export * from './matrix';
2
- export * from './vector2d';
3
- export * from './matrix2d';
4
2
  export * from './navigator';
@@ -1,4 +1,2 @@
1
1
  export * from './matrix';
2
- export * from './vector2d';
3
- export * from './matrix2d';
4
2
  export * from './navigator';
@@ -5,17 +5,135 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- export declare class MatrixNTI2D<V = any> {
9
- protected readonly _matrix: Array<Array<V>>;
8
+ export declare class Matrix {
10
9
  /**
11
- * The constructor creates a matrix with the specified number of rows and columns, and initializes all elements to a
12
- * given initial value or 0 if not provided.
13
- * @param options - An object containing the following properties:
10
+ * The constructor function initializes a matrix object with the provided data and options, or with
11
+ * default values if no options are provided.
12
+ * @param {number[][]} data - A 2D array of numbers representing the data for the matrix.
13
+ * @param [options] - The `options` parameter is an optional object that can contain the following
14
+ * properties:
14
15
  */
15
- constructor(options: {
16
- row: number;
17
- col: number;
18
- initialVal?: V;
16
+ constructor(data: number[][], options?: {
17
+ rows?: number;
18
+ cols?: number;
19
+ addFn?: (a: number, b: number) => any;
20
+ subtractFn?: (a: number, b: number) => any;
21
+ multiplyFn?: (a: number, b: number) => any;
19
22
  });
20
- toArray(): Array<Array<V>>;
23
+ protected _rows: number;
24
+ get rows(): number;
25
+ protected _cols: number;
26
+ get cols(): number;
27
+ protected _data: number[][];
28
+ get data(): number[][];
29
+ get addFn(): (a: number | undefined, b: number) => number | undefined;
30
+ get subtractFn(): (a: number, b: number) => number;
31
+ get multiplyFn(): (a: number, b: number) => number;
32
+ /**
33
+ * The `get` function returns the value at the specified row and column index if it is a valid index.
34
+ * @param {number} row - The `row` parameter represents the row index of the element you want to
35
+ * retrieve from the data array.
36
+ * @param {number} col - The parameter "col" represents the column number of the element you want to
37
+ * retrieve from the data array.
38
+ * @returns The `get` function returns a number if the provided row and column indices are valid.
39
+ * Otherwise, it returns `undefined`.
40
+ */
41
+ get(row: number, col: number): number | undefined;
42
+ /**
43
+ * The set function updates the value at a specified row and column in a two-dimensional array.
44
+ * @param {number} row - The "row" parameter represents the row index of the element in a
45
+ * two-dimensional array or matrix. It specifies the row where the value will be set.
46
+ * @param {number} col - The "col" parameter represents the column index of the element in a
47
+ * two-dimensional array.
48
+ * @param {number} value - The value parameter represents the number that you want to set at the
49
+ * specified row and column in the data array.
50
+ * @returns a boolean value. It returns true if the index (row, col) is valid and the value is
51
+ * successfully set in the data array. It returns false if the index is invalid and the value is not
52
+ * set.
53
+ */
54
+ set(row: number, col: number, value: number): boolean;
55
+ /**
56
+ * The function checks if the dimensions of the given matrix match the dimensions of the current
57
+ * matrix.
58
+ * @param {Matrix} matrix - The parameter `matrix` is of type `Matrix`.
59
+ * @returns a boolean value.
60
+ */
61
+ isMatchForCalculate(matrix: Matrix): boolean;
62
+ /**
63
+ * The `add` function adds two matrices together, returning a new matrix with the result.
64
+ * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
65
+ * @returns The `add` method returns a new `Matrix` object that represents the result of adding the
66
+ * current matrix with the provided `matrix` parameter.
67
+ */
68
+ add(matrix: Matrix): Matrix | undefined;
69
+ /**
70
+ * The `subtract` function performs element-wise subtraction between two matrices and returns a new
71
+ * matrix with the result.
72
+ * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class. It
73
+ * represents the matrix that you want to subtract from the current matrix.
74
+ * @returns a new Matrix object with the result of the subtraction operation.
75
+ */
76
+ subtract(matrix: Matrix): Matrix | undefined;
77
+ /**
78
+ * The `multiply` function performs matrix multiplication between two matrices and returns the result
79
+ * as a new matrix.
80
+ * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
81
+ * @returns a new Matrix object.
82
+ */
83
+ multiply(matrix: Matrix): Matrix | undefined;
84
+ /**
85
+ * The transpose function takes a matrix and returns a new matrix that is the transpose of the
86
+ * original matrix.
87
+ * @returns The transpose() function returns a new Matrix object with the transposed data.
88
+ */
89
+ transpose(): Matrix;
90
+ /**
91
+ * The `inverse` function calculates the inverse of a square matrix using Gaussian elimination.
92
+ * @returns a Matrix object, which represents the inverse of the original matrix.
93
+ */
94
+ inverse(): Matrix | undefined;
95
+ /**
96
+ * The dot function calculates the dot product of two matrices and returns a new matrix.
97
+ * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
98
+ * @returns a new Matrix object.
99
+ */
100
+ dot(matrix: Matrix): Matrix | undefined;
101
+ protected _addFn(a: number | undefined, b: number): number | undefined;
102
+ protected _subtractFn(a: number, b: number): number;
103
+ protected _multiplyFn(a: number, b: number): number;
104
+ /**
105
+ * The function checks if a given row and column index is valid within a specified range.
106
+ * @param {number} row - The `row` parameter represents the row index of a two-dimensional array or
107
+ * matrix. It is a number that indicates the specific row in the matrix.
108
+ * @param {number} col - The "col" parameter represents the column index in a two-dimensional array
109
+ * or grid. It is used to check if the given column index is valid within the bounds of the grid.
110
+ * @returns A boolean value is being returned.
111
+ */
112
+ protected isValidIndex(row: number, col: number): boolean;
113
+ /**
114
+ * The function `_swapRows` swaps the positions of two rows in an array.
115
+ * @param {number} row1 - The `row1` parameter is the index of the first row that you want to swap.
116
+ * @param {number} row2 - The `row2` parameter is the index of the second row that you want to swap
117
+ * with the first row.
118
+ */
119
+ protected _swapRows(row1: number, row2: number): void;
120
+ /**
121
+ * The function scales a specific row in a matrix by a given scalar value.
122
+ * @param {number} row - The `row` parameter represents the index of the row in the matrix that you
123
+ * want to scale. It is a number that indicates the position of the row within the matrix.
124
+ * @param {number} scalar - The scalar parameter is a number that is used to multiply each element in
125
+ * a specific row of a matrix.
126
+ */
127
+ protected _scaleRow(row: number, scalar: number): void;
128
+ /**
129
+ * The function `_addScaledRow` multiplies a row in a matrix by a scalar value and adds it to another
130
+ * row.
131
+ * @param {number} targetRow - The targetRow parameter represents the index of the row in which the
132
+ * scaled values will be added.
133
+ * @param {number} sourceRow - The sourceRow parameter represents the index of the row from which the
134
+ * values will be scaled and added to the targetRow.
135
+ * @param {number} scalar - The scalar parameter is a number that is used to scale the values in the
136
+ * source row before adding them to the target row.
137
+ */
138
+ protected _addScaledRow(targetRow: number, sourceRow: number, scalar: number): void;
21
139
  }
@@ -5,21 +5,404 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- // todo need to be improved
9
- export class MatrixNTI2D {
10
- _matrix;
11
- /**
12
- * The constructor creates a matrix with the specified number of rows and columns, and initializes all elements to a
13
- * given initial value or 0 if not provided.
14
- * @param options - An object containing the following properties:
15
- */
16
- constructor(options) {
17
- const { row, col, initialVal } = options;
18
- this._matrix = new Array(row).fill(undefined).map(() => new Array(col).fill(initialVal || 0));
19
- }
20
- /* The `toArray` method returns the matrix as a two-dimensional array. It converts the internal representation of the
21
- matrix, which is an array of arrays, into a format that is more commonly used in JavaScript. */
22
- toArray() {
23
- return this._matrix;
8
+ export class Matrix {
9
+ /**
10
+ * The constructor function initializes a matrix object with the provided data and options, or with
11
+ * default values if no options are provided.
12
+ * @param {number[][]} data - A 2D array of numbers representing the data for the matrix.
13
+ * @param [options] - The `options` parameter is an optional object that can contain the following
14
+ * properties:
15
+ */
16
+ constructor(data, options) {
17
+ if (options) {
18
+ const { rows, cols, addFn, subtractFn, multiplyFn } = options;
19
+ if (typeof rows === 'number' && rows > 0)
20
+ this._rows = rows;
21
+ else
22
+ this._rows = data.length;
23
+ if (typeof cols === 'number' && cols > 0)
24
+ this._cols = cols;
25
+ else
26
+ this._cols = data[0]?.length || 0;
27
+ if (addFn)
28
+ this._addFn = addFn;
29
+ if (subtractFn)
30
+ this._subtractFn = subtractFn;
31
+ if (multiplyFn)
32
+ this._multiplyFn = multiplyFn;
33
+ }
34
+ else {
35
+ this._rows = data.length;
36
+ this._cols = data[0]?.length ?? 0;
37
+ }
38
+ if (data.length > 0) {
39
+ this._data = data;
40
+ }
41
+ else {
42
+ this._data = [];
43
+ for (let i = 0; i < this.rows; i++) {
44
+ this._data[i] = new Array(this.cols).fill(0);
45
+ }
46
+ }
47
+ }
48
+ _rows = 0;
49
+ get rows() {
50
+ return this._rows;
51
+ }
52
+ _cols = 0;
53
+ get cols() {
54
+ return this._cols;
55
+ }
56
+ _data;
57
+ get data() {
58
+ return this._data;
59
+ }
60
+ get addFn() {
61
+ return this._addFn;
62
+ }
63
+ get subtractFn() {
64
+ return this._subtractFn;
65
+ }
66
+ get multiplyFn() {
67
+ return this._multiplyFn;
68
+ }
69
+ /**
70
+ * The `get` function returns the value at the specified row and column index if it is a valid index.
71
+ * @param {number} row - The `row` parameter represents the row index of the element you want to
72
+ * retrieve from the data array.
73
+ * @param {number} col - The parameter "col" represents the column number of the element you want to
74
+ * retrieve from the data array.
75
+ * @returns The `get` function returns a number if the provided row and column indices are valid.
76
+ * Otherwise, it returns `undefined`.
77
+ */
78
+ get(row, col) {
79
+ if (this.isValidIndex(row, col)) {
80
+ return this.data[row][col];
81
+ }
82
+ }
83
+ /**
84
+ * The set function updates the value at a specified row and column in a two-dimensional array.
85
+ * @param {number} row - The "row" parameter represents the row index of the element in a
86
+ * two-dimensional array or matrix. It specifies the row where the value will be set.
87
+ * @param {number} col - The "col" parameter represents the column index of the element in a
88
+ * two-dimensional array.
89
+ * @param {number} value - The value parameter represents the number that you want to set at the
90
+ * specified row and column in the data array.
91
+ * @returns a boolean value. It returns true if the index (row, col) is valid and the value is
92
+ * successfully set in the data array. It returns false if the index is invalid and the value is not
93
+ * set.
94
+ */
95
+ set(row, col, value) {
96
+ if (this.isValidIndex(row, col)) {
97
+ this.data[row][col] = value;
98
+ return true;
99
+ }
100
+ return false;
101
+ }
102
+ /**
103
+ * The function checks if the dimensions of the given matrix match the dimensions of the current
104
+ * matrix.
105
+ * @param {Matrix} matrix - The parameter `matrix` is of type `Matrix`.
106
+ * @returns a boolean value.
107
+ */
108
+ isMatchForCalculate(matrix) {
109
+ return this.rows === matrix.rows && this.cols === matrix.cols;
110
+ }
111
+ /**
112
+ * The `add` function adds two matrices together, returning a new matrix with the result.
113
+ * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
114
+ * @returns The `add` method returns a new `Matrix` object that represents the result of adding the
115
+ * current matrix with the provided `matrix` parameter.
116
+ */
117
+ add(matrix) {
118
+ if (!this.isMatchForCalculate(matrix)) {
119
+ throw new Error('Matrix dimensions must match for addition.');
120
+ }
121
+ const resultData = [];
122
+ for (let i = 0; i < this.rows; i++) {
123
+ resultData[i] = [];
124
+ for (let j = 0; j < this.cols; j++) {
125
+ const a = this.get(i, j), b = matrix.get(i, j);
126
+ if (a !== undefined && b !== undefined) {
127
+ const added = this._addFn(a, b);
128
+ if (added) {
129
+ resultData[i][j] = added;
130
+ }
131
+ }
132
+ }
133
+ }
134
+ return new Matrix(resultData, {
135
+ rows: this.rows,
136
+ cols: this.cols,
137
+ addFn: this.addFn,
138
+ subtractFn: this.subtractFn,
139
+ multiplyFn: this.multiplyFn
140
+ });
141
+ }
142
+ /**
143
+ * The `subtract` function performs element-wise subtraction between two matrices and returns a new
144
+ * matrix with the result.
145
+ * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class. It
146
+ * represents the matrix that you want to subtract from the current matrix.
147
+ * @returns a new Matrix object with the result of the subtraction operation.
148
+ */
149
+ subtract(matrix) {
150
+ if (!this.isMatchForCalculate(matrix)) {
151
+ throw new Error('Matrix dimensions must match for subtraction.');
152
+ }
153
+ const resultData = [];
154
+ for (let i = 0; i < this.rows; i++) {
155
+ resultData[i] = [];
156
+ for (let j = 0; j < this.cols; j++) {
157
+ const a = this.get(i, j), b = matrix.get(i, j);
158
+ if (a !== undefined && b !== undefined) {
159
+ const subtracted = this._subtractFn(a, b);
160
+ if (subtracted) {
161
+ resultData[i][j] = subtracted;
162
+ }
163
+ }
164
+ }
165
+ }
166
+ return new Matrix(resultData, {
167
+ rows: this.rows,
168
+ cols: this.cols,
169
+ addFn: this.addFn,
170
+ subtractFn: this.subtractFn,
171
+ multiplyFn: this.multiplyFn
172
+ });
173
+ }
174
+ /**
175
+ * The `multiply` function performs matrix multiplication between two matrices and returns the result
176
+ * as a new matrix.
177
+ * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
178
+ * @returns a new Matrix object.
179
+ */
180
+ multiply(matrix) {
181
+ if (this.cols !== matrix.rows) {
182
+ throw new Error('Matrix dimensions must be compatible for multiplication (A.cols = B.rows).');
183
+ }
184
+ const resultData = [];
185
+ for (let i = 0; i < this.rows; i++) {
186
+ resultData[i] = [];
187
+ for (let j = 0; j < matrix.cols; j++) {
188
+ let sum;
189
+ for (let k = 0; k < this.cols; k++) {
190
+ const a = this.get(i, k), b = matrix.get(k, j);
191
+ if (a !== undefined && b !== undefined) {
192
+ const multiplied = this.multiplyFn(a, b);
193
+ if (multiplied !== undefined) {
194
+ sum = this.addFn(sum, multiplied);
195
+ }
196
+ }
197
+ }
198
+ if (sum !== undefined)
199
+ resultData[i][j] = sum;
200
+ }
201
+ }
202
+ return new Matrix(resultData, {
203
+ rows: this.rows,
204
+ cols: matrix.cols,
205
+ addFn: this.addFn,
206
+ subtractFn: this.subtractFn,
207
+ multiplyFn: this.multiplyFn
208
+ });
209
+ }
210
+ /**
211
+ * The transpose function takes a matrix and returns a new matrix that is the transpose of the
212
+ * original matrix.
213
+ * @returns The transpose() function returns a new Matrix object with the transposed data.
214
+ */
215
+ transpose() {
216
+ if (this.data.some(row => row.length !== this.rows)) {
217
+ throw new Error('Matrix must be rectangular for transposition.');
218
+ }
219
+ const resultData = [];
220
+ for (let j = 0; j < this.cols; j++) {
221
+ resultData[j] = [];
222
+ for (let i = 0; i < this.rows; i++) {
223
+ const trans = this.get(i, j);
224
+ if (trans !== undefined)
225
+ resultData[j][i] = trans;
226
+ }
227
+ }
228
+ return new Matrix(resultData, {
229
+ rows: this.cols,
230
+ cols: this.rows,
231
+ addFn: this.addFn,
232
+ subtractFn: this.subtractFn,
233
+ multiplyFn: this.multiplyFn
234
+ });
235
+ }
236
+ /**
237
+ * The `inverse` function calculates the inverse of a square matrix using Gaussian elimination.
238
+ * @returns a Matrix object, which represents the inverse of the original matrix.
239
+ */
240
+ inverse() {
241
+ // Check if the matrix is square
242
+ if (this.rows !== this.cols) {
243
+ throw new Error('Matrix must be square for inversion.');
244
+ }
245
+ // Create an augmented matrix [this | I]
246
+ const augmentedMatrixData = [];
247
+ for (let i = 0; i < this.rows; i++) {
248
+ augmentedMatrixData[i] = this.data[i].slice(); // Copy the original matrix
249
+ for (let j = 0; j < this.cols; j++) {
250
+ augmentedMatrixData[i][this.cols + j] = i === j ? 1 : 0; // Append the identity matrix
251
+ }
252
+ }
253
+ const augmentedMatrix = new Matrix(augmentedMatrixData, {
254
+ rows: this.rows,
255
+ cols: this.cols * 2,
256
+ addFn: this.addFn,
257
+ subtractFn: this.subtractFn,
258
+ multiplyFn: this.multiplyFn
259
+ });
260
+ // Apply Gaussian elimination to transform the left half into the identity matrix
261
+ for (let i = 0; i < this.rows; i++) {
262
+ // Find pivot
263
+ let pivotRow = i;
264
+ while (pivotRow < this.rows && augmentedMatrix.get(pivotRow, i) === 0) {
265
+ pivotRow++;
266
+ }
267
+ if (pivotRow === this.rows) {
268
+ // Matrix is singular, and its inverse does not exist
269
+ throw new Error('Matrix is singular, and its inverse does not exist.');
270
+ }
271
+ // Swap rows to make the pivot the current row
272
+ augmentedMatrix._swapRows(i, pivotRow);
273
+ // Scale the pivot row to make the pivot element 1
274
+ const pivotElement = augmentedMatrix.get(i, i) ?? 1;
275
+ if (pivotElement === 0) {
276
+ // Handle division by zero
277
+ throw new Error('Matrix is singular, and its inverse does not exist (division by zero).');
278
+ }
279
+ augmentedMatrix._scaleRow(i, 1 / pivotElement);
280
+ // Eliminate other rows to make elements in the current column zero
281
+ for (let j = 0; j < this.rows; j++) {
282
+ if (j !== i) {
283
+ let factor = augmentedMatrix.get(j, i);
284
+ if (factor === undefined)
285
+ factor = 0;
286
+ augmentedMatrix._addScaledRow(j, i, -factor);
287
+ }
288
+ }
289
+ }
290
+ // Extract the right half of the augmented matrix as the inverse
291
+ const inverseData = [];
292
+ for (let i = 0; i < this.rows; i++) {
293
+ inverseData[i] = augmentedMatrix.data[i].slice(this.cols);
294
+ }
295
+ return new Matrix(inverseData, {
296
+ rows: this.rows,
297
+ cols: this.cols,
298
+ addFn: this.addFn,
299
+ subtractFn: this.subtractFn,
300
+ multiplyFn: this.multiplyFn
301
+ });
302
+ }
303
+ /**
304
+ * The dot function calculates the dot product of two matrices and returns a new matrix.
305
+ * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
306
+ * @returns a new Matrix object.
307
+ */
308
+ dot(matrix) {
309
+ if (this.cols !== matrix.rows) {
310
+ throw new Error('Number of columns in the first matrix must be equal to the number of rows in the second matrix for dot product.');
311
+ }
312
+ const resultData = [];
313
+ for (let i = 0; i < this.rows; i++) {
314
+ resultData[i] = [];
315
+ for (let j = 0; j < matrix.cols; j++) {
316
+ let sum;
317
+ for (let k = 0; k < this.cols; k++) {
318
+ const a = this.get(i, k), b = matrix.get(k, j);
319
+ if (a !== undefined && b !== undefined) {
320
+ const multiplied = this.multiplyFn(a, b);
321
+ if (multiplied !== undefined) {
322
+ sum = this.addFn(sum, multiplied);
323
+ }
324
+ }
325
+ }
326
+ if (sum !== undefined)
327
+ resultData[i][j] = sum;
328
+ }
329
+ }
330
+ return new Matrix(resultData, {
331
+ rows: this.rows,
332
+ cols: matrix.cols,
333
+ addFn: this.addFn,
334
+ subtractFn: this.subtractFn,
335
+ multiplyFn: this.multiplyFn
336
+ });
337
+ }
338
+ _addFn(a, b) {
339
+ if (a === undefined)
340
+ return b;
341
+ return a + b;
342
+ }
343
+ _subtractFn(a, b) {
344
+ return a - b;
345
+ }
346
+ _multiplyFn(a, b) {
347
+ return a * b;
348
+ }
349
+ /**
350
+ * The function checks if a given row and column index is valid within a specified range.
351
+ * @param {number} row - The `row` parameter represents the row index of a two-dimensional array or
352
+ * matrix. It is a number that indicates the specific row in the matrix.
353
+ * @param {number} col - The "col" parameter represents the column index in a two-dimensional array
354
+ * or grid. It is used to check if the given column index is valid within the bounds of the grid.
355
+ * @returns A boolean value is being returned.
356
+ */
357
+ isValidIndex(row, col) {
358
+ return row >= 0 && row < this.rows && col >= 0 && col < this.cols;
359
+ }
360
+ /**
361
+ * The function `_swapRows` swaps the positions of two rows in an array.
362
+ * @param {number} row1 - The `row1` parameter is the index of the first row that you want to swap.
363
+ * @param {number} row2 - The `row2` parameter is the index of the second row that you want to swap
364
+ * with the first row.
365
+ */
366
+ _swapRows(row1, row2) {
367
+ const temp = this.data[row1];
368
+ this.data[row1] = this.data[row2];
369
+ this.data[row2] = temp;
370
+ }
371
+ /**
372
+ * The function scales a specific row in a matrix by a given scalar value.
373
+ * @param {number} row - The `row` parameter represents the index of the row in the matrix that you
374
+ * want to scale. It is a number that indicates the position of the row within the matrix.
375
+ * @param {number} scalar - The scalar parameter is a number that is used to multiply each element in
376
+ * a specific row of a matrix.
377
+ */
378
+ _scaleRow(row, scalar) {
379
+ for (let j = 0; j < this.cols; j++) {
380
+ let multiplied = this.multiplyFn(this.data[row][j], scalar);
381
+ if (multiplied === undefined)
382
+ multiplied = 0;
383
+ this.data[row][j] = multiplied;
384
+ }
385
+ }
386
+ /**
387
+ * The function `_addScaledRow` multiplies a row in a matrix by a scalar value and adds it to another
388
+ * row.
389
+ * @param {number} targetRow - The targetRow parameter represents the index of the row in which the
390
+ * scaled values will be added.
391
+ * @param {number} sourceRow - The sourceRow parameter represents the index of the row from which the
392
+ * values will be scaled and added to the targetRow.
393
+ * @param {number} scalar - The scalar parameter is a number that is used to scale the values in the
394
+ * source row before adding them to the target row.
395
+ */
396
+ _addScaledRow(targetRow, sourceRow, scalar) {
397
+ for (let j = 0; j < this.cols; j++) {
398
+ let multiplied = this.multiplyFn(this.data[sourceRow][j], scalar);
399
+ if (multiplied === undefined)
400
+ multiplied = 0;
401
+ const scaledValue = multiplied;
402
+ let added = this.addFn(this.data[targetRow][j], scaledValue);
403
+ if (added === undefined)
404
+ added = 0;
405
+ this.data[targetRow][j] = added;
406
+ }
24
407
  }
25
408
  }
@@ -5,8 +5,8 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { ElementCallback, IterableWithSizeOrLength } from "../../types";
9
- import { IterableElementBase } from "../base";
8
+ import type { ElementCallback, IterableWithSizeOrLength } from '../../types';
9
+ import { IterableElementBase } from '../base';
10
10
  /**
11
11
  * 1. Operations at Both Ends: Supports adding and removing elements at both the front and back of the queue. This allows it to be used as a stack (last in, first out) and a queue (first in, first out).
12
12
  * 2. Efficient Random Access: Being based on an array, it offers fast random access capability, allowing constant time access to any element.