median.js (3700B)
1 "use strict"; 2 3 Object.defineProperty(exports, "__esModule", { 4 value: true 5 }); 6 exports.createMedian = void 0; 7 8 var _collection = require("../../utils/collection.js"); 9 10 var _array = require("../../utils/array.js"); 11 12 var _factory = require("../../utils/factory.js"); 13 14 var _improveErrorMessage = require("./utils/improveErrorMessage.js"); 15 16 var name = 'median'; 17 var dependencies = ['typed', 'add', 'divide', 'compare', 'partitionSelect']; 18 var createMedian = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) { 19 var typed = _ref.typed, 20 add = _ref.add, 21 divide = _ref.divide, 22 compare = _ref.compare, 23 partitionSelect = _ref.partitionSelect; 24 25 /** 26 * Recursively calculate the median of an n-dimensional array 27 * @param {Array} array 28 * @return {Number} median 29 * @private 30 */ 31 function _median(array) { 32 try { 33 array = (0, _array.flatten)(array.valueOf()); 34 var num = array.length; 35 36 if (num === 0) { 37 throw new Error('Cannot calculate median of an empty array'); 38 } 39 40 if (num % 2 === 0) { 41 // even: return the average of the two middle values 42 var mid = num / 2 - 1; 43 var right = partitionSelect(array, mid + 1); // array now partitioned at mid + 1, take max of left part 44 45 var left = array[mid]; 46 47 for (var i = 0; i < mid; ++i) { 48 if (compare(array[i], left) > 0) { 49 left = array[i]; 50 } 51 } 52 53 return middle2(left, right); 54 } else { 55 // odd: return the middle value 56 var m = partitionSelect(array, (num - 1) / 2); 57 return middle(m); 58 } 59 } catch (err) { 60 throw (0, _improveErrorMessage.improveErrorMessage)(err, 'median'); 61 } 62 } // helper function to type check the middle value of the array 63 64 65 var middle = typed({ 66 'number | BigNumber | Complex | Unit': function numberBigNumberComplexUnit(value) { 67 return value; 68 } 69 }); // helper function to type check the two middle value of the array 70 71 var middle2 = typed({ 72 'number | BigNumber | Complex | Unit, number | BigNumber | Complex | Unit': function numberBigNumberComplexUnitNumberBigNumberComplexUnit(left, right) { 73 return divide(add(left, right), 2); 74 } 75 }); 76 /** 77 * Compute the median of a matrix or a list with values. The values are 78 * sorted and the middle value is returned. In case of an even number of 79 * values, the average of the two middle values is returned. 80 * Supported types of values are: Number, BigNumber, Unit 81 * 82 * In case of a (multi dimensional) array or matrix, the median of all 83 * elements will be calculated. 84 * 85 * Syntax: 86 * 87 * math.median(a, b, c, ...) 88 * math.median(A) 89 * 90 * Examples: 91 * 92 * math.median(5, 2, 7) // returns 5 93 * math.median([3, -1, 5, 7]) // returns 4 94 * 95 * See also: 96 * 97 * mean, min, max, sum, prod, std, variance, quantileSeq 98 * 99 * @param {... *} args A single matrix or or multiple scalar values 100 * @return {*} The median 101 */ 102 103 return typed(name, { 104 // median([a, b, c, d, ...]) 105 'Array | Matrix': _median, 106 // median([a, b, c, d, ...], dim) 107 'Array | Matrix, number | BigNumber': function ArrayMatrixNumberBigNumber(array, dim) { 108 // TODO: implement median(A, dim) 109 throw new Error('median(A, dim) is not yet supported'); // return reduce(arguments[0], arguments[1], ...) 110 }, 111 // median(a, b, c, d, ...) 112 '...': function _(args) { 113 if ((0, _collection.containsCollections)(args)) { 114 throw new TypeError('Scalar values expected in function median'); 115 } 116 117 return _median(args); 118 } 119 }); 120 }); 121 exports.createMedian = createMedian;