Padding, changing key symbols, size improvements, etc
[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": "[sinh]", "type": "text", "text": "sinh", },
21                         "SOUTH": {"action": "[asinh]", "type": "text", "text": "asinh", },
22                         "showAllSlices": False,
23                 },
24                 (0, 1): {
25                         "CENTER": {"action": "[cosh]", "type": "text", "text": "cosh", },
26                         "SOUTH": {"action": "[acosh]", "type": "text", "text": "acosh", },
27                         "showAllSlices": False,
28                 },
29                 (0, 2): {
30                         "CENTER": {"action": "[tanh]", "type": "text", "text": "tanh", },
31                         "SOUTH": {"action": "[atanh]", "type": "text", "text": "atanh", },
32                         "showAllSlices": False,
33                 },
34                 (1, 0): {
35                         "CENTER": {"action": "[exp]", "type": "text", "text": "e ** x", },
36                         "NORTH": {"action": "[log]", "type": "text", "text": "ln", },
37                         "showAllSlices": True,
38                 },
39                 (1, 1): {
40                         "CENTER": {"action": "pi", "type": "text", "text": "pi", },
41                         "NORTH": {"action": "e", "type": "text", "text": "e", },
42                         "showAllSlices": True,
43                 },
44                 (1, 2): {
45                         "CENTER": {"action": "[rad]", "type": "text", "text": "-> rad", },
46                         "NORTH": {"action": "[deg]", "type": "text", "text": "-> deg", },
47                         "showAllSlices": True,
48                 },
49                 (2, 0): {
50                         "CENTER": {"action": "[sin]", "type": "text", "text": "sin", },
51                         "NORTH": {"action": "[asin]", "type": "text", "text": "asin", },
52                         "showAllSlices": False,
53                 },
54                 (2, 1): {
55                         "CENTER": {"action": "[cos]", "type": "text", "text": "cos", },
56                         "NORTH": {"action": "[acos]", "type": "text", "text": "acos", },
57                         "showAllSlices": False,
58                 },
59                 (2, 2): {
60                         "CENTER": {"action": "[tan]", "type": "text", "text": "tan", },
61                         "NORTH": {"action": "[atan]", "type": "text", "text": "atan", },
62                         "showAllSlices": False,
63                 },
64         },
65 }
66 _ICON_PATH = [os.path.join(os.path.dirname(__file__), "images")]
67 PLUGIN = plugin_utils.PieKeyboardPluginFactory(_NAME, _ICON, _MAP, _ICON_PATH)
68
69 pi = operation.Constant("pi", operation.Value(math.pi, operation.render_float_eng))
70 e = operation.Constant("e", operation.Value(math.e, operation.render_float_eng))
71
72 def float_or_complex(float_func, complex_func):
73
74         def switching_func(self, *args, **kwd):
75                 if any(
76                         isinstance(arg, complex)
77                         for arg in args
78                 ):
79                         return complex_func(*args, **kwd)
80                 else:
81                         return float_func(*args, **kwd)
82
83         switching_func.__name__ = complex_func.__name__
84         switching_func.__doc__ = complex_func.__doc__
85         return switching_func
86
87 exp = operation.generate_function(float_or_complex(math.exp, cmath.exp), "exp", operation.Function.REP_FUNCTION, 1)
88 log = operation.generate_function(float_or_complex(math.log, cmath.log), "log", operation.Function.REP_FUNCTION, 1)
89
90 PLUGIN.register_operation("exp", exp)
91 PLUGIN.register_operation("log", log)
92
93 cos = operation.generate_function(float_or_complex(math.cos, cmath.cos), "cos", operation.Function.REP_FUNCTION, 1)
94 acos = operation.generate_function(float_or_complex(math.acos, cmath.acos), "acos", operation.Function.REP_FUNCTION, 1)
95 sin = operation.generate_function(float_or_complex(math.sin, cmath.sin), "sin", operation.Function.REP_FUNCTION, 1)
96 asin = operation.generate_function(float_or_complex(math.asin, cmath.asin), "asin", operation.Function.REP_FUNCTION, 1)
97 tan = operation.generate_function(float_or_complex(math.tan, cmath.tan), "tan", operation.Function.REP_FUNCTION, 1)
98 atan = operation.generate_function(float_or_complex(math.atan, cmath.atan), "atan", operation.Function.REP_FUNCTION, 1)
99
100 PLUGIN.register_operation("cos", cos)
101 PLUGIN.register_operation("acos", acos)
102 PLUGIN.register_operation("sin", sin)
103 PLUGIN.register_operation("asin", asin)
104 PLUGIN.register_operation("tan", tan)
105 PLUGIN.register_operation("atan", atan)
106
107 cosh = operation.generate_function(float_or_complex(math.cosh, cmath.cosh), "cosh", operation.Function.REP_FUNCTION, 1)
108 acosh = operation.generate_function(cmath.acosh, "acosh", operation.Function.REP_FUNCTION, 1)
109 sinh = operation.generate_function(float_or_complex(math.sinh, cmath.sinh), "sinh", operation.Function.REP_FUNCTION, 1)
110 asinh = operation.generate_function(cmath.asinh, "asinh", operation.Function.REP_FUNCTION, 1)
111 tanh = operation.generate_function(float_or_complex(math.tanh, cmath.tanh), "tanh", operation.Function.REP_FUNCTION, 1)
112 atanh = operation.generate_function(cmath.atanh, "atanh", operation.Function.REP_FUNCTION, 1)
113
114 PLUGIN.register_operation("cosh", cosh)
115 PLUGIN.register_operation("acosh", acosh)
116 PLUGIN.register_operation("sinh", sinh)
117 PLUGIN.register_operation("asinh", asinh)
118 PLUGIN.register_operation("tanh", tanh)
119 PLUGIN.register_operation("atanh", atanh)
120
121 deg = operation.generate_function(math.degrees, "deg", operation.Function.REP_FUNCTION, 1)
122 rad = operation.generate_function(math.radians, "rad", operation.Function.REP_FUNCTION, 1)
123
124 PLUGIN.register_operation("deg", deg)
125 PLUGIN.register_operation("rad", rad)
126
127 # In 2.6
128 #phase = operation.generate_function(cmath.phase, "phase", operation.Function.REP_FUNCTION, 1)
129 #polar = operation.generate_function(cmath.polar, "polar", operation.Function.REP_FUNCTION, 1)
130 #rect = operation.generate_function(cmath.rect, "rect", operation.Function.REP_FUNCTION, 1)
131