forked from SensibilityTestbed/indoor-localization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trig_taylor.r2py
90 lines (65 loc) · 1.45 KB
/
trig_taylor.r2py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""
<Program Name>
trig.r2py
<Purpose>
This is a script of some basic Trigonometric functions, and the
distance calculation by geo coordinate
"""
dy_import_module_symbols("math.r2py")
pi = 3.14159265358979323846
e = math_e
# converts decimal degrees to radians
def deg2rad(deg):
return (deg * pi / 180)
# converts radians to decimal degrees
def rad2deg(rad):
return (rad * 180 / pi)
TERMS = 11
def factorial(x):
result = 1
for num in range(x):
result *= (num + 1)
return result
def choose(x):
numerator = 1.0
denominator = 1.0
for num in range(x+1, x * 2 + 1):
numerator *= num
for num in range(1, x+1):
denominator *= num
return numerator / denominator
def cos(x):
taylorsum = 0.0
for n in range(TERMS):
taylorsum += (-1)**n * (x**(2.0*n) / factorial(2*n))
return max(min(taylorsum, 1), -1)
def sin(x):
return (1 - cos(x)**2)**0.5
def tan(x):
return sin(x)/cos(x)
def asin(x):
taylorsum = 0.0
for n in range(TERMS):
taylorsum += choose(n) * x ** (2 * n + 1) / (4 ** n * (2 * n + 1))
return taylorsum
def acos(x):
return pi / 2 - asin(x)
def atan(x):
return asin(x/(x ** 2 + 1) ** 0.5)
def atan2(y, x):
if x == 0:
if y > 0:
result = pi/2
elif y < 0:
result = -pi/2
#else:
#error message
return result
result = atan(y/x)
if x < 0:
if y >= 0:
result = result + pi
else:
result = result - pi
return result
# -*- mode: python;-*-