From 9a743bba41cba857f943db1111cc552c7a730ca4 Mon Sep 17 00:00:00 2001 From: Arjun Patrawala Date: Sun, 1 Jul 2018 23:56:16 -0700 Subject: [PATCH 1/2] minor edits + a beautiful print method -refactored errors to JavaScript convention -created a beautiful print method (try it in Google Chrome) -cut down if statements and for statements where brackets weren't needed --- lib/matrix.js | 82 ++++++++++++++++++++++++++------------------------- 1 file changed, 42 insertions(+), 40 deletions(-) diff --git a/lib/matrix.js b/lib/matrix.js index b232d36..98bec41 100644 --- a/lib/matrix.js +++ b/lib/matrix.js @@ -10,11 +10,9 @@ class Matrix { copy() { let m = new Matrix(this.rows, this.cols); - for (let i = 0; i < this.rows; i++) { - for (let j = 0; j < this.cols; j++) { + for (let i = 0; i < this.rows; i++) + for (let j = 0; j < this.cols; j++) m.data[i][j] = this.data[i][j]; - } - } return m; } @@ -23,10 +21,8 @@ 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; - } + if (a.rows !== b.rows || a.cols !== b.cols) + throw new TypeError('Matrix dimensions do not match'); // Return a new Matrix a-b return new Matrix(a.rows, a.cols) @@ -35,11 +31,9 @@ class Matrix { toArray() { let arr = []; - for (let i = 0; i < this.rows; i++) { - for (let j = 0; j < this.cols; j++) { + for (let i = 0; i < this.rows; i++) + for (let j = 0; j < this.cols; j++) arr.push(this.data[i][j]); - } - } return arr; } @@ -49,14 +43,13 @@ class Matrix { add(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; - } + if (this.rows !== n.rows || this.cols !== n.cols) + throw new TypeError('Matrix dimensions do not match'); + return this.map((e, i, j) => e + n.data[i][j]); - } else { + } else return this.map(e => e + n); - } + } static transpose(matrix) { @@ -66,45 +59,36 @@ class Matrix { static multiply(a, b) { // Matrix product - if (a.cols !== b.rows) { - console.log('Columns of A must match rows of B.'); - return; - } + if (a.cols !== b.rows) + throw new TypeError('Matrix `a` columns do not match matrix `b` rows'); return new Matrix(a.rows, b.cols) .map((e, i, j) => { // Dot product of values in col let sum = 0; - for (let k = 0; k < a.cols; k++) { - sum += a.data[i][k] * b.data[k][j]; - } + for (let k = 0; k < a.cols; k++) sum += a.data[i][k] * b.data[k][j]; return sum; }); } 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; - } + if (this.rows !== n.rows || this.cols !== n.cols) + throw new TypeError('Matrix dimensions must match'); // hadamard product return this.map((e, i, j) => e * n.data[i][j]); - } else { - // Scalar product + } else // Scalar product return this.map(e => e * n); - } } map(func) { // Apply a function to every element of matrix - for (let i = 0; i < this.rows; i++) { + for (let i = 0; i < this.rows; i++) for (let j = 0; j < this.cols; j++) { let val = this.data[i][j]; this.data[i][j] = func(val, i, j); } - } return this; } @@ -114,8 +98,27 @@ class Matrix { .map((e, i, j) => func(matrix.data[i][j], i, j)); } - print() { - console.table(this.data); + print(decimalPoints) { + if (!decimalPoints) + decimalPoints = 3; + + let text = ''; + for (let i = 0; i < this.rows; i++) { + for (let j = 0; j < this.cols; j++) { + if (j !== 0) text += ' '; + if (this.data[i][j] > 0) text += ' '; + + text += this.data[i][j].toFixed(decimalPoints); + + if (j !== this.cols - 1) text += ' '; + } + text += '\n'; + } + if (typeof window.chrome === 'object') + console.log('%c' + text, 'font-size: 12px; border-left: 1px solid black; border-right: 1px solid black; padding: 4px 8px; color: #333333; margin: 5px;'); + else + console.log(text); + return this; } @@ -124,15 +127,14 @@ class Matrix { } static deserialize(data) { - if (typeof data == 'string') { + if (typeof data === 'string') data = JSON.parse(data); - } + let matrix = new Matrix(data.rows, data.cols); matrix.data = data.data; return matrix; } } -if (typeof module !== 'undefined') { +if (typeof module !== 'undefined') module.exports = Matrix; -} From cc81e9bc469498a54e39ebe73eedd216a5191054 Mon Sep 17 00:00:00 2001 From: Arjun Patrawala Date: Sat, 7 Jul 2018 21:28:26 -0700 Subject: [PATCH 2/2] update to es6 --- lib/matrix.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/matrix.js b/lib/matrix.js index 98bec41..28790d1 100644 --- a/lib/matrix.js +++ b/lib/matrix.js @@ -98,9 +98,7 @@ class Matrix { .map((e, i, j) => func(matrix.data[i][j], i, j)); } - print(decimalPoints) { - if (!decimalPoints) - decimalPoints = 3; + print(decimalPoints = 3) { let text = ''; for (let i = 0; i < this.rows; i++) {