Skip to content

Commit 764a155

Browse files
committed
Added implementation for matrix operations
1 parent 78e39b6 commit 764a155

File tree

1 file changed

+210
-0
lines changed

1 file changed

+210
-0
lines changed

pygorithm/math/matrix_operations.py

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
'''
2+
Author: OMKAR PATHAK
3+
Created at: 01st September 2017
4+
5+
Implementing various Matrix operations such as matrix addition, subtraction, multiplication.
6+
'''
7+
8+
class Matrix(object):
9+
'''
10+
Matrix class for performing various transformations
11+
12+
Matrix operations can be performed on two matrices with any number of dimensions
13+
'''
14+
15+
def __init__(self, matrix_one = None, matrix_two=None):
16+
'''
17+
:param matrix_one: matrix with nxn dimensions
18+
:param matrix_two: matrix with nxn dimensions
19+
20+
.. code-block:: python:
21+
22+
matrix_one = [[1, 2], [1, 3], [1, 4]] (a 3x2 matrix)
23+
'''
24+
self.matrix_one = matrix_one
25+
self.matrix_two = matrix_two
26+
27+
28+
def add(self):
29+
'''
30+
function for adding the two matrices
31+
32+
.. note::
33+
34+
Matrix addition requires both the matrices to be of same size.
35+
That is both the matrices should be of nxn dimensional.
36+
'''
37+
38+
# check if both the matrices are of same shape
39+
if not (len(self.matrix_one) == len(self.matrix_two)) or not (len(self.matrix_one[0]) == len(self.matrix_two[0])):
40+
raise Exception('Both Matrices should be of same dimensions')
41+
42+
added_matrix = [[0 for i in range(len(self.matrix_one))] for j in range(len(self.matrix_two))]
43+
44+
# iterate through rows
45+
for row in range(len(self.matrix_one)):
46+
# iterate through columns
47+
for column in range(len(self.matrix_one[0])):
48+
added_matrix[row][column] = self.matrix_one[row][column] + self.matrix_two[row][column]
49+
50+
return added_matrix
51+
52+
def subtract(self):
53+
'''
54+
function for subtracting the two matrices
55+
56+
.. note::
57+
58+
Matrix subtraction requires both the matrices to be of same size.
59+
That is both the matrices should be of nxn dimensional.
60+
'''
61+
62+
# check if both the matrices are of same shape
63+
if not (len(self.matrix_one) == len(self.matrix_two)) or not (len(self.matrix_one[0]) == len(self.matrix_two[0])):
64+
raise Exception('Both Matrices should be of same dimensions')
65+
66+
subtracted_matrix = [[0 for i in range(len(self.matrix_one))] for j in range(len(self.matrix_two))]
67+
68+
# iterate through rows
69+
for row in range(len(self.matrix_one)):
70+
# iterate through columns
71+
for column in range(len(self.matrix_one[0])):
72+
subtracted_matrix[row][column] = self.matrix_one[row][column] - self.matrix_two[row][column]
73+
74+
return subtracted_matrix
75+
76+
77+
def multiply(self):
78+
'''
79+
function for multiplying the two matrices
80+
81+
.. note::
82+
83+
Matrix multiplication can be carried out even on matrices with different dimensions.
84+
'''
85+
86+
multiplied_matrix = [[0 for i in range(len(self.matrix_two[0]))] for j in range(len(self.matrix_one))]
87+
88+
# iterate through rows
89+
for row_one in range(len(self.matrix_one)):
90+
# iterate through columns matrix_two
91+
for column in range(len(self.matrix_two[0])):
92+
# iterate through rows of matrix_two
93+
for row_two in range(len(self.matrix_two)):
94+
multiplied_matrix[row_one][column] += self.matrix_one[row_one][row_two] * self.matrix_two[row_two][column]
95+
96+
return multiplied_matrix
97+
98+
99+
def transpose(self):
100+
'''
101+
The transpose of a matrix is a new matrix whose rows are the columns of the original.
102+
(This makes the columns of the new matrix the rows of the original)
103+
'''
104+
transpose_matrix = [[0 for i in range(len(self.matrix_one))] for j in range(len(self.matrix_one[0]))]
105+
106+
# iterate through rows
107+
for row in range(len(self.matrix_one)):
108+
# iterate through columns
109+
for column in range(len(self.matrix_one[0])):
110+
transpose_matrix[column][row] = self.matrix_one[row][column]
111+
112+
return transpose_matrix
113+
114+
115+
def rotate(self):
116+
'''
117+
Given a matrix, clockwise rotate elements in it.
118+
119+
.. code-block:: python:
120+
121+
**Examples:**
122+
123+
Input
124+
1 2 3
125+
4 5 6
126+
7 8 9
127+
128+
Output:
129+
4 1 2
130+
7 5 3
131+
8 9 6
132+
133+
For detailed information visit: https://github.com/keon/algorithms/blob/master/matrix/matrix_rotation.txt
134+
'''
135+
136+
top = 0
137+
bottom = len(self.matrix_one) - 1
138+
left = 0
139+
right = len(self.matrix_one[0]) - 1
140+
141+
while left < right and top < bottom:
142+
# Store the first element of next row, this element will replace first element of
143+
# current row
144+
prev = self.matrix_one[top + 1][left]
145+
146+
# Move elements of top row one step right
147+
for i in range(left, right + 1):
148+
curr = self.matrix_one[top][i]
149+
self.matrix_one[top][i] = prev
150+
prev = curr
151+
152+
top += 1
153+
154+
# Move elements of rightmost column one step downwards
155+
for i in range(top, bottom+1):
156+
curr = self.matrix_one[i][right]
157+
self.matrix_one[i][right] = prev
158+
prev = curr
159+
160+
right -= 1
161+
162+
# Move elements of bottom row one step left
163+
for i in range(right, left-1, -1):
164+
curr = self.matrix_one[bottom][i]
165+
self.matrix_one[bottom][i] = prev
166+
prev = curr
167+
168+
bottom -= 1
169+
170+
# Move elements of leftmost column one step upwards
171+
for i in range(bottom, top-1, -1):
172+
curr = self.matrix_one[i][left]
173+
self.matrix_one[i][left] = prev
174+
prev = curr
175+
176+
left += 1
177+
178+
return self.matrix_one
179+
180+
181+
def count_unique_paths(self, m, n):
182+
'''
183+
Count the number of unique paths from a[0][0] to a[m-1][n-1]
184+
We are allowed to move either right or down from a cell in the matrix.
185+
Approaches-
186+
(i) Recursion - Recurse starting from a[m-1][n-1], upwards and leftwards,
187+
add the path count of both recursions and return count.
188+
(ii) Dynamic Programming- Start from a[0][0].Store the count in a count
189+
matrix. Return count[m-1][n-1]
190+
Time Complexity = O(mn), Space Complexity = O(mn)
191+
192+
:param m: number of rows
193+
:param n: number of columns
194+
'''
195+
if m < 1 or n < 1:
196+
return
197+
198+
count = [[None for j in range(n)] for i in range(m)]
199+
200+
# Taking care of the edge cases- matrix of size 1xn or mx1
201+
for i in range(n):
202+
count[0][i] = 1
203+
for j in range(m):
204+
count[j][0] = 1
205+
206+
for i in range(1, m):
207+
for j in range(1, n):
208+
count[i][j] = count[i-1][j] + count[i][j-1]
209+
210+
return count[m-1][n-1]

0 commit comments

Comments
 (0)