Skip to content

Commit

Permalink
refactor: rework a lot of things
Browse files Browse the repository at this point in the history
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
  • Loading branch information
targos committed Apr 25, 2019
1 parent def2977 commit 1b3cb03
Show file tree
Hide file tree
Showing 17 changed files with 3,245 additions and 2,271 deletions.
555 changes: 309 additions & 246 deletions matrix.d.ts

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"main": "matrix.js",
"module": "src/index.js",
"types": "matrix.d.ts",
"sideEffects": false,
"files": [
"matrix.d.ts",
"matrix.js",
Expand Down
8 changes: 4 additions & 4 deletions src/__tests__/matrix/creation.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('Matrix creation', () => {
expect(matrix.rows).toBe(3);
expect(matrix.columns).toBe(2);
expect(() => Matrix.from1DArray(3, 2, [0, 1, 2, 3])).toThrow(
/^Data length does not match given dimensions$/
/^data length does not match given dimensions$/
);
});

Expand Down Expand Up @@ -109,7 +109,7 @@ describe('Matrix creation', () => {

it('random with custom RNG', () => {
var fakeRNG = () => 2;
expect(Matrix.rand(2, 2, fakeRNG).to2DArray()).toStrictEqual([
expect(Matrix.rand(2, 2, { random: fakeRNG }).to2DArray()).toStrictEqual([
[2, 2],
[2, 2]
]);
Expand Down Expand Up @@ -148,13 +148,13 @@ describe('Matrix creation', () => {
]);
});

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

it('Symbol.species should work on static evaluated methods', () => {
it('static evaluated methods should return new instances of Matrix', () => {
var a = [[1, 2]];
var b = [[3, 1]];
expect(Matrix.subtract(a, b).to2DArray()).toStrictEqual([[-2, 1]]);
Expand Down
26 changes: 12 additions & 14 deletions src/__tests__/matrix/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,16 @@ describe('utility methods', () => {
[0, 1 / 2, 1],
[0, 1, 1 / 3]
]);
expect(matrix.scaleRows(1, 2).to2DArray()).toStrictEqual([
expect(matrix.scaleRows({ min: 1, max: 2 }).to2DArray()).toStrictEqual([
[1, 3 / 2, 2],
[1, 2, 4 / 3]
]);
expect(matrix.scaleRows(-2, -1).to2DArray()).toStrictEqual([
expect(matrix.scaleRows({ min: -2, max: -1 }).to2DArray()).toStrictEqual([
[-2, -3 / 2, -1],
[-2, -1, -5 / 3]
]);
expect(() => matrix.scaleRows(2, 1)).toThrow(
/min should be strictly smaller than max/
expect(() => matrix.scaleRows({ min: 2, max: 1 })).toThrow(
/^min must be smaller than max$/
);
});

Expand All @@ -164,18 +164,16 @@ describe('utility methods', () => {
[0, 1 / 2],
[1, 1]
]);
expect(matrix.scaleColumns(1, 2).to2DArray()).toStrictEqual([
expect(matrix.scaleColumns({ min: 1, max: 2 }).to2DArray()).toStrictEqual([
[13 / 7, 1],
[1, 3 / 2],
[2, 2]
]);
expect(matrix.scaleColumns(-2, -1).to2DArray()).toStrictEqual([
[-8 / 7, -2],
[-2, -3 / 2],
[-1, -1]
]);
expect(() => matrix.scaleColumns(2, 1)).toThrow(
/min should be strictly smaller than max/
expect(matrix.scaleColumns({ min: -2, max: -1 }).to2DArray()).toStrictEqual(
[[-8 / 7, -2], [-2, -3 / 2], [-1, -1]]
);
expect(() => matrix.scaleColumns({ min: 2, max: 1 })).toThrow(
/^min must be smaller than max$/
);
});

Expand Down Expand Up @@ -203,13 +201,13 @@ describe('utility methods', () => {
it('repeat matrix', () => {
var matrix = new Matrix([[1, 2], [3, 4]]);
expect(matrix.repeat().to2DArray()).toStrictEqual([[1, 2], [3, 4]]);
expect(matrix.repeat(2, 2).to2DArray()).toStrictEqual([
expect(matrix.repeat({ rows: 2, columns: 2 }).to2DArray()).toStrictEqual([
[1, 2, 1, 2],
[3, 4, 3, 4],
[1, 2, 1, 2],
[3, 4, 3, 4]
]);
expect(matrix.repeat(1, 2).to2DArray()).toStrictEqual([
expect(matrix.repeat({ columns: 2 }).to2DArray()).toStrictEqual([
[1, 2, 1, 2],
[3, 4, 3, 4]
]);
Expand Down
Loading

0 comments on commit 1b3cb03

Please sign in to comment.