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