-
Notifications
You must be signed in to change notification settings - Fork 1
/
infixnroot.py
49 lines (43 loc) · 1.5 KB
/
infixnroot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# definition of an Infix operator class
# this recipe also works in jython
# calling sequence for the infix is either:
# x |op| y
# or:
# x <<op>> y
class Infix:
def __init__(self, function):
self.function = function
def __ror__(self, other):
return Infix(lambda x, self=self, other=other: self.function(other, x))
def __or__(self, other):
return self.function(other)
def __pow__(self, other): # For hi prec. reverse order bec. right-assoc.
return Infix(lambda x, self=self, other=other: self.function(x, other))
def __rpow__(self, other):
return self.function(other)
def __rlshift__(self, other):
return Infix(lambda x, self=self, other=other: self.function(other, x))
def __rshift__(self, other):
return self.function(other)
def __call__(self, value1, value2):
return self.function(value1, value2)
## http://code.activestate.com/recipes/384122-infix-operators/
from sympy import root, log
nroot = ROOT = Infix(lambda n,x: root(x,n))
# Maybe could be less clunky, but I think it works in most cases.
# Just output "log_" as "LOG._"
class LogmakerA:
def __init__(self, val):
self.val = val
def __call__(self, other):
return log(other, self.val)
class LogmakerP:
def __pow__(self, other):
return LogmakerA(other)
def __getattr__(self, at):
if len(at) < 2:
return self
return LogmakerA(at[1:])
def __call__(self, arg):
return LogmakerA(arg)
LOG=LogmakerP()