# Copyright (c) 2026 The Anuvani-Diarization authors # SPDX-License-Identifier: MIT OR Apache-2.0 """numpy add-reduction (pairwise summation) vs diar_exact::pairwise, bitwise.""" import numpy as np from hypothesis import given, strategies as st from hypothesis.extra import numpy as hnp from conftest import bits_equal finite64 = st.floats(allow_nan=False, allow_infinity=False, width=64) any64 = st.floats(width=64) finite32 = st.floats(allow_nan=False, allow_infinity=False, width=32) @given(hnp.arrays(np.float64, st.integers(0, 600), elements=any64)) def test_sum_f64_any(core, x): assert bits_equal(np.float64(core.sum_f64(x)), np.add.reduce(x)) @given(hnp.arrays(np.float32, st.integers(0, 600), elements=finite32)) def test_sum_f32(core, x): assert bits_equal(np.float32(core.sum_f32(x)), np.add.reduce(x)) def test_lengths_around_block_edges(core): rng = np.random.default_rng(0) for n in list(range(0, 300)) + [511, 512, 513, 1023, 1024, 1025, 4097, 8191, 8192, 8193, 70000]: for scale in (1.0, 1e-30, 1e30): x = (rng.standard_normal(n) * scale).astype(np.float64) assert bits_equal(np.float64(core.sum_f64(x)), np.add.reduce(x)), n y = x.astype(np.float32) assert bits_equal(np.float32(core.sum_f32(y)), np.add.reduce(y)), n def test_signed_zeros(core): for x in ([-0.0], [-0.0, -0.0], [-0.0] * 9, [-0.0] * 200, []): a = np.array(x, dtype=np.float64) assert bits_equal(np.float64(core.sum_f64(a)), np.add.reduce(a)) @given(hnp.arrays(np.float32, st.tuples(st.integers(1, 40), st.integers(1, 300)), elements=finite32)) def test_sum_axes_f32(core, x): r1, r0 = core.sum_axes_f32(np.ascontiguousarray(x)) assert bits_equal(np.array(r1, dtype=np.float32), x.sum(axis=1)) assert bits_equal(np.array(r0, dtype=np.float32), x.sum(axis=0))