simple-squiggle

A restricted subset of Squiggle
Log | Files | Refs | README

mode.js (2295B)


      1 "use strict";
      2 
      3 Object.defineProperty(exports, "__esModule", {
      4   value: true
      5 });
      6 exports.createMode = void 0;
      7 
      8 var _array = require("../../utils/array.js");
      9 
     10 var _factory = require("../../utils/factory.js");
     11 
     12 var name = 'mode';
     13 var dependencies = ['typed', 'isNaN', 'isNumeric'];
     14 var createMode = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
     15   var typed = _ref.typed,
     16       isNaN = _ref.isNaN,
     17       isNumeric = _ref.isNumeric;
     18 
     19   /**
     20   * Computes the mode of a set of numbers or a list with values(numbers or characters).
     21   * If there are more than one modes, it returns a list of those values.
     22   *
     23   * Syntax:
     24   *
     25   *     math.mode(a, b, c, ...)
     26   *     math.mode(A)
     27   *
     28   * Examples:
     29   *
     30   *     math.mode(2, 1, 4, 3, 1)                            // returns [1]
     31   *     math.mode([1, 2.7, 3.2, 4, 2.7])                    // returns [2.7]
     32   *     math.mode(1, 4, 6, 1, 6)                             // returns [1, 6]
     33   *     math.mode('a','a','b','c')                           // returns ["a"]
     34   *     math.mode(1, 1.5, 'abc')                             // returns [1, 1.5, "abc"]
     35   *
     36   * See also:
     37   *
     38   *     median,
     39   *     mean
     40   *
     41   * @param {... *} args  A single matrix
     42   * @return {*} The mode of all values
     43   */
     44   return typed(name, {
     45     'Array | Matrix': _mode,
     46     '...': function _(args) {
     47       return _mode(args);
     48     }
     49   });
     50   /**
     51    * Calculates the mode in an 1-dimensional array
     52    * @param {Array} values
     53    * @return {Array} mode
     54    * @private
     55    */
     56 
     57   function _mode(values) {
     58     values = (0, _array.flatten)(values.valueOf());
     59     var num = values.length;
     60 
     61     if (num === 0) {
     62       throw new Error('Cannot calculate mode of an empty array');
     63     }
     64 
     65     var count = {};
     66     var mode = [];
     67     var max = 0;
     68 
     69     for (var i = 0; i < values.length; i++) {
     70       var value = values[i];
     71 
     72       if (isNumeric(value) && isNaN(value)) {
     73         throw new Error('Cannot calculate mode of an array containing NaN values');
     74       }
     75 
     76       if (!(value in count)) {
     77         count[value] = 0;
     78       }
     79 
     80       count[value]++;
     81 
     82       if (count[value] === max) {
     83         mode.push(value);
     84       } else if (count[value] > max) {
     85         max = count[value];
     86         mode = [value];
     87       }
     88     }
     89 
     90     return mode;
     91   }
     92 });
     93 exports.createMode = createMode;