sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.41k stars 475 forks source link

Matrix permutation normal form raises an error on empty matrices #36629

Closed videlec closed 11 months ago

videlec commented 11 months ago
sage: m = matrix(ZZ, 0, 0)
sage: m.permutation_normal_form()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[8], line 1
----> 1 m.permutation_normal_form()

File ~/micromamba/envs/flatsurf/lib/python3.10/site-packages/sage/matrix/matrix2.pyx:8442, in sage.matrix.matrix2.Matrix.permutation_normal_form (build/cythonized/sage/matrix/matrix2.c:58991)()
   8440                for i in range(nrows)]
   8441 # and find the maximal one:
-> 8442 first_row = max(sorted_rows)
   8443 first_rows = [j for j in range(nrows) if sorted_rows[j] == first_row]
   8444 # We construct an array S, which will record the subsymmetries of the

ValueError: max() arg is an empty sequence
fchapoton commented 11 months ago

one should use max(sorted_rows, default=...) for some value fo the default

or maybe just special-case the empty matrices

videlec commented 11 months ago

Anyway this code is also certainly much slower than bliss/nauty (on bipartite graphs).