Skip to content
Snippets Groups Projects
Commit 26da14b5 authored by iliya's avatar iliya
Browse files

feat: added missing type annotations

parent b9f6b029
Branches
No related tags found
No related merge requests found
...@@ -3,20 +3,20 @@ from rf_methods import rec_bisection, rec_regula_falsi, iter_bisection, \ ...@@ -3,20 +3,20 @@ from rf_methods import rec_bisection, rec_regula_falsi, iter_bisection, \
from Dual_Numbers import Dual_Number from Dual_Numbers import Dual_Number
def f(x: Dual_Number): def f(x: Dual_Number) -> float:
return ((x * x - 1) * Dual_Number.exp(x - 1)).d return ((x * x - 1) * Dual_Number.exp(x - 1)).d
def g(x: Dual_Number): def g(x: Dual_Number) -> float:
return (Dual_Number.sin(x * x) + Dual_Number.exp(x + 3) * return (Dual_Number.sin(x * x) + Dual_Number.exp(x + 3) *
Dual_Number.cos(5 - x)).d Dual_Number.cos(5 - x)).d
def dfdx(x: float): def dfdx(x: float) -> float:
return f(Dual_Number(x, 1)) return f(Dual_Number(x, 1))
def dgdx(x: float): def dgdx(x: float) -> float:
return g(Dual_Number(x, 1)) return g(Dual_Number(x, 1))
......
import numpy as np import numpy as np
from typing import Callable
def f(x: float) -> float: def f(x: float) -> float:
...@@ -17,7 +18,8 @@ def z(x: float) -> float: ...@@ -17,7 +18,8 @@ def z(x: float) -> float:
return x ** 3 - x ** 2 - 1 return x ** 3 - x ** 2 - 1
def rec_regula_falsi(start: float, stop: float, func, debug: bool) -> float: def rec_regula_falsi(start: float, stop: float, func: Callable[[float], float],
debug: bool) -> float:
cN = (start * func(stop) - stop * func(start)) / (func(stop) - func(start)) cN = (start * func(stop) - stop * func(start)) / (func(stop) - func(start))
if np.sign(func(cN)) != np.sign(func(start)): if np.sign(func(cN)) != np.sign(func(start)):
...@@ -31,13 +33,14 @@ def rec_regula_falsi(start: float, stop: float, func, debug: bool) -> float: ...@@ -31,13 +33,14 @@ def rec_regula_falsi(start: float, stop: float, func, debug: bool) -> float:
# Testing the y-axis # Testing the y-axis
if np.abs(func(cN)) < 1e-9: if np.abs(func(cN)) < 1e-9:
return cN return cN
return rec_regula_falsi(start, stop, func, debug) return rec_regula_falsi(start, stop, func, debug)
else: else:
return cN return cN
def iter_regula_falsi(init_start: float, init_stop: float, func, debug: bool) \ def iter_regula_falsi(init_start: float, init_stop: float, func:
-> float: Callable[[float], float], debug: bool) -> float:
cN = (init_start * func(init_stop) - init_stop * cN = (init_start * func(init_stop) - init_stop *
func(init_start)) / (func(init_stop) - func(init_start)) func(init_start)) / (func(init_stop) - func(init_start))
start = init_start start = init_start
...@@ -48,18 +51,22 @@ def iter_regula_falsi(init_start: float, init_stop: float, func, debug: bool) \ ...@@ -48,18 +51,22 @@ def iter_regula_falsi(init_start: float, init_stop: float, func, debug: bool) \
while np.abs(func(stop) - func(start)) > 1e-9 or np.abs(func(cN)) > 1e-9: while np.abs(func(stop) - func(start)) > 1e-9 or np.abs(func(cN)) > 1e-9:
if debug: if debug:
print(f"Iter = {iter}\tx = {cN}\tf(x) = {func(cN)}") print(f"Iter = {iter}\tx = {cN}\tf(x) = {func(cN)}")
cN = (start * func(stop) - stop * func(start)) / \ cN = (start * func(stop) - stop * func(start)) / \
(func(stop) - func(start)) (func(stop) - func(start))
if np.sign(func(cN)) != np.sign(func(start)): if np.sign(func(cN)) != np.sign(func(start)):
stop = cN stop = cN
elif np.sign(func(cN)) != np.sign(func(stop)): elif np.sign(func(cN)) != np.sign(func(stop)):
start = cN start = cN
iter += 1 iter += 1
return cN return cN
def rec_bisection(start: float, stop: float, func, debug: bool) -> float: def rec_bisection(start: float, stop: float, func: Callable[[float], float],
debug: bool) -> float:
cN = (stop + start) / 2 cN = (stop + start) / 2
if np.sign(func(cN)) != np.sign(func(start)): if np.sign(func(cN)) != np.sign(func(start)):
...@@ -70,13 +77,14 @@ def rec_bisection(start: float, stop: float, func, debug: bool) -> float: ...@@ -70,13 +77,14 @@ def rec_bisection(start: float, stop: float, func, debug: bool) -> float:
if np.abs(stop - start) > 1e-9: if np.abs(stop - start) > 1e-9:
if debug: if debug:
print(f"x = {cN}\tf(x) = {func(cN)}") print(f"x = {cN}\tf(x) = {func(cN)}")
return rec_bisection(start, stop, func, debug) return rec_bisection(start, stop, func, debug)
else: else:
return cN return cN
def iter_bisection(init_start: float, init_stop: float, func, debug: bool) -> \ def iter_bisection(init_start: float, init_stop: float, func:
float: Callable[[float], float], debug: bool) -> float:
cN = (init_stop + init_start) / 2 cN = (init_stop + init_start) / 2
start = init_start start = init_start
stop = init_stop stop = init_stop
...@@ -86,7 +94,9 @@ def iter_bisection(init_start: float, init_stop: float, func, debug: bool) -> \ ...@@ -86,7 +94,9 @@ def iter_bisection(init_start: float, init_stop: float, func, debug: bool) -> \
while np.abs(stop - start) > 1e-9: while np.abs(stop - start) > 1e-9:
if debug: if debug:
print(f"Iter = {iter}\tx = {cN}\tf(x) = {func(cN)}") print(f"Iter = {iter}\tx = {cN}\tf(x) = {func(cN)}")
cN = (stop + start) / 2 cN = (stop + start) / 2
if np.sign(func(cN)) != np.sign(func(start)): if np.sign(func(cN)) != np.sign(func(start)):
stop = cN stop = cN
elif np.sign(func(cN)) != np.sign(func(stop)): elif np.sign(func(cN)) != np.sign(func(stop)):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment