Advertisement
EWTD

Untitled

Jan 31st, 2020
477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.00 KB | None | 0 0
  1. from numpy import *
  2. import scipy.optimize
  3. import itertools
  4. file_data = open('b1.txt','r')
  5. n = int(file_data.readline())
  6. inp = []
  7. for line in file_data.readlines():
  8.     line = line.replace('\n', '')
  9.     t = line.split('\t')
  10.     inp.append(list(map(int, t)))
  11. matrix = []
  12. ans = []
  13. for i in range(n*n):
  14.     t = [0 for k in range(n*n)]
  15.     matrix.append(t)
  16.     ans.append(0)
  17. for i in range(n):
  18.     for j in range(n):
  19.         for dx in range(-1,2):
  20.             for dy in range(-1,2):
  21.                 if 0 <= i+dx < n and 0 <= j+dy < n:
  22.                     ans[n*i+j] = inp[i][j]
  23.                     matrix[n*i+j][n*(i+dx)+(j+dy)] = 1
  24. matrix = array(matrix)
  25. ans = array(ans)
  26. #data = scipy.optimize.lsq_linear(matrix, ans, bounds=(0, 100), tol=1e-10)
  27. data = linalg.solve(matrix, ans)
  28. file_out = open("ans.txt", 'a')
  29. k = 0
  30. for i in range(n):
  31.     line = ""
  32.     for j in range(n):
  33.         line += str(int(data[k])) + ' '
  34.         k += 1
  35.     line += '\n'
  36.     file_out.write(line)
  37. file_out.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement