npu.utils.num_utils

Classes

NumUtils()

class npu.utils.num_utils.NumUtils
Author:

Alberto M. Esmoris Pena

Class with util numerical methods.

static find_decimal_scale(x, min_scale=None)

Find the minimum decimal scale \(\lambda = 10^{k} \in \mathbb{R}\) that must be used to represent the numbers in \(\pmb{x}\) without loosing precision.

\[k = \lfloor{ \log_{10}\left( \min \left\{ \tilde{x}_{i} : 1 \leq i \leq m \right\} \right) }\rfloor\]

where \(m \in \mathbb{Z}_{>0}\) is the amount of input numbers and \(\tilde{x}_i\) is the \(i\)-th number in the sorted input

\[\pmb{\tilde{x}} = \left( \tilde{x}_1=x_{j_1}, \ldots, \tilde{x}_{m}=x_{j_m} \right) \;\text{s.t.}\; \tilde{x}_{i} > \tilde{x}_{i+1} .\]

Note that if the input is an array or a tensor of greater dimensionality than a vector it will be flattened and treated as a vector. On top of that, repeated values are discarded. In other words, if the input contains \(R \in \mathbb{Z}_{\geq 0}\) repeated numbers, the dimensionality considered in the computations will be \(m=N-R\), where \(N \in \mathbb{Z}_{>0}\) is the original amount of input numbers.

Parameters:
  • x (np.ndarray) – The input numbers.

  • min_scale (float) – The minimum decimal scale. If not given, then it will be automatically determined from input data.

Returns:

\(\lambda = 10^{k} \in \mathbb{R}\)

Return type:

float

static compute_decimal_scale(x)

Assist the NumUtils.find_decimal_scale() by computing

\[10^{k}\]

with

\[k = \lfloor{ \log_{10}\left( \min \left\{ \tilde{x}_{i} : 1 \leq i \leq m \right\} \right) }\rfloor .\]
Parameters:

x (float) – The decimal number that must be representable for the computed decimal scale.

Returns:

The decimal scale.