Refactoring for More Pythonic Code

This commit is contained in:
2022-12-06 21:27:56 -03:00
parent 4b863c0ad8
commit 1d62ac70df
6 changed files with 102 additions and 72 deletions

View File

@@ -19,46 +19,43 @@
import numpy as np
def eigen(a: np.ndarray) -> np.ndarray:
k = 0
def eigen(a: np.ndarray) -> np.ndarray:
l = np.ones((a.shape[0]))
at = a #variavel temporaria para A
b = np.random.rand(a.shape[0],a.shape[1])
at = a # variavel temporaria para A
b = np.random.rand(a.shape[0], a.shape[1])
while (k < at.shape[0]):
for k in range(at.shape[0]):
u = np.random.rand(at.shape[0],1)
u = u/max(u.min(), u.max(), key=abs)
u = np.random.rand(at.shape[0], 1)
u = u / max(u.min(), u.max(), key=abs)
ctrl = 0
while (ctrl != l[k]):
while ctrl != l[k]:
ctrl = l[k]
u = at.dot(u)
l[k] = max(u.min(), u.max(), key=abs)
u = u/l[k]
u = u / l[k]
alpha = 0.999*l[k]
alpha = 0.999 * l[k]
t = np.random.rand(a.shape[0],1)
t = np.random.rand(a.shape[0], 1)
b[k] = b[k]/max(b[k].min(), b[k].max(), key=abs)
t = l/max(l.min(), l.max(), key=abs)
b[k] = b[k] / max(b[k].min(), b[k].max(), key=abs)
t = l / max(l.min(), l.max(), key=abs)
while not (np.allclose(b[k],t,atol=10**(-17))):
while not (np.allclose(b[k], t, atol=10 ** (-17))):
t = b[k].copy()
b[k] = np.linalg.solve((a - alpha*np.identity(a.shape[0])),((l[k]-alpha)*t))
b[k] = b[k]/max(b[k].min(), b[k].max(), key=abs)
b[k] = np.linalg.solve((a - alpha * np.identity(a.shape[0])), ((l[k] - alpha) * t))
b[k] = b[k] / max(b[k].min(), b[k].max(), key=abs)
i = 0
while (u[i] == 0):
while u[i] == 0:
i += 1
at = at - (1/u[i])*u*at[i]
k += 1
at = at - (1 / u[i]) * u * at[i]
return l, b
return l, b

View File

@@ -19,30 +19,19 @@
import numpy as np
def matrix(matrix: np.ndarray) -> np.ndarray:
i = 0
while (i < matrix.shape[0]):
j = 0
while (j < matrix.shape[1]):
matrix[i][j] = float(input('Insira o elemento {}x{}: '.format((i+1),(j+1))))
j += 1
i += 1
return matrix
def matrix(a: np.ndarray) -> np.ndarray:
def vector(vector: np.ndarray) -> np.ndarray:
j=0
while (j < vector.shape[0]):
vector[j] = float(input('Insira o elemento b{}: '.format((j+1))))
j += 1
return vector
for i in range(a.shape[0]):
for j in range(a.shape[1]):
a[i][j] = float(input(f"Insert the element a{i+1}x{j+1}: "))
return a
def vector(v: np.ndarray) -> np.ndarray:
for j in range(v.shape[0]):
v[j] = float(input(f"Insert the element b{j+1}: "))
return v

View File

@@ -20,26 +20,13 @@
import numpy as np
import pandas as pd
def numpy(path:str, sep: str = None, decimal: str = None) -> np.ndarray:
if sep is None:
sep = ","
if decimal is None:
decimal = "."
df=pd.read_csv(path, sep=sep, decimal=decimal, header=None)
def numpy(path: str, sep: str = ",", decimal: str = ".") -> np.ndarray:
df = pd.read_csv(path, sep=sep, decimal=decimal, header=None)
array = df.to_numpy()
return array
def pandas(path: str, sep: str = None, decimal: str = None) -> pd.DataFrame:
if sep is None:
sep = ","
if decimal is None:
decimal = "."
def pandas(path: str, sep: str = ",", decimal: str = ".") -> pd.DataFrame:
return pd.read_csv(path, sep=sep, decimal=decimal)

View File

@@ -21,15 +21,14 @@ import pandas as pd
import numpy as np
import csv
def numpy(array: np.ndarray, path: str) -> np.ndarray:
with open(path, mode='w') as sistema_linear:
solution_writer = csv.writer(sistema_linear, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
with open(path, mode='w') as linear_system:
solution_writer = csv.writer(linear_system, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
solution_writer.writerows(array)
return array
def pandas(df: pd.DataFrame, path:str) -> None:
df.to_csv(path)
def pandas(df: pd.DataFrame, path: str) -> None:
df.to_csv(path)