Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a beautiful print method + real error throwing/checking #123

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 40 additions & 40 deletions lib/matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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)
Expand All @@ -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;
}

Expand All @@ -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) {
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}