max.js 3.0 KB

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