in_chainz

Untitled

Oct 2nd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.63 KB | None | 0 0
  1. class MatrixError(Exception):
  2.     def __init__(self, text):
  3.         MatrixError.txt = text
  4.  
  5.  
  6. class Mat:
  7.     def __init__(self, n, m, array):
  8.         self.n = n
  9.         self.m = m
  10.         self.a = array
  11.  
  12.     def __add__(self, other):
  13.         if self.n != other.n or self.m != other.m:
  14.             raise MatrixError("Matrices are of different sizes!")
  15.         for i in range(self.n):
  16.             for j in range(self.m):
  17.                 other.a[i][j] += self.a[i][j]
  18.  
  19.         return other
  20.  
  21.     def __sub__(self, other):
  22.         if self.n != other.n or self.m != other.m:
  23.             raise MatrixError("Matrices are of different sizes!")
  24.         for i in range(self.n):
  25.             for j in range(self.m):
  26.                 other.a[i][j] = self.a[i][j] - other.a[i][j]
  27.  
  28.         return other
  29.  
  30.     def __mul__(self, mat):
  31.         if isinstance(mat, int):
  32.             res = [[0 for i in range(self.m)] for j in range(self.n)]
  33.             for i in range(self.n):
  34.                 for j in range(self.m):
  35.                     res[i][j] = self.a[i][j] * mat
  36.             return Mat(self.n, self.m, res)
  37.  
  38.         if self.m != mat.n:
  39.             raise MatrixError("Unable to multiply matrices")
  40.  
  41.         res = [[0 for i in range(mat.m)] for j in range(self.n)]
  42.         for i in range(self.n):
  43.             for j in range(self.m):
  44.                 for k in range(mat.m):
  45.                     res[i][k] += self.a[i][j] * mat.a[j][k]
  46.         return Mat(self.n, mat.m, res)
  47.  
  48.     def transponsed(self):
  49.         out = []
  50.         for i in range(self.m):
  51.             rw = []
  52.             for j in range(self.n):
  53.                 rw.append(self.a[j][i])
  54.             out.append(rw)
  55.  
  56.         res = Mat(self.m, self.n, out)
  57.         return res
  58.  
  59.     def trace(self):
  60.         s = 0
  61.         for i in range(self.n):
  62.             s += self.a[i][i]
  63.         return s
  64.  
  65.     def print(self):
  66.         for i in range(self.n):
  67.             print(*self.a[i])
  68.  
  69. '''
  70. a = [
  71.    [4, 5, 3],
  72.    [2, -5, 1]
  73. ]
  74. b = [
  75.    [-6, -3, 4],
  76.    [5, -1, 6]
  77. ]
  78. c = [
  79.    [4, 3],
  80.    [-1, -2]
  81. ]
  82. d = [
  83.    [-6, -2],
  84.    [4, 6]
  85. ]
  86. '''
  87.  
  88. a = [
  89.     [-3, -6, -5],
  90.     [1, 2, 2]
  91. ]
  92. b = [
  93.     [5, 2, 6],
  94.     [4, 3, 1]
  95. ]
  96. c = [
  97.     [5, -5],
  98.     [-4, 3]
  99. ]
  100. d = [
  101.     [-3, 3],
  102.     [-6, 5]
  103. ]
  104.  
  105. A = Mat(2, 3, a)
  106. B = Mat(2, 3, b)
  107. C = Mat(2, 2, c)
  108. D = Mat(2, 2, d)
  109.  
  110. R = C * 2 * B * B.transponsed() + (A - B) * (A.transponsed() * A).trace() * (A.transponsed() + B.transponsed()) + C * C * 4 - C * 8 * D + D * D * 4
  111.  
  112. #R = D * 4 * A * A.transponsed() + (A + B) * (B.transponsed() * B).trace() * (A.transponsed() - B.transponsed()) - C * C * 2 + C * 4 * D - D * D * 2
  113. R.print()
Add Comment
Please, Sign In to add comment