-
-
Notifications
You must be signed in to change notification settings - Fork 51k
Expand file tree
/
Copy pathweddles_rule.py
More file actions
146 lines (115 loc) · 4.32 KB
/
Copy pathweddles_rule.py
File metadata and controls
146 lines (115 loc) · 4.32 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
from collections.abc import Callable
import numpy as np
from sympy import lambdify, symbols, sympify
def get_inputs() -> tuple[str, float, float]:
"""
Get user input for the function, lower limit, and upper limit.
Returns:
Tuple[str, float, float]: A tuple containing the function as a string,
the lower limit (a), and the upper limit (b) as floats.
Example:
>>> from unittest.mock import patch
>>> inputs = ['1/(1+x**2)', 1.0, -1.0]
>>> with patch('builtins.input', side_effect=inputs):
... get_inputs()
('1/(1+x**2)', 1.0, -1.0)
"""
func = input("Enter function with variable as x: ")
lower_limit = float(input("Enter lower limit: "))
upper_limit = float(input("Enter upper limit: "))
return func, lower_limit, upper_limit
def safe_function_eval(func_str: str) -> Callable:
"""
Safely evaluates the function by substituting x value using sympy.
Args:
func_str (str): Function expression as a string.
Returns:
Callable: A callable lambda function for numerical evaluation.
Examples:
>>> f = safe_function_eval('x**2')
>>> f(3)
9
>>> f = safe_function_eval('x + x**2')
>>> f(2)
6
"""
x = symbols("x")
func_expr = sympify(func_str)
lambda_func = lambdify(x, func_expr, modules=["numpy"])
return lambda_func
def compute_table(
func: Callable, lower_limit: float, upper_limit: float, acc: int
) -> tuple[np.ndarray, float]:
"""
Compute the table of function values based on the limits and accuracy.
Args:
func (Callable): The mathematical function as a callable.
lower_limit (float): The lower limit of the integral.
upper_limit (float): The upper limit of the integral.
acc (int): The number of subdivisions for accuracy.
Returns:
Tuple[np.ndarray, float]: A tuple containing the table
of values and the step size (h).
Example:
>>> compute_table(
... safe_function_eval('1/(1+x**2)'), 1, -1, 1
... )
(array([0.5 , 0.69230769, 0.9 , 1. , 0.9 ,
0.69230769, 0.5 ]), -0.3333333333333333)
"""
# Weddle's rule requires number of intervals as a multiple of 6 for accuracy
n_points = acc * 6 + 1
h = (upper_limit - lower_limit) / (n_points - 1)
x_vals = np.linspace(lower_limit, upper_limit, n_points)
table = func(x_vals) # Evaluate function values at all points
return table, h
def apply_weights(table: list[float]) -> list[float]:
"""
Apply Weddle's rule weights to the values in the table.
Args:
table (List[float]): A list of computed function values.
Returns:
List[float]: A list of weighted values.
Example:
>>> apply_weights([0.0, 0.866, 1.0, 0.866, 0.0, -0.866, -1.0])
[4.33, 1.0, 5.196, 0.0, -4.33]
"""
add = []
for i in range(1, len(table) - 1):
if i % 2 == 0 and i % 3 != 0:
add.append(table[i])
elif i % 2 != 0 and i % 3 != 0:
add.append(5 * table[i])
elif i % 6 == 0:
add.append(2 * table[i])
elif i % 3 == 0 and i % 2 != 0:
add.append(6 * table[i])
return add
def compute_solution(add: list[float], table: list[float], step_size: float) -> float:
"""
Compute the final solution using the weighted values and table.
Args:
add (List[float]): A list of weighted values from apply_weights.
table (List[float]): A list of function values.
step_size (float): The step size calculated from the limits and accuracy.
Returns:
float: The final computed integral solution.
Example:
>>> compute_solution([4.33, 6.0, 0.0, -4.33], [0.0, 0.866, 1.0, 0.866, 0.0,
... -0.866, -1.0], 0.5235983333333333)
0.7853975
"""
return 0.3 * step_size * (sum(add) + table[0] + table[-1])
if __name__ == "__main__":
from doctest import testmod
testmod()
# func_str, a, b = get_inputs()
# acc = 1
# solution = None
# func = safe_function_eval(func_str)
# while acc <= 100_000:
# table, h = compute_table(func, a, b, acc)
# add = apply_weights(table)
# solution = compute_solution(add, table, h)
# acc *= 10
# print(f"Solution: {solution}")