Skip to content

Commit 1b3cb03

Browse files
committed
refactor: rework a lot of things
BREAKING CHANGE: The signature of a few methods changed to take an options object: - Matrix.rand / Matrix.random - Matrix.randInt - Matrix.prototype.repeat - Matrix.prototype.scaleRows - Matrix.prototype.scaleColumns
1 parent def2977 commit 1b3cb03

17 files changed

+3245
-2271
lines changed

matrix.d.ts

Lines changed: 309 additions & 246 deletions
Large diffs are not rendered by default.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"main": "matrix.js",
66
"module": "src/index.js",
77
"types": "matrix.d.ts",
8+
"sideEffects": false,
89
"files": [
910
"matrix.d.ts",
1011
"matrix.js",

src/__tests__/matrix/creation.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ describe('Matrix creation', () => {
6767
expect(matrix.rows).toBe(3);
6868
expect(matrix.columns).toBe(2);
6969
expect(() => Matrix.from1DArray(3, 2, [0, 1, 2, 3])).toThrow(
70-
/^Data length does not match given dimensions$/
70+
/^data length does not match given dimensions$/
7171
);
7272
});
7373

@@ -109,7 +109,7 @@ describe('Matrix creation', () => {
109109

110110
it('random with custom RNG', () => {
111111
var fakeRNG = () => 2;
112-
expect(Matrix.rand(2, 2, fakeRNG).to2DArray()).toStrictEqual([
112+
expect(Matrix.rand(2, 2, { random: fakeRNG }).to2DArray()).toStrictEqual([
113113
[2, 2],
114114
[2, 2]
115115
]);
@@ -148,13 +148,13 @@ describe('Matrix creation', () => {
148148
]);
149149
});
150150

151-
it('Symbol.species should work for views', () => {
151+
it('views should return new instances of Matrix', () => {
152152
var matrix = new Matrix([[1, 1, 1], [1, 1, 1]]);
153153
var view = new MatrixTransposeView(matrix);
154154
expect(matrix.transpose().mmul(matrix)).toStrictEqual(view.mmul(matrix));
155155
});
156156

157-
it('Symbol.species should work on static evaluated methods', () => {
157+
it('static evaluated methods should return new instances of Matrix', () => {
158158
var a = [[1, 2]];
159159
var b = [[3, 1]];
160160
expect(Matrix.subtract(a, b).to2DArray()).toStrictEqual([[-2, 1]]);

src/__tests__/matrix/utility.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -144,16 +144,16 @@ describe('utility methods', () => {
144144
[0, 1 / 2, 1],
145145
[0, 1, 1 / 3]
146146
]);
147-
expect(matrix.scaleRows(1, 2).to2DArray()).toStrictEqual([
147+
expect(matrix.scaleRows({ min: 1, max: 2 }).to2DArray()).toStrictEqual([
148148
[1, 3 / 2, 2],
149149
[1, 2, 4 / 3]
150150
]);
151-
expect(matrix.scaleRows(-2, -1).to2DArray()).toStrictEqual([
151+
expect(matrix.scaleRows({ min: -2, max: -1 }).to2DArray()).toStrictEqual([
152152
[-2, -3 / 2, -1],
153153
[-2, -1, -5 / 3]
154154
]);
155-
expect(() => matrix.scaleRows(2, 1)).toThrow(
156-
/min should be strictly smaller than max/
155+
expect(() => matrix.scaleRows({ min: 2, max: 1 })).toThrow(
156+
/^min must be smaller than max$/
157157
);
158158
});
159159

@@ -164,18 +164,16 @@ describe('utility methods', () => {
164164
[0, 1 / 2],
165165
[1, 1]
166166
]);
167-
expect(matrix.scaleColumns(1, 2).to2DArray()).toStrictEqual([
167+
expect(matrix.scaleColumns({ min: 1, max: 2 }).to2DArray()).toStrictEqual([
168168
[13 / 7, 1],
169169
[1, 3 / 2],
170170
[2, 2]
171171
]);
172-
expect(matrix.scaleColumns(-2, -1).to2DArray()).toStrictEqual([
173-
[-8 / 7, -2],
174-
[-2, -3 / 2],
175-
[-1, -1]
176-
]);
177-
expect(() => matrix.scaleColumns(2, 1)).toThrow(
178-
/min should be strictly smaller than max/
172+
expect(matrix.scaleColumns({ min: -2, max: -1 }).to2DArray()).toStrictEqual(
173+
[[-8 / 7, -2], [-2, -3 / 2], [-1, -1]]
174+
);
175+
expect(() => matrix.scaleColumns({ min: 2, max: 1 })).toThrow(
176+
/^min must be smaller than max$/
179177
);
180178
});
181179

@@ -203,13 +201,13 @@ describe('utility methods', () => {
203201
it('repeat matrix', () => {
204202
var matrix = new Matrix([[1, 2], [3, 4]]);
205203
expect(matrix.repeat().to2DArray()).toStrictEqual([[1, 2], [3, 4]]);
206-
expect(matrix.repeat(2, 2).to2DArray()).toStrictEqual([
204+
expect(matrix.repeat({ rows: 2, columns: 2 }).to2DArray()).toStrictEqual([
207205
[1, 2, 1, 2],
208206
[3, 4, 3, 4],
209207
[1, 2, 1, 2],
210208
[3, 4, 3, 4]
211209
]);
212-
expect(matrix.repeat(1, 2).to2DArray()).toStrictEqual([
210+
expect(matrix.repeat({ columns: 2 }).to2DArray()).toStrictEqual([
213211
[1, 2, 1, 2],
214212
[3, 4, 3, 4]
215213
]);

0 commit comments

Comments
 (0)