-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Closed
Labels
Description
I am currently building a machine learning library and I often need to check whether given input is a matrix of numbers or strings or booleans or .... . I have written an extension method to deal with this within my project:
/**
* Checking the matrix is a matrix of a certain data type (e.g. number)
* The function also performs isMatrix against the passed in the dataset
* @param matrix
* @param {string} _type
*/
const isMatrixOf = (matrix, _type = 'number') => {
if (!isMatrix(matrix)) {
throw Error(`Cannot perform isMatrixOf ${_type} unless the data is matrix`);
}
// Checking each elements inside the matrix is not number
// Returns an array of result per row
const vectorChecks = matrix.map(arr =>
arr.some(x => {
// Checking type of each element
if (_type === 'number') {
return !_.isNumber(x);
} else {
throw Error('Cannot check matrix of an unknown type');
}
})
);
// All should be false
return vectorChecks.indexOf(true) === -1;
};
/**
* Checking the matrix is a data of multiple rows
* @param matrix
* @returns {boolean}
*/
const isMatrix = matrix => {
if (_.size(matrix) === 0) {
return false;
}
const isAllArray = matrix.map(arr => _.isArray(arr));
return isAllArray.indexOf(false) === -1;
};
Notes:
_is Lodash.- Above code example only deals with
number[][]... =) ...
My question is, does this functionality already exist in Math.js?