Given some list of symmetric integer matrices, I want to remove all duplicates in the following equivalence relation:
Two matrices kxk M1 , M2 equivalent if there exists some permutation s on {1,...,k} such that for all i and j in {1,...,k} we have M1_ij = M2_s(i)s(j) , i.e. two matrices are “equal” if I can get one from the other, while rearranging its rows and columns.
Unfortunately, my naive approach (when building a list, check that any of the permutations of the new matrix is already in the list) is too slow.
Probably the faster alternative that I could think of would be to put all matrices into a list, rearrange them to some “canonical permutation”, and then delete duplicates, as described, for example. here . However, I am not sure how to achieve such a "canonical permutation" in the code.
To narrow it down a bit: matrices are relatively small ( k <= 4 ), the list will contain a 5 or 6-digit number of matrices, and the dtype for matrices should be an integer type (currently intc , but I could change that).
The order of the final list does not matter, and if there is any representative of each equivalence class. If necessary, the whole process can take a number of hours, but not days.
Is there a reasonably effective way to achieve this? Have I (once again) missed some cool NumPy or SciPy tool that will help me with this?
As requested, some small examples showing how this equivalence relation works:
Matrices {{1,1,1},{1,2,0},{1,0,3}} and {{1,1,1},{1,3,0},{1,0,2}} equivalent, since the permutation {1,2,3}->{1,3,2} converts one into another.
Matrices {{1,1,1},{1,2,0},{1,0,3}} and {{1,1,0},{1,2,1},{0,1,3}} not equivalent, you cannot change the position of 1s without rearranging the diagonal.