Skip to content

Commit

Permalink
feat: add static min and max methods
Browse files Browse the repository at this point in the history
  • Loading branch information
targos committed Sep 9, 2015
1 parent e4c785c commit 41707af
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,42 @@ class Matrix extends Array {
return matrix;
}

/**
* Returns a matrix whose elements are the minimum between matrix1 and matrix2
* @param matrix1
* @param matrix2
* @returns {Matrix}
*/
static min(matrix1, matrix2) {
const rows = matrix1.length;
const columns = matrix1[0].length;
let result = new Matrix(rows, columns);
for (var i = 0; i < rows; i++) {
for(var j = 0; j < columns; j++) {
result[i][j] = Math.min(matrix1[i][j], matrix2[i][j]);
}
}
return result;
}

/**
* Returns a matrix whose elements are the maximum between matrix1 and matrix2
* @param matrix1
* @param matrix2
* @returns {Matrix}
*/
static max(matrix1, matrix2) {
const rows = matrix1.length;
const columns = matrix1[0].length;
let result = new Matrix(rows, columns);
for (var i = 0; i < rows; i++) {
for(var j = 0; j < columns; j++) {
result[i][j] = Math.max(matrix1[i][j], matrix2[i][j]);
}
}
return result;
}

/**
* Check that the provided value is a Matrix and tries to instantiate one if not
* @param value - The value to check
Expand Down
16 changes: 16 additions & 0 deletions test/matrix/minMax.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

var Matrix = require('../..');

describe('min - max', function () {
var matrix1 = new Matrix([[0, 1, 2], [3, 4, 5]]);
var matrix2 = new Matrix([[3, 0, 2], [-6, 2, 12]]);

it('min', function () {
Matrix.min(matrix1, matrix2).to2DArray().should.eql([[0, 0, 2], [-6, 2, 5]]);
});

it('max', function () {
Matrix.max(matrix1, matrix2).to2DArray().should.eql([[3, 1, 2], [3, 4, 12]]);
});
});

0 comments on commit 41707af

Please sign in to comment.