Page MenuHomec4science

vander.py
No OneTemporary

File Metadata

Created
Thu, Apr 18, 14:35

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, paramList
from rrompy.parameter import checkParameterList
from rrompy.utilities.exception_manager import RROMPyException, RROMPyAssert
__all__ = ['rbvander', 'polyvander', 'polyvanderTotal']
def rbvander(x:paramList, basis:str, reorder : List[int] = None,
directionalWeights : Np1D = None,
nNearestNeighbor : int = -1) -> Np2D:
"""Compute radial-basis-Vandermonde matrix."""
x = checkParameterList(x)[0]
x_un = x.unique()
nx = len(x)
if len(x_un) < nx:
raise RROMPyException("Sample points must be distinct.")
del x_un
x = x.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.")
isnearestneighbor = basis.upper() == "NEARESTNEIGHBOR"
Van = np.zeros((nx, nx))
for j in range(nx):
muDiff = (x.data - x[j]) * directionalWeights
r2j = np.sum(np.abs(muDiff) ** 2., axis = 1).reshape(1, -1)
if isnearestneighbor:
if nNearestNeighbor > 0 and nNearestNeighbor < len(x):
cutoffValue = np.partition(r2j, nNearestNeighbor - 1)[0,
nNearestNeighbor - 1]
r2j /= cutoffValue
else:
r2j[0, :] = 1. * (nNearestNeighbor == 0)
Van[j] = radialkernel(r2j)
return Van
def polyvander(x: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) -> Np2D:
"""
Compute full Hermite-Vandermonde matrix with specified derivative
directions.
"""
if derIdxs is not None and np.sum(np.sum(derIdxs)) > 0:
raise RROMPyException(("Cannot take derivatives of radial basis "
"function."))
basisp, basisr = basis.split("_")
VanR = rbvander(x, basisr, reorder = reorder,
directionalWeights = directionalWeights,
nNearestNeighbor = nNearestNeighbor)
VanP = pvP(x, degs, basisp, derIdxs = derIdxs, reorder = reorder,
scl = scl)
return np.block([[VanR, VanP],
[VanP.T.conj(), np.zeros(tuple([VanP.shape[1]] * 2))]])
def polyvanderTotal(x: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, List[List[int]], List[int]]:
"""
Compute full total degree Hermite-Vandermonde matrix with specified
derivative directions.
"""
if derIdxs is not None and np.sum(np.sum(derIdxs)) > 0:
raise RROMPyException(("Cannot take derivatives of radial basis "
"function."))
basisp, basisr = basis.split("_")
VanR = rbvander(x, basisr, reorder = reorder,
directionalWeights = directionalWeights,
nNearestNeighbor = nNearestNeighbor)
VanP, derIdxs, ordIdxs = pvTP(x, deg, basisp, derIdxs = derIdxs,
reorder = reorder, scl = scl)
ordIdxsEff = np.concatenate((np.arange(len(VanR)), ordIdxs + len(VanR)))
return (np.block([[VanR, VanP],
[VanP.T.conj(), np.zeros(tuple([VanP.shape[1]] * 2))]]),
derIdxs, ordIdxsEff)

Event Timeline