Page MenuHomec4science

fit_utils.py
No OneTemporary

File Metadata

Created
Sat, Apr 27, 13:48

fit_utils.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/>.
#
from numpy import pi, polynomial as po
from rrompy.utilities.base.types import DictAny
from rrompy.utilities.warning_manager import warn
__all__ = ['setupFitCallables']
def setupFitCallables(kind:str) -> DictAny:
"""Setup main callables and name for data fitting."""
kind = kind.upper()
if kind == "CHEBYSHEV":
val = po.chebyshev.chebval
valder = lambda x, c: po.chebyshev.chebval(x, po.chebyshev.chebder(c))
vander = po.chebyshev.chebvander
fitname = "chebfit"
roots = po.chebyshev.chebroots
domcoeff = lambda n: 2. ** (n - 1) if n > 0 else 1.
elif kind == "LEGENDRE":
val = po.legendre.legval
valder = lambda x, c: po.legendre.legval(x, po.legendre.legder(c))
vander = po.legendre.legvander
fitname = "legfit"
roots = po.legendre.legroots
from scipy.special import binom
domcoeff = lambda n: (2. ** n * (pi * n) ** -.5 if n > 10
else .5 ** n * binom(2 * n, n))
else:
if kind != "MONOMIAL":
warn("Fitting basis not recognized. Overriding to 'MONOMIAL'.")
val = po.polynomial.polyval
valder = lambda x, c: po.polynomial.polyval(x,po.polynomial.polyder(c))
vander = po.polynomial.polyvander
fitname = "polyfit"
roots = po.polynomial.polyroots
domcoeff = lambda n: 1.
return {"val":val, "valder":valder, "vander":vander, "fitname":fitname,
"roots":roots, "domcoeff":domcoeff}

Event Timeline