Might be a bug in to_permutation_matrix?
Hi, Currently I'm using you birkhoff module, it works fine in most cases, great work!
However, in the to_permutation_matrix(matches) functions, the length of matches might be different from the input matrix S(D) 's degree (m,n) (Somehow? I'm not sure about the algorithm itself.In the test, sometimes it works fine but sometimes failed), leading to a index_out_of_range exception, since you are using len(matches) as the length of returned matrix P.
After I create the right length of P in to_permutation_matrix(), it works.
Theoretically if S is a square doubly stochastic matrix of size n, the number of matches (which represents the number of elements being permuted) should be n every time through the loop. Can you find a specific example of an input for which this fails?
There may be some precision issues (like when very small numbers get rounded to zero in the doubly stochastic matrix), which is what I suspect the problem is here. One thing we could try is scaling the values in the matrix up by a large integer to protect against this problem.
Closing due to missing a minimal working example demonstrating the issue.
Hi,
Thanks for providing the package.
I have an example of a case where it does not work for. First of all the traceback:
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\birkhoff.py", line 223, in birkhoff_von_neumann_decomposition P = to_permutation_matrix(M)
File "C:\ProgramData\Anaconda3\lib\site-packages\birkhoff.py", line 60, in to_permutation_matrix P[list(zip(*(matches.items())))] = 1
IndexError: index 39 is out of bounds for axis 0 with size 39
And here the matrix I tried it for:
It's a 40x40 Matrix.
It would be awesome if you could reopen this issue and try to fix this.
I am using it with Python 3. I also tried to trace down the problem and found out, that the maximum_matching in line 211 at one point only returns len(M) = 78 instead of 80 like it should be. Maybe the problem lies somewhere around there.
Your help would be much appreciated!
Cheers, Laura
Thank you for providing an example and tracking down the issue! I'll take a look when I have some time.
There is a bug in the code and which can be fixed by the making minor edit. I am also putting the updated functions below. The error was due to the line, n = len(matches) in to_permutation function because len(matches) can eventually decrease as S becomes sparser.
def to_permutation_matrix(matches, n):
P = np.zeros((n, n))
P[list(zip(*(matches.items())))] = 1
return P
def birkhoff_von_neumann_decomposition(D):
m, n = D.shape
if m != n:
raise ValueError('Input matrix must be square ({} x {})'.format(m, n))
indices = list(itertools.product(range(m), range(n)))
coefficients = []
permutations = []
S = D.astype('float')
while not np.all(S == 0):
W = to_pattern_matrix(S)
X = to_bipartite_matrix(W)
G = from_numpy_matrix(X)
left_nodes = range(n)
M = maximum_matching(G, left_nodes)
M = {u: v % n for u, v in M.items() if u < n}
P = to_permutation_matrix(M, n)
q = min(S[i, j] for (i, j) in indices if P[i, j] == 1)
coefficients.append(q)
permutations.append(P)
S -= q * P
S[np.abs(S) < TOLERANCE] = 0.0
return list(zip(coefficients, permutations))
This worked for me, thank you! @prateeky2806