Initial Work on Cython Code

This commit is contained in:
2022-12-06 21:15:09 -03:00
parent be8f3fba42
commit 4b863c0ad8
25 changed files with 758 additions and 327 deletions

0
yoshi_seals/__init__.pxd Normal file
View File

18
yoshi_seals/__init__.py Normal file
View File

@@ -0,0 +1,18 @@
# Seals - Program made for educational intent, can be freely distributed
# and can be used for economical intent. I will not take legal actions
# unless my intelectual propperty, the code, is stolen or change without permission.
# Copyright (C) 2020 VItor Hideyoshi Nakazone Batista
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as published by
# the Free Software Foundation.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

View File

@@ -0,0 +1,20 @@
# Seals - Program made for educational intent, can be freely distributed
# and can be used for economical intent. I will not take legal actions
# unless my intelectual propperty, the code, is stolen or change without permission.
# Copyright (C) 2020 VItor Hideyoshi Nakazone Batista
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as published by
# the Free Software Foundation.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
from .eigen import eigen

View File

@@ -0,0 +1,64 @@
# Seals - Program made for educational intent, can be freely distributed
# and can be used for economical intent. I will not take legal actions
# unless my intelectual propperty, the code, is stolen or change without permission.
# Copyright (C) 2020 VItor Hideyoshi Nakazone Batista
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as published by
# the Free Software Foundation.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import numpy as np
def eigen(a: np.ndarray) -> np.ndarray:
k = 0
l = np.ones((a.shape[0]))
at = a #variavel temporaria para A
b = np.random.rand(a.shape[0],a.shape[1])
while (k < at.shape[0]):
u = np.random.rand(at.shape[0],1)
u = u/max(u.min(), u.max(), key=abs)
ctrl = 0
while (ctrl != l[k]):
ctrl = l[k]
u = at.dot(u)
l[k] = max(u.min(), u.max(), key=abs)
u = u/l[k]
alpha = 0.999*l[k]
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)
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)
i = 0
while (u[i] == 0):
i += 1
at = at - (1/u[i])*u*at[i]
k += 1
return l, b

View File

@@ -0,0 +1,21 @@
# Seals - Program made for educational intent, can be freely distributed
# and can be used for economical intent. I will not take legal actions
# unless my intelectual propperty, the code, is stolen or change without permission.
# Copyright (C) 2020 VItor Hideyoshi Nakazone Batista
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as published by
# the Free Software Foundation.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
from .insert import matrix
from .insert import vector

View File

@@ -0,0 +1,48 @@
# Seals - Program made for educational intent, can be freely distributed
# and can be used for economical intent. I will not take legal actions
# unless my intelectual propperty, the code, is stolen or change without permission.
# Copyright (C) 2020 VItor Hideyoshi Nakazone Batista
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as published by
# the Free Software Foundation.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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 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

View File

@@ -0,0 +1,35 @@
import yoshi_seals.process.process as ps
import numpy as np
def det(a: np.ndarray) -> float:
return ps.det(a)
def inverse(a: np.ndarray) -> np.ndarray:
return ps.inverse(a)
def hstack(a: np.ndarray, b: np.ndarray) -> np.ndarray:
return ps.hstack(a, b)
def vstack(a: np.ndarray, b: np.ndarray) -> np.ndarray:
return ps.vstack(a, b)
def gauss(a: np.ndarray, b: np.ndarray) -> np.ndarray:
return ps.gauss(a, b)
def cholesky(a: np.ndarray, b: np.ndarray) -> np.ndarray:
return ps.cholesky(a, b)
def decomposition(a: np.ndarray, b: np.ndarray) -> np.ndarray:
return ps.decomposition(a, b)
def cramer(a: np.ndarray, b: np.ndarray) -> np.ndarray:
return ps.cramer(a, b)

View File

@@ -0,0 +1,193 @@
# Seals - Program made for educational intent, can be freely distributed
# and can be used for economical intent. I will not take legal actions
# unless my intelectual propperty, the code, is stolen or change without permission.
# Copyright (C) 2020 VItor Hideyoshi Nakazone Batista
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as published by
# the Free Software Foundation.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
from yoshi_seals.shared cimport array
from libc.stdlib cimport malloc
from libc cimport math
cimport numpy as np
import numpy as np
cpdef double det(double[::,::] a):
return array.det(a)
cpdef np.ndarray[np.float64_t, ndim=2] inverse(double[::,::] matrix):
return np.asarray(array.inverse(matrix))
cpdef np.ndarray[np.float64_t, ndim=2] hstack(double[::,::] a, double[::,::] b):
return np.asarray(array.hstack(a, b))
cpdef np.ndarray[np.float64_t, ndim=2] vstack(double[::,::] a, double[::,::] b):
return np.asarray(array.vstack(a, b))
cpdef np.ndarray[np.float64_t, ndim=2] gauss(double[::,::] A, double[::,::] b):
cdef:
int i = 0, j = 0, k = 0, l = 0, reversed_index = 0
double[:] tmp
double sum_var
double[::,::] a = array.hstack(A,b)
double *c_pointer = <double *> malloc(A.shape[1]*sizeof(double))
double[:] x = <double[:A.shape[1]]>c_pointer
if not c_pointer:
raise MemoryError()
for i in range(A.shape[0]):
l = 1
while i < A.shape[1] and a[i][i] == 0 and (l + i) < A.shape[0]:
tmp = a[i]
a[i] = a[i+l]
a[i+l] = tmp
l += 1
for k in range(i + 1, A.shape[1]):
if a[k][i] != 0:
a[k] = array.subtract(a[k],array.mult(a[i], (a[k][i]/a[i][i])))
for j in range(A.shape[1]):
sum_var = 0
reversed_index = (A.shape[1] - 1) - j
for k in range(reversed_index,A.shape[1]):
sum_var += a[reversed_index][k]*x[k]
x[reversed_index] = (a[reversed_index][A.shape[1]] - sum_var)/a[reversed_index][reversed_index]
return np.asarray(x).reshape(b.shape[0],b.shape[1])
cpdef np.ndarray[np.float64_t, ndim=2] cholesky(double[:,:] A, double[:,:] b):
cdef:
int i = 0, j = 0, size_x = A.shape[0], size_y = A.shape[1]
double *c_pointer = <double *> malloc(size_x*size_y*sizeof(double))
double[::,::] g = <double[:size_x,:size_y]>c_pointer, y, x
while j < size_y:
while i < size_x:
if i == 0 and j == 0:
g[i][j] = math.sqrt(A[0][0])
elif j == 0:
g[i][j] = A[i][0] / g[0][0]
elif i == j:
k = 0
theta = 0
while k < i:
theta += g[i][k] ** 2
k += 1
g[i][j] = math.sqrt(A[i][i] - theta)
else:
k = 0
theta = 0
while k < j:
theta += g[i][k] * g[j][k]
k += 1
g[i][j] = (A[i][j] - theta) / g[j][j]
i += 1
j += 1
i = j
y = array.dot(array.inverse(g), b)
x = array.dot(array.inverse(array.transpose(g)), y)
return np.asarray(x)
cpdef np.ndarray[np.float64_t, ndim=2] decomposition(double[::,::] U, double[::,::] b):
cdef:
int i = 0, k = 0
double[::,::] L = array.identity(U.shape[0]), y, x
for i in range(U.shape[0]):
if U[i][i] == 0:
n = i
while (U[i][i] == 0) and (n < U.shape[0]):
temp = U[i].copy()
U[i] = U[n]
U[n] = temp
n += 1
for k in range(U.shape[0]):
if (k > i) and (U[i][i] != 0):
L[k][i] = U[k][i] / U[i][i]
U[k] = array.subtract(U[k], array.mult(U[i], L[k][i]))
y = array.dot(array.inverse(L), b)
x = array.dot(array.inverse(U), y)
return np.asarray(x)
cpdef np.ndarray[np.float64_t, ndim=2] cramer(double[:,:] A, double[:,:] b):
cdef:
int size_a_y = A.shape[0], size_a_x = A.shape[1]
int size_b_y = b.shape[0], size_b_x = b.shape[1]
int k = 0
double *c_pointer_tmp = <double *> malloc(size_a_x*size_a_y*sizeof(double))
double[::,::] tmp = <double[:size_a_y,:size_a_x]>c_pointer_tmp
double *c_pointer_x = <double *> malloc(size_b_x*size_b_y*sizeof(double))
double[::,::] x = <double[:size_b_y,:size_b_x]>c_pointer_x
if size_a_y != size_b_y:
raise ValueError("The matrices must have the same height.")
if size_b_x != 1:
raise ValueError("The b matrix must be a column matrix.")
for k in range(size_a_x):
tmp = A.copy()
for i in range(size_a_y):
tmp[i, k] = b[i,0]
x[k,0] = np.linalg.det(tmp) / np.linalg.det(A)
k += 1
return np.asarray(x)

View File

@@ -0,0 +1,21 @@
# Seals - Program made for educational intent, can be freely distributed
# and can be used for economical intent. I will not take legal actions
# unless my intelectual propperty, the code, is stolen or change without permission.
# Copyright (C) 2020 VItor Hideyoshi Nakazone Batista
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as published by
# the Free Software Foundation.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
from .scan import numpy as np
from .scan import pandas as pd

45
yoshi_seals/scan/scan.py Normal file
View File

@@ -0,0 +1,45 @@
# Seals - Program made for educational intent, can be freely distributed
# and can be used for economical intent. I will not take legal actions
# unless my intelectual propperty, the code, is stolen or change without permission.
# Copyright (C) 2020 VItor Hideyoshi Nakazone Batista
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as published by
# the Free Software Foundation.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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)
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 = "."
return pd.read_csv(path, sep=sep, decimal=decimal)

View File

View File

View File

@@ -0,0 +1,25 @@
cdef double[::] mult(double[::] array, double value)
cdef double[::] div(double[::] array, double value)
cdef double[::] addition(double[::] a, double[::] b)
cdef double[::] subtract(double[::] a, double[::] b)
cdef double[::,:] hstack(double[:,:] a, double[:,:] b)
cdef double[::,::] vstack(double[:,:] a, double[:,:] b)
cdef double[::,::] identity(int size)
cdef double[:,:] zeros((int, int) sizes)
cdef double[:,:] ones((int, int) sizes)
cdef double[:,:] inverse(double[:,:] a)
cdef double[:,:] transpose(double[:,:] a)
cdef double[:,:] dot(double[:,:] a, double[:,:] b)
cdef double det(double[::,::] a)

View File

@@ -0,0 +1,251 @@
from libc.stdlib cimport malloc
cimport numpy as np
import numpy as np
cdef double[::] mult(double[::] array, double value):
cdef int size = array.shape[0], i = 0
cdef:
double *c_pointer = <double *> malloc(size*sizeof(double))
double[::] mult_array = <double[:size]>c_pointer
for i in range(size):
mult_array[i] = array[i]*value
return mult_array
cdef double[::] div(double[::] array, double value):
cdef int size = array.shape[0], i = 0
cdef:
double *c_pointer = <double *> malloc(size*sizeof(double))
double[::] mult_array = <double[:size]>c_pointer
for i in range(size):
mult_array[i] = array[i]/value
return mult_array
cdef double[::] addition(double[::] a, double[::] b):
cdef int size_a = a.shape[0], size_b = b.shape[0], i = 0
cdef double *c_pointer = <double *> malloc(size_a*sizeof(double))
cdef double[::] mult_array = <double[:size_a]>c_pointer
for i in range(size_a):
mult_array[i] = a[i] + b[i]
return mult_array
cdef double[::] subtract(double[::] a, double[::] b):
cdef int size_a = a.shape[0], size_b = b.shape[0], i = 0
cdef double *c_pointer = <double *> malloc(size_a*sizeof(double))
cdef double[::] mult_array = <double[:size_a]>c_pointer
for i in range(size_a):
mult_array[i] = a[i] - b[i]
return mult_array
cdef double[::,::] identity(int size):
cdef int i = 0
cdef double *c_pointer = <double *> malloc(size*size*sizeof(double))
cdef double[::,:] matrix = <double[:size,:size]>c_pointer
for i in range(size):
for j in range(size):
if i == j:
matrix[i][j] = 1
elif i != j:
matrix[i][j] = 0
return matrix
cdef double[:,:] zeros((int, int) size):
cdef int i = 0, j = 0
cdef:
double *c_pointer = <double *> malloc(size[0]*size[1]*sizeof(double))
double[:,:] id_array = <double[:size[0],:size[1]]>c_pointer
if not c_pointer:
raise MemoryError()
for i in range(size[0]):
for j in range(size[1]):
id_array[i,j] = 0.0
return id_array
cdef double[:,:] ones((int, int) size):
cdef int i = 0, j = 0
cdef:
double *c_pointer = <double *> malloc(size[0]*size[1]*sizeof(double))
double[:,:] id_array = <double[:size[0],:size[1]]>c_pointer
for i in range(size[0]):
for j in range(size[1]):
id_array[i,j] = 1.0
return id_array
cdef double[:,:] hstack(double[:,:] a, double[:,:] b):
cdef:
int i, j
int a_x = a.shape[0], a_y = a.shape[1]
int b_x = b.shape[0], b_y = b.shape[1]
int size_x = a_x, size_y = a_y + b_y
double *c_pointer = <double *> malloc(size_x*size_y*sizeof(double))
double[::,::] matrix = <double[:size_x,:size_y]>c_pointer
if a_x != b_x:
raise ValueError("Cannot hstack matrices")
for i in range(size_x):
for j in range(size_y):
if j < a_y:
matrix[i,j] = a[i,j]
else:
matrix[i,j] = b[i,j-a_y]
return matrix
cdef double[:,:] vstack(double[:,:] a, double[:,:] b):
cdef:
int i, j
int a_x = a.shape[0], b_x = b.shape[0]
int a_y = a.shape[1], b_y = b.shape[1]
int size_x = a_x + b_x, size_y = a_y
double *c_pointer = <double *> malloc(size_x*size_y*sizeof(double))
double[:,:] matrix = <double[:size_x,:size_y]>c_pointer
if a_y != b_y:
raise ValueError("Cannot vstack matrices")
for i in range(size_x):
for j in range(size_y,):
if i < a_x:
matrix[i,j] = a[i,j]
else:
matrix[i,j] = b[i-a_x, j]
return matrix
cdef double[:,:] inverse(double[:,:] a):
cdef:
int i = 0, k = 0, n, size = a.shape[0]
double[:,:] matrix = hstack(a,identity(size))
double mult_const
double[:] tmp
if a.shape[0] != a.shape[1]:
raise ValueError("Non Quadratic Matrix doesn't have an Inverse Matrix")
for i in range(size):
if matrix[i][i] == 0:
n = i
while (matrix[i][i] == 0) and (n < size):
tmp = matrix[i]
matrix[i] = matrix[n].copy()
matrix[n] = tmp
n += 1
for k in range(size):
if (k != i) and (matrix[i][i] != 0):
mult_const = matrix[k][i]/matrix[i][i]
matrix[k] = subtract(matrix[k], mult(matrix[i], mult_const))
for k in range(size):
if matrix[k][k] != 0:
matrix[k] = div(matrix[k], matrix[k][k])
return matrix[:,size:]
cdef double[:,:] transpose(double[:,:] a):
cdef:
int size_x = a.shape[0], size_y = a.shape[1]
double *c_pointer = <double *> malloc(size_y*size_x*sizeof(double))
double[:,:] tmp = <double[:size_y,:size_x]>c_pointer
for i in range(size_x):
for j in range(size_y):
tmp[j,i] = a[i,j]
return tmp
cdef double[:,:] dot(double[:,:] a, double[:,:] b):
c = np.zeros((a.shape[0], b.shape[1]))
for i in range(a.shape[0]):
for j in range(b.shape[1]):
for k in range(a.shape[0]):
c[i][j] += a[i][k] * b[k][j]
return c
cdef double det(double[::,::] a):
cdef:
double[:,:] tmp
double total = 0, sub_det
int size_x = a.shape[0], size_y = a.shape[1], i
if size_x != size_y:
raise ValueError("Determinant Operation is only valid for Quadratic Matrices.")
if size_x == 2 and size_y == 2:
total = a[0][0] * a[1][1] - a[1][0] * a[0][1]
return total
for i in range(size_x):
tmp = a.copy()
tmp = tmp[1:]
for i in range(size_y):
tmp[i] = addition(tmp[i][0:i], tmp[i][i + 1:])
sign = (-1) ** (i % 2)
sub_det = det(tmp)
total += sign * a[0][i] * sub_det
return total

View File

@@ -0,0 +1,21 @@
# Seals - Program made for educational intent, can be freely distributed
# and can be used for economical intent. I will not take legal actions
# unless my intelectual propperty, the code, is stolen or change without permission.
# Copyright (C) 2020 VItor Hideyoshi Nakazone Batista
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as published by
# the Free Software Foundation.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
from .write import numpy as np
from .write import pandas as pd

View File

@@ -0,0 +1,35 @@
# Seals - Program made for educational intent, can be freely distributed
# and can be used for economical intent. I will not take legal actions
# unless my intelectual propperty, the code, is stolen or change without permission.
# Copyright (C) 2020 VItor Hideyoshi Nakazone Batista
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as published by
# the Free Software Foundation.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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)
solution_writer.writerows(array)
return array
def pandas(df: pd.DataFrame, path:str) -> None:
df.to_csv(path)