Source code for torx.arrays.array_functions_m
"""Array utility functions."""
import numpy as np
from torx.decorators import autodoc_function
[docs]
@autodoc_function
def find_nearest_index(array, value):
"""Return the index in the array that is nearest to the value."""
array = np.asarray(array)
idx = (np.abs(array - value)).argmin()
return idx
[docs]
@autodoc_function
def second_smallest(array):
"""Find the second smallest, unique entry in an array."""
m1 = m2 = float("inf")
for x in array.flat:
if x < m1:
m1, m2 = x, m1
elif x < m2 and x != m1:
m2 = x
return m2