Advertisement
Namuh

det(A+B)

Mar 4th, 2019
488
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.69 KB | None | 0 0
  1. def nb_changes(a,b):
  2.   s = 1
  3.   for x in a:
  4.     for y in b:
  5.       if x > y:
  6.         s *= -1
  7.   return s
  8.  
  9. def helper(A, B, as1, as2, bs1, bs2):
  10.   M = A[as1,as2]
  11.   N = B[bs1,bs2]
  12.   return nb_changes(as1,bs1) * nb_changes(as2,bs2) * det(M) * det(N)
  13.  
  14. def det_sum(A,B,n):
  15.   rn = Set(range(n))
  16.   result = 0
  17.   for ss1,ss2 in cartesian_product([Subsets(rn),Subsets(rn)]):
  18.     if ss1.cardinality() != ss2.cardinality():
  19.       continue
  20.     Css1 = rn.difference(ss1)
  21.     Css2 = rn.difference(ss2)
  22.     r1 = result
  23.     result += helper(A,B,list(ss1),list(ss2),list(Css1),list(Css2))
  24.   return result
  25.  
  26. n = 7
  27. A = random_matrix(ZZ,n,n)
  28. B = random_matrix(ZZ,n,n)
  29. print det_sum(A,B,n)
  30. print det(A+B)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement