-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShuZhiJiFen.py
278 lines (228 loc) · 8.6 KB
/
ShuZhiJiFen.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
from typing import List, Callable, Dict, Union
from matplotlib import pyplot as plt
import numpy as np
import math
class ShuZhiJiFen:
def __init__(self, a: float, b: float, f: Callable[[float], float]):
if a > b:
self.a = b
self.b = a
self.f = lambda x: -f(x)
else:
self.a = a
self.b = b
self.f = f
def fu_he_ti_xing_ji_fen(self, h=None) -> float:
"""
:param h: step length
:return:
"""
if h is None:
h = min(1e-5, (self.b-self.a)/1e5)
result = 0
if self.a != float('-inf') and self.b != float('inf'):
start = self.a
now = start
end = self.b
while now + h <= end:
st = h * (self.f(now) + self.f(now + h)) / 2
if st != float('nan'):
pass
elif (h-1e-10) * (self.f(now+1e-10) + self.f(now + h)) / 2 != float('nan'):
st = (h-1e-10) * (self.f(now+1e-10) + self.f(now + h)) / 2
else:
st = (h + 1e-10) * (self.f(now) + self.f(now + h + 1e-10)) / 2
result += st
now += h
if now + h < end:
result += (end - now) * (self.f(now) + self.f(end)) / 2
else:
raise NotImplementedError
return result
def fu_he_simpson_ji_fen(self, h=None) -> float:
"""
:param h: step length
:return:
"""
if h is None:
h = min(1e-5, (self.b-self.a)/1e5)
result = 0
if self.a != float('-inf') and self.b != float('inf'):
start = self.a
now = start
end = self.b
while now + h <= end:
st = h * (self.f(now) + self.f(now + h) + self.f(now + h/2) * 4) / 6
result += st
now += h
if now + h < end:
result += (end - now) * (self.f(now) + self.f(end) + self.f((now+end) / 2) * 4) / 6
else:
raise NotImplementedError
return result
def long_bei_ge_ji_fen(self, epsilon=None) -> float:
"""
:param epsilon: expected wu cha
:return:
"""
if epsilon is None:
epsilon = 1e-10
k = 0
h = self.b - self.a
f = self.f
T = [[0.0 for _ in range(100)] for _ in range(100)]
k += 1
T[0][0] = (f(self.b) + f(self.a)) * h / 2
T[0][1] = T[0][0] / 2 + h / 2 * sum([self.f(j / pow(2, 1) * (self.b - self.a) + self.a) for j in range(1, int(pow(2, 1)), 2)])
T[1][0] = pow(4, 1) / (pow(4, 1) - 1) * T[0][1] - 1 / (pow(4, 1) - 1) * T[0][0]
k = 1
while abs(T[k][0] - T[k-1][0]) >= epsilon:
k += 1
h = (self.b - self.a) / pow(2, k-1)
T[0][k] = T[0][k-1] / 2 + h / 2 * sum([self.f(j / pow(2, k) * (self.b - self.a) + self.a) for j in range(1, int(pow(2, k)), 2)])
for m in range(1, k+1):
T[m][k-m] = pow(4, m) / (pow(4, m) - 1) * T[m-1][k-m+1] - 1 / (pow(4, m) - 1) * T[m-1][k-m]
return T[k][0]
def zi_shi_ying_ji_fen(self, epsilon=None):
"""
>>> import math
>>> f = lambda x: math.exp(-x**2 / 2) / math.sqrt(2*math.pi)
>>> jifen = ShuZhiJiFen(-1, 1, f)
:param epsilon: maximum expected error
:return:
"""
if epsilon is None:
epsilon = 1e-10
def simpson(start: float, end: float, func: Callable[[float], float]) -> float:
return (end-start) / 6 * (func(start) + 4 * func((start+end)/2) + func(end))
a = self.a
b = self.b
f = self.f
def calc_split(a: float, b: float, f: Callable[[float], float], epsi: float) -> float:
S1 = simpson(a, b, f)
S2 = simpson(a, (b+a)/2, f) + simpson((b+a)/2, b, f)
if abs(S1 - S2) <= epsi:
return S2 + (S2 - S1) / 15
else:
return calc_split(a, (a+b)/2, f, epsi/2) + calc_split((a+b)/2, b, f, epsi/2)
return calc_split(a, b, f, epsilon * 15)
class ErWeiShuZhiJiFen:
def __init__(self, a: float, b: float, c: Union[float, Callable[[float], float]], d: Union[float, Callable[[float], float]], f: Callable[[float, float], float]):
"""
二维积分,积分形式: intergrate(a~b) intergrate(c~d) f(x, y) dy dx
:param a:
:param b:
:param c:
:param d:
:param f:
"""
self.a = a
self.b = b
self.c = c
self.d = d
self.f = f
def fu_he_er_wei_simpson(self, h=None, k=None):
if h is None:
h = min(1e-5, (self.b-self.a)/1e5)
if (not isinstance(self.c, (float, int))) or (not isinstance(self.d, (float, int))):
raise NotImplementedError
y0 = self.c
ym = self.d
a = self.a
b = self.b
y_half = [y0 + k * (i+0.5) for i in range(0, int((ym-y0) / k))]
ys = [y0 + k * i for i in range(1, int((ym-y0) / k))]
result = 0
def g(_y):
return lambda x: self.f(x, _y)
result += ShuZhiJiFen(a, b, g(y0)).fu_he_simpson_ji_fen(h) + ShuZhiJiFen(a, b, g(ym)).fu_he_simpson_ji_fen(h)
for y in y_half:
result += 4 * ShuZhiJiFen(a, b, g(y)).fu_he_simpson_ji_fen(h)
for y in ys:
result += 2 * ShuZhiJiFen(a, b, g(y)).fu_he_simpson_ji_fen(h)
result *= k / 6
return result
def gauss_er_wei_ji_fen(self, n=None):
"""
Gauss-Legendre 求积公式,小数位精度1e-7
:param n: 代数精度2n+1次
:return:
"""
if n is None:
n = 5
if n not in range(6):
raise NotImplementedError
if (not isinstance(self.c, (float, int))) or (not isinstance(self.d, (float, int))):
raise NotImplementedError
xs = [[0],
[0.5773503, -0.5773503],
[0.7745967, -0.7745967, 0],
[0.8611363, -0.8611363, 0.3399810, -0.3399810],
[0.9061798, -0.9061798, 0.5384693, -0.5384693, 0],
[0.9324695, -0.9324695, 0.6612904, -0.6612904, 0.2386192, -0.2386192]
]
As = [[2],
[1, 1],
[5/9, 5/9, 8/9, 8/9],
[0.3478548, 0.3478548, 0.6521452, 0.6521452],
[0.2369269, 0.2369269, 0.4786287, 0.4786287, 0.568889],
[0.1713245, 0.1713245, 0.3607616, 0.3607616, 0.4679139, 0.4679139]
]
a = self.a
b = self.b
c = self.c
d = self.d
def g(x, y):
# 变换积分上下限
return self.f((x+1)/2*(d-c)+c, (y+1)/2*(b-a)+a) * (b-a) * (d-c) / 4
xk = xs[n]
Ak = As[n]
result = 0
for i in range(len(xk)):
for j in range(len(xk)):
result += Ak[i] * Ak[j] * g(xk[i], xk[j])
return result
def fei_ju_xing_fu_he_simpson_ji_fen(self, h=None):
"""
for integration like integrate(a~b) integrate(c(x)~d(x)) f(x, y) dy dx
:param h: step length for integration between a~b using 复合辛普森公式
:return:
"""
c = self.c
d = self.d
if isinstance(c, (float, int)):
c = lambda x: c
if isinstance(d, (float, int)):
d = lambda x: d
if h is None:
h = min(1e-5, (self.b-self.a)/1e5)
c = self.c
d = self.d
f = self.f
k = lambda x: (d(x) - c(x)) / 2
simp_f = lambda x: k(x) / 3 * (f(x, c(x)) + 4 * f(x, c(x) + k(x)) + f(x, d(x)))
calc = ShuZhiJiFen(self.a, self.b, simp_f)
return calc.fu_he_simpson_ji_fen(h)
if __name__ == "__main__":
def real_f(x):
if x == 0:
return 0
return pow(x, 0.5) * math.log(x)
# return x + x*x + x*x*x + 2
# return pow(x, 1.5)
def real_f2(x, y):
return pow(math.e, -x*y)
test1 = ShuZhiJiFen(0, 1, real_f)
print('复合梯形 ', test1.fu_he_ti_xing_ji_fen(1e-5))
print('复合辛普森', test1.fu_he_simpson_ji_fen(1e-4))
print('龙贝格 ', test1.long_bei_ge_ji_fen(1e-8))
print('自适应 ', test1.zi_shi_ying_ji_fen(1e-12))
test2 = ErWeiShuZhiJiFen(0, 1, 0, 1, real_f2)
print('复合辛普森二维', test2.fu_he_er_wei_simpson(0.25, 0.25))
print('高斯二维积分 ', test2.gauss_er_wei_ji_fen(4))
def c(x):
return 0.0
def d(x):
return pow(1 - pow(x, 2), 0.5)
test3 = ErWeiShuZhiJiFen(0, 1, c, d, real_f2)
print('复合辛普森圆内', test3.fei_ju_xing_fu_he_simpson_ji_fen(0.25))