Page MenuHomec4science

base.py
No OneTemporary

File Metadata

Created
Wed, May 29, 21:31
# 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.types import Np1D, Tuple
from rrompy.utilities.exception_manager import RROMPyException
__all__ = ['sparsekinds', 'sparseMap']
sparsekinds = ["PIECEWISE_LINEAR_" + k for k in ["UNIFORM", "CLENSHAWCURTIS"]]
def centerNormalize(x:Np1D, lims:Tuple[np.complex, np.complex],
forward : bool = True) -> Np1D:
"""If forward, x in [-1, 1] -> y in lims. Otherwise, the opposite."""
center, width = .5 * (lims[0] + lims[-1]), .5 * (lims[-1] - lims[0])
if forward: return width * x + center
return np.real((x - center) / width)
def sparseMap(x:Np1D, lims:Tuple[np.complex, np.complex], kind:str,
forward : bool = True) -> Np1D:
"""If forward, x in [-1, 1] -> y in lims. Otherwise, the opposite."""
kind = kind.upper().strip().replace(" ", "").split("_")[-1].split("-")[0]
if kind == "UNIFORM":
return centerNormalize(x, lims, forward)
elif kind == "CLENSHAWCURTIS":
if forward:
x0 = np.cos(.5 * np.pi * (1. - x))
return centerNormalize(x0, lims, forward)
x0 = centerNormalize(x, lims, forward)
return 1. - 2. / np.pi * np.arccos(np.clip(x0, -1., 1.))
else:
raise RROMPyException("Sparse map kind not recognized.")

Event Timeline