mirror of
https://github.com/josdejong/mathjs.git
synced 2025-12-08 19:46:04 +00:00
101 lines
2.5 KiB
JavaScript
101 lines
2.5 KiB
JavaScript
module.exports = function (math) {
|
|
var util = require('../../util/index.js'),
|
|
|
|
Matrix = require('../../type/Matrix.js'),
|
|
collection = require('../../type/collection.js'),
|
|
|
|
isCollection = collection.isCollection;
|
|
|
|
/**
|
|
* Compute the maximum value of a list of values
|
|
*
|
|
* max(a, b, c, ...)
|
|
* max([a, b, c, ...])
|
|
*
|
|
* @param {... *} args A single matrix or or multiple scalar values
|
|
* @return {*} res
|
|
*/
|
|
math.max = function max(args) {
|
|
if (arguments.length == 0) {
|
|
throw new Error('Function max requires one or more parameters (0 provided)');
|
|
}
|
|
|
|
if (isCollection(args)) {
|
|
// max([a, b, c, d, ...]])
|
|
if (arguments.length > 1) {
|
|
throw Error('Wrong number of parameters (1 matrix or multiple scalars expected)');
|
|
}
|
|
|
|
var size = math.size(args).valueOf(),
|
|
asMatrix = (args instanceof Matrix),
|
|
array = asMatrix ? args.valueOf() : args;
|
|
|
|
if (size.length == 1) {
|
|
// vector
|
|
if (args.length == 0) {
|
|
throw new Error('Cannot calculate max of an empty vector');
|
|
}
|
|
|
|
return _max(array);
|
|
}
|
|
else if (size.length == 2) {
|
|
// 2 dimensional matrix
|
|
if (size[0] == 0 || size[1] == 0) {
|
|
throw new Error('Cannot calculate max of an empty matrix');
|
|
}
|
|
|
|
var res = _max2(array, size[0], size[1]);
|
|
return asMatrix ? new Matrix(res) : res;
|
|
}
|
|
else {
|
|
// TODO: implement max for n-dimensional matrices
|
|
throw new RangeError('Cannot calculate max for multi dimensional matrix');
|
|
}
|
|
}
|
|
else {
|
|
// max(a, b, c, d, ...)
|
|
return _max(arguments);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Calculate the max of a one dimensional array
|
|
* @param {Array} array
|
|
* @return {Number} max
|
|
* @private
|
|
*/
|
|
function _max(array) {
|
|
var res = array[0];
|
|
for (var i = 1, iMax = array.length; i < iMax; i++) {
|
|
var value = array[i];
|
|
if (math.larger(value, res)) {
|
|
res = value;
|
|
}
|
|
}
|
|
return res;
|
|
}
|
|
|
|
/**
|
|
* Calculate the max of a two dimensional array
|
|
* @param {Array} array
|
|
* @param {Number} rows
|
|
* @param {Number} cols
|
|
* @return {Number[]} max
|
|
* @private
|
|
*/
|
|
function _max2(array, rows, cols) {
|
|
var res = [];
|
|
for (var c = 0; c < cols; c++) {
|
|
var max = array[0][c];
|
|
for (var r = 1; r < rows; r++) {
|
|
var value = array[r][c];
|
|
if (math.larger(value, max)) {
|
|
max = value;
|
|
}
|
|
}
|
|
res[c] = max;
|
|
}
|
|
return res;
|
|
}
|
|
};
|