import rustymath as rmSearching, sorting and selection
Ordering
rm.search provides stable sort, argsort and lexsort, searchsorted, integer bincount, class counts and group_indices. These functions are also available directly on rm. Sorts work along the last axis unless you give axis. Ordering and insertion searches compare exactly and equate signed zeros. NaNs come last in ascending order and first with sort(..., descending=True).
scores = rm.array([8., 2, 5, 1, 9, 3])
rm.sort(scores), rm.argsort(scores)(Array([1.0, 2.0, 3.0, 5.0, 8.0, 9.0], dtype=float64),
Array([3, 1, 5, 2, 0, 4], dtype=int64))
Partial selection
partition(x, k) puts the item of rank k in its sorted position without sorting either side. argpartition gives the positions instead of the values. A negative rank counts from the end. Median and quantiles use selection rather than fully sorting the array; percentile takes probabilities in percent. Quantiles interpolate linearly between neighbouring ranks:
rm.partition(scores, 2), rm.median(scores), rm.quantile(scores, [0.25, 0.5, 0.75])(Array([1.0, 2.0, 3.0, 5.0, 8.0, 9.0], dtype=float64),
4.0,
Array([2.25, 4.0, 7.25], dtype=float64))
Matching and classification
unique, index and isin compare floats with a relative tolerance, controlled by ct (default rm.CT). They match Booleans and integers exactly. unique keeps values in order of first occurrence, with each one’s first position, each item’s class number and each class’s size. index(x, y) gives where in x each item of y first matches, or the size of x where nothing does:
rm.unique([0.3, 1.0, 0.1 + 0.2]), rm.index([0.3, 1.0], 0.1 + 0.2)(Unique(values=Array([0.3, 1.0], dtype=float64), firsts=Array([0, 1], dtype=int64), numbers=Array([0, 1, 0], dtype=int64), counts=Array([2, 1], dtype=int64)),
0)
Set operations use the same tolerant matching as isin and retain first-occurrence order, rather than sorting their results. Their inputs are flattened; ct=0 selects exact matching.
rm.union1d([3, 1, 3, 2], [2, 4]), rm.intersect1d([3, 1, 3, 2], [2, 4]), rm.setdiff1d([3, 1, 3, 2], [2, 4])(Array([3, 1, 2, 4], dtype=int64),
Array([2], dtype=int64),
Array([3, 1], dtype=int64))