diff --git a/lib/matrix.js b/lib/matrix.js index b232d36..28790d1 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,25 @@ class Matrix { .map((e, i, j) => func(matrix.data[i][j], i, j)); } - print() { - console.table(this.data); + print(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 +125,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; -}