A robot calibration export can carry several contributions to the same coefficient. One report counts the input triplets, another counts stored entries in a sparse array, and a third asks how many coefficients are numerically nonzero. Those counts can differ even when the numerical result is correct.

Check a fixture with a repeated coordinate

The example assigns rows to calibration constraints and columns to parameter corrections. Two contributions, 2 and 3, share coordinate (0, 1). The coefficient at (1, 0) is 4. The last triplet records an explicit zero at (1, 1). The coordinate format retains all four stored entries. Conversion to compressed sparse row format sums the repeated pair, so the corresponding coefficient becomes 5.

from scipy.sparse import coo_array

rows = [0, 0, 1, 1]

cols = [1, 1, 0, 1]

values = [2.0, 3.0, 4.0, 0.0]

triplets = coo_array((values, (rows, cols)), shape=(2, 2))

matrix = triplets.tocsr()

assert triplets.nnz == 4

assert matrix.nnz == 3

assert matrix.toarray().tolist() == [[0.0, 5.0], [4.0, 0.0]]

matrix.eliminate_zeros()

assert matrix.nnz == 2

SciPy 1.18.1 and NumPy 2.5.5 produced the expected array. The first count drops during conversion because the repeated coordinate is combined. The second drops when the stored zero is removed in place. The dense numeric result is the same before and after removing the zero. The explicit shape retains both constraint rows and both parameter columns.

Decide which counts belong in the report

Summation fits an assembly process that adds independent contributions to a coefficient. An export with a contract of one value per coordinate needs a duplicate check before conversion, since after the sum a reader cannot tell which triplets produced the result. Keep the row and column identifiers with the triplet data so duplicate findings can be traced to their measurements.

Use a separate coverage record if downstream readers distinguish an observed zero from an unobserved position. An omitted coefficient and an explicit zero have the same value when read from the numerical array. Also test opposite-signed duplicate contributions. They can cancel to zero and still leave a stored entry until zero elimination. Track source rows, unique coordinates, and numeric nonzeros as separate quantities when auditing the export.

Sources

SciPy coo_array reference for coordinate storage and duplicate conversion

https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.coo_array.html

SciPy csr_array.eliminate_zeros reference for in-place removal

https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csr_array.eliminate_zeros.html