Page MenuHomec4science

homogeneization.py
No OneTemporary

File Metadata

Created
Sat, Feb 22, 05:01

homogeneization.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.base.types import Np1D, Np2D, Tuple, List, paramList
from rrompy.utilities.poly_fitting.polynomial import (polyvander,
hashIdxToDerivative as hashI)
from rrompy.parameter import checkParameterList
__all__ = ['homogeneizationMask', 'homogeneizedpolyvander',
'homogeneizedToFull']
def homogeneizationMask(degs:List[int]) -> Tuple[List[int], List[bool]]:
dim = len(degs)
N = np.prod([d + 1 for d in degs])
maskN = np.arange(N, dtype = int)
remN = np.zeros((N, dim), dtype = int)
for j in range(dim - 1, -1, -1):
remN[:, j] = maskN % (degs[j] + 1)
maskN //= degs[j] + 1
mask = np.sum(remN, axis = 1) <= min(degs)
return remN[mask], mask
def homogeneizedpolyvander(x:paramList, deg:int, basis:str,
derIdxs : List[List[List[int]]] = None,
reorder : List[int] = None, scl : Np1D = None)\
-> Tuple[Np2D, List[List[int]], List[int]]:
x = checkParameterList(x)[0]
degs = [deg] * x.shape[1]
VanBase = polyvander(x, degs, basis, derIdxs, reorder, scl)
derIdxs, mask = homogeneizationMask(degs)
ordIdxs = np.empty(len(derIdxs), dtype = int)
derTotal = np.array([np.sum(y) for y in derIdxs])
idxPrev = 0
rangeAux = np.arange(len(derIdxs))
for j in range(deg + 1):
idxLocal = rangeAux[derTotal == j][::-1]
idxPrev += len(idxLocal)
ordIdxs[idxPrev - len(idxLocal) : idxPrev] = idxLocal
return VanBase[:, mask], derIdxs, ordIdxs
def homogeneizedToFull(shapeFull:Tuple[int], dim:int, coeffs:Np2D) -> Np2D:
full = np.zeros(shapeFull, dtype = coeffs.dtype)
for j in range(len(coeffs)):
full[tuple(hashI(j, dim))] = coeffs[j]
return full

Event Timeline