Initial Refactoring of yoshi_otter and Test Implementation

This commit is contained in:
2022-12-08 05:30:17 -03:00
parent 3b319573ee
commit b24723467e
106 changed files with 1010 additions and 8186 deletions

View File

@@ -0,0 +1,52 @@
# Otter - 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_otter.algebra.integral.double import Double
from yoshi_otter.algebra.integral.simple import Simple
from yoshi_otter.algebra.roots import Roots
from yoshi_otter.algebra.edo import Edo
from typing import Callable, Union
from inspect import signature
from yoshi_otter.shared import InvalidFunctionSignature
class Algebra:
def __init__(self, function: Callable[[float], float] | Callable[[float, float], float]) -> None:
self.f = function
self.integral = self.__Integral(self.f)
self.roots = Roots(self.f)
self.edo = Edo(self.f)
def d(self, x: float, e: float = 10 ** -4) -> float:
if len(signature(self.f).parameters) == 1:
return (self.f(x + e) - self.f(x - e)) / (2 * e)
else:
raise InvalidFunctionSignature("This method is only valid for one dimensional functions.")
class __Integral:
def __init__(self, function: Union[Callable[[float], float], Callable[[float, float], float]]) -> None:
self.f = function
self.simple = Simple(self.f)
self.double = Double(self.f)

View File

@@ -0,0 +1 @@
from .__algebra import Algebra

View File

@@ -0,0 +1,51 @@
from typing import Callable
class Edo:
def __init__(self, function: Callable[[float], float]) -> None:
self.F = function
def euler(self, a: float, y: float, b: float, n: int = 10**6) -> float:
dx = (b - a) / n
def x(i):
return a + i * dx
for i in range(n):
y = y + (self.F(x(i), y)) * dx
return y
def runge(self, a: float, y: float, b: float, n: int = 10**6) -> float:
dx = (b - a) / n
def x(i):
return a + i * dx
for i in range(n):
y = y + (dx / 2) * (self.F(x(i), y) + self.F(x(i + 1), (y + (dx * self.F(x(i), y)))))
return y
def adams(self, a: float, y: float, b: float, n: int = None
) -> float:
if n is None:
n = 10 ** 6
dx = (b - a) / n
def x(i):
return a + i * dx
for i in range(n):
f0 = self.F(x(i), y)
f1 = self.F(x(i + 1), y + dx * self.F(x(i) + (dx / 2), y + (dx / 2) * self.F(x(i), y)))
f2 = self.F(x(i + 2), y + (dx / 2) * (3 * f1 - f0))
y += (dx / 12) * (5 * f2 + 8 * f1 - f0)
return y

View File

@@ -0,0 +1 @@
from .__edo import Edo

View File

View File

@@ -0,0 +1,79 @@
from typing import Callable
class Double:
def __init__(self, function: Callable[[float, float], float]):
self.f = function
def riemann(self, a: float, b: float, c: float, d: float,
n: int = 10 ** 4, m: int = None) -> float:
if m is None:
m = n
dx = (b - a) / n
dy = (d - c) / m
kappa = a
psi = c
theta = 0
while (psi + dy) < d:
while (kappa + dx) < b:
theta = theta + self.f(kappa, psi)
kappa = kappa + dx
psi = psi + dy
kappa = a
return theta * dx * dy
def simpson(self, a: float, b: float, c: float, d: float,
n: int = 10 ** 4, m: int = None) -> float:
if m is None:
m = n
dx = (b - a) / n
dy = (d - c) / m
def x(i):
return a + i * dx
def y(i):
return c + i * dy
def g(i):
sigma = 0
upsilon = 0
zeta = 1
csi = 1
while zeta <= (m / 2):
sigma += self.f(x(i), y(2 * zeta - 1))
zeta += 1
while csi <= ((m / 2) - 1):
upsilon += self.f(x(i), y(2 * csi))
csi += 1
return (dy / 3) * (self.f(x(i), y(0)) + self.f(x(i), y(m)) + 4 * sigma + 2 * upsilon)
eta = 0
theta = 0
psi = 1
kappa = 1
while psi <= (n / 2):
eta += g(2 * psi - 1)
psi += 1
while kappa <= ((n / 2) - 1):
theta += g(2 * kappa)
kappa += 1
return (dx / 3) * (g(0) + g(n) + 4 * eta + 2 * theta)

View File

@@ -0,0 +1,44 @@
from typing import Callable
class Simple:
def __init__(self, function: Callable[[float], float]) -> None:
self.f = function
def riemann(self, a: float, b: float, n: int = 10 ** 6) -> float:
delta = (b - a) / n
psi = a
theta = 0
while (psi + delta) <= b:
theta += (self.f(psi) + self.f(psi + delta)) / 2
psi += delta
integral = delta * theta
return integral
def simpson(self, a: float, b: float, n: int = 10 ** 6) -> float:
def x(i):
return a + i * h
h = (b - a) / n
eta = 0
theta = 0
psi = 1
kappa = 1
while psi <= (n / 2):
eta = eta + self.f(x(2 * psi - 1))
psi = psi + 1
while kappa <= ((n / 2) - 1):
theta = theta + self.f(x(2 * kappa))
kappa = kappa + 1
return (h / 3) * (self.f(x(0)) + self.f(x(n)) + 4 * eta + 2 * theta)

View File

@@ -0,0 +1 @@
from .__roots import Roots

View File

@@ -0,0 +1,80 @@
from typing import Callable
class Roots:
def __init__(self, function: Callable[[float], float] = None
) -> float:
if function is not None:
self.f = function
def bissec(self, a: float, b: float, e: float = 10**-6) -> float:
fa = self.f(a)
while abs(a - b) > e:
c = (a + b) / 2
fc = self.f(c)
if (fa * fc) < 0:
b = c
else:
a = c
c = (a + b) / 2
return c
def __d(self, x: float, e: float) -> float:
return (self.f(x + e) - self.f(x - e)) / (2 * e)
def newton(self, a: float, e: float = 10**-6) -> float:
fa = self.f(a)
da = self.__d(a, e)
b = a - fa / da
while abs(a - b) > e:
b = a
a -= (fa / da)
fa = self.f(a)
da = self.__d(a, e)
return a
def bissec_newton(self, a: float, b: float, e: float = 10**-6) -> float:
fa = self.f(a)
c = (a + b) / 2 # 'c' é a raiz calculada
while abs(a - b) > 0.1:
fc = self.f(c)
if fa * fc < 0:
b = c
else:
a = c
fa = self.f(a)
c = (a + b) / 2
fc = self.f(c)
dc = self.__d(c, e)
h = c - fc / dc # 'h' é uma variável de controle
while abs(c - h) > e:
h = c
c -= (fc / dc)
fc = self.f(c)
dc = self.__d(c, e)
return c