Source code for solrat.engine.functions.general
import math
from functools import lru_cache
[docs]
def ensure_positive_if_zero(a: float):
"""
Make sure there are no -0 floats.
"""
if a == -0.0:
return 0.0
else:
return a
[docs]
def half_int_to_str(a: float):
"""
Converts half-integer floats like 0, 0.5, 1, ... to string in a stable manner
(eliminates float artifacts)
"""
assert a % 1 in [0, 0.0, 0.5]
return str(ensure_positive_if_zero(round(float(a), 1)))
[docs]
def m1p(a):
"""
real (-1)^a
a: integer
"""
return 1 - 2 * (a % 2)
[docs]
def delta(a, b):
"""
real delta_ab
a, b: integer
"""
return 1 if a == b else 0
[docs]
@lru_cache(maxsize=None)
def fact2(a):
"""
int (a/2)!
a is doubled integer
Input integrity assumed
"""
return math.factorial(int(a) // 2)
# @lru_cache(maxsize=None)
[docs]
def n_proj(*args):
"""
n -> 2 n + 1. If multiple arguments - multiply
"""
result = 1
for arg in args:
result *= 2 * arg + 1
return result