max.js 3.3 KB

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