Trying to better follow DRY
[ejpi] / src / plugins / trig.py
1 from __future__ import division
2
3 import math
4 import cmath
5
6 import operation
7
8 import sys
9 sys.path.append("../")
10 import plugin_utils
11
12
13 _NAME = "Trigonometry"
14 _MAP = {
15         "name": _NAME,
16         "keys": {
17                 (0, 0): {
18                         "CENTER": {"action": "[sinh]", "type": "text", "text": "sinh", },
19                         "SOUTH": {"action": "[asinh]", "type": "text", "text": "asinh", },
20                         "showAllSlices": False,
21                 },
22                 (0, 1): {
23                         "CENTER": {"action": "[cosh]", "type": "text", "text": "cosh", },
24                         "SOUTH": {"action": "[acosh]", "type": "text", "text": "acosh", },
25                         "showAllSlices": False,
26                 },
27                 (0, 2): {
28                         "CENTER": {"action": "[tanh]", "type": "text", "text": "tanh", },
29                         "SOUTH": {"action": "[atanh]", "type": "text", "text": "atanh", },
30                         "showAllSlices": False,
31                 },
32                 (1, 0): {
33                         "CENTER": {"action": "[exp]", "type": "text", "text": "exp", },
34                         "NORTH": {"action": "[log]", "type": "text", "text": "log", },
35                         "showAllSlices": True,
36                 },
37                 (1, 1): {
38                         "CENTER": {"action": "pi", "type": "text", "text": "pi", },
39                         "NORTH": {"action": "e", "type": "text", "text": "e", },
40                         "showAllSlices": True,
41                 },
42                 (1, 2): {
43                         "CENTER": {"action": "[rad]", "type": "text", "text": "rad", },
44                         "NORTH": {"action": "[deg]", "type": "text", "text": "deg", },
45                         "showAllSlices": True,
46                 },
47                 (2, 0): {
48                         "CENTER": {"action": "[sin]", "type": "text", "text": "sin", },
49                         "SOUTH": {"action": "[asin]", "type": "text", "text": "asin", },
50                         "showAllSlices": False,
51                 },
52                 (2, 1): {
53                         "CENTER": {"action": "[cos]", "type": "text", "text": "cos", },
54                         "SOUTH": {"action": "[acos]", "type": "text", "text": "acos", },
55                         "showAllSlices": False,
56                 },
57                 (2, 2): {
58                         "CENTER": {"action": "[tan]", "type": "text", "text": "tan", },
59                         "SOUTH": {"action": "[atan]", "type": "text", "text": "atan", },
60                         "showAllSlices": False,
61                 },
62         },
63 }
64 PLUGIN = plugin_utils.PieKeyboardPluginFactory(_NAME, _MAP, [])
65
66 pi = operation.Constant("pi", operation.Value(math.pi, operation.render_float_eng))
67 e = operation.Constant("e", operation.Value(math.e, operation.render_float_eng))
68
69 def float_or_complex(float_func, complex_func):
70
71         def switching_func(self, *args, **kwd):
72                 if any(
73                         isinstance(arg, complex)
74                         for arg in args
75                 ):
76                         return complex_func(*args, **kwd)
77                 else:
78                         return float_func(*args, **kwd)
79
80         switching_func.__name__ = complex_func.__name__
81         switching_func.__doc__ = complex_func.__doc__
82         return switching_func
83
84 exp = operation.generate_function(float_or_complex(math.exp, cmath.exp), "exp", operation.Function.REP_FUNCTION, 1)
85 log = operation.generate_function(float_or_complex(math.log, cmath.log), "log", operation.Function.REP_FUNCTION, 1)
86
87 PLUGIN.register_operation("exp", exp)
88 PLUGIN.register_operation("log", log)
89
90 cos = operation.generate_function(float_or_complex(math.cos, cmath.cos), "cos", operation.Function.REP_FUNCTION, 1)
91 acos = operation.generate_function(float_or_complex(math.acos, cmath.acos), "acos", operation.Function.REP_FUNCTION, 1)
92 sin = operation.generate_function(float_or_complex(math.sin, cmath.sin), "sin", operation.Function.REP_FUNCTION, 1)
93 asin = operation.generate_function(float_or_complex(math.asin, cmath.asin), "asin", operation.Function.REP_FUNCTION, 1)
94 tan = operation.generate_function(float_or_complex(math.tan, cmath.tan), "tan", operation.Function.REP_FUNCTION, 1)
95 atan = operation.generate_function(float_or_complex(math.atan, cmath.atan), "atan", operation.Function.REP_FUNCTION, 1)
96
97 PLUGIN.register_operation("cos", cos)
98 PLUGIN.register_operation("acos", acos)
99 PLUGIN.register_operation("sin", sin)
100 PLUGIN.register_operation("asin", asin)
101 PLUGIN.register_operation("tan", tan)
102 PLUGIN.register_operation("atan", atan)
103
104 cosh = operation.generate_function(float_or_complex(math.cosh, cmath.cosh), "cosh", operation.Function.REP_FUNCTION, 1)
105 acosh = operation.generate_function(cmath.acosh, "acosh", operation.Function.REP_FUNCTION, 1)
106 sinh = operation.generate_function(float_or_complex(math.sinh, cmath.sinh), "sinh", operation.Function.REP_FUNCTION, 1)
107 asinh = operation.generate_function(cmath.asinh, "asinh", operation.Function.REP_FUNCTION, 1)
108 tanh = operation.generate_function(float_or_complex(math.tanh, cmath.tanh), "tanh", operation.Function.REP_FUNCTION, 1)
109 atanh = operation.generate_function(cmath.atanh, "atanh", operation.Function.REP_FUNCTION, 1)
110
111 PLUGIN.register_operation("cosh", cosh)
112 PLUGIN.register_operation("acosh", acosh)
113 PLUGIN.register_operation("sinh", sinh)
114 PLUGIN.register_operation("asinh", asinh)
115 PLUGIN.register_operation("tanh", tanh)
116 PLUGIN.register_operation("atanh", atanh)
117
118 deg = operation.generate_function(math.degrees, "deg", operation.Function.REP_FUNCTION, 1)
119 rad = operation.generate_function(math.radians, "rad", operation.Function.REP_FUNCTION, 1)
120
121 PLUGIN.register_operation("deg", deg)
122 PLUGIN.register_operation("rad", rad)
123
124 # In 2.6
125 #phase = operation.generate_function(cmath.phase, "phase", operation.Function.REP_FUNCTION, 1)
126 #polar = operation.generate_function(cmath.polar, "polar", operation.Function.REP_FUNCTION, 1)
127 #rect = operation.generate_function(cmath.rect, "rect", operation.Function.REP_FUNCTION, 1)
128