Skip to content

Commit

Permalink
release 1.4.3 support complex calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
Xanonymous-GitHub committed Jun 11, 2020
1 parent d2d2084 commit 803a283
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ print(my_matrix2.iv)
#The determinant is zero, can't be inverse.
#None

my_matrix3 = mv("1,1,1;1,2,3;1,4,5")
my_matrix3 = xm("1,1,1;1,2,3;1,4,5")

print(my_matrix3)
#result:
Expand All @@ -156,7 +156,7 @@ print(my_matrix3.inverse)
[1, -2, 1]
[-1, 1.5, -0.5]

my_matrix4 = mv("1,1,2,1;1,1,0,0;1,1,0,1;1,0,1,0")
my_matrix4 = xm("1,1,2,1;1,1,0,0;1,1,0,1;1,0,1,0")

print(my_matrix4)
#result:
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.4.2",
version="1.4.3",
author="Xanonymous",
author_email="[email protected]",
description="Help you calculate matrix.",
Expand Down
26 changes: 19 additions & 7 deletions xmatrix/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@


class Matrix:
def __init__(self, u: str or list):
if not isinstance(u, list):
def __init__(self, u: str or list = None):
if u is None:
self.__error_handler("Unknown matrix!")
self.__del__()
return
if u is not None and not isinstance(u, list):
u = u.split(";")
for i, _ in enumerate(u):
u[i] = u[i].split(",")
Expand Down Expand Up @@ -57,7 +61,7 @@ def __sub__(self, other):

# Matrix multiplication
def __mul__(self, other):
if isinstance(other, int) or isinstance(other, float) or isinstance(other, complex):
if isinstance(other, (int, float, complex)):
# method accept single integer to be calculated.
new = self.__make_copy(self.__storage)
return Matrix(self.__rate(new, other))
Expand Down Expand Up @@ -306,13 +310,21 @@ def __make_copy(old: list) -> list:
def __add_same_len_list(list_a: list, list_b: list, scalar=1) -> list:
result = list()
for i, x in enumerate(list_a):
result.append(float(round(x, 4)) + float(scalar * round(list_b[i], 4)))
result.append(
(round(x, 4) if not isinstance(x, complex) else (round(x.real, 4) + round(x.imag, 4) * 1j)) +
(scalar * ((round(list_b[i], 4) if not isinstance(list_b[i], complex) else (
round(list_b[i].real, 4) + round(list_b[i].imag, 4) * 1j))))
)
return result

# find the max nearly round number of the float value.
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)
def __get_near_number(self, data: float or int or complex, pos: int) -> int or float:
if not isclose(data, (
(round(data, pos) if not isinstance(data, complex) else (
round(data.real, pos) + round(data.imag, pos) * 1j))),
rel_tol=1e-4):
return (round(data, pos + 1) if not isinstance(data, complex) else (
round(data.real, pos + 1) + round(data.imag, pos + 1) * 1j))
return self.__get_near_number(data, pos - 1)


Expand Down

0 comments on commit 803a283

Please sign in to comment.