maths.weddles_rule¶
Functions¶
|
Apply Weddle's rule weights to the values in the table. |
|
Compute the final solution using the weighted values and table. |
|
Compute the table of function values based on the limits and accuracy. |
|
Get user input for the function, lower limit, and upper limit. |
|
Safely evaluates the function by substituting x value using sympy. |
Module Contents¶
- maths.weddles_rule.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]
- maths.weddles_rule.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
- maths.weddles_rule.compute_table(func: collections.abc.Callable, lower_limit: float, upper_limit: float, acc: int) tuple[numpy.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)
- maths.weddles_rule.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)
- maths.weddles_rule.safe_function_eval(func_str: str) collections.abc.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