Page MenuHomec4science

fitting.py
No OneTemporary

File Metadata

Created
Mon, Nov 4, 10:49

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.base import customFit
from rrompy.reduction_methods.base.fit_utils import (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)
assert np.allclose(roots((-1, 1, -1, 1)), np.array([-.5, 0., 1.]))
Phi = vander(np.arange(5), 4)
y = 2. * np.arange(5)
cFit = customFit(Phi, y)
assert np.allclose(cFit, [0, 2, 0, 0, 0])
assert np.allclose(val(np.arange(5), cFit), y)
assert np.allclose(valder(np.arange(5), cFit), 2. * np.ones(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)
assert np.allclose(roots((1, 2, 3, 4)),
np.array([-0.85099543, -0.11407192, 0.51506735]))
Phi = vander(np.arange(5), 4)
y = 2. * np.arange(5)
cFit = customFit(Phi, y)
assert np.allclose(cFit, [0, 2, 0, 0, 0])
assert np.allclose(val(np.arange(5), cFit), y)
assert np.allclose(valder(np.arange(5), cFit), 2. * np.ones(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.)
assert np.allclose(roots([0.+0.j, 1.+0.j, 0.+0.j, 1.+0.j]),
np.array([0., 1.j, -1.j]))
Phi = vander(np.arange(5), 4)
y = 2. * np.arange(5)
cFit = customFit(Phi, y)
assert np.allclose(cFit, [0, 2, 0, 0, 0])
assert np.allclose(val(np.arange(5), cFit), y)
assert np.allclose(valder(np.arange(5), cFit), 2. * np.ones(5))

Event Timeline