-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasics.py
More file actions
38 lines (30 loc) · 867 Bytes
/
basics.py
File metadata and controls
38 lines (30 loc) · 867 Bytes
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
import numpy as np
from math import log
def Eta(x):
''' Implements the function eta(x) = - x * ln(x) with eta(0) = 0 '''
return -x * log(x, 2) if x > 0 else 0.0
def Multinomial(lst):
# Source: https://stackoverflow.com/questions/46374185/does-python-have-a-function-which-computes-multinomial-coefficients
res, i = 1, 1
for a in lst:
for j in range(1, a + 1):
res *= i
res //= j
i += 1
return res
def Multinomial_NP(array):
# Adapted from Multinomial
res, i = 1, 1
for a in np.nditer(array):
for j in range(1, a + 1):
res *= i
res //= j
i += 1
return res
def kPartitions(n, k):
if k > 1:
for i in range(n + 1):
for p in kPartitions(n - i, k - 1):
yield p + (i,)
else:
yield (n,)