Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Matrix2:
- def __init__(self, data): #takes in a double array no matter the size of it.
- self.data = data
- self.rows = len(data) #determines row by number of items in list
- self.cols = len(data[0]) #determines columns by number of items withing list of a list
- self.size = [self.rows, self.cols] #determines how big the matrix is by number of rows and columns
- def multiply(self, other):
- if self.cols == other.rows: #number of columns in first matrix should be same as number of rows on 2nd matric
- result = [[0 for i in range(other.cols)] for i in range(self.rows)] #number of rows as 1st matrix + number of columns of 2nd matrix
- #print(result)
- #iterate by rows of first matrix
- for i in range(self.rows):
- # iterating by columns of second matrix
- for j in range(other.cols):
- # iterating by rows of second matrix
- for k in range(other.rows):
- result[i][j] += self.data[i][k] * other.data[k][j]
- return Matrix2(result)
- else:
- return "cant be multiplied"
- def add(self, other): #works good
- if self.size == other.size:
- result = result = [[0 for i in range(self.cols)] for i in range(self.rows)]
- for i in range(self.rows):
- # iterate through columns
- for j in range(other.cols):
- result[i][j] = self.data[i][j] + other.data[i][j]
- return Matrix2(result)
- else:
- return " can't be added, dimensions are not equal"
- def subtract(self, other):
- if self.size == other.size:
- result = result = [[0 for i in range(self.cols)] for i in range(self.rows)]
- for i in range(self.rows):
- # iterate through columns
- for j in range(other.cols):
- result[i][j] = self.data[i][j] - other.data[i][j]
- return Matrix2(result)
- else:
- return " can't be subtracted, dimensions are not equal"
- def scalar_multi(self, number):
- self.data = [[i * number for i in sublist] for sublist in self.data] #returns scalar multiplication
- return self.data
- # for x in range(self.cols):
- # i[x] = i[x]*number
- # return self.data
- def print_matrix(self):
- for i in self.data:
- print(*i)
- def inverse(self):
- arr = self.data # arr is a list of a list
- # Add identity matrix on the right
- for i in range(self.rows):
- for j in range(self.cols):
- if j == i:
- arr[i].append(1)
- else:
- arr[i].append(0)
- # Reset all that is below the main diagonal
- for i in range(self.rows):
- arr[i] = [round(x * (1 / arr[i][i]), 4) for x in arr[i]]
- for j in range(self.cols):
- try:
- j += i
- arr1 = [round(x * (-arr[j + 1][i]), 4) for x in arr[i]]
- arr[j + 1] = map(lambda x, y: round(x + y, 4), arr[j + 1], arr1)
- except IndexError:
- break
- # Reset all that is above the main diagonal
- for i in range(self.rows - 1, -1, -1):
- arr[i] = [round(x * (1 / arr[i][i]), 4) for x in arr[i]]
- for j in range(self.cols - 1, 0, -1):
- if j <= i:
- arr1 = [round(x * (-arr[j - 1][i]), 4) for x in arr[i]]
- arr[j - 1] = map(lambda x, y: round(x + y, 4), arr[j - 1], arr1)
- arr2 = [arr[l][self.rows:] for l in range(self.rows)]
- return Matrix2(arr2)
- def get_data(self):
- return self.data
- def get_rows(self):
- return self.rows
- def get_colms(self):
- return self.cols
- def transpose(self):
- trans= [list(i) for i in zip(*self.data)]
- return Matrix2(trans)
- # def mean(self):
- # lst = self.data
- # result = [sum(x) for x in zip(*lst)]
- # for i, v in enumerate(result):
- # result[i] = v /len(lst)
- # return result
- one = Matrix2([[1,2],[7,9]])
- inverse = one.inverse()
- print(inverse)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement