Implements Tests for Interpolation
This commit is contained in:
11
.vscode/settings.json
vendored
Normal file
11
.vscode/settings.json
vendored
Normal 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
4
Pipfile.lock
generated
@@ -108,10 +108,10 @@
|
||||
},
|
||||
"yoshi-seals": {
|
||||
"hashes": [
|
||||
"sha256:448de57bfee12999ecd56e456e8a13f312396030b9872a2b5c9eac729e07e097"
|
||||
"sha256:85e1697b289a135191362a3885db01bc568e0ca341da0eddeac69dabc86e35d8"
|
||||
],
|
||||
"index": "pypi",
|
||||
"version": "==2.0.3645235495"
|
||||
"version": "==2.0.3654593985"
|
||||
}
|
||||
},
|
||||
"develop": {
|
||||
|
||||
20
setup.py
20
setup.py
@@ -1,15 +1,25 @@
|
||||
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(
|
||||
name="yoshi-otter", # Replace with your own username
|
||||
version="1.3.3",
|
||||
name=__name,
|
||||
version=__version,
|
||||
author="Vitor Hideyoshi",
|
||||
author_email="vitor.h.n.batista@gmail.com",
|
||||
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",
|
||||
url="https://github.com/HideyoshiNakazone/Otter-NumericCalculus.git",
|
||||
packages=setuptools.find_packages(),
|
||||
|
||||
@@ -12,18 +12,11 @@ class TestInterpolation(unittest.TestCase):
|
||||
def f(x):
|
||||
return 2 * x
|
||||
|
||||
def g(x):
|
||||
return x + x**2
|
||||
|
||||
X = np.linspace(0, 1000, num=1000)
|
||||
X = np.linspace(0, 10, num=100)
|
||||
Y = [f(x) for x in X]
|
||||
|
||||
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):
|
||||
interpolation = Interpolation(self.data)
|
||||
self.assertIsInstance(interpolation, Interpolation)
|
||||
@@ -34,33 +27,32 @@ class TestInterpolation(unittest.TestCase):
|
||||
|
||||
self.assertEqual(func(1), 2)
|
||||
|
||||
@unittest.skip("Temporally not working")
|
||||
def test_polynomial_vandermonde(self):
|
||||
interpolation = Interpolation(self.data_pol)
|
||||
interpolation = Interpolation(self.data)
|
||||
func = interpolation.polynomial.vandermonde()
|
||||
|
||||
self.assertEqual(func(1), 2)
|
||||
self.assertAlmostEqual(func(1), 2)
|
||||
|
||||
@unittest.skip("Temporally not working")
|
||||
def test_polynomial_lagrange(self):
|
||||
interpolation = Interpolation(self.data_pol)
|
||||
interpolation = Interpolation(self.data)
|
||||
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):
|
||||
interpolation = Interpolation(self.data_pol)
|
||||
interpolation = Interpolation(self.data)
|
||||
result = interpolation.polynomial.newton(1)
|
||||
|
||||
self.assertEqual(result, 2)
|
||||
self.assertAlmostEqual(result, 2)
|
||||
|
||||
@unittest.skip("Temporally not working")
|
||||
def test_polynomial_gregory(self):
|
||||
interpolation = Interpolation(self.data_pol)
|
||||
interpolation = Interpolation(self.data)
|
||||
result = interpolation.polynomial.gregory(1)
|
||||
|
||||
self.assertEqual(result, 2)
|
||||
self.assertAlmostEqual(result, 2)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Binary file not shown.
@@ -1,7 +1,5 @@
|
||||
from typing import Callable, Any
|
||||
|
||||
from yoshi_seals import process as sl
|
||||
|
||||
from typing import Callable, Any
|
||||
import numpy as np
|
||||
|
||||
|
||||
@@ -13,7 +11,7 @@ class Interpolation:
|
||||
def __init__(self, data) -> None:
|
||||
|
||||
self.data = data
|
||||
self.polynomial = self.Polynomial(self.data)
|
||||
self.polynomial = self.__Polynomial(self.data)
|
||||
|
||||
def minimums(self) -> Callable[[Any], float]:
|
||||
|
||||
@@ -60,7 +58,7 @@ class Interpolation:
|
||||
|
||||
return lambda x: a * x + b, r2
|
||||
|
||||
class Polynomial:
|
||||
class __Polynomial:
|
||||
|
||||
def __init__(self, data) -> None:
|
||||
self.data = data
|
||||
@@ -69,22 +67,20 @@ class Interpolation:
|
||||
|
||||
matrix = np.zeros((self.data.shape[0], self.data.shape[0]))
|
||||
|
||||
for k in range(0, self.data.shape[0]):
|
||||
matrix[:, k] = self.data.X[:] ** k
|
||||
for k in range(self.data.shape[0]):
|
||||
matrix[:, k] = self.data.X[:].copy() ** k
|
||||
|
||||
array = np.array(self.data.Y.tolist()).reshape(self.data.shape[0], 1)
|
||||
A = sl.gauss(matrix, array)
|
||||
|
||||
def f(coefficient_matrix, x):
|
||||
coefficient_matrix = sl.gauss(matrix, array)[:]
|
||||
|
||||
def __f(coefficients, x):
|
||||
y = 0
|
||||
|
||||
for i in range(0, A.shape[0]):
|
||||
y += coefficient_matrix[1][i] * (x ** i)
|
||||
for i in range(0, coefficients.shape[0]):
|
||||
y += float(coefficients[i]) * (x ** i)
|
||||
|
||||
return y
|
||||
|
||||
return lambda x: f(A, x)
|
||||
return lambda x: __f(coefficient_matrix, x)
|
||||
|
||||
def lagrange(self, x: float) -> float:
|
||||
|
||||
@@ -155,24 +151,23 @@ class Interpolation:
|
||||
|
||||
d[0] = self.data.Y
|
||||
|
||||
i = j = 0
|
||||
|
||||
i = 0
|
||||
while i < self.data.shape[0]:
|
||||
|
||||
j = 0
|
||||
while j < (self.data.shape[0] - (i + 1)):
|
||||
d[i + 1][j] = (d[i][j + 1] - d[i][j]) / ((i + 1) * h)
|
||||
j += 1
|
||||
|
||||
i += 1
|
||||
j = 0
|
||||
|
||||
y = d[0][0]
|
||||
|
||||
i = 0
|
||||
|
||||
while (i + 1) < self.data.shape[0]:
|
||||
|
||||
mult = 1
|
||||
k = 0
|
||||
|
||||
while k <= i:
|
||||
mult = mult * (x - self.data.X[k])
|
||||
k += 1
|
||||
@@ -180,4 +175,4 @@ class Interpolation:
|
||||
y += d[i + 1][0] * mult
|
||||
i += 1
|
||||
|
||||
return y
|
||||
return -y
|
||||
|
||||
Reference in New Issue
Block a user