Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
python: Matrix operations using objecs "poorly structured"
#-------------------------------------------------------------------------------
# Name: Matrix
# Purpose:
#
# Author: Nicolas Figueroa
#
# Created: 06/10/2012
# Copyright: (c) Nicolas Figueroa 2012
#-------------------------------------------------------------------------------
import random
class Matrix(object):
"""Contains the instance of a matrix and default
set and get methods
"""
def __init__(self, rows, cols):
self.rows = rows
self.cols = cols
self.matrix = []
def setRows(self,r):
self.rows = r
def getRows(self):
return self.rows
def setCols(self, c):
self.cols = c
def getCols(self):
return self.cols
def setMatrix(self, matrix):
self.matrix = matrix
def getMatrix(self):
return self.matrix
class MatrixOperations(Matrix):
""" Contains matrix operations """
def fillMatrix(self):
'''(int,int) -> list[number]
Returns a matrix (list of lists) filled by the user with given rows and cols as
the matrix size
>>>fillMatrix(2,2)
Enter the [0][0] position of the matrix
1
Enter the [0][1] position of the matrix
2
Enter the [1][0] position of the matrix
3
Enter the [1][1] position of the matrix
4
[[1 , 2], [3 , 4]]
'''
for i in range(self.rows):
self.matrix.append([])
for j in range(self.cols):
self.matrix[i].append(int(input("Enter the [%d][%d] position of the matrix\n" % (i+1, j+1))))
return self.matrix
def randomMatrix(self,limit):
'''(int, int,int) -> list
Returns a random matrix with given rows and cols as the
matrix size and limit as the max number contained in the
matrix
>>> randomMatrix(2,2,10)
[[7, 0], [1, 9]]
>>> randomMatrix(3,3,13)
[[9, 10, 5], [1, 0, 3], [13, 9, 1]]
'''
for i in range(self.rows):
self.matrix.append([])
for j in range(self.cols):
self.matrix[i].append(random.randint(0,limit))
return self.matrix
def printMatrix(self):
'''(matrix) -> output
Prints a matrix, each rown in its own line to give readability
to the user
>>> printMatrix(matrix)
[[1, 2]]
[[3, 4]]
'''
for i in self.matrix:
print(i)
print("-----------------------------------------")
def multiplyMatrix(self, matrix2):
'''(matrix,matrix) -> matrix
Returns a matrix multiplication between matrix1 and matrix2
if the matrix size (n*m m*p) is wrong prints a message.
>>>matrix1 = fillMatrix(2,2)
Enter the [0][0] position of the matrix
1
Enter the [0][1] position of the matrix
2
Enter the [1][0] position of the matrix
3
Enter the [1][1] position of the matrix
4
>>>matrix2 = fillMatrix(2,2)
Enter the [0][0] position of the matrix
1
Enter the [0][1] position of the matrix
2
Enter the [1][0] position of the matrix
3
Enter the [1][1] position of the matrix
4
>>> matrix3 = multiplyMatrix(matrix1, matrix2)
>>> print(matrix3)
[[12, 6]]
[[26, 16]]
'''
if len(self.matrix) != matrix2.cols:
print("Wrong matrix size")
else:
res_matrix = []
for i in range(len(self.matrix)):
list_a = []
for j in range(matrix2.cols):
s = 0
for k in range(len(self.matrix[0])):
s += self.matrix[i][k] * matrix2.matrix[k][j]
list_a.append(s)
res_matrix.append(list_a)
return res_matrix
"""MENU"""
rows = int(input("Enter the number of rows"))
cols = int(input("Enter the number of cols"))
"""Program execution"""
#Creates ,gets and prints matrices
m1 = MatrixOperations(rows,cols)
m1.getMatrix()
m2 = MatrixOperations(rows,cols)
m2.getMatrix()
m3 = MatrixOperations(m1.cols,m2.rows)
m3.getMatrix()
#Fill objecs
m1.fillMatrix()
m2.fillMatrix()
#Creates objects
m3 = MatrixOperations(m1.cols,m2.rows)
m3.getMatrix()
#Multiply
result = m1.multiplyMatrix(m2)
m3.setMatrix(result)
m3.printMatrix()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.