Advertisement
MaxDvc

class_matrix

Jan 11th, 2020
663
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.29 KB | None | 0 0
  1. class matrix:
  2.     def __init__(self,numRows,numColumns):
  3.     #@param:
  4.     #   numRow: int;
  5.     #   numColumns: int;
  6.         self.rows = numRows
  7.         self.cols = numColumns
  8.         self.mat = {}
  9.     def printMat(self):
  10.         keys = list(self.mat.keys())
  11.         p = makeMatrix(self.rows,self.cols)
  12.         for i in range(0,len(keys)):
  13.             m = int(keys[i][0])
  14.             n = int(keys[i][1])
  15.             p[m][n] = self.mat[keys[i]]
  16.         for i in range(0,len(p)):
  17.             print(p[i])
  18.     def value(self,i):
  19.     #@param:
  20.     #   i: tuple, (row_index,col_index);
  21.     #   return: float
  22.         return(self.mat[i])
  23.     def modif(self,i,v):
  24.     #@param:
  25.     #   i: tuple, (row_index,col_index);
  26.     #   v: float;
  27.         self.mat[i] = v
  28.     def add(self,i,v):
  29.     #@param:
  30.     #   i: tuple, (row_index,col_index);
  31.     #   v: float;
  32.         self.mat[i] = v
  33.     def matSum(self,otherMat):
  34.     #@param:
  35.     #   otherMat: dict, class_matrix;
  36.     #   return: dict, class_matrix;
  37.         if self.rows!=otherMat.rows or self.cols!=otherMat.cols:
  38.             print("Matrices are not compatrible")
  39.             return []
  40.         else:
  41.             key = list(otherMat.mat.keys())
  42.             for i in key:
  43.                 self.mat[i] = self.mat.get(i,0) + otherMat.mat[i]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement