Skip to content

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 8 commits into from
Jan 18, 2020
Merged
Changes from 7 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
62 changes: 62 additions & 0 deletions maths/line_length.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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:
Copy link
Member

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.

Copy link
Contributor Author

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.


"""
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
>>> '%.6f' % line_length(f, 0, 1, 10)
Copy link
Member

Choose a reason for hiding this comment

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

f-strings are shorter and faster...

f"{line_length(f, 0, 1, 10):.6f}"

'1.414214'

>>> def f(x):
... return 1
>>> '%.6f' % line_length(f, -5.5, 4.5, 100)
Copy link
Member

Choose a reason for hiding this comment

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

Please remove the trailing 100 parameter in this test to demonstrate that there is default value and it works.

'10.000000'

>>> def f(x):
... return m.sin(5 * x) + m.cos(10 * x) + x * x/10
>>> '%.6f' % line_length(f, 0.0, 10.0, 10000)
'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:
length = line_length(f, -10, 10, i)
print("With {} steps: {}".format(i, length))
Copy link
Member

Choose a reason for hiding this comment

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

print(f"With {i} steps: {line_length(f, -10, 10, i)}")

i *= 10