-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithms_integration.py
More file actions
155 lines (125 loc) · 4.84 KB
/
algorithms_integration.py
File metadata and controls
155 lines (125 loc) · 4.84 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
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
import numpy as np
class IntegrationSolver:
def __init__(self, x_data, y_data):
self.x_data = np.array(x_data, dtype=float)
self.y_data = np.array(y_data, dtype=float)
self.n_points = len(x_data)
self.n_intervals = self.n_points - 1
self.h = self._check_equal_spacing()
def _check_equal_spacing(self):
if self.n_points < 2: return None
diffs = np.diff(self.x_data)
h = diffs[0]
if np.allclose(diffs, h, atol=1e-5):
return h
return None
def trapezoidal_rule(self):
if self.h is None: return None, "Requires equal spacing."
y = self.y_data
h = self.h
# Formula: h/2 * [ (y0 + yn) + 2 * sum(others) ]
sum_ends = y[0] + y[-1]
sum_mid = np.sum(y[1:-1])
result = (h / 2) * (sum_ends + 2 * sum_mid)
details = {
"h": h,
"y0": y[0], "yn": y[-1],
"sum_mid": sum_mid,
"n": self.n_intervals
}
return result, details
def simpson_13_rule(self):
if self.h is None: return None, "Requires equal spacing."
if self.n_intervals % 2 != 0: return None, "Simpson's 1/3 Rule requires an even number of intervals (odd number of points)."
y = self.y_data
h = self.h
# Formula: h/3 * [ (y0 + yn) + 4*odd + 2*even ]
sum_ends = y[0] + y[-1]
sum_odd = np.sum(y[1:-1:2])
sum_even = np.sum(y[2:-1:2])
result = (h / 3) * (sum_ends + 4 * sum_odd + 2 * sum_even)
details = {
"h": h,
"y0": y[0], "yn": y[-1],
"sum_odd": sum_odd,
"sum_even": sum_even,
"n": self.n_intervals
}
return result, details
def simpson_38_rule(self):
if self.h is None: return None, "Requires equal spacing."
if self.n_intervals % 3 != 0: return None, "Simpson's 3/8 Rule requires number of intervals to be a multiple of 3."
y = self.y_data
h = self.h
n = self.n_intervals
# Formula: 3h/8 * [ (y0 + yn) + 3*(others not multiple of 3) + 2*(multiples of 3) ]
sum_ends = y[0] + y[-1]
# Indices for multiples of 3 (excluding 0 and n)
# 3, 6, 9 ...
idx_mult3 = [i for i in range(3, n, 3)]
sum_mult3 = np.sum(y[idx_mult3]) if idx_mult3 else 0
# Indices for others (excluding 0 and n)
idx_others = [i for i in range(1, n) if i % 3 != 0]
sum_others = np.sum(y[idx_others]) if idx_others else 0
result = (3 * h / 8) * (sum_ends + 3 * sum_others + 2 * sum_mult3)
details = {
"h": h,
"y0": y[0], "yn": y[-1],
"sum_others": sum_others,
"sum_mult3": sum_mult3,
"n": self.n_intervals
}
return result, details
def booles_rule(self):
if self.h is None: return None, "Requires equal spacing."
if self.n_intervals % 4 != 0: return None, "Boole's Rule requires number of intervals to be a multiple of 4."
y = self.y_data
h = self.h
n = self.n_intervals
sum_ends = y[0] + y[-1]
sum_32, sum_12, sum_14 = 0, 0, 0
for i in range(1, n):
if i % 4 == 0:
sum_14 += y[i]
elif i % 2 == 0:
sum_12 += y[i]
else: # odd
sum_32 += y[i]
result = (2 * h / 45) * (7 * sum_ends + 32 * sum_32 + 12 * sum_12 + 14 * sum_14)
details = {
"h": h,
"y0": y[0], "yn": y[-1],
"sum_32": sum_32,
"sum_12": sum_12,
"sum_14": sum_14,
"n": self.n_intervals
}
return result, details
def weddles_rule(self):
if self.h is None: return None, "Requires equal spacing."
if self.n_intervals % 6 != 0: return None, "Weddle's Rule requires number of intervals to be a multiple of 6."
y = self.y_data
h = self.h
n = self.n_intervals
sum_ends = y[0] + y[-1]
sum_5, sum_1, sum_6, sum_2 = 0, 0, 0, 0
for i in range(1, n):
if i % 6 == 1 or i % 6 == 5:
sum_5 += y[i]
elif i % 6 == 2 or i % 6 == 4:
sum_1 += y[i]
elif i % 6 == 3:
sum_6 += y[i]
elif i % 6 == 0:
sum_2 += y[i]
result = (3 * h / 10) * (sum_ends + 5*sum_5 + sum_1 + 6*sum_6 + 2*sum_2)
details = {
"h": h,
"y0": y[0], "yn": y[-1],
"sum_5": sum_5,
"sum_1": sum_1,
"sum_6": sum_6,
"sum_2": sum_2,
"n": self.n_intervals
}
return result, details