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