Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. from scipy.io import mmread
  2. import math,sys
  3.  
  4. def trace(arr):
  5. out = 0
  6. rows = len(arr)
  7. cols = len(arr[0])
  8. for i in range(rows):
  9. out += arr[i][i]
  10. return out
  11.  
  12. def frobenious(arr):
  13. out = 0
  14. rows = len(arr)
  15. cols = len(arr[0])
  16. for i in range(rows):
  17. for j in range(cols):
  18. out += arr[i][j]*arr[i][j]
  19. return math.sqrt(out)
  20.  
  21. def diagdom(arr):
  22. out = True
  23. rows = len(arr)
  24. cols = len(arr[0])
  25. for i in range(rows):
  26. for j in range(cols):
  27. if(arr[i][j] > arr[i][i]):
  28. out = False
  29. return out
  30.  
  31. def symmetric(arr):
  32. out = True
  33. rows = len(arr)
  34. cols = len(arr[0])
  35. for i in range(rows):
  36. for j in range(cols):
  37. if(arr[i][j] != arr[j][i]):
  38. out = False
  39. return out
  40.  
  41. def nnz(arr):
  42. out = 0
  43. rows = len(arr)
  44. cols = len(arr[0])
  45. for i in range(rows):
  46. for j in range(cols):
  47. if(arr[i][j] != 0):
  48. out += 1
  49. return out
  50.  
  51.  
  52. if __name__ == '__main__':
  53. try:
  54. print(sys.argv[1])
  55. a = mmread(sys.argv[1])
  56. b = a.toarray()
  57. print("Enter 1 for trace, 2 for frobenious, 3 for diagonal dominant, 4 for symmetric and 5 for number of non-zero")
  58. inp = int(input())
  59. # print(inp)
  60. if(inp==1):
  61. print(trace(b))
  62. elif(inp==2):
  63. print(frobenious(b))
  64. elif(inp==3):
  65. print(diagdom(b))
  66. elif(inp==4):
  67. print(symmetric(b))
  68. elif(inp==5):
  69. print(nnz(b))
  70. else:
  71. print("Enter correct code")
  72. except:
  73. print("Enter in right format: python3 features.py matrix-market-filename")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement