forked from mit-dig/air-reasoner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cwm_trigo.py
147 lines (112 loc) · 4.3 KB
/
cwm_trigo.py
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"""Trigonometrical Built-Ins for CWM
Allows CWM to do do trigonometrical
http://www.python.org/doc/2.3/lib/module-math.html
This module is inspired by the math module.
See http://www.w3.org/2000/10/swap/cwm_math.py
cf. http://www.w3.org/2000/10/swap/cwm.py
See http://ilrt.org/discovery/chatlogs/rdfig/2003-09-23.html#T22-37-54
http://rdfig.xmlhack.com/2003/09/23/2003-09-23.html#1064356689.846120
"""
__author__ = 'Karl Dubost'
__cvsid__ = '$Id: cwm_trigo.py,v 1.13 2007/06/26 02:36:15 syosi Exp $'
__version__ = '$Revision: 1.13 $'
__all__ = ["evaluateObject"]
from math import sin, acos, asin, atan, atan2, cos, cosh, sinh, tan, tanh
from term import LightBuiltIn, Function, ReverseFunction
import types
from diag import progress
from cwm_math import *
MATH_NS_URI = 'http://www.w3.org/2000/10/swap/math#'
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# Trigonometrical Features
#
#
# Light Built-in classes - these are all reverse functions
#
# Note that asin the arc sine i s just the reverse function of sin,
# handled by sin being a reverse function as well as a function
# cosine, hyperbolic or not
# sine, hyperbolic or not
# tangent, arc tangent, arc tangent (y/x)
#
class BI_atan2(LightBuiltIn, Function):
def evaluateObject(self, subj_py):
"""atan2(y, x)
Return the arc tangent (measured in radians) of y/x.
Unlike atan(y/x), the signs of both x and y are considered.
-- Karl"""
if len(numeric(subj_py)) == 2:
return atan2(numeric(subj_py[0]),numeric(subj_py[1]))
else: return None
class BI_cos(LightBuiltIn, Function, ReverseFunction):
def evaluateObject(self, subj_py):
"""cos(x)
Return the cosine of x (measured in radians)."""
return cos(numeric(subj_py))
def evaluateSubject(self, x):
try:
return acos(numeric(x))
except ValueError:
return None
class BI_cosh(LightBuiltIn, Function):
def evaluateObject(self, subj_py):
"""cosh(x)
Return the hyperbolic cosine of x."""
return cosh(numeric(subj_py))
# def evaluateSubject(self, x):
# return acosh(numeric(x))
class BI_degrees(LightBuiltIn, Function, ReverseFunction):
"""Angles are in radians. This property is the equivalent in degrees.
It can be calculated either way."""
def evaluateObject(self, subj_py):
"""Angles are in radians. This property is the equivalent in degrees."""
return numeric(subj_py) * 180 / 3.14159265358979323846
def evaluateSubject(self, obj_py):
"""radians(x) -> converts angle x from degrees to radian"""
return numeric(obj_py) * 3.14159265358979323846 / 180
class BI_sin(LightBuiltIn, Function, ReverseFunction):
"""sin(x)
x.math:sin is the sine of x (x measured in radians)."""
def evaluateObject(self, subj_py):
return sin(numeric(subj_py))
def evaluateSubject(self, x):
try:
return asin(numeric(x))
except:
return None
class BI_sinh(LightBuiltIn, Function):
def evaluateObject(self, subj_py):
"""sinh(x)
Return the hyperbolic sine of x."""
return sinh(numeric(subj_py))
# def evaluateSubject(self, x):
# return asinh(numeric(x))
class BI_tan(LightBuiltIn, Function, ReverseFunction):
def evaluateObject(self, subj_py):
"""tan(x)
Return the tangent of x (measured in radians)."""
return tan(numeric(subj_py))
def evaluateSubject(self, x):
"""tan(x)
Return the tangent of x (measured in radians)."""
return atan(numeric(x))
class BI_tanh(LightBuiltIn, Function):
"""tanh(x)
Return the hyperbolic tangent of x."""
def evaluateObject(self, subj_py):
return tanh(numeric(subj_py))
# def evaluateSubject(self, x):
# return atanh(numeric(x))
# Register the string built-ins with the store
def register(store):
str = store.symbol(MATH_NS_URI[:-1])
str.internFrag('cos', BI_cos)
str.internFrag('cosh', BI_cosh)
str.internFrag('degrees', BI_degrees)
str.internFrag('sin', BI_sin)
str.internFrag('sinh', BI_sinh)
str.internFrag('tan', BI_tan)
str.internFrag('tanh', BI_tanh)
if __name__=="__main__":
print __doc__.strip()