Skip to content

Commit 9fe4a37

Browse files
authored
Merge pull request #11 from volf52/matrix-ops-python
2D Matrix Operations without using Numpy
2 parents 054a51f + 7fb6c00 commit 9fe4a37

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Muhammad Arslan <rslnkrmt2552@gmail.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Created on 2017-09-09
2+
3+
Author : Muhammad Arslan <rslnkrmt@gmail.com>
4+
5+
Licence : MIT
6+
7+
2-D Matrix operations without the use of numpy module
8+
------------------------------------------------------------------
9+
10+
In situations where numpy module isn't available, you can use these functions to calculate the inverse, determinant, transpose of matrix, calculate the minors of it's elements, and multiply two matrices together.
11+
Any advice to make these functions better will be appreciated.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
def transpose(matrix):
2+
return [[row[i] for row in matrix] for i in range(len(matrix[0]))]
3+
4+
5+
def multip(X, Y):
6+
return [[sum(a*b for a, b in zip(X_row, Y_col)) for Y_col in zip(*Y)] for X_row in X]
7+
8+
9+
def getMatrixMinor(m, i, j):
10+
return [row[:j] + row[j+1:] for row in (m[:i]+m[i+1:])]
11+
12+
13+
def getMatrixDeternminant(m):
14+
# base case for 2x2 matrix
15+
if len(m) == 2:
16+
return (m[0][0]*m[1][1]-m[0][1]*m[1][0])*1.0
17+
18+
determinant = 0
19+
for c in range(len(m)):
20+
determinant += ((-1.0)**c)*m[0][c]*getMatrixDeternminant(getMatrixMinor(m, 0, c))
21+
return determinant
22+
23+
24+
def getMatrixInverse(m):
25+
determinant = getMatrixDeternminant(m)
26+
# special case for 2x2 matrix:
27+
if len(m) == 2:
28+
return [[m[1][1]/determinant, -1*m[0][1]/determinant],
29+
[-1*m[1][0]/determinant, m[0][0]/determinant]]
30+
31+
# find matrix of cofactors
32+
cofactors = []
33+
for r in range(len(m)):
34+
cofactorRow = []
35+
for c in range(len(m)):
36+
minor = getMatrixMinor(m,r,c)
37+
cofactorRow.append(((-1)**(r+c)) * getMatrixDeternminant(minor))
38+
cofactors.append(cofactorRow)
39+
cofactors = transpose(cofactors)
40+
for r in range(len(cofactors)):
41+
for c in range(len(cofactors)):
42+
cofactors[r][c] = cofactors[r][c]/determinant
43+
return cofactors

0 commit comments

Comments
 (0)