Page MenuHomec4science

poly_fitting.py
No OneTemporary

File Metadata

Created
Thu, May 16, 19:24

poly_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
from rrompy.utilities.poly_fitting.polynomial import (polybases, polyfitname,
polydomcoeff, polyval,
polyroots, polyvander)
from rrompy.utilities.numerical import nextDerivativeIndices
from rrompy.parameter import checkParameterList
def test_chebyshev():
assert "CHEBYSHEV" in polybases
fitname = polyfitname("CHEBYSHEV")
domcoeff = polydomcoeff(5, "CHEBYSHEV")
assert fitname == "chebfit"
assert np.isclose(domcoeff, 16, rtol = 1e-5)
assert np.allclose(polyroots((-1, 1, -1, 1), "CHEBYSHEV"),
np.array([-.5, 0., 1.]), rtol = 1e-5)
Phi = polyvander(np.arange(5), 4, "CHEBYSHEV")
y = 2. * np.arange(5)
cFit = customFit(Phi, y)
assert np.allclose(cFit, [0, 2, 0, 0, 0], rtol = 1e-5)
assert np.allclose(polyval(np.arange(5), cFit, "CHEBYSHEV"), y,
rtol = 1e-5)
assert np.allclose(polyval(np.arange(5), cFit, "CHEBYSHEV", m = 1),
2. * np.ones(5), rtol = 1e-5)
def test_legendre():
assert "LEGENDRE" in polybases
fitname = polyfitname("LEGENDRE")
domcoeff = polydomcoeff([0, 5], "LEGENDRE")
assert fitname == "legfit"
assert np.allclose(domcoeff, [1., 63. / 8], rtol = 1e-5)
assert np.allclose(polyroots((1, 2, 3, 4), "LEGENDRE"),
np.array([-0.85099543, -0.11407192, 0.51506735]),
rtol = 1e-5)
Phi = polyvander(np.arange(5), 4, "LEGENDRE")
y = 2. * np.arange(5)
cFit = customFit(Phi, y)
assert np.allclose(cFit, [0, 2, 0, 0, 0], rtol = 1e-5)
assert np.allclose(polyval(np.arange(5), cFit, "LEGENDRE"), y, rtol = 1e-5)
assert np.allclose(polyval(np.arange(5), cFit, "LEGENDRE", m = 1),
2. * np.ones(5), rtol = 1e-5)
def test_monomial():
assert "MONOMIAL" in polybases
fitname = polyfitname("MONOMIAL")
domcoeff = polydomcoeff(5, "MONOMIAL")
assert fitname == "polyfit"
assert np.isclose(domcoeff, 1., rtol = 1e-5)
assert np.allclose(polyroots([0.+0.j, 1.+0.j, 0.+0.j, 1.+0.j], "MONOMIAL"),
np.array([0., 1.j, -1.j]), rtol = 1e-5)
Phi = polyvander(np.arange(5), 4, "MONOMIAL")
y = 2. * np.arange(5)
cFit = customFit(Phi, y)
assert np.allclose(cFit, [0, 2, 0, 0, 0], rtol = 1e-5)
assert np.allclose(polyval(np.arange(5), cFit, "MONOMIAL"), y, rtol = 1e-5)
assert np.allclose(polyval(np.arange(5), cFit, "MONOMIAL", m = 1),
2. * np.ones(5), rtol = 1e-5)
def test_cheb_confluence():
x = np.arange(5)
x = np.delete(x, 3)
Phi = polyvander(x, 4, "CHEBYSHEV", [[0, 1]] + [[0]] * 3,
reorder = [0, 2, 3, 1, 4])
y = 2. * np.arange(5)
y[3] = 2.
cFit = customFit(Phi, y)
mask = np.arange(len(y)) != 3
assert np.allclose(cFit, [0, 2, 0, 0, 0], rtol = 1e-5)
assert np.allclose(polyval(x, cFit, "CHEBYSHEV"), y[mask],
rtol = 1e-5)
assert np.allclose(polyval(x[0], cFit, "CHEBYSHEV", m = 1), y[~mask],
rtol = 1e-5)
def test_mon_confluence_2d():
x, _ = checkParameterList([[0, 0], [1, 1], [-1, -1]])
y = np.array([3., 5., 1., 2., 12., -2.]).reshape((-1, 1)) # 3+y+5x+2xy+x2y
Phi = polyvander(x, [2, 1], "MONOMIAL",
[[[0, 0], [1, 0], [0, 1], [1, 1]]] + [[[0, 0]]] * 2)
cFit = customFit(Phi, y).reshape((3, 2))
mask = np.array([0, 4, 5])
assert np.allclose(cFit.flatten(), [3, 1, 5, 2, 0, 1], atol = 1e-5)
assert np.allclose(polyval(x, cFit, "MONOMIAL").flatten(),
y[mask].flatten(), rtol = 1e-5)
assert np.allclose(polyval([x[0]], cFit, "MONOMIAL", m = [1, 0]), y[1],
rtol = 1e-5)
assert np.allclose(polyval([x[0]], cFit, "MONOMIAL", m = [0, 1]), y[2],
rtol = 1e-5)
assert np.allclose(polyval([x[0]], cFit, "MONOMIAL", m = [1, 1]), y[3],
rtol = 1e-5)
def test_derivative_indices_4d():
idxs = nextDerivativeIndices([], 4, 70)
idxMag = [np.sum(idx) for idx in idxs]
idxMagUnique, idxMagCount = np.unique(idxMag, return_counts = True)
idxMagCount = np.cumsum(idxMagCount)
assert np.allclose(idxMagUnique, np.arange(5), atol = 1e-10)
assert np.allclose(idxMagCount, [1, 5, 15, 35, 70], atol = 1e-10)

Event Timeline