This repository has been archived by the owner on Feb 5, 2021. It is now read-only.
forked from rdemaria/pytpsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dop.py
70 lines (59 loc) · 1.69 KB
/
dop.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#Copyright (c) 2008, Riccardo De Maria
#All rights reserved.
from pol import pol, normder
from polmap import polmap
class dop(pol):
def __init__(self,val=None,order=None,eps=1E-13,loc={},m='eval'):
self.coef={}
self.vars=[]
self.order=1000
self.eps=eps
if val!=None:
if isinstance(val,dop):
self.coef.update(val.coef)
self.vars=val.vars[:]
self.order=val.order
self.eps=val.eps
elif isinstance(val,str):
if m=='eval':
c=compile(val,'eval','eval')
l=dict( (i,dop(i,m='name')) for i in c.co_names)
l.update(loc)
dop.__init__(self,eval(c,{},l))
elif m=='name':
self.vars=[val]
self.coef[(1,)]=pol(1.)
if order is not None:
self.order=order
def _derpol(self,other):
new=pol(order=min(self.order,other.order))
new.vars=list(set(other.vars+self.vars))
for i in self.coef:
for j in other.coef:
c=self.coef[i]*other.coef[j]
expi=dict(zip(self.vars,i))
expj=dict(zip(other.vars,j))
newexp=tuple([-expi.get(k,0)+expj.get(k,0) for k in new.vars])
for k in self.vars:
c*=normder(expj.get(k,0), expi.get(k,0))
new.coef[newexp]=new.coef.get(newexp,0.)+c
return new.truncate().dropneg()
def __call__(self,other):
if isinstance(other,pol):
return self._derpol(other)
elif isinstance(other,polmap):
m=polmap(other)
for n,v in m.items():
m[n]=self(v)
return m
# def truncate(self):
# return self
def pop(h,vars):
h=pol(h)
vars=list(vars)
out=0
while vars:
x=vars.pop(0)
p=vars.pop(0)
out+=dop(x)*(-dop(p)(h))+dop(p)*dop(x)(h)
return out