65 lines
1.6 KiB
JavaScript

module.exports = function (math) {
var Matrix = require('../../type/Matrix.js'),
collection = require('../../type/collection.js'),
isCollection = collection.isCollection;
/**
* Compute the mean value of a list of values
* In case of a multi dimensional array, the mean of the flattened array
* will be calculated.
*
* mean(a, b, c, ...)
* mean([a, b, c, ...])
*
* @param {... *} args A single matrix or or multiple scalar values
* @return {*} res
*/
math.mean = function mean(args) {
if (arguments.length == 0) {
throw new SyntaxError('Function mean requires one or more parameters (0 provided)');
}
if (isCollection(args)) {
if (arguments.length == 1) {
// mean([a, b, c, d, ...])
return _mean(args);
}
else if (arguments.length == 2) {
// mean([a, b, c, d, ...], dim)
// TODO: implement support for calculating the mean over specified dimension
throw new Error('Specifying a dimension is not yet implemented...');
}
else {
throw new SyntaxError('Wrong number of parameters');
}
}
else {
// mean(a, b, c, d, ...)
return _mean(arguments);
}
};
/**
* Recursively calculate the mean value in an n-dimensional array
* @param {Array} array
* @return {Number} mean
* @private
*/
function _mean(array) {
var sum = 0;
var num = 0;
collection.deepForEach(array, function (value) {
sum = math.add(sum, value);
num++;
});
if (num === 0) {
throw new Error('Cannot calculate mean of an empty array');
}
return math.divide(sum, num);
}
};