Page MenuHomec4science

vander.py
No OneTemporary

File Metadata

Created
Thu, Jun 6, 07:11

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 rrompy.utilities.poly_fitting.polynomial import polyder
from rrompy.utilities.base.types import Np2D, List, paramList
from rrompy.parameter import checkParameterList
from rrompy.utilities.exception_manager import RROMPyException
__all__ = ['polyvander']
#def polyvanderconfluence(x:Np1D, deg:int, basis:str,
# scl : float = None) -> Np2D:
# """Compute Vandermonde matrix even in case of confluence."""
## does not work with parameterList
# x_un, idx_un, cnt_un = np.unique(x, return_inverse = True,
# return_counts = True)
# Van = polyvander(x, deg, basis)
# der_max = np.max(cnt_un) - 1
# if der_max > 0: # must have square-like structure
# C_der = np.zeros((deg + 1, deg + 1), dtype = float)
# for j in range(deg + 1):
# ej = np.zeros(deg + 1)
# ej[j] = 1.
# j_der = polyder(ej, basis, 1, scl)
# C_der[: len(j_der), j] = j_der
# for der in range(1, der_max + 1):
# # remove first occurrence of each node
# for i_un in np.nonzero(cnt_un > der - 1)[0]:
# idx_un[np.nonzero(idx_un == i_un)[0][0]] = -1
# idx_loc = np.nonzero(idx_un > -1)[0]
# Van[idx_loc, :] = Van[idx_loc, :].dot(C_der[:, :]) / der
# return Van
def polyvander(x:paramList, degs:List[int], basis:str) -> Np2D:
if not isinstance(degs, (list,tuple,np.ndarray,)): degs = [degs]
ideg = [int(d) for d in degs]
is_valid = [id == d and id >= 0 for id, d in zip(ideg, degs)]
dim = len(ideg)
if is_valid != [1] * dim:
raise RROMPyException("Degrees must be non-negative integers")
x, wasPar = checkParameterList(x, dim)
try:
vanderbase = {"CHEBYSHEV" : np.polynomial.chebyshev.chebvander,
"LEGENDRE" : np.polynomial.legendre.legvander,
"MONOMIAL" : np.polynomial.polynomial.polyvander
}[basis.upper()]
except:
raise RROMPyException("Polynomial basis not recognized.")
v = vanderbase(x(0), ideg[0])
for j, dj in enumerate(ideg[1:]):
v = v[..., None] * vanderbase(x(j + 1), dj)[..., None, :]
return v.reshape(v.shape[:-dim] + (-1,))

Event Timeline