simple-squiggle

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

max.js (3365B)


      1 "use strict";
      2 
      3 Object.defineProperty(exports, "__esModule", {
      4   value: true
      5 });
      6 exports.createMax = void 0;
      7 
      8 var _collection = require("../../utils/collection.js");
      9 
     10 var _factory = require("../../utils/factory.js");
     11 
     12 var _improveErrorMessage = require("./utils/improveErrorMessage.js");
     13 
     14 var name = 'max';
     15 var dependencies = ['typed', 'config', 'numeric', 'larger'];
     16 var createMax = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
     17   var typed = _ref.typed,
     18       config = _ref.config,
     19       numeric = _ref.numeric,
     20       larger = _ref.larger;
     21 
     22   /**
     23    * Compute the maximum value of a matrix or a  list with values.
     24    * In case of a multi dimensional array, the maximum of the flattened array
     25    * will be calculated. When `dim` is provided, the maximum over the selected
     26    * dimension will be calculated. Parameter `dim` is zero-based.
     27    *
     28    * Syntax:
     29    *
     30    *     math.max(a, b, c, ...)
     31    *     math.max(A)
     32    *     math.max(A, dim)
     33    *
     34    * Examples:
     35    *
     36    *     math.max(2, 1, 4, 3)                  // returns 4
     37    *     math.max([2, 1, 4, 3])                // returns 4
     38    *
     39    *     // maximum over a specified dimension (zero-based)
     40    *     math.max([[2, 5], [4, 3], [1, 7]], 0) // returns [4, 7]
     41    *     math.max([[2, 5], [4, 3]], [1, 7], 1) // returns [5, 4, 7]
     42    *
     43    *     math.max(2.7, 7.1, -4.5, 2.0, 4.1)    // returns 7.1
     44    *     math.min(2.7, 7.1, -4.5, 2.0, 4.1)    // returns -4.5
     45    *
     46    * See also:
     47    *
     48    *    mean, median, min, prod, std, sum, variance
     49    *
     50    * @param {... *} args  A single matrix or or multiple scalar values
     51    * @return {*} The maximum value
     52    */
     53   return typed(name, {
     54     // max([a, b, c, d, ...])
     55     'Array | Matrix': _max,
     56     // max([a, b, c, d, ...], dim)
     57     'Array | Matrix, number | BigNumber': function ArrayMatrixNumberBigNumber(array, dim) {
     58       return (0, _collection.reduce)(array, dim.valueOf(), _largest);
     59     },
     60     // max(a, b, c, d, ...)
     61     '...': function _(args) {
     62       if ((0, _collection.containsCollections)(args)) {
     63         throw new TypeError('Scalar values expected in function max');
     64       }
     65 
     66       return _max(args);
     67     }
     68   });
     69   /**
     70    * Return the largest of two values
     71    * @param {*} x
     72    * @param {*} y
     73    * @returns {*} Returns x when x is largest, or y when y is largest
     74    * @private
     75    */
     76 
     77   function _largest(x, y) {
     78     try {
     79       return larger(x, y) ? x : y;
     80     } catch (err) {
     81       throw (0, _improveErrorMessage.improveErrorMessage)(err, 'max', y);
     82     }
     83   }
     84   /**
     85    * Recursively calculate the maximum value in an n-dimensional array
     86    * @param {Array} array
     87    * @return {number} max
     88    * @private
     89    */
     90 
     91 
     92   function _max(array) {
     93     var res;
     94     (0, _collection.deepForEach)(array, function (value) {
     95       try {
     96         if (isNaN(value) && typeof value === 'number') {
     97           res = NaN;
     98         } else if (res === undefined || larger(value, res)) {
     99           res = value;
    100         }
    101       } catch (err) {
    102         throw (0, _improveErrorMessage.improveErrorMessage)(err, 'max', value);
    103       }
    104     });
    105 
    106     if (res === undefined) {
    107       throw new Error('Cannot calculate max of an empty array');
    108     } // make sure returning numeric value: parse a string into a numeric value
    109 
    110 
    111     if (typeof res === 'string') {
    112       res = numeric(res, config.number);
    113     }
    114 
    115     return res;
    116   }
    117 });
    118 exports.createMax = createMax;