Added faster and simpler versions of max_index and min_index.

This commit is contained in:
Adrian Mariano 2019-06-23 13:17:04 -04:00
parent aa31fb9ddd
commit 49365add3c

View file

@ -70,33 +70,27 @@ function approx(a,b,eps=EPSILON) = let(c=a-b) (is_num(c)? abs(c) : norm(c)) <= e
// Function: min_index() // Function: min_index()
// Usage: // Usage:
// min_index(vals); // min_index(vals,[all]);
// Description: // Description:
// Returns the index of the minimal value in the given list. // Returns the index of the first occurrence of the mainimum value in the given list.
function min_index(vals, _minval, _minidx, _i=0) = // If `all` is true then returns a list of all indices where the minimum value occurs.
_i>=len(vals)? _minidx : // Arguments:
min_index( // vals = vector of values
vals, // all = set to true to return indices of all occurences of the minimum. Default: false
((_minval == undef || vals[_i] < _minval)? vals[_i] : _minval), function min_index(vals, all=false) =
((_minval == undef || vals[_i] < _minval)? _i : _minidx), all ? search(min(vals),vals,0) : search(min(vals), vals)[0];
_i+1
);
// Function: max_index() // Function: max_index()
// Usage: // Usage:
// max_index(vals); // max_index(vals,[all]);
// Description: // Description:
// Returns the index of the maximum value in the given list. // Returns the index of the first occurrence of the maximum value in the given list.
function max_index(vals, _maxval, _maxidx, _i=0) = // If `all` is true then returns a list of all indices where the maximum value occurs.
_i>=len(vals)? _maxidx : // Arguments:
max_index( // vals = vector of values
vals, // all = set to true to return indices of all occurences of the maximum. Default: false
((_maxval == undef || vals[_i] > _maxval)? vals[_i] : _maxval), function max_index(vals, all=false) =
((_maxval == undef || vals[_i] > _maxval)? _i : _maxidx), all ? search(max(vals),vals,0) : search(max(vals), vals)[0];
_i+1
);
// Function: posmod() // Function: posmod()
// Usage: // Usage: