-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
weddle's integration rule #11773
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Soham-KT
wants to merge
17
commits into
TheAlgorithms:master
Choose a base branch
from
Soham-KT:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
weddle's integration rule #11773
Changes from 8 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
1ad1ed3
weddle's integration rule
Soham-KT 8a4638e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 48ca80f
Merge branch 'TheAlgorithms:master' into master
Soham-KT cc5911a
checks passed
Soham-KT 162595b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7ce066c
added return type hint
Soham-KT 410ab38
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 3df440b
Added type hints to function parameters and return types
Soham-KT 56a0526
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] aa66c4b
added descriptive names
Soham-KT d44d0a8
safe eval used
Soham-KT b13ce58
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4e29f54
changed parameter hint
Soham-KT cb4e8dd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 2cdd70b
updated function signatures, type hints, and docstrings; modified fun…
Soham-KT 10bdd07
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] da0a975
changes made in doctest
Soham-KT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
import numpy as np | ||
from sympy import lambdify, symbols, sympify | ||
|
||
|
||
def get_inputs() -> tuple: | ||
""" | ||
Get user input for the function, lower limit, and upper limit. | ||
|
||
Returns: | ||
tuple: 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: ") | ||
a = float(input("Enter lower limit: ")) | ||
b = float(input("Enter upper limit: ")) | ||
return func, a, b | ||
|
||
|
||
def safe_function_eval(func_str: str) -> float: | ||
""" | ||
Safely evaluates the function by substituting x value using sympy. | ||
|
||
Args: | ||
func_str (str): Function expression as a string. | ||
|
||
Returns: | ||
float: The evaluated function result. | ||
|
||
Examples: | ||
>>> f = safe_function_eval('x**2') | ||
>>> f(3) | ||
9 | ||
|
||
>>> f = safe_function_eval('sin(x)') | ||
>>> round(f(3.14), 2) | ||
0.0 | ||
|
||
>>> f = safe_function_eval('x + x**2') | ||
>>> f(2) | ||
6 | ||
""" | ||
x = symbols("x") | ||
func_expr = sympify(func_str) | ||
|
||
# Convert the function to a callable lambda function | ||
lambda_func = lambdify(x, func_expr, modules=["numpy"]) | ||
return lambda_func | ||
|
||
|
||
def compute_table(func: str, a: float, b: float, acc: int) -> tuple: | ||
""" | ||
Compute the table of function values based on the limits and accuracy. | ||
|
||
Args: | ||
func (str): The mathematical function with the variable 'x' as a string. | ||
a (float): The lower limit of the integral. | ||
b (float): The upper limit of the integral. | ||
acc (int): The number of subdivisions for accuracy. | ||
|
||
Returns: | ||
tuple: 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 = (b - a) / (n_points - 1) | ||
x_vals = np.linspace(a, b, n_points) | ||
|
||
# Evaluate function values at all points | ||
table = func(x_vals) | ||
return table, h | ||
|
||
|
||
def apply_weights(table: list) -> list: | ||
""" | ||
Apply Simpson's rule weights to the values in the table. | ||
|
||
Args: | ||
table (list): A list of computed function values. | ||
|
||
Returns: | ||
list: 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]) | ||
if 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, table: list, h: float) -> float: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: |
||
""" | ||
Compute the final solution using the weighted values and table. | ||
|
||
Args: | ||
add (list): A list of weighted values from apply_weights. | ||
table (list): A list of function values. | ||
h (float): The step size (h) 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 * h * (sum(add) + table[0] + table[-1]) | ||
|
||
|
||
if __name__ == "__main__": | ||
from doctest import testmod | ||
|
||
testmod() | ||
|
||
# func, a, b = get_inputs() | ||
# acc = 1 | ||
# solution = None | ||
|
||
# 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}') |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide descriptive name for the parameter:
a
Please provide descriptive name for the parameter:
b