-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
281 lines (226 loc) · 7.98 KB
/
functions.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
"""A simple python library consisting of combinatorics functions
These functions are some of the most common functions used in combinatorics.
They are not intended to be extreemly performant but they should be accurate.
"""
import typing
from typing import Literal
def P(n: int, r: int) -> int:
"""
n permutation r
counts the number of ways to arrange r elements from a set of n distinct elements
args:
n: number of elements in set
r: number of elements in permutation
"""
if n < 0 or r < 0:
raise ValueError(f"n and r must be non-negative")
ans = 1
for i in range(n - r + 1, n + 1):
ans *= i
return ans
def C(n: int, r: int) -> int:
"""
n choose r
counts the number of ways to choose r elements from a set of n distinct elements
args:
n: number of elements in set (must be non-negative)
r: number of elements in combination (may be negative)
"""
if r < 0:
raise ValueError("r must be non-negative")
if n < 0:
return C(-n + r - 1, r) * (-1) ** r
return P(n, r) // fact(r)
def fact(n: int) -> int:
"""
calculates the factorial of n, n!
args:
n: number to calculate factorial of (must be non-negative)
"""
return P(n, n)
def multinomial(n: int, *args: int) -> int:
"""
multinomial coefficient: calculates the number of distinct ways to arange n elements where each value in args is a group of identical elements
args:
n: number of elements to arrange (must be non-negative)
args: array storing size of groups of identical elements (must be non-negative)
"""
if n < 0 or any(each < 0 for each in args):
raise ValueError("all arguments must be non-negative")
if sum(args) > n:
raise ValueError("sum of args must not exceed n")
den = 1
for k in args:
den *= fact(k)
return fact(n) // den
def stirling1(n: int, k: int) -> int:
"""
Stirling cycle number or Stirling number of the first kind: counts # of ways to arrange n distinct elements into k distinct cycles
args:
n: number of elements to arrange (must be non-negative)
k: number of cycles (must be non-negative)
Example: stirling1(3,2) == 3
for example. If you have 6 people and 3 three tables,
there are sterling1(6,3) unique ways for them to be seated assuming
that all tables are identical, every table has at least 1 person sitting at it, and that we do not want to count rotations or reflections"""
if n < 0:
raise ValueError("n must be positive")
if k > n:
raise ValueError("k must be in the range [0,n]")
if n == k == 0:
return 0
def __S1(n, k):
if n == k == 0:
return 1
elif n == 0 or k == 0:
return 0
return -(n - 1) * __S1(n - 1, k) + __S1(n - 1, k - 1)
ans = __S1(n, k)
return ans if ans >= 0 else -ans
def stirling2(n: int, k: int) -> int:
"""
Stirling partition number or stirling number of the second kind:
counts # of ways to partition n distinct elements into k non-empty groups
args:
n: number of elements to partition (must be non-negative)
k: number of groups (must be non-negative)
Example: stirling2(3,2) == 3
If we have 3 unique people and 2 teams to put them on,
there are stirling2(3,2) unique ways to put them on teams
"""
ans = 0
for i in range(k + 1):
ans += (-1) ** i * C(k, i) * (k - i) ** n
return ans // fact(k)
def bell(n: int) -> int:
"""
Bell number: counts the number of ways to partition a set of size n into subsets
args:
n: number of elements to partition (must be non-negative)
calculates the number of nonempty subsets a set of size 'n' can
be partitioned into"""
if n < 0:
raise ValueError("n must be non-negative")
ans = 0
for k in range(1, n + 1):
ans += stirling2(n, k)
return ans if ans != 0 else 1
def ordered_bell(n: int) -> int:
"""
Ordered Bell number: counts the number of ways to partition a set of size n into weakly ordered subsets
args:
n: number of elements to partition (must be non-negative)
counts the number of permutations of non-empty sets that a set of size 'n' can be partitioned into"""
if n < 0:
raise ValueError("n must be non-negative")
ans = 0
for k in range(1, n + 1):
ans += fact(k) * stirling2(n, k)
return ans if ans != 0 else 1
# should i just delete this?
def binomial_theorem(a, b, n) -> int:
"""
binomial_theorem(a,b,n)
(a+b)**n
"""
ans = 0
for i in range(n + 1):
ans += C(n, i) * a ** (n - i) * b ** (i)
return ans
def stars_bars(n: int, k: int) -> int:
"""
stars_bars aka. multi-choose equal to C(n+k-1,k-1)
args:
n: number of identical items
k: number of buckets to place them in
Where n is the number of identical items and k is tne number of buckets to place them in
"""
return C(n + k - 1, k - 1)
def catalan(n: int) -> int:
"""
catalan number C(n) = (2n)!/(n+1)!n!
args:
n: number of elements to partition (must be non-negative)
calculates the nth catalan number.
catalan numbers appear in many counting problems including Dyck words of length 2n and number of structurally unique BSTs of size n"""
return fact(2 * n) // (fact(n + 1) * fact(n))
def paths_in_matrix(m: int, n: int) -> int:
"""
number of paths from one corner to the other in a mXn matrix
args:
m: number of rows in matrix
n: number of columns number of columns in matrix
calculates the number of shortest paths from one corner to
the other in a mXn matrix
"""
return C(n + m - 2, n - 1)
def pbinom(n: int, r: int, p: float = 0.5,
type: Literal["gt", "ge", "eq", "ne", "lt", "le"] = "eq") -> float:
"""probability of getting r successes in n attempts with success probability p
args:
n: number of trials
r: number of successes
p: probability of success
type: type of probability to calculate, must be one of ["gt", "ge", "eq", "ne", "lt", "le"]
"""
type = type.lower()
ans = 0
if r < 0:
if (
type == "gt"
or type == "greater_than"
or type == "ge"
or type == "greater_than_or_equal"
or type == "not_equal"
or type == "ne"
):
return 1
return 0
if r > n:
if (
type == "lt"
or type == "less_than"
or type == "le"
or type == "less_than_or_equal"
or type == "not_equal"
or type == "ne"
):
return 1
return 0
if p < 0 or p > 1:
raise ValueError(f"p must be in range [0,1]")
if type == "equal" or type == "eq":
return C(n, r) * p**r * (1 - p) ** (n - r)
elif type == "less_than" or type == "lt":
for i in range(r):
ans += C(n, i) * p**i * (1 - p) ** (n - i)
return ans
elif type == "less_than_or_equal" or type == "le":
for i in range(r + 1):
ans += C(n, i) * p**i * (1 - p) ** (n - i)
return ans
elif type == "greater_than" or type == "gt":
for i in range(r + 1, n + 1):
ans += C(n, i) * p**i * (1 - p) ** (n - i)
return ans
elif type == "greater_than_or_equal" or type == "ge":
for i in range(r, n + 1):
ans += C(n, i) * p**i * (1 - p) ** (n - i)
return ans
elif type == "not_equal" or type == "ne":
return 1 - (C(n, r) * p**r * (1 - p) ** (n - r))
else:
raise ValueError(f"type must be one of ['gt', 'ge', 'eq', 'ne', 'lt', 'le']")
def derangments(n: int) -> int:
"""
number of derangements of n elements
args:
n: number of elements to derange (must be non-negative)
calculates the number of derangements of n elements
"""
if n < 0:
raise ValueError("n must be non-negative")
s = 0
for i in range(n + 1):
s += (-1) ** i / fact(i)
return round(fact(n) * s)