-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
162 lines (134 loc) · 5.28 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import logging
from enum import StrEnum
import math
import cmath
from typing import Any, Callable, Iterator
from vectors import Vector, LorentzVector
try:
from symbolica import Sample
except ImportError:
pass
class Colour(StrEnum):
PURPLE = '\033[95m'
CYAN = '\033[96m'
DARKCYAN = '\033[36m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
END = '\033[0m'
logging.basicConfig(
format=f'{Colour.GREEN}%(levelname)s{Colour.END} {Colour.BLUE}%(funcName)s l.%(lineno)d{Colour.END} {Colour.CYAN}t=%(asctime)s.%(msecs)03d{Colour.END} > %(message)s',
datefmt='%Y-%m-%d,%H:%M:%S'
)
logger = logging.getLogger('Triangler')
TOLERANCE: float = 1e-10
def rsqrt(arg: complex, is_plus: bool) -> complex:
if abs(arg.imag) > 0.:
return cmath.sqrt(arg)
else:
if arg.real > 0.:
return complex(math.sqrt(arg.real), 0.)
else:
if is_plus:
return complex(math.sqrt(arg.real), 0.)
else:
return complex(-math.sqrt(arg.real), 0.)
def rlog(arg: complex, is_plus: bool) -> complex:
if abs(arg.imag) > 0.:
return cmath.log(arg)
else:
if arg.real > 0.:
return complex(math.log(arg.real), 0.)
else:
if is_plus:
return complex(math.log(-arg.real), math.pi)
else:
return complex(math.log(-arg.real), -math.pi)
class ThermostatException(Exception):
pass
def chunks(a_list: list[Any], n: int) -> Iterator[list[Any]]:
"""Yield successive n-sized chunks from lst."""
for i in range(0, len(a_list), n):
yield a_list[i:i + n]
class SymbolicaSample(object):
def __init__(self, sample: Sample):
self.c: list[float] = sample.c
self.d: list[int] = sample.d
class Topology(object):
RESCALING: float = 1.0
def __init__(self, scale: float, *args, **opts):
self.scale = scale
def integrand_xspace(self, xs: list[float], parameterization: str, integrand_implementation: str, improved_ltd: bool = False, multi_channeling: bool | int = True) -> float:
raise NotImplementedError("Abstract method not implemented.")
def integrand(self, loop_momentum: Vector, integrand_implementation: str, improved_ltd: bool = False) -> float:
raise NotImplementedError("Abstract method not implemented.")
def parameterize(self, xs: list[float], parameterisation: str, origin: Vector | None = None) -> tuple[Vector, float]:
match parameterisation:
case 'cartesian': return self.cartesian_parameterize(xs, origin)
case 'spherical': return self.spherical_parameterize(xs, origin)
case _: raise ThermostatException(f'Parameterisation {parameterisation} not implemented.')
def cartesian_parameterize(self, xs: list[float], origin: Vector | None = None) -> tuple[Vector, float]:
return self.cartesian_parameterize_v3(xs, origin)
def cartesian_parameterize_v1(self, xs: list[float], origin: Vector | None = None) -> tuple[Vector, float]:
x, y, z = xs
scale = self.scale * self.RESCALING
v = Vector(
(1/(1-x)-1/x),
(1/(1-y)-1/y),
(1/(1-z)-1/z)
)*scale
if origin is not None:
v = v + origin
jac = scale * (1/(1-x)**2+1/x**2)
jac *= scale * (1/(1-y)**2+1/y**2)
jac *= scale * (1/(1-z)**2+1/z**2)
return (v, jac)
def cartesian_parameterize_v2(self, xs: list[float], origin: Vector | None = None) -> tuple[Vector, float]:
x, y, z = xs
scale = self.scale * self.RESCALING
v = Vector(
math.tan((x-0.5)*math.pi),
math.tan((y-0.5)*math.pi),
math.tan((z-0.5)*math.pi),
)*scale
if origin is not None:
v = v + origin
jac = scale * math.pi / math.cos((x-0.5)*math.pi)**2
jac *= scale * math.pi / math.cos((y-0.5)*math.pi)**2
jac *= scale * math.pi / math.cos((z-0.5)*math.pi)**2
return (v, jac)
def cartesian_parameterize_v3(self, xs: list[float], origin: Vector | None = None) -> tuple[Vector, float]:
x, y, z = xs
scale = self.scale * self.RESCALING
v = Vector(
math.log(x)-math.log(1-x),
math.log(y)-math.log(1-y),
math.log(z)-math.log(1-z),
)*scale
if origin is not None:
v = v + origin
jac = scale * (1 / x + 1 / (1-x))
jac *= scale * (1 / y + 1 / (1-y))
jac *= scale * (1 / z + 1 / (1-z))
return (v, jac)
def spherical_parameterize(self, xs: list[float], origin: Vector | None = None) -> tuple[Vector, float]:
rx, costhetax, phix = xs
scale = self.scale * self.RESCALING
r = rx / (1 - rx) * scale
# r = math.log(1. / (1. - rx)) * scale
costheta = (0.5-costhetax)*2
sintheta = math.sqrt(1-costheta**2)
phi = phix * 2 * math.pi
v = Vector(
r * sintheta * math.cos(phi),
r * sintheta * math.sin(phi),
r * costheta
)
if origin is not None:
v = v + origin
jac = 2 * (2 * math.pi) * (r**2 * scale / (1-rx)**2)
# jac = 2 * (2 * math.pi) * (r**2 * scale / (1-rx))
return (v, jac)