-
Notifications
You must be signed in to change notification settings - Fork 0
/
libmath.py
68 lines (36 loc) · 875 Bytes
/
libmath.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
from __future__ import division
from math import *
half = 1/2
third = 1/3
twothirds = 2/3
quarter = 1/4
root_pi = sqrt(pi)
def inverse(x_):
return 1/float(x_)
def squared(x_):
return x_**2
def cubed(x_):
return x_**3
def cubed_root(x_):
return x_**third
def sgn(x_):
return copysign(1,x_)
def heaviside_step(x_):
return half*(1+sgn(x_))
def binomial(n_, k_):
return factorial(n_)/(factorial(k_)*factorial(n_-k_))
def double_binomial(n_, k1_, k2_):
return binomial(n_,k1_)*binomial(n_,k2_)
def multinomial(*klist):
n = 0
k_ = 1
for k in klist:
k_ = k_ * factorial(k)
n = n + k
return factorial(n)/k_
def factorial_approx(n_):
fact = sqrt(pi)*(n_/e)**n_
fact *= (((8*n_ + 4)*n_ + 1)*n_ + 1/30.)**(1./6.)
if isinstance(n_, int):
fact = int(fact)
return fact