Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.38 KB | None | 0 0
  1. import numpy as np
  2. from fractions import Fraction as frac
  3. import sympy as sy
  4.  
  5. #matrix= [[3,-3,1,2,0,3],
  6. # [0,-2,3,-2,2,1],
  7. # [-2,2,3,0,-2,3],
  8. # [2,3,-3,1,-2,3],
  9. # [2,0,-2,-3,2,0]]
  10.  
  11. matrix= [[1,-2,-2,-2,-1,2],
  12. [0,3,-2,-3,1,3],
  13. [3,0,0,1,-1,2],
  14. [3,-3,-2,0,1,1],
  15. [0,-3,3,-3,-3,2]]
  16.  
  17. #n = 2497969412496091
  18. n=2497969412496091
  19.  
  20.  
  21. #computes EEA
  22. def EEA(a, b):
  23. if a == 0:
  24. return (b, 0, 1)
  25. else:
  26. gcd, u, v = EEA(b % a, a)
  27. return (gcd, v - (b//a) * u, u)
  28.  
  29. #Method that takes an element pos and number n and multiply them togheter mod m
  30. def generateElement(pos,n,m):
  31. return (pos * n) % m
  32.  
  33. #Method that takes a matrix A, and computes its row echelon form
  34. def row_echelon(A):
  35. counter=0
  36. #loop through every row and make that one row, row reduced
  37. for row in A:
  38. tempCounter = 0
  39. gcd, u, v = EEA(n, row[counter])
  40. if gcd != 1:
  41. print(gcd, "Is a factor :)")
  42. break
  43.  
  44. #If leading coefficient already is not 1, then make it into one by using EEA to calculate the number in which the (coefficient*v)%n=1
  45. ##the rest of the elements in the same row will also need to be adjusted, depending on the v
  46. if (row[counter] != 1):
  47. newRow=[]
  48. for g in range(0, len(row)):
  49. newRow.append(0)
  50.  
  51. for element in row:
  52. newLeader = generateElement(element, v, n)
  53. newRow[tempCounter] = newLeader
  54. tempCounter += 1
  55. A[counter] = newRow
  56.  
  57. ##after making the leading coefficient 1, make every element under it 0
  58. ##this is done by multiplying the row belonging to the current leading coefficient by the leading coefficient in the row you want to reduce to 0
  59. ##then
  60. for bellowCoefficient in range(counter + 1, len(A)):
  61. tempCounter2 = 1 + counter
  62. row = A[bellowCoefficient]
  63. if (row[counter] == 0):
  64. continue
  65. else:
  66. minusRow = []
  67. minusRowLeadingCoefficient = row[counter]
  68. for leadingCoeffcientRowElement in A[counter]:
  69. minusRow.append((leadingCoeffcientRowElement * minusRowLeadingCoefficient) % n)
  70. tempRow = []
  71. for iterateCounter in range(0, len(minusRow)):
  72. tempRow.append((minusRow[iterateCounter] - row[iterateCounter]) % n)
  73. A[bellowCoefficient] = tempRow
  74. tempCounter2 += 1
  75.  
  76. counter+=1
  77.  
  78. print("Row reduced ")
  79. printMatrix(A)
  80. return A
  81.  
  82.  
  83. ##support method that prints a matrix, in a nice readable way
  84. def printMatrix(M):
  85. for row in M:
  86. print(row)
  87. print("\n")
  88.  
  89. ##dont mind this method too mutch, it was an atempt to check the general solution and then use it too double check if my answer was correct.
  90. ##Not sure if either my original reduction is wrong or if my coded check is wrong :)
  91. def generalSolution(modulaResult):
  92. x5 = 0
  93. x4 = 0
  94. x3 = 0
  95. x2 = 0
  96. x1 = 0
  97.  
  98. matrix = [[1, -2, -2, -2, -1, 2],
  99. [0, 3, -2, -3, 1, 3],
  100. [3, 0, 0, 1, -1, 2],
  101. [3, -3, -2, 0, 1, 1],
  102. [0, -3, 3, -3, -3, 2]]
  103. #matrix = [[3, -3, 1, 2, 0, 3],
  104. # [0, -2, 3, -2, 2, 1],
  105. # [-2, 2, 3, 0, -2, 3],
  106. # [2, 3, -3, 1, -2, 3],
  107. # [2, 0, -2, -3, 2, 0]]
  108.  
  109. counterX = 0
  110. print("Starting general solution: ")
  111. for i in range(0, 5):
  112. # print(modulaResult[4-counterX])
  113. # lmao=modulaResult[4-counterX][5-counterX]
  114. if (i == 0):
  115. x5 = modulaResult[4 - counterX][5 - counterX]
  116. #print(x5)
  117. if (i == 1):
  118. x4 = (modulaResult[4 - counterX][5] - (modulaResult[4 - counterX][5 - counterX] * x5) % n) % n
  119. #print(x4)
  120. if (i == 2):
  121. # x3=(modulaResult[4-counterX][5-counterX]-(x4*modulaResult[4-counterX][4])%n-(x5*modulaResult[4-counterX][5])%n)%n
  122. x3 = (modulaResult[4 - counterX][5] - (x4 * modulaResult[4 - counterX][5 - counterX]) % n - (
  123. x5 * modulaResult[4 - counterX][4]) % n) % n
  124. #print(x3)
  125. if (i == 3):
  126. # x2=(modulaResult[4-counterX][5-counterX]-(x3*modulaResult[4-counterX][3])%n-(x4*modulaResult[4-counterX][4])%n-(x5*modulaResult[4-counterX][5])%n)%n
  127. x2 = (modulaResult[4 - counterX][5] - (x3 * modulaResult[4 - counterX][5 - counterX]) % n - (
  128. x4 * modulaResult[4 - counterX][3]) % n - (x5 * modulaResult[4 - counterX][4]) % n) % n
  129. #print(x2)
  130. if (i == 4):
  131. # x1=modulaResult[4-counterX][5-counterX]-
  132. x1 = (modulaResult[4 - counterX][5] - (x2 * modulaResult[4 - counterX][5 - counterX]) % n - (
  133. x3 * modulaResult[4 - counterX][2]) % n - (x4 * modulaResult[4 - counterX][3]) % n - (
  134. x5 * modulaResult[4 - counterX][4]) % n) % n
  135. # print(x1)
  136. #print(n/x1)
  137. counterX += 1
  138.  
  139. #tempX1=0
  140. #tempTestMatrix=[]
  141. #for i in range(0, len(matrix)):
  142. # tempTestMatrix.append(0)
  143.  
  144. #counterX = 0
  145. #for i in range(0, len(matrix)):
  146. # tempX1= (modulaResult[4 - counterX][5] - (x2 * modulaResult[4 - counterX][5 - counterX]) % n - (
  147. # x3 * modulaResult[4 - counterX][2]) % n - (x4 * modulaResult[4 - counterX][3]) % n - (
  148. # x5 * modulaResult[4 - counterX][4]) % n) % n
  149.  
  150.  
  151. xMatrix=[]
  152. print("\n")
  153. print("x1", x1)
  154. print("x2", x2)
  155. print("x3", x3)
  156. print("x4", x4)
  157. print("x5", x5)
  158. xMatrix.append(x1)
  159. xMatrix.append(x2)
  160. xMatrix.append(x3)
  161. xMatrix.append(x4)
  162. xMatrix.append(x5)
  163.  
  164. check=[]
  165.  
  166. print("\nTesting the X values found to double check: \n")
  167. counter = 1
  168. for row in matrix:
  169. z = 0
  170. result = 0
  171. for element in row:
  172. if z == len(row)-1:
  173. break
  174. result+=(element*xMatrix[z])%n
  175. result=result%n
  176. z+=1
  177. print("Result for row", counter,":" , result)
  178. counter+=1
  179.  
  180. ##driver method everything starts from here
  181. def main():
  182. print("Original matrix")
  183. printMatrix(matrix)
  184. matrix3 = row_echelon(matrix)
  185. generalSolution(matrix3)
  186.  
  187. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement