@smockle/matrix 4.0.2 → 4.0.3
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/dist/index.js +1 -1
- package/package.json +4 -4
- package/dist/__tests__/index.test.d.ts +0 -1
- package/dist/__tests__/index.test.js +0 -355
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@smockle/matrix",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.3",
|
|
4
4
|
"description": "Single and multi dimensional matrices and matrix functions.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"build": "tsc",
|
|
9
|
-
"test": "jest"
|
|
9
|
+
"test": "NODE_OPTIONS='--no-warnings=ExperimentalWarning --experimental-vm-modules' npx jest"
|
|
10
10
|
},
|
|
11
11
|
"repository": {
|
|
12
12
|
"type": "git",
|
|
@@ -19,11 +19,11 @@
|
|
|
19
19
|
},
|
|
20
20
|
"homepage": "https://github.com/smockle/matrix#readme",
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"lodash": "^4.17.
|
|
22
|
+
"lodash-es": "^4.17.21",
|
|
23
23
|
"mathjs": "^14.0.1"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
|
-
"@types/lodash": "^4.17.
|
|
26
|
+
"@types/lodash-es": "^4.17.12",
|
|
27
27
|
"jest": "^29.7.0",
|
|
28
28
|
"ts-jest": "^29.1.1",
|
|
29
29
|
"typescript": "^5.2.2"
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,355 +0,0 @@
|
|
|
1
|
-
import { inspect } from "node:util";
|
|
2
|
-
import { describe, test, expect } from "@jest/globals";
|
|
3
|
-
import Matrix from "../index.js";
|
|
4
|
-
describe("Matrix", () => {
|
|
5
|
-
const error = new TypeError("Matrix must be a number or array of numbers");
|
|
6
|
-
test("instanceof Matrix", () => {
|
|
7
|
-
expect(Matrix([])).toBeInstanceOf(Matrix);
|
|
8
|
-
});
|
|
9
|
-
test("throws unnecessary nesting", () => {
|
|
10
|
-
expect(Matrix.bind(Matrix, [[1, 2]])).toThrowError(error);
|
|
11
|
-
});
|
|
12
|
-
test("throws uneven rows", () => {
|
|
13
|
-
expect(Matrix.bind(Matrix, [[1, 2], [3], [4, 5]])).toThrowError(error);
|
|
14
|
-
});
|
|
15
|
-
test("does not throw number", () => {
|
|
16
|
-
expect(Matrix.bind(Matrix, 1)).not.toThrowError(error);
|
|
17
|
-
});
|
|
18
|
-
test("does not throw number array", () => {
|
|
19
|
-
expect(Matrix.bind(Matrix, [1])).not.toThrowError(error);
|
|
20
|
-
});
|
|
21
|
-
test("does not throw 2D number array", () => {
|
|
22
|
-
expect(Matrix.bind(Matrix, [[1], [2]])).not.toThrowError(error);
|
|
23
|
-
});
|
|
24
|
-
});
|
|
25
|
-
describe("Matrix.addable", () => {
|
|
26
|
-
test("addable mismatched columns", () => {
|
|
27
|
-
expect(Matrix.addable(Matrix([
|
|
28
|
-
[1, 2],
|
|
29
|
-
[3, 4],
|
|
30
|
-
]), Matrix([
|
|
31
|
-
[1, 2, 3],
|
|
32
|
-
[4, 5, 6],
|
|
33
|
-
]))).toBeFalsy();
|
|
34
|
-
});
|
|
35
|
-
test("addable mismatched rows", () => {
|
|
36
|
-
expect(Matrix.addable(Matrix([
|
|
37
|
-
[1, 2],
|
|
38
|
-
[3, 4],
|
|
39
|
-
]), Matrix([
|
|
40
|
-
[1, 2],
|
|
41
|
-
[4, 5],
|
|
42
|
-
[7, 8],
|
|
43
|
-
]))).toBeFalsy();
|
|
44
|
-
});
|
|
45
|
-
test("addable", () => {
|
|
46
|
-
expect(Matrix.addable(Matrix([
|
|
47
|
-
[1, 2],
|
|
48
|
-
[3, 4],
|
|
49
|
-
]), Matrix([
|
|
50
|
-
[5, 6],
|
|
51
|
-
[7, 8],
|
|
52
|
-
]))).toBeTruthy();
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
describe("Matrix#addable", () => {
|
|
56
|
-
test("addable mismatched columns", () => {
|
|
57
|
-
expect(Matrix([
|
|
58
|
-
[1, 2],
|
|
59
|
-
[3, 4],
|
|
60
|
-
]).addable(Matrix([
|
|
61
|
-
[1, 2, 3],
|
|
62
|
-
[4, 5, 6],
|
|
63
|
-
]))).toBeFalsy();
|
|
64
|
-
});
|
|
65
|
-
test("addable mismatched rows", () => {
|
|
66
|
-
expect(Matrix([
|
|
67
|
-
[1, 2],
|
|
68
|
-
[3, 4],
|
|
69
|
-
]).addable(Matrix([
|
|
70
|
-
[1, 2],
|
|
71
|
-
[4, 5],
|
|
72
|
-
[7, 8],
|
|
73
|
-
]))).toBeFalsy();
|
|
74
|
-
});
|
|
75
|
-
test("addable", () => {
|
|
76
|
-
expect(Matrix([
|
|
77
|
-
[1, 2],
|
|
78
|
-
[3, 4],
|
|
79
|
-
]).addable(Matrix([
|
|
80
|
-
[5, 6],
|
|
81
|
-
[7, 8],
|
|
82
|
-
]))).toBeTruthy();
|
|
83
|
-
});
|
|
84
|
-
});
|
|
85
|
-
describe("Matrix.add", () => {
|
|
86
|
-
test("add throws typerror", () => {
|
|
87
|
-
expect(Matrix.add.bind(Matrix, Matrix([
|
|
88
|
-
[1, 2],
|
|
89
|
-
[3, 4],
|
|
90
|
-
]), Matrix([
|
|
91
|
-
[1, 2, 3],
|
|
92
|
-
[4, 5, 6],
|
|
93
|
-
]))).toThrowError(new TypeError("Matrices are not addable"));
|
|
94
|
-
});
|
|
95
|
-
test("add", () => {
|
|
96
|
-
expect(Matrix.add(Matrix([
|
|
97
|
-
[1, 2],
|
|
98
|
-
[3, 4],
|
|
99
|
-
]), Matrix([
|
|
100
|
-
[5, 6],
|
|
101
|
-
[7, 8],
|
|
102
|
-
]))).toStrictEqual(Matrix([
|
|
103
|
-
[6, 8],
|
|
104
|
-
[10, 12],
|
|
105
|
-
]));
|
|
106
|
-
});
|
|
107
|
-
});
|
|
108
|
-
describe("Matrix#add", () => {
|
|
109
|
-
const addmatrix = Matrix([
|
|
110
|
-
[1, 2],
|
|
111
|
-
[3, 4],
|
|
112
|
-
]);
|
|
113
|
-
test("add throws typerror", () => {
|
|
114
|
-
expect(addmatrix.add.bind(addmatrix, Matrix([
|
|
115
|
-
[1, 2, 3],
|
|
116
|
-
[4, 5, 6],
|
|
117
|
-
]))).toThrowError(new TypeError("Matrices are not addable"));
|
|
118
|
-
});
|
|
119
|
-
test("add", () => {
|
|
120
|
-
expect(Matrix([
|
|
121
|
-
[1, 2],
|
|
122
|
-
[3, 4],
|
|
123
|
-
]).add(Matrix([
|
|
124
|
-
[5, 6],
|
|
125
|
-
[7, 8],
|
|
126
|
-
]))).toStrictEqual(Matrix([
|
|
127
|
-
[6, 8],
|
|
128
|
-
[10, 12],
|
|
129
|
-
]));
|
|
130
|
-
});
|
|
131
|
-
});
|
|
132
|
-
describe("Matrix.multipliable", () => {
|
|
133
|
-
test("multipliable mismatched columns and rows", () => {
|
|
134
|
-
expect(Matrix.multipliable(Matrix([
|
|
135
|
-
[1, 2, 3],
|
|
136
|
-
[4, 5, 6],
|
|
137
|
-
]), Matrix([
|
|
138
|
-
[1, 2],
|
|
139
|
-
[4, 5],
|
|
140
|
-
]))).toBeFalsy();
|
|
141
|
-
});
|
|
142
|
-
test("multipliable", () => {
|
|
143
|
-
expect(Matrix.multipliable(Matrix([
|
|
144
|
-
[1, 2],
|
|
145
|
-
[3, 4],
|
|
146
|
-
]), Matrix([
|
|
147
|
-
[5, 6],
|
|
148
|
-
[7, 8],
|
|
149
|
-
]))).toBeTruthy();
|
|
150
|
-
});
|
|
151
|
-
});
|
|
152
|
-
describe("Matrix#multipliable", () => {
|
|
153
|
-
test("multipliable mismatched columns and rows", () => {
|
|
154
|
-
expect(Matrix([
|
|
155
|
-
[1, 2, 3],
|
|
156
|
-
[4, 5, 6],
|
|
157
|
-
]).multipliable(Matrix([
|
|
158
|
-
[1, 2],
|
|
159
|
-
[4, 5],
|
|
160
|
-
]))).toBeFalsy();
|
|
161
|
-
});
|
|
162
|
-
test("multipliable", () => {
|
|
163
|
-
expect(Matrix([
|
|
164
|
-
[1, 2],
|
|
165
|
-
[3, 4],
|
|
166
|
-
]).multipliable(Matrix([
|
|
167
|
-
[5, 6],
|
|
168
|
-
[7, 8],
|
|
169
|
-
]))).toBeTruthy();
|
|
170
|
-
});
|
|
171
|
-
});
|
|
172
|
-
describe("Matrix.multiply", () => {
|
|
173
|
-
const multiplymatrix = Matrix([1, 2, 3]);
|
|
174
|
-
test("multiply throws typerror", () => {
|
|
175
|
-
expect(multiplymatrix.multiply.bind(multiplymatrix, Matrix([
|
|
176
|
-
[1, 2],
|
|
177
|
-
[3, 4],
|
|
178
|
-
]))).toThrowError(new TypeError("Matrices are not multipliable"));
|
|
179
|
-
});
|
|
180
|
-
test("multiply number", () => {
|
|
181
|
-
expect(Matrix(3).multiply(Matrix(6))).toStrictEqual(Matrix(18));
|
|
182
|
-
});
|
|
183
|
-
test("multiply A (1x2) and B (2x1)", () => {
|
|
184
|
-
expect(Matrix([1, 2]).multiply(Matrix([[3], [4]]))).toStrictEqual(Matrix([11]));
|
|
185
|
-
});
|
|
186
|
-
test("multiply A (2x2) and B (2x2)", () => {
|
|
187
|
-
expect(Matrix([
|
|
188
|
-
[1, 2],
|
|
189
|
-
[3, 4],
|
|
190
|
-
]).multiply(Matrix([
|
|
191
|
-
[5, 6],
|
|
192
|
-
[7, 8],
|
|
193
|
-
]))).toStrictEqual(Matrix([
|
|
194
|
-
[19, 22],
|
|
195
|
-
[43, 50],
|
|
196
|
-
]));
|
|
197
|
-
});
|
|
198
|
-
test("multiply A (2x3) and B (3x2)", () => {
|
|
199
|
-
expect(Matrix([
|
|
200
|
-
[1, 9, 7],
|
|
201
|
-
[8, 1, 2],
|
|
202
|
-
]).multiply(Matrix([
|
|
203
|
-
[3, 2, 1, 5],
|
|
204
|
-
[5, 4, 7, 3],
|
|
205
|
-
[6, 9, 6, 8],
|
|
206
|
-
]))).toStrictEqual(Matrix([
|
|
207
|
-
[90, 101, 106, 88],
|
|
208
|
-
[41, 38, 27, 59],
|
|
209
|
-
]));
|
|
210
|
-
});
|
|
211
|
-
});
|
|
212
|
-
describe("Matrix#multiply", () => {
|
|
213
|
-
test("multiply throws typerror", () => {
|
|
214
|
-
expect(Matrix.multiply.bind(Matrix, Matrix([1, 2, 3]), Matrix([
|
|
215
|
-
[1, 2],
|
|
216
|
-
[3, 4],
|
|
217
|
-
]))).toThrowError(new TypeError("Matrices are not multipliable"));
|
|
218
|
-
});
|
|
219
|
-
test("multiply number", () => {
|
|
220
|
-
expect(Matrix.multiply(Matrix(3), Matrix(6))).toStrictEqual(Matrix(18));
|
|
221
|
-
});
|
|
222
|
-
test("multiply A (1x2) and B (2x1)", () => {
|
|
223
|
-
expect(Matrix.multiply(Matrix([1, 2]), Matrix([[3], [4]]))).toStrictEqual(Matrix([11]));
|
|
224
|
-
});
|
|
225
|
-
test("multiply A (2x2) and B (2x2)", () => {
|
|
226
|
-
expect(Matrix.multiply(Matrix([
|
|
227
|
-
[1, 2],
|
|
228
|
-
[3, 4],
|
|
229
|
-
]), Matrix([
|
|
230
|
-
[5, 6],
|
|
231
|
-
[7, 8],
|
|
232
|
-
]))).toStrictEqual(Matrix([
|
|
233
|
-
[19, 22],
|
|
234
|
-
[43, 50],
|
|
235
|
-
]));
|
|
236
|
-
});
|
|
237
|
-
test("multiply A (2x3) and B (3x2)", () => {
|
|
238
|
-
expect(Matrix.multiply(Matrix([
|
|
239
|
-
[1, 9, 7],
|
|
240
|
-
[8, 1, 2],
|
|
241
|
-
]), Matrix([
|
|
242
|
-
[3, 2, 1, 5],
|
|
243
|
-
[5, 4, 7, 3],
|
|
244
|
-
[6, 9, 6, 8],
|
|
245
|
-
]))).toStrictEqual(Matrix([
|
|
246
|
-
[90, 101, 106, 88],
|
|
247
|
-
[41, 38, 27, 59],
|
|
248
|
-
]));
|
|
249
|
-
});
|
|
250
|
-
});
|
|
251
|
-
describe("Matrix#valueOf", () => {
|
|
252
|
-
const array = [3];
|
|
253
|
-
test("valueOf Matrix", () => {
|
|
254
|
-
expect(Matrix(array).valueOf()).toBe(array);
|
|
255
|
-
});
|
|
256
|
-
});
|
|
257
|
-
describe("Matrix#countRows", () => {
|
|
258
|
-
test("countRows number", () => {
|
|
259
|
-
expect(Matrix(1).countRows()).toBe(0);
|
|
260
|
-
});
|
|
261
|
-
test("countRows number array", () => {
|
|
262
|
-
expect(Matrix([1]).countRows()).toBe(1);
|
|
263
|
-
});
|
|
264
|
-
test("countRows 2D number array", () => {
|
|
265
|
-
expect(Matrix([
|
|
266
|
-
[1, 2],
|
|
267
|
-
[3, 4],
|
|
268
|
-
[5, 6],
|
|
269
|
-
]).countRows()).toBe(3);
|
|
270
|
-
});
|
|
271
|
-
});
|
|
272
|
-
describe("Matrix#countColumns", () => {
|
|
273
|
-
test("countColumns number", () => {
|
|
274
|
-
expect(Matrix(1).countColumns()).toBe(0);
|
|
275
|
-
});
|
|
276
|
-
test("countColumns number array", () => {
|
|
277
|
-
expect(Matrix([1]).countColumns()).toBe(1);
|
|
278
|
-
});
|
|
279
|
-
test("countColumns 2D number array", () => {
|
|
280
|
-
expect(Matrix([[1], [2]]).countColumns()).toBe(1);
|
|
281
|
-
});
|
|
282
|
-
test("countColumns many 2D number array", () => {
|
|
283
|
-
expect(Matrix([
|
|
284
|
-
[1, 2],
|
|
285
|
-
[3, 4],
|
|
286
|
-
[5, 6],
|
|
287
|
-
]).countColumns()).toBe(2);
|
|
288
|
-
});
|
|
289
|
-
});
|
|
290
|
-
describe("Matrix#transpose", () => {
|
|
291
|
-
test("transpose number", () => {
|
|
292
|
-
expect(Matrix(1).transpose()).toStrictEqual(Matrix(1));
|
|
293
|
-
});
|
|
294
|
-
test("transpose number array", () => {
|
|
295
|
-
expect(Matrix([1, 2]).transpose()).toStrictEqual(Matrix([[1], [2]]));
|
|
296
|
-
});
|
|
297
|
-
test("transpose 2D number array", () => {
|
|
298
|
-
expect(Matrix([
|
|
299
|
-
[1, 2, 3],
|
|
300
|
-
[4, 5, 6],
|
|
301
|
-
]).transpose()).toStrictEqual(Matrix([
|
|
302
|
-
[1, 4],
|
|
303
|
-
[2, 5],
|
|
304
|
-
[3, 6],
|
|
305
|
-
]));
|
|
306
|
-
});
|
|
307
|
-
});
|
|
308
|
-
describe("Matrix#invert", () => {
|
|
309
|
-
test("invert 2D number array", () => {
|
|
310
|
-
expect(Matrix([
|
|
311
|
-
[1, 2],
|
|
312
|
-
[3, 4],
|
|
313
|
-
]).invert()).toStrictEqual(Matrix([
|
|
314
|
-
[-2, 1],
|
|
315
|
-
[1.5, -0.5],
|
|
316
|
-
]));
|
|
317
|
-
});
|
|
318
|
-
});
|
|
319
|
-
describe("Matrix#map", () => {
|
|
320
|
-
const matrix = Matrix([1, 2, 3]);
|
|
321
|
-
test("map returns a matrix", () => {
|
|
322
|
-
expect(matrix.map((x) => x + 1)).toBeInstanceOf(Matrix);
|
|
323
|
-
});
|
|
324
|
-
test("map identity", () => {
|
|
325
|
-
expect(matrix.map((x) => x)).toStrictEqual(matrix);
|
|
326
|
-
});
|
|
327
|
-
test("map increment", () => {
|
|
328
|
-
expect(matrix.map((x) => x + 1)).toStrictEqual(Matrix([2, 3, 4]));
|
|
329
|
-
});
|
|
330
|
-
test("map non-mutable", () => {
|
|
331
|
-
expect(matrix).toStrictEqual(Matrix([1, 2, 3]));
|
|
332
|
-
});
|
|
333
|
-
});
|
|
334
|
-
describe("Matrix#inspect", () => {
|
|
335
|
-
test("inspect number", () => {
|
|
336
|
-
expect(inspect(Matrix(3))).toBe("3");
|
|
337
|
-
});
|
|
338
|
-
test("inspect number array", () => {
|
|
339
|
-
expect(inspect(Matrix([1, 2, 3]))).toBe("[ 1 2 3 ]");
|
|
340
|
-
});
|
|
341
|
-
test("inspect 2D number array", () => {
|
|
342
|
-
expect(inspect(Matrix([
|
|
343
|
-
[1, 2, 3],
|
|
344
|
-
[4, 5, 6],
|
|
345
|
-
[7, 8, 9],
|
|
346
|
-
]))).toBe("[ 1 2 3 ]\n[ 4 5 6 ]\n[ 7 8 9 ]");
|
|
347
|
-
});
|
|
348
|
-
test("inspect padded 2D number array", () => {
|
|
349
|
-
expect(inspect(Matrix([
|
|
350
|
-
[1, 2, 3],
|
|
351
|
-
[-10, 11, -12],
|
|
352
|
-
[100, 0, 0],
|
|
353
|
-
]))).toBe("[ 1 2 3 ]\n[ -10 11 -12 ]\n[ 100 0 0 ]");
|
|
354
|
-
});
|
|
355
|
-
});
|