ml-matrix 6.10.1 → 6.10.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +111 -100
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -53,78 +53,84 @@ var A = new Matrix([
|
|
|
53
53
|
[1, 1],
|
|
54
54
|
[2, 2],
|
|
55
55
|
]);
|
|
56
|
+
|
|
56
57
|
var B = new Matrix([
|
|
57
58
|
[3, 3],
|
|
58
59
|
[1, 1],
|
|
59
60
|
]);
|
|
61
|
+
|
|
60
62
|
var C = new Matrix([
|
|
61
63
|
[3, 3],
|
|
62
64
|
[1, 1],
|
|
63
65
|
]);
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
#### Operations
|
|
69
|
+
```js
|
|
70
|
+
const addition = Matrix.add(A, B); // addition = Matrix [[4, 4], [3, 3], rows: 2, columns: 2]
|
|
71
|
+
const subtraction = Matrix.sub(A, B); // subtraction = Matrix [[-2, -2], [1, 1], rows: 2, columns: 2]
|
|
72
|
+
const multiplication = A.mmul(B); // multiplication = Matrix [[4, 4], [8, 8], rows: 2, columns: 2]
|
|
73
|
+
const mulByNumber = Matrix.mul(A, 10); // mulByNumber = Matrix [[10, 10], [20, 20], rows: 2, columns: 2]
|
|
74
|
+
const divByNumber = Matrix.div(A, 10); // divByNumber = Matrix [[0.1, 0.1], [0.2, 0.2], rows: 2, columns: 2]
|
|
75
|
+
const modulo = Matrix.mod(B, 2); // modulo = Matrix [[1, 1], [1, 1], rows: 2, columns: 2]
|
|
76
|
+
const maxMatrix = Matrix.max(A, B); // max = Matrix [[3, 3], [2, 2], rows: 2, columns: 2]
|
|
77
|
+
const minMatrix = Matrix.min(A, B); // max = Matrix [[1, 1], [1, 1], rows: 2, columns: 2]
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
#### Inplace Operations
|
|
81
|
+
```js
|
|
82
|
+
C.add(A); // => C = C + A
|
|
83
|
+
C.sub(A); // => C = C - A
|
|
84
|
+
C.mul(10); // => C = 10 * C
|
|
85
|
+
C.div(10); // => C = C / 10
|
|
86
|
+
C.mod(2); // => C = C % 2
|
|
87
|
+
```
|
|
64
88
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
//
|
|
68
|
-
|
|
69
|
-
// operations :
|
|
70
|
-
const addition = Matrix.add(A, B); // addition = Matrix [[4, 4], [3, 3], rows: 2, columns: 2]
|
|
71
|
-
const subtraction = Matrix.sub(A, B); // subtraction = Matrix [[-2, -2], [1, 1], rows: 2, columns: 2]
|
|
72
|
-
const multiplication = A.mmul(B); // multiplication = Matrix [[4, 4], [8, 8], rows: 2, columns: 2]
|
|
73
|
-
const mulByNumber = Matrix.mul(A, 10); // mulByNumber = Matrix [[10, 10], [20, 20], rows: 2, columns: 2]
|
|
74
|
-
const divByNumber = Matrix.div(A, 10); // divByNumber = Matrix [[0.1, 0.1], [0.2, 0.2], rows: 2, columns: 2]
|
|
75
|
-
const modulo = Matrix.mod(B, 2); // modulo = Matrix [[ 1, 1], [1, 1], rows: 2, columns: 2]
|
|
76
|
-
const maxMatrix = Matrix.max(A, B); // max = Matrix [[3, 3], [2, 2], rows: 2, columns: 2]
|
|
77
|
-
const minMatrix = Matrix.min(A, B); // max = Matrix [[1, 1], [1, 1], rows: 2, columns: 2]
|
|
78
|
-
|
|
79
|
-
// Inplace operations : (consider that Cinit = C before all the operations below)
|
|
80
|
-
C.add(A); // => C = Cinit + A
|
|
81
|
-
C.sub(A); // => C = Cinit
|
|
82
|
-
C.mul(10); // => C = 10 * Cinit
|
|
83
|
-
C.div(10); // => C = Cinit
|
|
84
|
-
C.mod(2); // => C = Cinit % 2
|
|
85
|
-
|
|
86
|
-
// Standard Math operations : (abs, cos, round, etc.)
|
|
89
|
+
#### Math Operations
|
|
90
|
+
```js
|
|
91
|
+
// Standard Math operations: (abs, cos, round, etc.)
|
|
87
92
|
var A = new Matrix([
|
|
88
|
-
[1,
|
|
93
|
+
[ 1, 1],
|
|
89
94
|
[-1, -1],
|
|
90
95
|
]);
|
|
91
|
-
var exponential = Matrix.exp(A); // exponential = Matrix [[Math.exp(1), Math.exp(1)], [Math.exp(-1), Math.exp(-1)], rows: 2, columns: 2].
|
|
92
|
-
var cosinus = Matrix.cos(A); // cosinus = Matrix [[Math.cos(1), Math.cos(1)], [Math.cos(-1), Math.cos(-1)], rows: 2, columns: 2].
|
|
93
|
-
var absolute = Matrix.abs(A); // expon = absolute [[1, 1], [1, 1], rows: 2, columns: 2].
|
|
94
|
-
// you can use 'abs', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atanh', 'cbrt', 'ceil', 'clz32', 'cos', 'cosh', 'exp', 'expm1', 'floor', 'fround', 'log', 'log1p', 'log10', 'log2', 'round', 'sign', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc'
|
|
95
|
-
// Note : you can do it inplace too as A.abs()
|
|
96
|
-
|
|
97
|
-
// ============================
|
|
98
|
-
// Manipulation of the matrix :
|
|
99
|
-
// =============================
|
|
100
|
-
|
|
101
|
-
var numberRows = A.rows; // A has 2 rows
|
|
102
|
-
var numberCols = A.columns; // A has 2 columns
|
|
103
|
-
var firstValue = A.get(0, 0); // get(rows, columns)
|
|
104
|
-
var numberElements = A.size; // 2 * 2 = 4 elements
|
|
105
|
-
var isRow = A.isRowVector(); // false because A has more that 1 row
|
|
106
|
-
var isColumn = A.isColumnVector(); // false because A has more that 1 column
|
|
107
|
-
var isSquare = A.isSquare(); // true, because A is 2 * 2 matrix
|
|
108
|
-
var isSym = A.isSymmetric(); // false, because A is not symmetric
|
|
109
|
-
// remember : A = Matrix [[1, 1], [-1, -1], rows: 2, columns: 2]
|
|
110
|
-
A.set(1, 0, 10); // A = Matrix [[1, 1], [10, -1], rows: 2, columns: 2]. We have change the second row and the first column
|
|
111
|
-
var diag = A.diag(); // diag = [1, -1], i.e values in the diagonal.
|
|
112
|
-
var m = A.mean(); // m = 2.75
|
|
113
|
-
var product = A.prod(); // product = -10, i.e product of all values of the matrix
|
|
114
|
-
var norm = A.norm(); // norm = 10.14889156509222, i.e Frobenius norm of the matrix
|
|
115
|
-
var transpose = A.transpose(); // transpose = Matrix [[1, 10], [1, -1], rows: 2, columns: 2]
|
|
116
|
-
|
|
117
|
-
// ============================
|
|
118
|
-
// Instantiation of matrix :
|
|
119
|
-
// =============================
|
|
120
96
|
|
|
97
|
+
var exponential = Matrix.exp(A); // exponential = Matrix [[Math.exp(1), Math.exp(1)], [Math.exp(-1), Math.exp(-1)], rows: 2, columns: 2].
|
|
98
|
+
var cosinus = Matrix.cos(A); // cosinus = Matrix [[Math.cos(1), Math.cos(1)], [Math.cos(-1), Math.cos(-1)], rows: 2, columns: 2].
|
|
99
|
+
var absolute = Matrix.abs(A); // absolute = Matrix [[1, 1], [1, 1], rows: 2, columns: 2].
|
|
100
|
+
// Note: you can do it inplace too as A.abs()
|
|
101
|
+
```
|
|
102
|
+
Available Methods:
|
|
103
|
+
```js
|
|
104
|
+
abs, acos, acosh, asin, asinh, atan, atanh, cbrt, ceil, clz32, cos, cosh, exp, expm1, floor, fround, log, log1p, log10, log2, round, sign, sin, sinh, sqrt, tan, tanh, trunc
|
|
105
|
+
```
|
|
106
|
+
#### Manipulation of the matrix
|
|
107
|
+
```js
|
|
108
|
+
// remember: A = Matrix [[1, 1], [-1, -1], rows: 2, columns: 2]
|
|
109
|
+
|
|
110
|
+
var numberRows = A.rows; // A has 2 rows
|
|
111
|
+
var numberCols = A.columns; // A has 2 columns
|
|
112
|
+
var firstValue = A.get(0, 0); // get(rows, columns)
|
|
113
|
+
var numberElements = A.size; // 2 * 2 = 4 elements
|
|
114
|
+
var isRow = A.isRowVector(); // false because A has more than 1 row
|
|
115
|
+
var isColumn = A.isColumnVector(); // false because A has more than 1 column
|
|
116
|
+
var isSquare = A.isSquare(); // true, because A is 2 * 2 matrix
|
|
117
|
+
var isSym = A.isSymmetric(); // false, because A is not symmetric
|
|
118
|
+
A.set(1, 0, 10); // A = Matrix [[1, 1], [10, -1], rows: 2, columns: 2]. We have changed the second row and the first column
|
|
119
|
+
var diag = A.diag(); // diag = [1, -1] (values in the diagonal)
|
|
120
|
+
var m = A.mean(); // m = 2.75
|
|
121
|
+
var product = A.prod(); // product = -10 (product of all values of the matrix)
|
|
122
|
+
var norm = A.norm(); // norm = 10.14889156509222 (Frobenius norm of the matrix)
|
|
123
|
+
var transpose = A.transpose(); // transpose = Matrix [[1, 10], [1, -1], rows: 2, columns: 2]
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
#### Instantiation of matrix
|
|
127
|
+
```js
|
|
121
128
|
var z = Matrix.zeros(3, 2); // z = Matrix [[0, 0], [0, 0], [0, 0], rows: 3, columns: 2]
|
|
122
|
-
var z = Matrix.ones(2, 3);
|
|
123
|
-
var z = Matrix.eye(3, 4);
|
|
129
|
+
var z = Matrix.ones(2, 3); // z = Matrix [[1, 1, 1], [1, 1, 1], rows: 2, columns: 3]
|
|
130
|
+
var z = Matrix.eye(3, 4); // z = Matrix [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], rows: 3, columns: 4]. there are 1 only in the diagonal
|
|
124
131
|
```
|
|
125
132
|
|
|
126
133
|
### Maths
|
|
127
|
-
|
|
128
134
|
```js
|
|
129
135
|
const {
|
|
130
136
|
Matrix,
|
|
@@ -136,121 +142,124 @@ const {
|
|
|
136
142
|
CholeskyDecomposition,
|
|
137
143
|
EigenvalueDecomposition,
|
|
138
144
|
} = require('ml-matrix');
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
//===========================
|
|
143
|
-
|
|
145
|
+
```
|
|
146
|
+
#### Inverse and Pseudo-inverse
|
|
147
|
+
```js
|
|
144
148
|
var A = new Matrix([
|
|
145
149
|
[2, 3, 5],
|
|
146
150
|
[4, 1, 6],
|
|
147
151
|
[1, 3, 0],
|
|
148
152
|
]);
|
|
153
|
+
|
|
149
154
|
var inverseA = inverse(A);
|
|
150
155
|
var B = A.mmul(inverseA); // B = A * inverse(A), so B ~= Identity
|
|
151
156
|
|
|
157
|
+
|
|
152
158
|
// if A is singular, you can use SVD :
|
|
153
159
|
var A = new Matrix([
|
|
154
160
|
[1, 2, 3],
|
|
155
161
|
[4, 5, 6],
|
|
156
162
|
[7, 8, 9],
|
|
157
|
-
]);
|
|
163
|
+
]);
|
|
164
|
+
// A is singular, so the standard computation of inverse won't work (you can test if you don't trust me^^)
|
|
165
|
+
|
|
158
166
|
var inverseA = inverse(A, (useSVD = true)); // inverseA is only an approximation of the inverse, by using the Singular Values Decomposition
|
|
159
167
|
var B = A.mmul(inverseA); // B = A * inverse(A), but inverse(A) is only an approximation, so B doesn't really be identity.
|
|
160
|
-
|
|
168
|
+
```
|
|
169
|
+
```js
|
|
161
170
|
// if you want the pseudo-inverse of a matrix :
|
|
162
171
|
var A = new Matrix([
|
|
163
172
|
[1, 2],
|
|
164
173
|
[3, 4],
|
|
165
174
|
[5, 6],
|
|
166
175
|
]);
|
|
176
|
+
|
|
167
177
|
var pseudoInverseA = A.pseudoInverse();
|
|
168
178
|
var B = A.mmul(pseudoInverseA).mmul(A); // with pseudo inverse, A*pseudo-inverse(A)*A ~= A. It's the case here
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
// Least square is the following problem : We search x, such as A.x = b (A, x and b are matrix or vectors).
|
|
175
|
-
// Below, how to solve least square with our function
|
|
176
|
-
|
|
179
|
+
```
|
|
180
|
+
#### Least square
|
|
181
|
+
Least square is the following problem: We search for `x`, such that `A.x = B` (`A`, `x` and `B` are matrix or vectors).
|
|
182
|
+
Below, how to solve least square with our function
|
|
183
|
+
```js
|
|
177
184
|
// If A is non singular :
|
|
178
185
|
var A = new Matrix([
|
|
179
|
-
[3,
|
|
186
|
+
[3, 1],
|
|
180
187
|
[4.25, 1],
|
|
181
|
-
[5.5,
|
|
182
|
-
[8,
|
|
188
|
+
[5.5, 1],
|
|
189
|
+
[8, 1],
|
|
183
190
|
]);
|
|
184
|
-
var b = Matrix.columnVector([4.5, 4.25, 5.5, 5.5]);
|
|
185
|
-
var x = solve(A, b);
|
|
186
|
-
var error = Matrix.sub(b, A.mmul(x)); // The error enables to evaluate the solution x found.
|
|
187
191
|
|
|
192
|
+
var B = Matrix.columnVector([4.5, 4.25, 5.5, 5.5]);
|
|
193
|
+
var x = solve(A, B);
|
|
194
|
+
var error = Matrix.sub(B, A.mmul(x)); // The error enables to evaluate the solution x found.
|
|
195
|
+
```
|
|
196
|
+
```js
|
|
188
197
|
// If A is non singular :
|
|
189
198
|
var A = new Matrix([
|
|
190
199
|
[1, 2, 3],
|
|
191
200
|
[4, 5, 6],
|
|
192
201
|
[7, 8, 9],
|
|
193
202
|
]);
|
|
194
|
-
var b = Matrix.columnVector([8, 20, 32]);
|
|
195
|
-
var x = solve(A, b, (useSVD = true)); // there are many solutions. x can be [1, 2, 1].transpose(), or [1.33, 1.33, 1.33].transpose(), etc.
|
|
196
|
-
var error = Matrix.sub(b, A.mmul(x)); // The error enables to evaluate the solution x found.
|
|
197
203
|
|
|
198
|
-
|
|
199
|
-
//
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
204
|
+
var B = Matrix.columnVector([8, 20, 32]);
|
|
205
|
+
var x = solve(A, B, (useSVD = true)); // there are many solutions. x can be [1, 2, 1].transpose(), or [1.33, 1.33, 1.33].transpose(), etc.
|
|
206
|
+
var error = Matrix.sub(B, A.mmul(x)); // The error enables to evaluate the solution x found.
|
|
207
|
+
```
|
|
208
|
+
#### Decompositions
|
|
203
209
|
|
|
210
|
+
##### QR Decomposition
|
|
211
|
+
```js
|
|
204
212
|
var A = new Matrix([
|
|
205
213
|
[2, 3, 5],
|
|
206
214
|
[4, 1, 6],
|
|
207
215
|
[1, 3, 0],
|
|
208
216
|
]);
|
|
217
|
+
|
|
209
218
|
var QR = new QrDecomposition(A);
|
|
210
219
|
var Q = QR.orthogonalMatrix;
|
|
211
220
|
var R = QR.upperTriangularMatrix;
|
|
212
221
|
// So you have the QR decomposition. If you multiply Q by R, you'll see that A = Q.R, with Q orthogonal and R upper triangular
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
222
|
+
```
|
|
223
|
+
##### LU Decomposition
|
|
224
|
+
```js
|
|
216
225
|
var A = new Matrix([
|
|
217
226
|
[2, 3, 5],
|
|
218
227
|
[4, 1, 6],
|
|
219
228
|
[1, 3, 0],
|
|
220
229
|
]);
|
|
230
|
+
|
|
221
231
|
var LU = new LuDecomposition(A);
|
|
222
232
|
var L = LU.lowerTriangularMatrix;
|
|
223
233
|
var U = LU.upperTriangularMatrix;
|
|
224
234
|
var P = LU.pivotPermutationVector;
|
|
225
235
|
// So you have the LU decomposition. P includes the permutation of the matrix. Here P = [1, 2, 0], i.e the first row of LU is the second row of A, the second row of LU is the third row of A and the third row of LU is the first row of A.
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
236
|
+
```
|
|
237
|
+
##### Cholesky Decomposition
|
|
238
|
+
```js
|
|
229
239
|
var A = new Matrix([
|
|
230
240
|
[2, 3, 5],
|
|
231
241
|
[4, 1, 6],
|
|
232
242
|
[1, 3, 0],
|
|
233
243
|
]);
|
|
244
|
+
|
|
234
245
|
var cholesky = new CholeskyDecomposition(A);
|
|
235
246
|
var L = cholesky.lowerTriangularMatrix;
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
247
|
+
```
|
|
248
|
+
##### Eigenvalues & eigenvectors
|
|
249
|
+
```js
|
|
239
250
|
var A = new Matrix([
|
|
240
251
|
[2, 3, 5],
|
|
241
252
|
[4, 1, 6],
|
|
242
253
|
[1, 3, 0],
|
|
243
254
|
]);
|
|
255
|
+
|
|
244
256
|
var e = new EigenvalueDecomposition(A);
|
|
245
257
|
var real = e.realEigenvalues;
|
|
246
258
|
var imaginary = e.imaginaryEigenvalues;
|
|
247
259
|
var vectors = e.eigenvectorMatrix;
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
//=======
|
|
252
|
-
|
|
253
|
-
// Linear dependencies
|
|
260
|
+
```
|
|
261
|
+
#### Linear dependencies
|
|
262
|
+
```js
|
|
254
263
|
var A = new Matrix([
|
|
255
264
|
[2, 0, 0, 1],
|
|
256
265
|
[0, 1, 6, 0],
|
|
@@ -258,7 +267,9 @@ var A = new Matrix([
|
|
|
258
267
|
[0, 0, 1, 0],
|
|
259
268
|
[0, 1, 2, 0],
|
|
260
269
|
]);
|
|
261
|
-
|
|
270
|
+
|
|
271
|
+
var dependencies = linearDependencies(A);
|
|
272
|
+
// dependencies is a matrix with the dependencies of the rows. When we look row by row, we see that the first row is [0, 0, 0, 0, 0], so it means that the first row is independent, and the second row is [ 0, 0, 0, 4, 1 ], i.e the second row = 4 times the 4th row + the 5th row.
|
|
262
273
|
```
|
|
263
274
|
|
|
264
275
|
## License
|