Skip to content
Merged
Show file tree
Hide file tree
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
16 changes: 15 additions & 1 deletion lib/matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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 {
Expand Down
38 changes: 37 additions & 1 deletion lib/matrix.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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.')
});

Expand Down