Page MenuHomec4science

monomial_creator.py
No OneTemporary

File Metadata

Created
Wed, Jul 31, 22:33

monomial_creator.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.numerical.factorials import multibinom
from rrompy.utilities.numerical.hash_derivative import (nextDerivativeIndices,
hashIdxToDerivative as hashI,
hashDerivativeToIdx as hashD)
from rrompy.utilities.base.types import List, TupleAny
__all__ = ["createMonomial", "createMonomialList"]
def createMonomial(deg:List[int],
return_derivatives : bool = False) -> List[List[TupleAny]]:
if not hasattr(deg, "__len__"): deg = [deg]
dim = len(deg)
degj = hashD(deg)
expr = []
for k in range(degj * return_derivatives + 1):
degder = hashI(k, dim)
derdiff = [x - y for (x, y) in zip(deg, degder)]
if all([d >= 0 for d in derdiff]):
mult = multibinom(deg, degder)
if np.sum(derdiff) == 0:
exprLoc = (mult,)
else:
exprLoc = ("prod", {"axis" : 1}, ("data", "x", "**", derdiff))
if not np.isclose(mult, 1):
exprLoc = exprLoc + ("*", mult,)
expr += [exprLoc]
else:
expr += [(0.,)]
if return_derivatives: expr += [None]
return expr
def createMonomialList(n:int, dim:int,
return_derivatives : bool = False) -> List[List[TupleAny]]:
derIdxs = nextDerivativeIndices([], dim, n)
idxList = []
for j, der in enumerate(derIdxs):
idxList += [createMonomial(der, return_derivatives)]
return idxList

Event Timeline