-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Added an algorithm that approximates line lengths #1692
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
baf7a8d
A recursive insertion sort
shrabian 0af0c94
added doctests and typehints
shrabian 1445753
Added arc length and numerical integration calculators
shrabian 1129e09
fixed doc test
shrabian 52c1a58
Fixed some conversion errors
shrabian a258cb1
Fixed some commenting
shrabian 3e60489
Deleted numerical integration to allow 1 file per push
shrabian 4ab2263
Changed string formatting method
shrabian 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,61 @@ | ||
from typing import Callable, Union | ||
import math as m | ||
|
||
def line_length(fnc: Callable[[Union[int, float]], Union[int, float]], | ||
x_start: Union[int, float], | ||
x_end: Union[int, float], | ||
steps: int = 100) -> float: | ||
|
||
""" | ||
Approximates the arc length of a line segment by treating the curve as a | ||
sequence of linear lines and summing their lengths | ||
:param fnc: a function which defines a curve | ||
:param x_start: left end point to indicate the start of line segment | ||
:param x_end: right end point to indicate end of line segment | ||
:param steps: an accuracy gauge; more steps increases accuracy | ||
:return: a float representing the length of the curve | ||
|
||
>>> def f(x): | ||
... return x | ||
>>> f"{line_length(f, 0, 1, 10):.6f}" | ||
'1.414214' | ||
|
||
>>> def f(x): | ||
... return 1 | ||
>>> f"{line_length(f, -5.5, 4.5):.6f}" | ||
'10.000000' | ||
|
||
>>> def f(x): | ||
... return m.sin(5 * x) + m.cos(10 * x) + x * x/10 | ||
>>> f"{line_length(f, 0.0, 10.0, 10000):.6f}" | ||
'69.534930' | ||
""" | ||
|
||
x1 = x_start | ||
fx1 = fnc(x_start) | ||
length = 0.0 | ||
|
||
for i in range(steps): | ||
|
||
# Approximates curve as a sequence of linear lines and sums their length | ||
x2 = (x_end - x_start) / steps + x1 | ||
fx2 = fnc(x2) | ||
length += m.hypot(x2 - x1, fx2 - fx1) | ||
|
||
# Increment step | ||
x1 = x2 | ||
fx1 = fx2 | ||
|
||
return length | ||
|
||
if __name__ == "__main__": | ||
|
||
def f(x): | ||
return m.sin(10*x) | ||
|
||
print("f(x) = sin(10 * x)") | ||
print("The length of the curve from x = -10 to x = 10 is:") | ||
i = 10 | ||
while i <= 100000: | ||
print(f"With {i} steps: {line_length(f, -10, 10, i)}") | ||
i *= 10 |
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,63 @@ | ||
""" | ||
Approximates the area under the curve using the trapezoidal rule | ||
""" | ||
|
||
from typing import Callable, Union | ||
|
||
def trapezoidal_area(fnc: Callable[[Union[int, float]], Union[int, float]], | ||
x_start: Union[int, float], | ||
x_end: Union[int, float], | ||
steps: int = 100) -> float: | ||
|
||
""" | ||
Treats curve as a collection of linear lines and sums the area of the | ||
trapezium shape they form | ||
:param fnc: a function which defines a curve | ||
:param x_start: left end point to indicate the start of line segment | ||
:param x_end: right end point to indicate end of line segment | ||
:param steps: an accuracy gauge; more steps increases the accuracy | ||
:return: a float representing the length of the curve | ||
|
||
>>> def f(x): | ||
... return 5 | ||
>>> '%.3f' % trapezoidal_area(f, 12.0, 14.0, 1000) | ||
'10.000' | ||
|
||
>>> def f(x): | ||
... return 9*x**2 | ||
>>> '%.4f' % trapezoidal_area(f, -4.0, 0, 10000) | ||
'192.0000' | ||
|
||
>>> '%.4f' % trapezoidal_area(f, -4.0, 4.0, 10000) | ||
'384.0000' | ||
""" | ||
x1 = x_start | ||
fx1 = fnc(x_start) | ||
area = 0.0 | ||
|
||
for i in range(steps): | ||
|
||
# Approximates small segments of curve as linear and solve | ||
# for trapezoidal area | ||
x2 = (x_end - x_start)/steps + x1 | ||
fx2 = fnc(x2) | ||
area += abs(fx2 + fx1) * (x2 - x1)/2 | ||
|
||
# Increment step | ||
x1 = x2 | ||
fx1 = fx2 | ||
return area | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
def f(x): | ||
return x**3 | ||
|
||
print("f(x) = x^3") | ||
print("The area between the curve, x = -10, x = 10 and the x axis is:") | ||
i = 10 | ||
while i <= 100000: | ||
area = trapezoidal_area(f, -5, 5, i) | ||
print("with {} steps: {}".format(i, area)) | ||
i*=10 |
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.
This is returning str, not float.
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.
My function should return a float. In the doctests, I've converted the return value into a string to allow formatting. I've kept it as float for now, let me know if that's ok.