-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
290 lines (266 loc) · 8.29 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import { fill, padStart, unzip } from "lodash-es";
import { inv } from "mathjs";
type Matrix = {
__value: number | (number | number[])[];
countRows: () => number;
countColumns: () => number;
addable: (y: Matrix) => boolean;
add: (y: Matrix) => Matrix;
multipliable: (y: Matrix) => boolean;
multiply: (y: Matrix) => Matrix;
transpose: () => Matrix;
invert: () => Matrix;
map: (x: any) => Matrix;
valueOf: () => number | (number | number[])[];
};
/**
* Creates a Matrix
* @constructor
* @alias module:matrix
* @param {number|(number | number[])[]} x - Values to store in matrix
* @throws {TypeError} Argument x must be a number or number array
* @return {Matrix} Single or multi dimensional matrix
*/
function Matrix(x: number | (number | number[])[]): Matrix {
// extra nesting
if (Array.isArray(x) && Array.isArray(x[0]) && x.length === 1) {
throw new TypeError("Matrix must be a number or array of numbers");
}
// uneven rows
if (
Array.isArray(x) &&
Array.isArray(x[0]) &&
x.some(
(row) => Array.isArray(row) && row.length !== (x[0] as number[]).length
)
) {
throw new TypeError("Matrix must be a number or array of numbers");
}
/* Single or multi dimensional matrix */
const matrix = Object.create(Matrix.prototype);
matrix.__value = x;
return matrix;
}
/**
* Determines whether two matrices can be summed
* @alias module:matrix.addable
* @param {Matrix} x - Matrix to check
* @param {Matrix} y - Matrix to check
* @return {boolean} Whether two matrices can be summed (using matrix addition)
*/
Matrix.addable = function (x: Matrix, y: Matrix): boolean {
return (
x.countRows() === y.countRows() && x.countColumns() === y.countColumns()
);
};
/**
* Adds two matrices using matrix addition
* @alias module:matrix.add
* @param {Matrix} x - Matrix to add
* @param {Matrix} y - Matrix to add
* @throws {TypeError} Matrices are not addable
* @return {Matrix} New matrix with the summation
*/
Matrix.add = function (x: Matrix, y: Matrix): Matrix {
if (!Matrix.addable(x, y)) throw new TypeError("Matrices are not addable");
return x.map((row: number[], i: number): number[] =>
row.map(
(column: number, j: number): number =>
column + (y.__value as number[][])[i][j]
)
);
};
/**
* Determines whether two matrices can be multiplied
* @alias module:matrix.multipliable
* @param {Matrix} x - Matrix to check
* @param {Matrix} y - Matrix to check
* @return {boolean} Whether two matrices can be summed (using matrix multiplication)
*/
Matrix.multipliable = function (x: Matrix, y: Matrix): boolean {
return x.countColumns() === y.countRows();
};
/**
* Calculates the inner product of two matrices
* @param {Matrix} x - Matrix to multiply
* @param {Matrix} y - Matrix to multiply
* @param {number} i - Column in matrix y to multiply
* @return {number} Inner product of matrices
*/
function innerproduct(x: Matrix, y: Matrix, i: number): number {
const _x: number[] = x.__value as number[];
const _y: number[] =
Array.isArray(unzip<number>(y.__value as number[][])) &&
unzip<number>(y.__value as number[][]).length === 0
? unzip([y.__value as number[]])[i]
: unzip(y.__value as number[][])[i];
return ([] as number[])
.concat(_x)
.reduce((z: number, _z: number, j: number): number => z + _z * _y[j], 0);
}
/**
* Calculates the dot product of two matrices
* @alias module:matrix.multiply
* @param {Matrix} x - Matrix to multiply
* @param {Matrix} y - Matrix to multiply
* @return {Matrix} New matrix with the dot product
*/
Matrix.multiply = function (x: Matrix, y: Matrix): Matrix {
if (!Matrix.multipliable(x, y)) {
throw new TypeError("Matrices are not multipliable");
}
if (x.countColumns() === 0 && y.countRows() === 0) {
return Matrix((x.__value as number) * (y.__value as number));
}
/* New matrix with the dot product */
const z: Matrix = Matrix(
fill(
Array(x.countRows()),
x.countRows() !== 1 ? fill(Array(y.countColumns()), 0) : 0
)
);
return z.map((_z: number | number[], i: number): number | number[] => {
if (typeof _z === "number") return innerproduct(x, y, i);
return _z.map((_, j) =>
innerproduct(Matrix((x.__value as number[])[i]), y, j)
);
});
};
/**
* Inverts a matrix
* @alias module:matrix.invert
* @param {x} Matrix to invert
* @return {Matrix} Matrix inverse
*/
Matrix.invert = function (x: Matrix): Matrix {
return Matrix(inv<any>(x instanceof Matrix ? x.__value : x));
};
/**
* Counts rows in this matrix
* @alias module:matrix#countRows
* @return {number} Number of rows
*/
Matrix.prototype.countRows = function (this: Matrix): number {
if (typeof this.__value === "number") return 0;
if (typeof this.__value[0] === "number") return 1;
return this.__value.length;
};
/**
* Counts columns in this matrix
* @alias module:matrix#countColumns
* @return {number} Number of columns
*/
Matrix.prototype.countColumns = function (this: Matrix): number {
if (typeof this.__value === "number") return 0;
if (typeof this.__value[0] === "number") return this.__value.length;
return this.__value[0].length;
};
/**
* Determines whether this matrix can be summed
* @alias module:matrix#addable
* @param {Matrix} y - Matrix to check
* @return {boolean} Whether this matrix can be summed (using matrix addition)
*/
Matrix.prototype.addable = function (this: Matrix, y: Matrix): boolean {
return Matrix.addable(this, y);
};
/**
* Adds this matrix using matrix addition
* @alias module:matrix#add
* @param {Matrix} y - Matrix to add
* @return {Matrix} New matrix with the summation
*/
Matrix.prototype.add = function (this: Matrix, y: Matrix): Matrix {
return Matrix.add(this, y);
};
/**
* Determines whether this matrix can be multiplied
* @alias module:matrix#multipliable
* @param {Matrix} y - Matrix to check
* @return {boolean} Whether two matrices can be summed (using matrix multiplication)
*/
Matrix.prototype.multipliable = function (this: Matrix, y: Matrix): boolean {
return Matrix.multipliable(this, y);
};
/**
* Calculates the dot product of this matrix
* @alias module:matrix#multiply
* @param {Matrix} y - Matrix to multiply
* @return {Matrix} New matrix with the dot product
*/
Matrix.prototype.multiply = function (this: Matrix, y: Matrix): Matrix {
return Matrix.multiply(this, y);
};
/**
* Calculates the transpose of this matrix
* @alias module:matrix#transpose
* @return {Matrix} New matrix with the transpose
*/
Matrix.prototype.transpose = function (this: Matrix): Matrix {
switch (this.countRows()) {
case 0:
return Matrix(this.__value as number);
case 1:
return Matrix(unzip([this.__value as number[]]));
default:
return Matrix(unzip(this.__value as number[][]));
}
};
/**
* Inverts this matrix
* @alias module:matrix#invert
* @return {Matrix} Matrix inverse
*/
Matrix.prototype.invert = function (this: Matrix): Matrix {
return Matrix.invert(this);
};
/**
* Maps over this matrix
* @alias module:matrix#map
* @return {Matrix} Matrix inverse
*/
Matrix.prototype.map = function (this: Matrix, x: any): Matrix {
if (typeof this.__value === "number") return Matrix(x(this.__value));
return Matrix(this.__value.map(x));
};
/**
* Returns the number or number array value
* @alias module:matrix#valueOf
* @return {number|number[]} Number of number array value
*/
Matrix.prototype.valueOf = function (
this: Matrix
): number | (number | number[])[] {
return this.__value;
};
/**
* Formats and prints the matrix value
* @alias module:matrix#inspect
* @return {string} Formatted matrix value
*/
Matrix.prototype[Symbol.for("nodejs.util.inspect.custom")] = function (
this: Matrix
): string {
switch (this.countRows()) {
case 0:
return `${this.__value}`;
case 1:
return `[ ${(this.__value as number[]).join(" ")} ]`;
default:
/* Output array filled with zeroes */
const padding: number[] = unzip(this.__value as number[][]).map(
(column: number[]) =>
column.reduce((length, x) => Math.max(`${x}`.length, length), 0)
);
return (this.__value as number[][])
.reduce(
(output, row) =>
`${output}[ ${row
.map((x, i) => padStart(`${x}`, padding[i]))
.join(" ")} ]`,
""
)
.replace(/]\[/g, "]\n[");
}
};
export default Matrix;