Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I asked this thing because a friend of mine made code for this but it is very time taking. For 2nd order matrices output comes out instantly. For 3rd order , output starts coming but it is taking a lot of time. For 4th order , no output is shown even after hours. Since for 2nd order, number of elements in V.List is 2^4, for 3rd order it is 2^9 and for 4th order it is 2^16 so it is time taking. Can any change be made to make this code run faster?

Program to get all the combinations of 0,1 and 0,-1 matrices which when added up, give the required matrix.

Write your matrix

H = matrix(2,2, [1, 1,1, -1])

H=matrix(3,3,[1,0,1,-1,1,0,0,-1,1])

H=matrix(4,4,[1, 1, 1, 1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, -1, 1])

show("H=",H)

n = int(input("Enter the order of the matrix H : "))

V = GF(2)^(n^2)

show("V.List=", V.list())

R1=V.list()[1:] #We are removing the all-zero list because not needed for our purpose

show("R1=", R1)

R2=R1[:-1] #We are removing the all-ones list because not needed for our purpose

show("R2=", R2)

A = [] for v in R2: A.append(matrix(ZZ,n,n,v)) #Here we are forming n by n matrices from list in R2

show("A=",A) #This contains all 0,1 matrices

B=[] for a in A: b=-a B.append(b)

show("B=",B) #Here we are forming n by n matrices from A

                                           #by taking their negative.
                                          #This contains all 0,-1 matrices

C = [] for a in A: C.append(a) for b in B: C.append(b)

show("C=",C) #Here we are taking the union of A and B

test_sum = int(input("Into how many matrices you want to break H :"))

test_comb = Combinations(C,test_sum) # Here we are forming all "test_sum"-set combinations from C". That is if #test_sum =3, then test_comb, gives all 3 set combinations from C"

for t in test_comb: if (sum(t)==H): #Checking which combinations in test_sum give the sum = H and printing those

    print("A combination is: ")    
    show(t)                     
    print("\n")

I asked this thing because a friend of mine made code for this but it is very time taking. For 2nd order matrices output comes out instantly. For 3rd order , output starts coming but it is taking a lot of time. For 4th order , no output is shown even after hours. Since for 2nd order, number of elements in V.List is 2^4, for 3rd order it is 2^9 and for 4th order it is 2^16 so it is time taking. Can any change be made to make this code run faster?

Program to get all the combinations of 0,1 and 0,-1 matrices which when added up, give the required matrix.

Write your matrix

H = matrix(2,2, [1, 1,1, -1])

H=matrix(3,3,[1,0,1,-1,1,0,0,-1,1])

H=matrix(4,4,[1, 1, 1, 1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, -1, 1])

show("H=",H)

n = int(input("Enter the order of the matrix H : "))

V = GF(2)^(n^2)

show("V.List=", V.list())

R1=V.list()[1:] #We are removing the all-zero list because not needed for our purpose

show("R1=", R1)

R2=R1[:-1] #We are removing the all-ones list because not needed for our purpose

show("R2=", R2)

A = [] for v in R2: A.append(matrix(ZZ,n,n,v)) # Forming n by n matrices from list in R2. Contains all 0,1 matrices

B=[] for a in A: b=-a B.append(b)
#Here we are forming n by n matrices from list in R2

show("A=",A) #This contains all 0,1 matrices

B=[] for a in A: b=-a B.append(b)

show("B=",B) #Here we are forming n A by n matrices from A

                                           #by taking their negative.
                                          #This negative. his contains all 0,-1 matrices
matrices

C = [] for a in A: C.append(a) for b in B: C.append(b)

show("C=",C) C.append(b) #Here we are taking the union of A and B

test_sum = int(input("Into how many matrices you want to break H :"))

test_comb = Combinations(C,test_sum) # Here we are forming all "test_sum"-set combinations from C". That is if #test_sum =3, then test_comb, gives all 3 set combinations from C"

for t in test_comb: if (sum(t)==H): #Checking which combinations in test_sum give the sum = H and printing those

    print("A combination is: ")    
    show(t)                     
    print("\n")

I asked this thing because a friend of mine made code for this but it is very time taking. For 2nd order matrices output comes out instantly. For 3rd order , output starts coming but it is taking a lot of time. For 4th order , no output is shown even after hours. Since for 2nd order, number of elements in V.List is 2^4, for 3rd order it is 2^9 and for 4th order it is 2^16 so it is time taking. Can any change be made to make this code run faster?

Write your matrix

H = matrix(2,2, [1, 1,1, -1])

show("H=",H)

n = int(input("Enter the order of the matrix H : "))

V = GF(2)^(n^2)

R1=V.list()[1:]
#We are removing the all-zero list because not needed for our purpose

R2=R1[:-1]
#We are removing the all-ones list because not needed for our purpose

A = [] for v in R2: A.append(matrix(ZZ,n,n,v))
# Forming n by n matrices from list in R2. Contains all 0,1 matricesmatrices.

B=[] for a in A: b=-a B.append(b)
#Here we are forming # Forming n by n matrices from A by taking their negative. his contains Contains all 0,-1 matrices

C = [] for a in A: C.append(a) for b in B: C.append(b) #Here we are taking the C.append(b) #Taking union of A and B

test_sum = int(input("Into how many matrices you want to break H :"))

test_comb = Combinations(C,test_sum)
# Here we are forming all "test_sum"-set combinations from C".
#
That is is, if #test_sum test_sum =3, then test_comb, gives all 3 set combinations from C"

for t in test_comb: if (sum(t)==H):
#Checking which combinations in test_sum give the sum = H and printing those

    print("A combination is: ")    
    show(t)                     
    print("\n")
click to hide/show revision 4
No.4 Revision

I asked this thing because a friend of mine made code for this but it is very time taking. For 2nd order matrices output comes out instantly. For 3rd order , output starts coming but it is taking a lot of time. For 4th order , no output is shown even after hours. Since for 2nd order, number of elements in V.List is 2^4, for 3rd order it is 2^9 and for 4th order it is 2^16 so it is time taking. Can any change be made to make this code run faster?

Write
#Write your matrix

matrix H = matrix(2,2, [1, 1,1, -1])

show("H=",H)

show("H=",H) n = int(input("Enter the order of the matrix H : "))

")) V = GF(2)^(n^2)

R1=V.list()[1:]
#We are removing the all-zero list because not needed for our purpose

purpose R2=R1[:-1]
#We are removing the all-ones list because not needed for our purpose

purpose A = [] for v in R2: A.append(matrix(ZZ,n,n,v))
# Forming n by n matrices from list in R2. Contains all 0,1 matrices.

matrices B=[] for a in A: b=-a B.append(b)
# Forming
B.append(b) #Here we are forming n by n matrices from A by taking their negative. Contains his contains all 0,-1 matrices

C = [] for a in A: C.append(a) for b in B: C.append(b) #Taking #Here we are taking the union of A and B

B test_sum = int(input("Into how many matrices you want to break H :"))

:")) test_comb = Combinations(C,test_sum)
Combinations(C,test_sum) # Here we are forming all "test_sum"-set combinations from C".
That is if # That is, if test_sum =3, then test_comb, gives all 3 set combinations from C"

for t in test_comb: if (sum(t)==H):
#Checking
# Checking which combinations in test_sum give the sum = H and printing those

those
        print("A combination is: ")    
     show(t)                     
     print("\n")

I asked this thing because a friend of mine made code for this but it is very time taking. For 2nd order matrices output comes out instantly. For 3rd order , output starts coming but it is taking a lot of time. For 4th order , no output is shown even after hours. Since for 2nd order, number of elements in V.List is 2^4, for 3rd order it is 2^9 and for 4th order it is 2^16 so it is time taking. Can any change be made to make this code run faster?

#Write faster?(Please note that after writing the code it appeared differently in preview, I tried changing but could not. So indentation is required where B and C are mentioned.)

Write your matrix matrix

H = matrix(2,2, [1, 1,1, -1]) show("H=",H)

show("H=",H)

n = int(input("Enter the order of the matrix H : ")) "))

V = GF(2)^(n^2)

R1=V.list()[1:]
#We are removing the all-zero list because not needed for our purpose purpose

R2=R1[:-1]
#We are removing the all-ones list because not needed for our purpose purpose

A = [] for v in R2: A.append(matrix(ZZ,n,n,v))
# Forming n by n matrices from list in R2. Contains all 0,1 matrices matrices.

B=[] for a in A: b=-a B.append(b) #Here we are forming A:
b=-a # Forming
n by n matrices from A by taking their negative. his contains Contains all 0,-1 matrices
B.append(b)

C = [] for a in A: C.append(a) for b in B: C.append(b) #Here we are taking the union #Union of A and B B

test_sum = int(input("Into how many matrices you want to break H :")) :"))

test_comb = Combinations(C,test_sum) Combinations(C,test_sum)
# Here we are forming all "test_sum"-set combinations from C".
#
That is is, if # test_sum =3, then test_comb, gives all 3 set combinations from C"

for t in test_comb: if (sum(t)==H): # Checking
#Checking
which combinations in test_sum give the sum = H and printing those those

    print("A combination is: ")    
     show(t)                     
     print("\n")

I asked this thing because a friend of mine made code for this but it is very time taking. For 2nd order matrices output comes out instantly. For 3rd order , output starts coming but it is taking a lot of time. For 4th order , no output is shown even after hours. Since for 2nd order, number of elements in V.List is 2^4, for 3rd order it is 2^9 and for 4th order it is 2^16 so it is time taking. Can any change be made to make this code run faster?(Please note that after writing the code it appeared differently in preview, I tried changing but could not. So indentation is required where B and C are mentioned.)

Write
#Write your matrix

matrix H = matrix(2,2, [1, 1,1, -1])

show("H=",H)

show("H=",H) n = int(input("Enter the order of the matrix H : "))

")) V = GF(2)^(n^2)

R1=V.list()[1:]
#We are removing the all-zero list because not needed for our purpose

purpose R2=R1[:-1]
#We are removing the all-ones list because not needed for our purpose

purpose A = [] for v in R2: A.append(matrix(ZZ,n,n,v))
# Forming n by n matrices from list in R2. Contains all 0,1 matrices.

B=[] for a in A:
b=-a # Forming n by n matrices from A by taking their negative. Contains all 0,-1 matrices
B.append(b)

B.append(b) C = [] for a in A: C.append(a) for b in B: C.append(b) #Union of A and B

B test_sum = int(input("Into how many matrices you want to break H :"))

:")) test_comb = Combinations(C,test_sum)
# Here we are forming all "test_sum"-set combinations from C".
# That is, if test_sum =3, then test_comb, gives all 3 set combinations from C"

for t in test_comb: if (sum(t)==H):
#Checking which combinations in test_sum give the sum = H and printing those

those

        print("A combination is: ")    
     show(t)                     
     print("\n")

I asked this thing because a friend of mine made code for this but it is very time taking. For 2nd order matrices output comes out instantly. For 3rd order , output starts coming but it is taking a lot of time. For 4th order , no output is shown even after hours. Since for 2nd order, number of elements in V.List is 2^4, for 3rd order it is 2^9 and for 4th order it is 2^16 so it is time taking. Can any change be made to make this code run faster?(Please note that after writing the code it appeared differently in preview, I tried changing but could not. So indentation is required where B and C are mentioned.)

#Write your matrix
H = matrix(2,2, [1, 1,1, -1]) 

show("H=",H)

n = int(input("Enter the order of the matrix H : "))

V = GF(2)^(n^2)               

R1=V.list()[1:]                    
 #We are removing the all-zero list because not needed for our purpose

R2=R1[:-1]                          
 #We are removing the all-ones list because not needed for our purpose

A = []
for v in  R2:
    A.append(matrix(ZZ,n,n,v))          
 # Forming n by n matrices from  list in  R2. Contains all 0,1 matrices.                                               

B=[]
for a in A:                       
     b=-a                                           # Forming n by n matrices from A by taking their negative. Contains all 0,-1 matrices    
     B.append(b)

C = []
for a in A:
    C.append(a)
for b in B:
    C.append(b)         #Union of A and B

test_sum = int(input("Into how many matrices you want to break H :"))

test_comb = Combinations(C,test_sum)  
 # Here we are forming all "test_sum"-set combinations  from C".  
 # That is, if test_sum =3, then test_comb, gives all 3 set combinations  from C" 

for t in test_comb:
    if (sum(t)==H):                 
        #Checking which combinations in  test_sum give the  sum = H and printing those

        print("A combination is: ")    
        show(t)                     
        print("\n")

To reduce search space I had specified the number of non-negative and non-positive summands as shown below:

 #Write your matrix
H = matrix(2,2, [1, 1,1, -1]) 

show("H=",H)

n = int(input("Enter the order of the matrix H : "))

V = GF(2)^(n^2)               
R1=V.list()[1:]                    
 #We are removing the all-zero list because not needed for our purpose

R2=R1[:-1]                          
 #We are removing the all-ones list because not needed for our purpose

A = []
for v in  R2:
    A.append(matrix(ZZ,n,n,v))          
 # Forming n by n matrices from  list in  R2. Contains all 0,1 matrices.                                               

B=[]
for a in A:                       
     b=-a                                         
     B.append(b)
 # Forming n by n matrices from A by taking their negative. Contains all 0,-1 matrices         

test_sum = int(input("Into how many matrices you want to break H :"))

num_nonneg = int(input("How many non-negative matrices u want :"))
num_nonpos = (test_sum)-(num_nonneg)

nonneg_comb = Combinations(A,num_nonneg)
nonpos_comb = Combinations(B,num_nonpos)         



for x in nonneg_comb:
    for y in nonpos_comb:
          if (sum(x+y)==H):

               print("A combination is: ")
               show(x+y)
               print("\n")
print("No Combination exists")