Describe the problem
The functions util.value_representation.sig_dig and util.value_representation.sig_dig_list seem to be a bit slow (they convert the float input to a string and back).
def sig_dig(x, n_sig_dig=16):
num_of_digits = len(str(x).replace(".", ""))
if n_sig_dig >= num_of_digits:
return x
n = math.floor(math.log10(abs(x)) + 1 - n_sig_dig)
result = decimal.Decimal(str(np.round(x * 10 ** (-n)))) * decimal.Decimal(
str(10**n)
)
return float(result)
E.g., this function seemed to be more than 100 times faster. It created some floating point issues though and is thus not implemented.
def round_to_sig_digits(x, n_sig_dig):
x = np.asarray(x, dtype=np.float64) # Ensure consistent precision
x_positive = np.where(np.isfinite(x) & (x != 0), np.abs(x), 10 ** (n_sig_dig - 1))
mags = 10 ** (n_sig_dig - 1 - np.floor(np.log10(x_positive)))
return np.around(x * mags) / mags
The function does not seem to be used much so this might not be urgent.
Describe the problem
The functions
util.value_representation.sig_digandutil.value_representation.sig_dig_listseem to be a bit slow (they convert the float input to a string and back).E.g., this function seemed to be more than 100 times faster. It created some floating point issues though and is thus not implemented.
The function does not seem to be used much so this might not be urgent.