min.js 3.0 KB

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