Skip to content

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
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions maths/weddles_rule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
from math import *

Check failure on line 1 in maths/weddles_rule.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F403)

maths/weddles_rule.py:1:1: F403 `from math import *` used; unable to detect undefined names


def get_inputs():

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: get_inputs. If the function does not return a value, please provide the type hint as: def function() -> None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: get_inputs. If the function does not return a value, please provide the type hint as: def function() -> None:

"""
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.

Check failure on line 9 in maths/weddles_rule.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

maths/weddles_rule.py:9:89: E501 Line too long (115 > 88)

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 compute_table(func, a, b, acc):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: compute_table. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: func

Please provide type hint for the parameter: a

Please provide descriptive name for the parameter: a

Please provide type hint for the parameter: b

Please provide descriptive name for the parameter: b

Please provide type hint for the parameter: acc

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: compute_table. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: func

Please provide type hint for the parameter: a

Please provide descriptive name for the parameter: a

Please provide type hint for the parameter: b

Please provide descriptive name for the parameter: b

Please provide type hint for the parameter: acc

"""
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('1/(1+x**2)', 1, -1, 1)
([0.5, 0.4235294117647058, 0.36, 0.3076923076923077, 0.26470588235294124, 0.22929936305732482, 0.2], -0.3333333333333333)

Check failure on line 39 in maths/weddles_rule.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

maths/weddles_rule.py:39:89: E501 Line too long (129 > 88)
"""
h = (b - a) / (acc * 6)
table = [0 for _ in range(acc * 6 + 1)]
for j in range(acc * 6 + 1):
x = a + j / (acc * 6)

Check failure on line 44 in maths/weddles_rule.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F841)

maths/weddles_rule.py:44:9: F841 Local variable `x` is assigned to but never used
table[j] = eval(func)

Check failure on line 45 in maths/weddles_rule.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (S307)

maths/weddles_rule.py:45:20: S307 Use of possibly insecure function; consider using `ast.literal_eval`
return table, h


def apply_weights(table):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: apply_weights. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: table

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: apply_weights. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: table

"""
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, table, h):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: compute_solution. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: add

Please provide type hint for the parameter: table

Please provide type hint for the parameter: h

Please provide descriptive name for the parameter: h

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: compute_solution. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: add

Please provide type hint for the parameter: table

Please provide type hint for the parameter: h

Please provide descriptive name for the parameter: h

"""
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)

Check failure on line 89 in maths/weddles_rule.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

maths/weddles_rule.py:89:89: E501 Line too long (118 > 88)
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 <= 100000:
table, h = compute_table(func, a, b, acc)
add = apply_weights(table)
solution = compute_solution(add, table, h)
acc *= 10

print(f"Solution: {solution}")
Loading