示例#1
0
文件: polmap.py 项目: pylhc/pytpsa
 def eval(self, *v, **a):
     """Compose self with other using the python interpreter. The argument can be a dictionary, another polmap or a set of named arguments.
 >>> from polmap import *
 >>> p=polmap(x='1+x+y')
 >>> print p({'x':4})
 x=5.0 + y
 >>> print p(x=4)
 x=5.0 + y
 >>> print p(polmap(x=4))
 x=5.0 + y
 >>> print p(p)
 x=2.0 + x + 2.0*y
 """
     loc = {}
     try:
         loc.update(v[0])
     except:
         pass
     loc.update(a)
     for n, v in loc.items():
         if isinstance(v, str):
             loc[n] = pol(v)
     out = polmap()
     for n, v in self.items():
         out[n] = pol(v.eval(**loc))
     return out
示例#2
0
文件: polmap.py 项目: pylhc/pytpsa
 def eval(self,*v,**a):
   """Compose self with other using the python interpreter. The argument can be a dictionary, another polmap or a set of named arguments.
   >>> from polmap import *
   >>> p=polmap(x='1+x+y')
   >>> print p({'x':4})
   x=5.0 + y
   >>> print p(x=4)
   x=5.0 + y
   >>> print p(polmap(x=4))
   x=5.0 + y
   >>> print p(p)
   x=2.0 + x + 2.0*y
   """
   loc={}
   try:
     loc.update(v[0])
   except:
     pass
   loc.update(a)
   for n,v in loc.items():
     if isinstance(v,str):
       loc[n]=pol(v)
   out=polmap()
   for n,v in self.items():
     out[n]=pol(v.eval(**loc))
   return out
示例#3
0
文件: polmap.py 项目: pylhc/pytpsa
 def pretty(self):
     """Pretty print
 """
     out = []
     for n, v in self.items():
         out.append('%s=%s' % (n, pol(v).pretty()))
     return '\n'.join(out)
示例#4
0
文件: polmap.py 项目: pylhc/pytpsa
 def pretty(self):
   """Pretty print
   """
   out=[]
   for n,v in self.items():
     out.append('%s=%s' % (n,pol(v).pretty()))
   return '\n'.join(out)
示例#5
0
文件: polmap.py 项目: pylhc/pytpsa
def compose(a, b):
    temp = {}  #bookkeeping vars
    tempvalues = {}  #bookkeeping values
    newm = polmap()  #final map
    lm = 0  #couter for temp values
    for i in a:  #store input pol in temp
        tempvalues[i] = b[i]
    for n, i in a.items():  # for each name,pol in map
        new = pol(0)  # initialize new pol
        new.order = i.order
        new.eps = i.eps
        for j, c in i.items():  # for each mon in pol
            m = []  # prod vector
            for k in range(len(j)):  # expand powers
                vv = i.vars[k]  # mon var
                if vv in b:  # keep
                    m += [i.vars[k]] * j[k]  # add has list of mul
                else:
                    c *= pol(vv)**j[k]  # transfer in the coefficient
#      print "coefficient and monomial"
#      print c,m
#      print "reduce multiplications"
            if m:  # if mon is not a constant
                while len(m) > 1:  # reduce multiplication
                    p = m.pop(), m.pop()  # terms
                    if p in temp:  # if terms already calculated
                        t = temp[p]  # take it
                    else:  # calculate new term
                        lm += 1  # create temp var
                        temp[p] = lm  # bookkeep var
                        t = lm  # store for putting in m
                        newv = tempvalues[p[0]] * tempvalues[p[1]]  # compute
                        tempvalues[t] = newv  # store
                    m.append(t)  # put var
#        print m
                new += c * tempvalues[
                    m[0]]  # add terms, newv carries always the result
            else:
                new += c  # case mon is a constant


#      print new
        newm[n] = new  # put in the resulting map
    return newm
示例#6
0
文件: polmap.py 项目: pylhc/pytpsa
def compose(a,b):
  temp={} #bookkeeping vars
  tempvalues={} #bookkeeping values
  newm=polmap() #final map
  lm=0 #couter for temp values
  for i in a: #store input pol in temp
    tempvalues[i]=b[i]
  for n,i in a.items(): # for each name,pol in map
    new=pol(0) # initialize new pol
    new.order=i.order
    new.eps=i.eps
    for j,c in i.items(): # for each mon in pol
      m=[] # prod vector
      for k in range(len(j)): # expand powers
        vv=i.vars[k] # mon var
        if vv in b: # keep
          m+=[i.vars[k]]*j[k] # add has list of mul
        else:
          c*=pol(vv)**j[k] # transfer in the coefficient
#      print "coefficient and monomial"
#      print c,m
#      print "reduce multiplications"
      if m: # if mon is not a constant
        while len(m)>1: # reduce multiplication
          p=m.pop(),m.pop() # terms
          if p in temp: # if terms already calculated
            t=temp[p]  # take it
          else: # calculate new term
            lm+=1  # create temp var
            temp[p]=lm # bookkeep var
            t=lm # store for putting in m
            newv=tempvalues[p[0]]*tempvalues[p[1]] # compute
            tempvalues[t]=newv # store
          m.append(t) # put var
#        print m
        new+=c*tempvalues[m[0]] # add terms, newv carries always the result
      else:
        new+=c # case mon is a constant
#      print new
    newm[n]=new # put in the resulting map
  return newm
示例#7
0
文件: polmap.py 项目: pylhc/pytpsa
 def reorder(self, vars):
     vars = list(vars)
     for k, v in self.items():
         self[k] = pol(v).reorder(vars)
     return self
示例#8
0
文件: polmap.py 项目: pylhc/pytpsa
 def __init__(self, *s, **vars):
     o = vars.pop('order', 6)
     dict.__init__(self, *s, **vars)
     for k, v in self.items():
         self[k] = pol(val=v, order=o)
示例#9
0
文件: polmap.py 项目: pylhc/pytpsa
 def reorder(self,vars):
   vars=list(vars)
   for k,v in self.items():
     self[k]=pol(v).reorder(vars)
   return self
示例#10
0
文件: polmap.py 项目: pylhc/pytpsa
 def __init__(self,*s,**vars):
   o = vars.pop('order',6)
   dict.__init__(self,*s,**vars)
   for k,v in self.items():
     self[k]=pol(val=v,order=o)
示例#11
0
 def __init__(self,*s,**vars):
   dict.__init__(self,*s,**vars)
   for k,v in self.items():
     self[k]=pol(v)