Page MenuHomec4science

fitting.py
No OneTemporary

File Metadata

Created
Mon, Nov 18, 08:46

fitting.py

# Copyright (C) 2018 by the RROMPy authors
#
# This file is part of RROMPy.
#
# RROMPy is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# RROMPy 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with RROMPy. If not, see <http://www.gnu.org/licenses/>.
#
import numpy as np
from rrompy.utilities.poly_fitting import (customFit, polybases, polyval,
polyvalder, polyvander, polyfitname, polyroots, polydomcoeff)
def test_chebyshev():
assert "CHEBYSHEV" in polybases
val = polyval["CHEBYSHEV"]
valder = polyvalder["CHEBYSHEV"]
vander = polyvander["CHEBYSHEV"]
fitname = polyfitname["CHEBYSHEV"]
roots = polyroots["CHEBYSHEV"]
domcoeff = polydomcoeff["CHEBYSHEV"]
assert fitname == "chebfit"
assert np.isclose(domcoeff(5), 16, rtol = 1e-5)
assert np.allclose(roots((-1, 1, -1, 1)), np.array([-.5, 0., 1.]),
rtol = 1e-5)
Phi = vander(np.arange(5), 4)
y = 2. * np.arange(5)
cFit = customFit(Phi, y)
assert np.allclose(cFit, [0, 2, 0, 0, 0], rtol = 1e-5)
assert np.allclose(val(np.arange(5), cFit), y, rtol = 1e-5)
assert np.allclose(valder(np.arange(5), cFit), 2. * np.ones(5),
rtol = 1e-5)
def test_legendre():
assert "LEGENDRE" in polybases
val = polyval["LEGENDRE"]
valder = polyvalder["LEGENDRE"]
vander = polyvander["LEGENDRE"]
fitname = polyfitname["LEGENDRE"]
roots = polyroots["LEGENDRE"]
domcoeff = polydomcoeff["LEGENDRE"]
assert fitname == "legfit"
assert np.isclose(domcoeff(5), 63. / 8, rtol = 1e-5)
assert np.allclose(roots((1, 2, 3, 4)),
np.array([-0.85099543, -0.11407192, 0.51506735]),
rtol = 1e-5)
Phi = vander(np.arange(5), 4)
y = 2. * np.arange(5)
cFit = customFit(Phi, y)
assert np.allclose(cFit, [0, 2, 0, 0, 0], rtol = 1e-5)
assert np.allclose(val(np.arange(5), cFit), y, rtol = 1e-5)
assert np.allclose(valder(np.arange(5), cFit), 2. * np.ones(5),
rtol = 1e-5)
def test_monomial():
assert "MONOMIAL" in polybases
val = polyval["MONOMIAL"]
valder = polyvalder["MONOMIAL"]
vander = polyvander["MONOMIAL"]
fitname = polyfitname["MONOMIAL"]
roots = polyroots["MONOMIAL"]
domcoeff = polydomcoeff["MONOMIAL"]
assert fitname == "polyfit"
assert np.isclose(domcoeff(5), 1., rtol = 1e-5)
assert np.allclose(roots([0.+0.j, 1.+0.j, 0.+0.j, 1.+0.j]),
np.array([0., 1.j, -1.j]), rtol = 1e-5)
Phi = vander(np.arange(5), 4)
y = 2. * np.arange(5)
cFit = customFit(Phi, y)
assert np.allclose(cFit, [0, 2, 0, 0, 0], rtol = 1e-5)
assert np.allclose(val(np.arange(5), cFit), y, rtol = 1e-5)
assert np.allclose(valder(np.arange(5), cFit), 2. * np.ones(5),
rtol = 1e-5)
def test_cheb_confluence():
val = polyval["CHEBYSHEV"]
vander = polyvander["CHEBYSHEV"]
valder = polyvalder["CHEBYSHEV"]
x = np.arange(5)
x[3] = 0
Phi = vander(x, 4)
y = 2. * x
y[3] = 2.
cFit = customFit(Phi, y)
mask = np.arange(len(x)) != 3
assert np.allclose(cFit, [0, 2, 0, 0, 0], rtol = 1e-5)
assert np.allclose(val(x[mask], cFit), y[mask], rtol = 1e-5)
assert np.isclose(valder(x[~mask], cFit), y[~mask], rtol = 1e-5)

Event Timeline