Ask Your Question

Can't find error in my code

asked 2022-10-03 16:34:28 +0200

sgmth gravatar image

updated 2022-10-04 06:17:12 +0200

Please refer to my question at

According to the suggestion given in the end by Max Alekseyev I have made a complete code. Here I am showing only the initial part , as problem lies in it . I don't know what I am missing or what mistake I have made.

M = matrix(3,3, [1, 1, 1, -1, 1, 1, -1, -1, 1])
n = 3
V = GF(2)^(n^2)
# Removing the all-zero list because not needed for our purpose
#Removing the all-ones list because not needed for our purpose
for v in VL2:    
 #Forming n by n matrices from  list in  VL2. Contains all 0,1 matrices.                                               
for p in P:
 #Forming n by n matrices from P by taking their negative. Contains all 0,-1 matrices 
U = []
for a in P:
for b in N:
#Taking union of P and N   
def mat_to_vector(m):
      temp = []
      for i in range(m.ncols()):
               for j in range(m.nrows()):
  #Defining a function which converts matrix to vector
for i in range(len(U)):
       X = ZZ^(n^2)
       Y = X.span(mat_to_vector(T[i]) for i in range(len(T))) 
       for w in W:
              if mat_to_vector(w)  in Y:
       VTM=[matrix(ZZ,n,n,u) for u in CAND]
       for b in VTM:
       B = set(VTM)

According to the suggestion, len(B) should come out to be less than len(U). But it is coming out to be same as len(U). Kindly find the error.

edit retag flag offensive close merge delete


Can you explain what your code does, what are B and U and why length of B should be smaller than that of U?

Max Alekseyev gravatar imageMax Alekseyev ( 2022-10-03 21:39:09 +0200 )edit


for a in P:

can be simplified to U.extend(P).

Max Alekseyev gravatar imageMax Alekseyev ( 2022-10-03 21:44:44 +0200 )edit

Commented the code, to make it clear. First, formed (in P) all possible 0,1 matrices(excluding all zero and all ones matrices). Took their negatives(in N) and took U to be union of P and N, to get all possible 0,1 and 0,-1 matrices(len(U)=1020) . The candidates(pl. see the previous link) for A2,A3,A4 should come from U only. In List_SU taking A1 to be U[i] and running i over the range(len(U)) we are going over all 0,1 and 0,-1 matrices A1. W being a copy of U, removed this U[i] and checked which of the remaining 1019 elements in W are in the span of T(=tuple(List_SU)). The corresponding matrices VTM would be be the candidates for A2,A3,A4. To remove repetions in VTM formed its set B. But len(B) is also ...(more)

sgmth gravatar imagesgmth ( 2022-10-04 06:17:26 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2022-10-05 03:30:53 +0200

Max Alekseyev gravatar image

updated 2022-10-05 17:58:03 +0200

The error is in combining candidates for different U[i] in the same list CAND. They should processed independently and mixing these candidates together does not make sense. Also, your code is quite messy and not Pythonic. Here is cleaned and extended version that constructs solutions for any matrix M:

import itertools

k = 4   # number of matrices in the required linear combinations

#M = matrix(3,3, [1, 1, 1, -1, 1, 1, -1, -1, 1])
M = matrix(2,2,[1,1,1,-1])
n = M.nrows()
d = M.minpoly().degree()

assert k >= d

List_S=[ M^i for i in range(d)]
VL2 = (GF(2)^(n^2)).list()[1:-1]
# Removing the all-zero and all-one lists because not needed for our purpose

P = [ matrix(ZZ,n,n,v) for v in VL2 ]
#Forming n by n matrices from  list in  VL2. Contains all 0,1 matrices.

N = [-p for p in P]
#Forming n by n matrices from P by taking their negative. Contains all 0,-1 matrices 

U = P + N
#Taking concatenation of P and N   


def mat_to_vector(m):
    return vector( sum(map(list,m.rows()), []), immutable=True )
#Defining a function which converts matrix to vector

X = ZZ^(n^2)
result = set()

for u in Combinations(U,k-d):
    Y = X.span(mat_to_vector(t) for t in List_S + u) 
    CAND = [ w for w in U if w not in u and mat_to_vector(w) in Y ]
    for v in Combinations(CAND,d):
        if sum(u) + sum(v) == M:
            sol = frozenset( Matrix(w,immutable=True) for w in u + v )   # a candidate solution as a set
            if sol not in result:
                Z = X.span(mat_to_vector(t) for t in u+v)
                # the following check is still required
                if all( mat_to_vector(w[0]*w[1]) in Z for w in itertools.product(sol,repeat=2) ):
                    result.add( sol )

edit flag offensive delete link more


Had processed them individually only, but didn't get all combinations.See below(So wrongly thought perhaps your suggestion points otherwise and hence had done that ).

For simplicity let us take M=matrix(2,2,[1,1,1,-1]) and n=2 in my code . (As it will take me time to make changes to extend your code kindly bear with my messy code for the time being) Kindly replace the portion in my code after the line "Y=...." with the following:

   for w in W:
             if mat_to_vector(w)  in Y:                      

   VTM=[matrix(ZZ,n,n,u) for u in ICAND]

   for t in Z:
        if sum(t)+U[i]==M:
sgmth gravatar imagesgmth ( 2022-10-05 14:40:36 +0200 )edit

Note that , to get the "final required combinations" , for the candidates obtained in VTM we have to check following two things:

(i) Which 3 combinations of them summing up with U[i] would give M.

(ii) Out of those obtained in (i) for which of them, all products AiAj are a linear combination of A1,A2,A3,A4.

Now in the code, I have checked (i) only, but even output of (i) fails to show for example the following valid "final required combination"

  {  [1 0]      [0 1]     [0 0]     [ 0  0]
     [0 0] ,    [0 0]  ,  [1 0] ,   [ 0 -1]  }
sgmth gravatar imagesgmth ( 2022-10-05 14:43:22 +0200 )edit

@sgmth: Number of elements to add to List_S should be equal to the difference between the length of a linear required combination (k in my code) and the degree of minimal polynomial (d) of matrix M. For your 2x2 matrix, this difference is 2. Also, since we used only necessary condition, each candidate solution $A_1,A_2,A_3,A_4$ will further need to be tested on whether $A_iA_j$ is in the span $\langle A_1,A_2,A_3,A_4\rangle$. I've updated my code to produce solutions for any given matrix based on this approach.

Max Alekseyev gravatar imageMax Alekseyev ( 2022-10-05 17:29:01 +0200 )edit

Thanks a lot. Appreciate your time and effort, and the detailed explanation. Works fine for n=2. Have run for n=3 and k=5 , four hours back but still processing. I guess because of the combinatorial nature, nothing further much can be done to speed up . Thanks again.

sgmth gravatar imagesgmth ( 2022-10-06 09:18:28 +0200 )edit

There is definitely space for further improvement.

Max Alekseyev gravatar imageMax Alekseyev ( 2022-10-06 13:30:02 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2022-10-03 16:34:28 +0200

Seen: 263 times

Last updated: Oct 05 '22