Implements Tests for Interpolation

This commit is contained in:
2022-12-09 20:58:06 -03:00
parent b24723467e
commit 053a134541
6 changed files with 53 additions and 45 deletions

11
.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,11 @@
{
"python.testing.unittestArgs": [
"-v",
"-s",
"./tests",
"-p",
"test_*.py"
],
"python.testing.pytestEnabled": false,
"python.testing.unittestEnabled": true
}

4
Pipfile.lock generated
View File

@@ -108,10 +108,10 @@
}, },
"yoshi-seals": { "yoshi-seals": {
"hashes": [ "hashes": [
"sha256:448de57bfee12999ecd56e456e8a13f312396030b9872a2b5c9eac729e07e097" "sha256:85e1697b289a135191362a3885db01bc568e0ca341da0eddeac69dabc86e35d8"
], ],
"index": "pypi", "index": "pypi",
"version": "==2.0.3645235495" "version": "==2.0.3654593985"
} }
}, },
"develop": { "develop": {

View File

@@ -1,15 +1,25 @@
import setuptools import setuptools
import os
with open("yoshi-otter1.3.3/README.md", "r") as fh:
long_description = fh.read() __name = "yoshi-otter"
__version_sufix = os.environ.get('VERSION_SUFIX')
if not __version_sufix:
__version_sufix = "dev"
__version = f"2.0.{__version_sufix}"
with open("README.md", "r") as fh:
__long_description = fh.read()
setuptools.setup( setuptools.setup(
name="yoshi-otter", # Replace with your own username name=__name,
version="1.3.3", version=__version,
author="Vitor Hideyoshi", author="Vitor Hideyoshi",
author_email="vitor.h.n.batista@gmail.com", author_email="vitor.h.n.batista@gmail.com",
description="Numeric Calculus python module in the topic of Algebra Functions", description="Numeric Calculus python module in the topic of Algebra Functions",
long_description=long_description, long_description=__long_description,
long_description_content_type="text/markdown", long_description_content_type="text/markdown",
url="https://github.com/HideyoshiNakazone/Otter-NumericCalculus.git", url="https://github.com/HideyoshiNakazone/Otter-NumericCalculus.git",
packages=setuptools.find_packages(), packages=setuptools.find_packages(),

View File

@@ -12,18 +12,11 @@ class TestInterpolation(unittest.TestCase):
def f(x): def f(x):
return 2 * x return 2 * x
def g(x): X = np.linspace(0, 10, num=100)
return x + x**2
X = np.linspace(0, 1000, num=1000)
Y = [f(x) for x in X] Y = [f(x) for x in X]
self.data = pd.DataFrame(data={'X': X, 'Y': Y}) self.data = pd.DataFrame(data={'X': X, 'Y': Y})
Y = [g(x) for x in X]
self.data_pol = pd.DataFrame(data={'X': X, 'Y': Y})
def test_class_instantiation(self): def test_class_instantiation(self):
interpolation = Interpolation(self.data) interpolation = Interpolation(self.data)
self.assertIsInstance(interpolation, Interpolation) self.assertIsInstance(interpolation, Interpolation)
@@ -34,33 +27,32 @@ class TestInterpolation(unittest.TestCase):
self.assertEqual(func(1), 2) self.assertEqual(func(1), 2)
@unittest.skip("Temporally not working")
def test_polynomial_vandermonde(self): def test_polynomial_vandermonde(self):
interpolation = Interpolation(self.data_pol) interpolation = Interpolation(self.data)
func = interpolation.polynomial.vandermonde() func = interpolation.polynomial.vandermonde()
self.assertEqual(func(1), 2) self.assertAlmostEqual(func(1), 2)
@unittest.skip("Temporally not working") @unittest.skip("Temporally not working")
def test_polynomial_lagrange(self): def test_polynomial_lagrange(self):
interpolation = Interpolation(self.data_pol) interpolation = Interpolation(self.data)
result = interpolation.polynomial.lagrange(1) result = interpolation.polynomial.lagrange(1)
self.assertEqual(result, 2) self.assertAlmostEqual(result, 2)
@unittest.skip("Temporally not working") # @unittest.skip("Temporally not working")
def test_polynomial_newton(self): def test_polynomial_newton(self):
interpolation = Interpolation(self.data_pol) interpolation = Interpolation(self.data)
result = interpolation.polynomial.newton(1) result = interpolation.polynomial.newton(1)
self.assertEqual(result, 2) self.assertAlmostEqual(result, 2)
@unittest.skip("Temporally not working") @unittest.skip("Temporally not working")
def test_polynomial_gregory(self): def test_polynomial_gregory(self):
interpolation = Interpolation(self.data_pol) interpolation = Interpolation(self.data)
result = interpolation.polynomial.gregory(1) result = interpolation.polynomial.gregory(1)
self.assertEqual(result, 2) self.assertAlmostEqual(result, 2)
if __name__ == '__main__': if __name__ == '__main__':

Binary file not shown.

View File

@@ -1,7 +1,5 @@
from typing import Callable, Any
from yoshi_seals import process as sl from yoshi_seals import process as sl
from typing import Callable, Any
import numpy as np import numpy as np
@@ -13,7 +11,7 @@ class Interpolation:
def __init__(self, data) -> None: def __init__(self, data) -> None:
self.data = data self.data = data
self.polynomial = self.Polynomial(self.data) self.polynomial = self.__Polynomial(self.data)
def minimums(self) -> Callable[[Any], float]: def minimums(self) -> Callable[[Any], float]:
@@ -60,7 +58,7 @@ class Interpolation:
return lambda x: a * x + b, r2 return lambda x: a * x + b, r2
class Polynomial: class __Polynomial:
def __init__(self, data) -> None: def __init__(self, data) -> None:
self.data = data self.data = data
@@ -69,22 +67,20 @@ class Interpolation:
matrix = np.zeros((self.data.shape[0], self.data.shape[0])) matrix = np.zeros((self.data.shape[0], self.data.shape[0]))
for k in range(0, self.data.shape[0]): for k in range(self.data.shape[0]):
matrix[:, k] = self.data.X[:] ** k matrix[:, k] = self.data.X[:].copy() ** k
array = np.array(self.data.Y.tolist()).reshape(self.data.shape[0], 1) array = np.array(self.data.Y.tolist()).reshape(self.data.shape[0], 1)
A = sl.gauss(matrix, array) coefficient_matrix = sl.gauss(matrix, array)[:]
def f(coefficient_matrix, x):
def __f(coefficients, x):
y = 0 y = 0
for i in range(0, coefficients.shape[0]):
for i in range(0, A.shape[0]): y += float(coefficients[i]) * (x ** i)
y += coefficient_matrix[1][i] * (x ** i)
return y return y
return lambda x: f(A, x) return lambda x: __f(coefficient_matrix, x)
def lagrange(self, x: float) -> float: def lagrange(self, x: float) -> float:
@@ -155,24 +151,23 @@ class Interpolation:
d[0] = self.data.Y d[0] = self.data.Y
i = j = 0 i = 0
while i < self.data.shape[0]: while i < self.data.shape[0]:
j = 0
while j < (self.data.shape[0] - (i + 1)): while j < (self.data.shape[0] - (i + 1)):
d[i + 1][j] = (d[i][j + 1] - d[i][j]) / ((i + 1) * h) d[i + 1][j] = (d[i][j + 1] - d[i][j]) / ((i + 1) * h)
j += 1 j += 1
i += 1 i += 1
j = 0
y = d[0][0] y = d[0][0]
i = 0 i = 0
while (i + 1) < self.data.shape[0]: while (i + 1) < self.data.shape[0]:
mult = 1 mult = 1
k = 0 k = 0
while k <= i: while k <= i:
mult = mult * (x - self.data.X[k]) mult = mult * (x - self.data.X[k])
k += 1 k += 1
@@ -180,4 +175,4 @@ class Interpolation:
y += d[i + 1][0] * mult y += d[i + 1][0] * mult
i += 1 i += 1
return y return -y