Initial checkin
[ejpi] / src / plugins / trig.py
1 from __future__ import division
2
3 import os
4 import operator
5 import math
6 import cmath
7
8 import operation
9
10 import sys
11 sys.path.append("../")
12 import plugin_utils
13
14
15 _MAP_FILE_PATH = os.path.join(os.path.dirname(__file__), "trig.map")
16 PLUGIN = plugin_utils.PieKeyboardPluginFactory("Trigonometry", _MAP_FILE_PATH)
17
18 pi = operation.Constant("pi", operation.Value(math.pi, operation.render_float_eng))
19 e = operation.Constant("e", operation.Value(math.e, operation.render_float_eng))
20
21 def float_or_complex(float_func, complex_func):
22
23         def switching_func(self, *args, **kwd):
24                 if any(
25                         isinstance(arg, complex)
26                         for arg in args
27                 ):
28                         return complex_func(*args, **kwd)
29                 else:
30                         return float_func(*args, **kwd)
31
32         switching_func.__name__ = complex_func.__name__
33         switching_func.__doc__ = complex_func.__doc__
34         return switching_func
35
36 exp = operation.generate_function(float_or_complex(math.exp, cmath.exp), "exp", operation.Function.REP_FUNCTION, 1)
37 log = operation.generate_function(float_or_complex(math.exp, cmath.exp), "log", operation.Function.REP_FUNCTION, 1)
38
39 PLUGIN.register_operation("exp", exp)
40 PLUGIN.register_operation("log", log)
41
42 cos = operation.generate_function(float_or_complex(math.cos, cmath.cos), "cos", operation.Function.REP_FUNCTION, 1)
43 acos = operation.generate_function(float_or_complex(math.acos, cmath.acos), "acos", operation.Function.REP_FUNCTION, 1)
44 sin = operation.generate_function(float_or_complex(math.sin, cmath.sin), "sin", operation.Function.REP_FUNCTION, 1)
45 asin = operation.generate_function(float_or_complex(math.asin, cmath.asin), "asin", operation.Function.REP_FUNCTION, 1)
46 tan = operation.generate_function(float_or_complex(math.tan, cmath.tan), "tan", operation.Function.REP_FUNCTION, 1)
47 atan = operation.generate_function(float_or_complex(math.atan, cmath.atan), "atan", operation.Function.REP_FUNCTION, 1)
48
49 PLUGIN.register_operation("cos", cos)
50 PLUGIN.register_operation("acos", acos)
51 PLUGIN.register_operation("sin", sin)
52 PLUGIN.register_operation("asin", asin)
53 PLUGIN.register_operation("tan", tan)
54 PLUGIN.register_operation("atan", atan)
55
56 cosh = operation.generate_function(float_or_complex(math.cosh, cmath.cosh), "cosh", operation.Function.REP_FUNCTION, 1)
57 acosh = operation.generate_function(cmath.acosh, "acosh", operation.Function.REP_FUNCTION, 1)
58 sinh = operation.generate_function(float_or_complex(math.sinh, cmath.sinh), "sinh", operation.Function.REP_FUNCTION, 1)
59 asinh = operation.generate_function(cmath.asinh, "asinh", operation.Function.REP_FUNCTION, 1)
60 tanh = operation.generate_function(float_or_complex(math.tanh, cmath.tanh), "tanh", operation.Function.REP_FUNCTION, 1)
61 atanh = operation.generate_function(cmath.atanh, "atanh", operation.Function.REP_FUNCTION, 1)
62
63 PLUGIN.register_operation("cosh", cosh)
64 PLUGIN.register_operation("acosh", acosh)
65 PLUGIN.register_operation("sinh", sinh)
66 PLUGIN.register_operation("asinh", asinh)
67 PLUGIN.register_operation("tanh", tanh)
68 PLUGIN.register_operation("atanh", atanh)
69
70 deg = operation.generate_function(math.degrees, "deg", operation.Function.REP_FUNCTION, 1)
71 rad = operation.generate_function(math.radians, "rad", operation.Function.REP_FUNCTION, 1)
72
73 PLUGIN.register_operation("deg", deg)
74 PLUGIN.register_operation("rad", rad)
75
76 # In 2.6
77 #phase = operation.generate_function(cmath.phase, "phase", operation.Function.REP_FUNCTION, 1)
78 #polar = operation.generate_function(cmath.polar, "polar", operation.Function.REP_FUNCTION, 1)
79 #rect = operation.generate_function(cmath.rect, "rect", operation.Function.REP_FUNCTION, 1)
80