From b3ccbf56e20f83a28dc7f5b6b0e23230b3cfdaa0 Mon Sep 17 00:00:00 2001 From: RichieHakim Date: Sat, 23 Mar 2024 04:17:43 -0400 Subject: [PATCH] Add native Python operations for argsort, argmax, and argmin --- bnpm/indexing.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/bnpm/indexing.py b/bnpm/indexing.py index f1396d5..2dc1682 100644 --- a/bnpm/indexing.py +++ b/bnpm/indexing.py @@ -818,4 +818,54 @@ def cp_to_dense(cp, weights=None): dense = einsum(str_einsum, *[cp[m] * weights for m in range(n_modes)]) - return dense \ No newline at end of file + return dense + + +#################################################################################################### +################################## NATIVE PYTHON OPERATIONS ######################################## +#################################################################################################### + +def native_argsort(l): + """ + Native Python argsort. Returns the indices that would sort a list. + RH 2024 + + Args: + l (list): + List to sort + + Returns: + (list): + List of indices that would sort the input list. + """ + return sorted(range(len(l)), key=lambda k: l[k]) + +def native_argmax(l): + """ + Native Python argmax. Returns the index of the maximum value in a list. + RH 2024 + + Args: + l (list): + List to find the argmax of + + Returns: + (int): + Index of the maximum value in the list. + """ + return max(range(len(l)), key=lambda k: l[k]) + +def native_argmin(l): + """ + Native Python argmin. Returns the index of the minimum value in a list. + RH 2024 + + Args: + l (list): + List to find the argmin of + + Returns: + (int): + Index of the minimum value in the list. + """ + return min(range(len(l)), key=lambda k: l[k]) \ No newline at end of file