Page MenuHomec4science

degree.py
No OneTemporary

File Metadata

Created
Sun, Dec 1, 00:46

degree.py

# Copyright (C) 2018-2020 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 scipy.special import binom
from rrompy.utilities.base.types import Np2D, Tuple, List
from .kroneckerer import kroneckerer
from rrompy.utilities.exception_manager import RROMPyException
__all__ = ['degreeToDOFs', 'reduceDegreeMN', 'reduceDegreeN', 'degreeSet',
'mapTotalToTensor']
def checkPrimaryDegree(degs:List[int]):
idx = np.where(np.array(degs) != degs[0])[0]
if len(idx) not in [0, len(degs) - 1]:
raise RROMPyException("Primary degree must be first entry.")
def degreeToDOFs(degs:List[int], degtype:str) -> int:
npar = len(degs)
if npar < 1: return 0
if npar < 2: return 1 + degs[0] * npar
checkPrimaryDegree(degs)
deg, degS = degs[0], degs[1]
degtype = degtype.upper().strip().replace(" ","")
if degtype == "TENSOR": return (1 + deg) * (1 + degS) ** (npar - 1)
if degtype == "TOTAL":
tot = 0
for j in range(min(npar, deg // max(1, degS + 1) + 1)):
tot += ((1 - 2 * (j % 2)) * int(binom(npar - 1, j))
* int(binom(deg - (degS + 1) * j + npar, npar)))
return tot
raise RROMPyException("Degree type not recognized.")
def reduceDegreeMN(degM:int, degN:int, S:int, npar:int, degtype:str,
degMS : int = None, degNS : int = None,
weight : float = 1., Mfirst : bool = 1) -> int:
if "_" not in degtype: degtype = "{0}_{0}".format(degtype)
degtypeM, degtypeN = degtype.split("_")
if degMS is None: degMS = degM
if degNS is None: degNS = degN
degsM, degsN = [degMS] * (npar - 1), [degNS] * (npar - 1)
dM = degreeToDOFs([degM] + degsM, degtypeM) - 1
dN = degreeToDOFs([degN] + degsN, degtypeN) - 1
while degM > 0 or degN > 0:
if Mfirst:
if S >= dM + dN / weight + 1: break
if degM > 0:
degM -= 1
dM = degreeToDOFs([degM] + degsM, degtypeM) - 1
Mfirst = 1
if S >= dM + dN / weight + 1: break
if degN > 0:
degN -= 1
dN = degreeToDOFs([degN] + degsN, degtypeN) - 1
return degM, degN
def reduceDegreeN(deg:int, S:int, npar:int, degtype:str,
degS : int = None) -> int:
if degS is None: degS = deg
degs = [degS] * (npar - 1)
while deg > 0 and S < degreeToDOFs([deg] + degs, degtype): deg -= 1
return deg
def degreeSet(degs:List[int], degtype:str,
return_mask : bool = False) -> List[List[int]]:
npar = len(degs)
checkPrimaryDegree(degs)
degtype = degtype.upper().strip().replace(" ","")
if degtype == "TENSOR":
idxs = np.empty((degreeToDOFs(degs, degtype), npar), dtype = int)
for j in range(npar):
nL = degreeToDOFs(degs[: j], "TENSOR") if j else 1
nR = degreeToDOFs(degs[j + 1 :], "TENSOR") if npar - 1 - j else 1
idxs[:, j] = kroneckerer(np.arange(degs[j] + 1), nL, nR)
if return_mask: return idxs, np.ones(len(idxs), dtype = bool)
return idxs
if degtype == "TOTAL":
idxs, mask = degreeSet(degs, "TENSOR", True)
if npar > 1:
mask = np.sum(idxs, axis = 1) <= degs[0]
idxs = idxs[mask]
if return_mask: return idxs, mask
return idxs
raise RROMPyException("Degree type not recognized.")
def mapTotalToTensor(shapeTensor:Tuple[int], npar:int, coeffs:Np2D) -> Np2D:
if npar < 2: return coeffs.reshape(shapeTensor)
checkPrimaryDegree(shapeTensor[: npar])
from .hash_derivative import hashIdxToDerivative as hashI
degS = shapeTensor[1] - 1
mask = np.arange(npar) != 0
full = np.zeros(shapeTensor, dtype = coeffs.dtype)
ij, j = 0, 0
while j < len(coeffs):
idx = np.array(hashI(ij, npar))
if np.sum(idx) >= shapeTensor[0]:
raise RROMPyException("Too many coefficients for prescribed size.")
if np.max(idx[mask]) <= degS:
full[tuple(idx)] = coeffs[j]
j += 1
ij += 1
return full

Event Timeline