Page MenuHomec4science

vander.py
No OneTemporary

File Metadata

Created
Sun, Apr 28, 11:55

vander.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 .kernel import (radialGaussian, thinPlateSpline, multiQuadric,
nearestNeighbor)
from rrompy.utilities.poly_fitting.polynomial.vander import (polyvander as pvP,
polyvanderTotal as pvTP)
from rrompy.utilities.base.types import (Np1D, Np2D, Tuple, List, paramVal,
paramList)
from rrompy.parameter import checkParameter, checkParameterList
from rrompy.utilities.exception_manager import RROMPyException, RROMPyAssert
__all__ = ['mlsweights', 'polyvander', 'polyvanderTotal']
def mlsweights(x:paramVal, xSupp:paramList, basis:str,
reorder : List[int] = None, directionalWeights : Np1D = None,
nNearestNeighbor : int = -1) -> Np2D:
"""Compute moving least squares weight vector."""
x = checkParameter(x)
xSupp = checkParameterList(xSupp)[0]
x = x.data
xSupp = xSupp.data
if directionalWeights is None:
directionalWeights = np.ones(x.shape[1])
elif not hasattr(directionalWeights, "__len__"):
directionalWeights = directionalWeights * np.ones(x.shape[1])
RROMPyAssert(len(directionalWeights), x.shape[1],
"Number of directional weights")
try:
radialkernel = {"GAUSSIAN" : radialGaussian,
"THINPLATE" : thinPlateSpline,
"MULTIQUADRIC" : multiQuadric,
"NEARESTNEIGHBOR" : nearestNeighbor}[basis.upper()]
except:
raise RROMPyException("Radial basis not recognized.")
if reorder is not None: xSupp = xSupp[reorder]
muDiff = (xSupp.data - x) * directionalWeights
r2 = np.sum(np.abs(muDiff) ** 2., axis = 1).reshape(1, -1)
if basis.upper() == "NEARESTNEIGHBOR":
if nNearestNeighbor > 0 and nNearestNeighbor < len(xSupp):
cutoffValue = np.partition(r2, nNearestNeighbor - 1)[0,
nNearestNeighbor - 1]
r2 /= cutoffValue
else:
r2[0, :] = 1. * (nNearestNeighbor == 0)
return radialkernel(r2)[0]
def polyvander(x:paramVal, xSupp:paramList, degs:List[int], basis:str,
derIdxs : List[List[List[int]]] = None,
reorder : List[int] = None, directionalWeights : Np1D = None,
scl : Np1D = None,
nNearestNeighbor : int = -1) -> Tuple[Np2D, Np2D]:
"""
Compute full Hermite-Vandermonde matrix with specified derivative
directions.
"""
basisp, basisr, _ = basis.split("_")
Weights = mlsweights(x, xSupp, basisr, reorder, directionalWeights,
nNearestNeighbor)
VanP = pvP(xSupp, degs, basisp, derIdxs = derIdxs, reorder = reorder,
scl = scl)
RHP = VanP.T.conj() * Weights
return RHP.dot(VanP), RHP
def polyvanderTotal(x:paramList, xSupp:paramList, deg:int, basis:str,
derIdxs : List[List[List[int]]] = None,
reorder : List[int] = None,
directionalWeights : Np1D = None, scl : Np1D = None,
nNearestNeighbor : int = -1)\
-> Tuple[Np2D, Np2D, List[List[int]], List[int]]:
"""
Compute full total degree Hermite-Vandermonde matrix with specified
derivative directions.
"""
basisp, basisr, _ = basis.split("_")
Weights = mlsweights(x, xSupp, basisr, reorder, directionalWeights,
nNearestNeighbor)
VanP, derIdxs, ordIdxs = pvTP(x, deg, basisp, derIdxs = derIdxs,
reorder = reorder, scl = scl)
RHP = VanP.T.conj() * Weights
return RHP.dot(VanP), RHP, derIdxs, ordIdxs

Event Timeline