TheAlgorithms-Python
123 строки · 3.8 Кб
1"""
2https://en.wikipedia.org/wiki/Taylor_series#Trigonometric_functions
3"""
4
5from math import factorial, pi
6
7
8def maclaurin_sin(theta: float, accuracy: int = 30) -> float:
9"""
10Finds the maclaurin approximation of sin
11
12:param theta: the angle to which sin is found
13:param accuracy: the degree of accuracy wanted minimum
14:return: the value of sine in radians
15
16
17>>> from math import isclose, sin
18>>> all(isclose(maclaurin_sin(x, 50), sin(x)) for x in range(-25, 25))
19True
20>>> maclaurin_sin(10)
21-0.5440211108893691
22>>> maclaurin_sin(-10)
230.5440211108893704
24>>> maclaurin_sin(10, 15)
25-0.544021110889369
26>>> maclaurin_sin(-10, 15)
270.5440211108893704
28>>> maclaurin_sin("10")
29Traceback (most recent call last):
30...
31ValueError: maclaurin_sin() requires either an int or float for theta
32>>> maclaurin_sin(10, -30)
33Traceback (most recent call last):
34...
35ValueError: maclaurin_sin() requires a positive int for accuracy
36>>> maclaurin_sin(10, 30.5)
37Traceback (most recent call last):
38...
39ValueError: maclaurin_sin() requires a positive int for accuracy
40>>> maclaurin_sin(10, "30")
41Traceback (most recent call last):
42...
43ValueError: maclaurin_sin() requires a positive int for accuracy
44"""
45
46if not isinstance(theta, (int, float)):
47raise ValueError("maclaurin_sin() requires either an int or float for theta")
48
49if not isinstance(accuracy, int) or accuracy <= 0:
50raise ValueError("maclaurin_sin() requires a positive int for accuracy")
51
52theta = float(theta)
53div = theta // (2 * pi)
54theta -= 2 * div * pi
55return sum(
56(-1) ** r * theta ** (2 * r + 1) / factorial(2 * r + 1) for r in range(accuracy)
57)
58
59
60def maclaurin_cos(theta: float, accuracy: int = 30) -> float:
61"""
62Finds the maclaurin approximation of cos
63
64:param theta: the angle to which cos is found
65:param accuracy: the degree of accuracy wanted
66:return: the value of cosine in radians
67
68
69>>> from math import isclose, cos
70>>> all(isclose(maclaurin_cos(x, 50), cos(x)) for x in range(-25, 25))
71True
72>>> maclaurin_cos(5)
730.2836621854632268
74>>> maclaurin_cos(-5)
750.2836621854632265
76>>> maclaurin_cos(10, 15)
77-0.8390715290764524
78>>> maclaurin_cos(-10, 15)
79-0.8390715290764521
80>>> maclaurin_cos("10")
81Traceback (most recent call last):
82...
83ValueError: maclaurin_cos() requires either an int or float for theta
84>>> maclaurin_cos(10, -30)
85Traceback (most recent call last):
86...
87ValueError: maclaurin_cos() requires a positive int for accuracy
88>>> maclaurin_cos(10, 30.5)
89Traceback (most recent call last):
90...
91ValueError: maclaurin_cos() requires a positive int for accuracy
92>>> maclaurin_cos(10, "30")
93Traceback (most recent call last):
94...
95ValueError: maclaurin_cos() requires a positive int for accuracy
96"""
97
98if not isinstance(theta, (int, float)):
99raise ValueError("maclaurin_cos() requires either an int or float for theta")
100
101if not isinstance(accuracy, int) or accuracy <= 0:
102raise ValueError("maclaurin_cos() requires a positive int for accuracy")
103
104theta = float(theta)
105div = theta // (2 * pi)
106theta -= 2 * div * pi
107return sum((-1) ** r * theta ** (2 * r) / factorial(2 * r) for r in range(accuracy))
108
109
110if __name__ == "__main__":
111import doctest
112
113doctest.testmod()
114
115print(maclaurin_sin(10))
116print(maclaurin_sin(-10))
117print(maclaurin_sin(10, 15))
118print(maclaurin_sin(-10, 15))
119
120print(maclaurin_cos(5))
121print(maclaurin_cos(-5))
122print(maclaurin_cos(10, 15))
123print(maclaurin_cos(-10, 15))
124