diff --git a/lib/matrix.js b/lib/matrix.js index 0de7dd9..a3a2686 100644 --- a/lib/matrix.js +++ b/lib/matrix.js @@ -13,6 +13,11 @@ class Matrix { } static subtract(a, b) { + if(a.rows!==b.rows || a.cols!==b.cols){ + console.log('Columns and Rows of A must match Columns and Rows of B.'); + return; + } + // Return a new Matrix a-b return new Matrix(a.rows, a.cols) .map((_, i, j) => a.data[i][j] - b.data[i][j]); @@ -34,7 +39,11 @@ class Matrix { add(n) { if (n instanceof Matrix) { - return this.map((e, i, j) => e + n.data[i][j]); + if(this.rows!==n.rows || this.cols!==n.cols){ + console.log('Columns and Rows of A must match Columns and Rows of B.'); + return; + } + return this.map((e, i, j) => e + n.data[i][j]); } else { return this.map(e => e + n); } @@ -65,6 +74,11 @@ class Matrix { multiply(n) { if (n instanceof Matrix) { + if(this.rows!==n.rows || this.cols!==n.cols){ + console.log('Columns and Rows of A must match Columns and Rows of B.'); + return; + } + // hadamard product return this.map((e, i, j) => e * n.data[i][j]); } else { diff --git a/lib/matrix.test.js b/lib/matrix.test.js index 603155c..26eb67b 100644 --- a/lib/matrix.test.js +++ b/lib/matrix.test.js @@ -223,6 +223,42 @@ test('mapping with instance map', () => { }); }); +test('error handling of addition when columns and rows of A don\'t match columns and rows of B.', () => { + //Replace console.log with a jest mock so we can see if it has been called + global.console.log = jest.fn(); + + let m1 = new Matrix(1, 2); + let m2 = new Matrix(3, 4); + m1.add(m2); + + //Check if the mock console.log has been called + expect(global.console.log).toHaveBeenCalledWith('Columns and Rows of A must match Columns and Rows of B.') +}); + +test('error handling of static subtraction when columns and rows of A don\'t match columns and rows of B.', () => { + //Replace console.log with a jest mock so we can see if it has been called + global.console.log = jest.fn(); + + let m1 = new Matrix(1, 2); + let m2 = new Matrix(3, 4); + Matrix.subtract(m1,m2); + + //Check if the mock console.log has been called + expect(global.console.log).toHaveBeenCalledWith('Columns and Rows of A must match Columns and Rows of B.') +}); + +test('error handling of hadamard product when columns and rows of A don\'t match columns and rows of B.', () => { + //Replace console.log with a jest mock so we can see if it has been called + global.console.log = jest.fn(); + + let m1 = new Matrix(1, 2); + let m2 = new Matrix(3, 4); + m1.multiply(m2); + + //Check if the mock console.log has been called + expect(global.console.log).toHaveBeenCalledWith('Columns and Rows of A must match Columns and Rows of B.') +}); + test('error handling of matrix product when columns of A don\'t match rows of B.', () => { //Replace console.log with a jest mock so we can see if it has been called global.console.log = jest.fn(); @@ -231,7 +267,7 @@ test('error handling of matrix product when columns of A don\'t match rows of B. let m2 = new Matrix(3, 4); Matrix.multiply(m1, m2); - //Check if the mock console.log has been called + //Check if the mock console.log has been called expect(global.console.log).toHaveBeenCalledWith('Columns of A must match rows of B.') });