From a5dd07c3707a0d3ebde0321ce7984082b3d322ff Mon Sep 17 00:00:00 2001 From: Chris O <46587501+ChrisO345@users.noreply.github.com> Date: Sun, 23 Oct 2022 05:17:07 +1300 Subject: [PATCH] Maclaurin approximation of cos (#7507) * renamed maclaurin_sin.py to maclaurin_series.py and included function for cos approximation * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * attempt to fix pytest error Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- maths/maclaurin_series.py | 121 ++++++++++++++++++++++++++++++++++++++ maths/maclaurin_sin.py | 64 -------------------- 2 files changed, 121 insertions(+), 64 deletions(-) create mode 100644 maths/maclaurin_series.py delete mode 100644 maths/maclaurin_sin.py diff --git a/maths/maclaurin_series.py b/maths/maclaurin_series.py new file mode 100644 index 000000000..57edc90bf --- /dev/null +++ b/maths/maclaurin_series.py @@ -0,0 +1,121 @@ +""" +https://en.wikipedia.org/wiki/Taylor_series#Trigonometric_functions +""" +from math import factorial, pi + + +def maclaurin_sin(theta: float, accuracy: int = 30) -> float: + """ + Finds the maclaurin approximation of sin + + :param theta: the angle to which sin is found + :param accuracy: the degree of accuracy wanted minimum + :return: the value of sine in radians + + + >>> from math import isclose, sin + >>> all(isclose(maclaurin_sin(x, 50), sin(x)) for x in range(-25, 25)) + True + >>> maclaurin_sin(10) + -0.544021110889369 + >>> maclaurin_sin(-10) + 0.5440211108893703 + >>> maclaurin_sin(10, 15) + -0.5440211108893689 + >>> maclaurin_sin(-10, 15) + 0.5440211108893703 + >>> maclaurin_sin("10") + Traceback (most recent call last): + ... + ValueError: maclaurin_sin() requires either an int or float for theta + >>> maclaurin_sin(10, -30) + Traceback (most recent call last): + ... + ValueError: maclaurin_sin() requires a positive int for accuracy + >>> maclaurin_sin(10, 30.5) + Traceback (most recent call last): + ... + ValueError: maclaurin_sin() requires a positive int for accuracy + >>> maclaurin_sin(10, "30") + Traceback (most recent call last): + ... + ValueError: maclaurin_sin() requires a positive int for accuracy + """ + + if not isinstance(theta, (int, float)): + raise ValueError("maclaurin_sin() requires either an int or float for theta") + + if not isinstance(accuracy, int) or accuracy <= 0: + raise ValueError("maclaurin_sin() requires a positive int for accuracy") + + theta = float(theta) + div = theta // (2 * pi) + theta -= 2 * div * pi + return sum( + (((-1) ** r) * ((theta ** (2 * r + 1)) / factorial(2 * r + 1))) + for r in range(accuracy) + ) + + +def maclaurin_cos(theta: float, accuracy: int = 30) -> float: + """ + Finds the maclaurin approximation of cos + + :param theta: the angle to which cos is found + :param accuracy: the degree of accuracy wanted + :return: the value of cosine in radians + + + >>> from math import isclose, cos + >>> all(isclose(maclaurin_cos(x, 50), cos(x)) for x in range(-25, 25)) + True + >>> maclaurin_cos(5) + 0.28366218546322675 + >>> maclaurin_cos(-5) + 0.2836621854632266 + >>> maclaurin_cos(10, 15) + -0.8390715290764525 + >>> maclaurin_cos(-10, 15) + -0.8390715290764521 + >>> maclaurin_cos("10") + Traceback (most recent call last): + ... + ValueError: maclaurin_cos() requires either an int or float for theta + >>> maclaurin_cos(10, -30) + Traceback (most recent call last): + ... + ValueError: maclaurin_cos() requires a positive int for accuracy + >>> maclaurin_cos(10, 30.5) + Traceback (most recent call last): + ... + ValueError: maclaurin_cos() requires a positive int for accuracy + >>> maclaurin_cos(10, "30") + Traceback (most recent call last): + ... + ValueError: maclaurin_cos() requires a positive int for accuracy + """ + + if not isinstance(theta, (int, float)): + raise ValueError("maclaurin_cos() requires either an int or float for theta") + + if not isinstance(accuracy, int) or accuracy <= 0: + raise ValueError("maclaurin_cos() requires a positive int for accuracy") + + theta = float(theta) + div = theta // (2 * pi) + theta -= 2 * div * pi + return sum( + (((-1) ** r) * ((theta ** (2 * r)) / factorial(2 * r))) for r in range(accuracy) + ) + + +if __name__ == "__main__": + print(maclaurin_sin(10)) + print(maclaurin_sin(-10)) + print(maclaurin_sin(10, 15)) + print(maclaurin_sin(-10, 15)) + + print(maclaurin_cos(5)) + print(maclaurin_cos(-5)) + print(maclaurin_cos(10, 15)) + print(maclaurin_cos(-10, 15)) diff --git a/maths/maclaurin_sin.py b/maths/maclaurin_sin.py deleted file mode 100644 index 3c27ccf63..000000000 --- a/maths/maclaurin_sin.py +++ /dev/null @@ -1,64 +0,0 @@ -""" -https://en.wikipedia.org/wiki/Taylor_series#Trigonometric_functions -""" -from math import factorial, pi - - -def maclaurin_sin(theta: float, accuracy: int = 30) -> float: - """ - Finds the maclaurin approximation of sin - - :param theta: the angle to which sin is found - :param accuracy: the degree of accuracy wanted minimum ~ 1.5 theta - :return: the value of sine in radians - - - >>> from math import isclose, sin - >>> all(isclose(maclaurin_sin(x, 50), sin(x)) for x in range(-25, 25)) - True - >>> maclaurin_sin(10) - -0.544021110889369 - >>> maclaurin_sin(-10) - 0.5440211108893703 - >>> maclaurin_sin(10, 15) - -0.5440211108893689 - >>> maclaurin_sin(-10, 15) - 0.5440211108893703 - >>> maclaurin_sin("10") - Traceback (most recent call last): - ... - ValueError: maclaurin_sin() requires either an int or float for theta - >>> maclaurin_sin(10, -30) - Traceback (most recent call last): - ... - ValueError: maclaurin_sin() requires a positive int for accuracy - >>> maclaurin_sin(10, 30.5) - Traceback (most recent call last): - ... - ValueError: maclaurin_sin() requires a positive int for accuracy - >>> maclaurin_sin(10, "30") - Traceback (most recent call last): - ... - ValueError: maclaurin_sin() requires a positive int for accuracy - """ - - if not isinstance(theta, (int, float)): - raise ValueError("maclaurin_sin() requires either an int or float for theta") - - if not isinstance(accuracy, int) or accuracy <= 0: - raise ValueError("maclaurin_sin() requires a positive int for accuracy") - - theta = float(theta) - div = theta // (2 * pi) - theta -= 2 * div * pi - return sum( - (((-1) ** r) * ((theta ** (2 * r + 1)) / factorial(2 * r + 1))) - for r in range(accuracy) - ) - - -if __name__ == "__main__": - print(maclaurin_sin(10)) - print(maclaurin_sin(-10)) - print(maclaurin_sin(10, 15)) - print(maclaurin_sin(-10, 15))