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