Skip to content

workbench_algorithms.experimental.utils.numeric_utils

Assorted numerical utils.

l1_normalize

l1_normalize(input_list: Iterable[float]) -> list[float]

Normalize an input list with respect to the L1-orm of the elements.

l2_normalize

l2_normalize(
    input_list: Iterable[float],
    allow_complex: bool = False,
    eps: float = 1e-15,
) -> np.ndarray

Normalize a 1D array so that the sum of squares equals 1 (L2 norm).

Parameters:

Name Type Description Default
input_list Iterable[float]

Input array

required
allow_complex bool

Whether to allow complex numbers

False
eps float

Tolerance for zero norm

1e-15

Returns:

Type Description
ndarray

L2-normalized array

l2_norm_diff

l2_norm_diff(arr1: np.ndarray, arr2: np.ndarray) -> float

Computes the L2-norm (Euclidean distance) between two real or complex arrays.

Parameters:

Name Type Description Default
arr1 ndarray

First input array (real or complex).

required
arr2 ndarray

Second input array (real or complex).

required

Returns:

Type Description
float

L2-norm distance between arr1 and arr2.

Raises:

Type Description
ValueError

If array shapes don't match or types don't match.

generate_complex_array

generate_complex_array(
    n: int,
    real_range: tuple[int, int] = (-1, 1),
    imag_range: tuple[int, int] = (-1, 1),
    random_number_generator: np.random.Generator
    | None = None,
) -> np.ndarray

Generate an array of n complex numbers.

The array is generated with real and imaginary parts sampled from uniform distributions within specified ranges.

Parameters:

Name Type Description Default
n int

Number of complex numbers to generate.

required
real_range tuple[int, int]

Range (min, max) for real parts.

(-1, 1)
imag_range tuple[int, int]

Range (min, max) for imaginary parts.

(-1, 1)
random_number_generator Generator | None

Generator for the random numbers.

None

Returns:

Type Description
ndarray

Array of complex numbers.