Page MenuHomec4science

hash_derivative.py
No OneTemporary

File Metadata

Created
Sat, Feb 22, 01:14

hash_derivative.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/>.
#
from scipy.special import binom
from rrompy.utilities.base.types import List
__all__ = ['hashDerivativeToIdx', 'hashIdxToDerivative']
def shellCount(shell:int, dim:int) -> int:
return int(binom(shell + dim, dim))
def hashDerivativeToIdx(derIdx:List[int]) -> int:
dim = len(derIdx)
if dim == 0: return 0
derMag = sum(derIdx)
base = shellCount(derMag - 1, dim)
if derMag == derIdx[0]: return base
return base + hashDerivativeToIdx(derIdx[1:])
def hashIdxToDerivative(n:int, dim:int) -> List[int]:
if n == 0: return [0] * dim
shell = 0
shellOld = -1
shellNew = 1
while shellNew <= n:
shell += 1
shellOld = shellNew
shellNew = shellCount(shell, dim)
rest = hashIdxToDerivative(n - shellOld, dim - 1)
return [shell - sum(rest)] + rest

Event Timeline