Repository navigation
Expand file tree
/
Copy pathnumerical integral.py
More file actions
107 lines (87 loc) · 3.52 KB
/
Copy pathnumerical integral.py
File metadata and controls
107 lines (87 loc) · 3.52 KB
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
import math
# A non-rigorous evaluation of c_0, by explicitly computing $\lfloor y \rfloor \log(\lceil y/e \rceil / (y/e)) dy/y^2$ on pieces up to some
# threshold $b$, then using e/2b as an estimate for the remainder (the rationale being that for large $y$ one has $\log(\lceil y/e \rceil / (y/e)) \approx \{ y/e\}/(y/e)$, that $\lfloor y \rfloor \approx y$, and $latex \{y/e\}$ is approximately $1/2$ on the average, so that the tail is heuristically $\approx \int_b^\infty y \frac{1}{2} / (y/e) dy/y^2 = e/2b$)
def compute_c0():
sum = math.pi**2 / 6 # subtract 1/k^2 from sum to accelerate convergence
for k in range(1,200000):
sum += (math.log(1 + 1/k))**2 - 1/k**2
if k % 1000 == 0:
print(f"After {k} iterations, initial sum is known to be in: [{(sum-1/(2*k*k))/(2*math.e)},{sum/(2*math.e)}]")
# This is the main portion of the integral; one then has to subtract off a fairly rapidly convergent integral
integral = sum/(2*math.e) + 2 * math.exp(-2) - math.log(2) / (2 * math.e)
a = math.e
N = 2
M = 2
for i in range(50000):
if N+1 < math.e * M:
b = N+1
N_new = N+1
M_new = M
else:
b = math.e * M
N_new = N
M_new = M+1
integral += N * ((-math.log(M) + math.log(b))/b - (-math.log(M) + math.log(a))/a) / math.e
integral -= ((math.log(M)+1)*(math.log(b)-math.log(a)) - (math.log(b)**2 - math.log(a)**2)/2) / math.e
if (i%1000) == 0:
print(f"Integral in: [{integral - 1/(2*b*b)}, {integral}], possibly {integral - 1/(8*b*b)}")
N = N_new
M = M_new
a = b
return integral
def integrand(x):
if x == 0:
return 0
return math.floor(1/x) * math.log((math.e * x * math.ceil(1/(math.e * x)))) * math.log(1/x)
# Integral of \lfloor 1/x \rfloor log ( ex \lceil 1/ex \rceil ) log(1/x) dx from 0 to 1
def compute_c1p():
N = 550000
sum = 0
altsum = 0
for k in range(1, N+1):
x = k / N
sum += integrand(x) / N
altsum += math.floor(1/x) * math.log((math.e * x * math.ceil(1/(math.e * x)))) / N
print(f"c0 is approximately {altsum/math.e}")
return sum / math.e
def antideriv(N,M, a):
val = (math.log(a)**2 + math.log(a) + 1) / a
val -= math.log(M) * (math.log(a) + 1) / a
val *= N
return val
def compute_c1p_alt():
N = 1
M = 1
integral = 0
a=1
for i in range(50000):
if N+1 < math.e * M:
b = N+1
N_new = N+1
M_new = M
else:
b = math.e * M
N_new = N
M_new = M+1
integral += antideriv(N,M,b) - antideriv(N,M,a)
if (i%1000) == 0:
print(f"Integral is: {integral/math.e}, projected to be {integral/math.e + math.log(math.e*b)/(2*b)}")
N = N_new
M = M_new
a = b
return integral/math.e + math.log(math.e*b)/(2*b)
# \sum_{k=1}^\infty \frac{1}{k} \log\left( \frac{e}{k} \left\lceil \frac{k}{e} \right\rceil \right)
def compute_c1pp():
sum = 0
for k in range(1, 100000):
sum += math.log( (math.e/k) * math.ceil(k/math.e) ) / k
if k % 1000 == 0:
print(f"After {k} iterations, c1'' has summed to {sum}; projected value {sum + math.e/(2*k)}")
return sum + math.e/(2*k)
c0 = compute_c0()
c1p = compute_c1p_alt()
c1pp = compute_c1pp()
# c'_1 + c_0 c''_1 - ec_0^2/2
c1 = c1p + c0 * c1pp - (math.e * c0**2) / 2
print(f"c0 = {c0}, c1p = {c1p}, c1pp = {c1pp}, c1 = {c1}")
#compute_c0()