Skip to content

Commit

Permalink
1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Xanonymous-GitHub committed Jun 9, 2020
1 parent e857d8d commit 4e18ac5
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,4 @@ dmypy.json

#IDE files
.idea/
.vscode
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.pythonPath": "/usr/local/bin/python3"
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ print(my_matrix.raw)

#### get identity Matrix
```python
i = Mx.UnitMatrix(3)
i = Mx.IdentityMatrix(3)

#result:
print(i)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="xmatrix",
version="1.1.3",
version="1.2.0",
author="Xanonymous",
author_email="[email protected]",
description="Help you calculate matrix.",
Expand Down
16 changes: 16 additions & 0 deletions xmatrix/identity_matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from .matrix import *


class IdentityMatrix(Matrix):
def __init__(self, n):
tmp_n = n
tmp = list()
while tmp_n:
tmp_tmp = list()
m = n
while m:
tmp_tmp.insert(0, 1 if m == tmp_n else 0)
m -= 1
tmp.insert(0, tmp_tmp)
tmp_n -= 1
super().__init__(tmp)
29 changes: 7 additions & 22 deletions xmatrix/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ def __init__(self, u: str or list):
def __str__(self):
return self.__pretty(self.__storage)

__repr__ = __str__

def __del__(self):
del self

Expand Down Expand Up @@ -73,7 +75,7 @@ def __mul__(self, other):
self.__error_handler("Cannot be multiplied.")
return
# if the second matrix is an identity_matrix, the answer will be the same as first matrix.
if self.is_unit_matrix(resource):
if self.is_identity_matrix(resource):
new = self.__make_copy(self.__storage)
return Matrix(new)
new = list()
Expand All @@ -92,7 +94,7 @@ def __rmul__(self, other):

def __pow__(self, power: int, modulo=None):
# if self is an identity matrix or power assign 1, than return self.
if self.is_unit_matrix(self.__storage) or power == 1:
if self.is_identity_matrix(self.__storage) or power == 1:
return Matrix(self.__make_copy(self.__storage))
# if power == 0, than return identity matrix.
if not power:
Expand Down Expand Up @@ -149,7 +151,8 @@ def inverse(self):
ans_tmp = list()
for j, y in enumerate(x):
h = (-1 if i % 2 else 1) * (-1 if j % 2 else 1)
ans_tmp.append(h * self.__determinant(self.__get_ans_range(i, j, new)))
ans_tmp.append(
h * self.__determinant(self.__get_ans_range(i, j, new)))
ans.append(ans_tmp)
ans = self.__transpose(ans)
return Matrix(self.__rate(ans, 1 / determinant))
Expand Down Expand Up @@ -212,7 +215,7 @@ def __transpose(resource: list) -> list:
return list(map(list, zip(*resource)))

@staticmethod
def is_unit_matrix(resource: list) -> bool:
def is_identity_matrix(resource: list) -> bool:
for i, x in enumerate(resource):
for j, y in enumerate(x):
if i == j and resource[i][j] != 1:
Expand Down Expand Up @@ -262,21 +265,3 @@ def __get_near_number(self, data: float, pos: int) -> int or float:
if not isclose(data, round(data, pos), rel_tol=1e-4):
return round(data, pos + 1)
return self.__get_near_number(data, pos - 1)


class UnitMatrix(Matrix):
def __init__(self, n):
tmp_n = n
tmp = list()
while tmp_n:
tmp_tmp = list()
m = n
while m:
tmp_tmp.insert(0, 1 if m == tmp_n else 0)
m -= 1
tmp.insert(0, tmp_tmp)
tmp_n -= 1
super().__init__(tmp)

def __str__(self):
return super().__str__()

0 comments on commit 4e18ac5

Please sign in to comment.