Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Diff of /branches/ein16/synth/d2/obj_apply.py
ViewVC logotype

Diff of /branches/ein16/synth/d2/obj_apply.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3945, Thu Jun 9 20:00:15 2016 UTC revision 3946, Sat Jun 11 00:39:19 2016 UTC
# Line 8  Line 8 
8  from obj_field import *  from obj_field import *
9    
10  class apply:  class apply:
11      def __init__(self, name, opr, lhs, rhs, oty, isroot):      def __init__(self, name, opr, lhs, rhs, oty, isrootlhs, isrootrhs):
12          self.name=name          self.name=name
13          self.opr=opr          self.opr=opr
14          self.lhs=lhs          self.lhs=lhs
15          self.rhs=rhs          self.rhs=rhs
16          self.oty=oty          self.oty=oty
17          self.isroot=isroot          self.isrootlhs=isrootlhs
18      def toStr(self):          self.isrootrhs=isrootrhs
19          print "Apply: name:",self.name,"opr:",self.opr.name,"isroot:",self.isroot,"oty:"+str(self.oty.name)      def toStr(self, layer):
20            if (self==None):
21                return "None"
22            s = (str(layer)+": opr:"+self.opr.name+" isrootlhs: "+str(self.isrootlhs)+" isrootrhs: "+str(self.isrootrhs)+" oty: "+str(self.oty.name))
23          lhs = self.lhs          lhs = self.lhs
24          rhs= self.rhs          rhs= self.rhs
25          if(self.isroot):          if(self.isrootlhs):
26              print "\tlhs", field.toStr(lhs)              if(lhs!=None):
27                    s+="\n"+str( layer)+"\tlhs:"+ field.toStr(lhs)
28            else:
29                s+= "\nnot a root: look to lhs"
30                if (lhs!=None):
31                    s+="\n"+str( layer)+"lhs:"+apply.toStr(lhs, layer+1)
32                else:
33                    s+="\n"+str( layer)+"lhs:None"
34            if (self.isrootrhs):
35                if(rhs!=None):
36                    s+="\n"+str( layer)+"\trhs:"+field.toStr(rhs)
37            else:
38                s+= "\nnot a root: look to rhs"
39              if (rhs!= None):              if (rhs!= None):
40                  print "\trhs", field.toStr(rhs)                  s+="\n"+str( layer)+"rhs:"+apply.toStr(rhs, layer+1)
41          else:          else:
42              apply.toStr(lhs)                  s+="\n"+str( layer)+"rhs:None"
43          return          return s
44    
45      def checkDim(self):      def checkDim(self):
46          if (self.isroot):          if (self.isroot):
# Line 48  Line 63 
63          return (f,g)          return (f,g)
64    
65      def get_types(self):      def get_types(self):
66          if (self.isroot):          #if (self.isroot):
67              rtn=[]              rtn=[]
68              if(self.lhs!=None):              if(self.lhs!=None):
69                  rtn.append(self.lhs.fldty)                  rtn.append(self.lhs.fldty)
70              if(self.rhs!=None):              if(self.rhs!=None):
71                  rtn.append(self.rhs.fldty)                  rtn.append(self.rhs.fldty)
72              return rtn              return rtn
73          else :              #else :
74              raise "not a root app"              #raise "not a root app"
75    
76      # single list      # single list
77      def get_exps(self):      def get_exps(self):
# Line 79  Line 94 
94                  rtn=rtn+(apply.get_root_types(i))                  rtn=rtn+(apply.get_root_types(i))
95              return rtn              return rtn
96      def get_root_app(self):      def get_root_app(self):
97          if (self.isroot):          if (self.isrootlhs):
98              return self              return self
99          else :          else :
100              if(self.rhs== None):              if(self.rhs== None):
# Line 87  Line 102 
102              else:              else:
103                  raise "more expressions in root"                  raise "more expressions in root"
104    
105        def get_all_Fields(self):
106            flds = []
107            lhs = self.lhs
108            rhs = self.rhs
109            if (lhs!=None):
110                if (self.isrootlhs):
111                    flds.append(lhs)
112                else :
113                    flds = flds + apply.get_all_Fields(lhs)
114            if (rhs!=None):
115                if (self.isrootrhs):
116                    flds.append(rhs)
117                else :
118                    flds = flds + apply.get_all_Fields(rhs)
119            return flds
120    
121      def get_oty(self):      def get_oty(self):
122          return self.oty          return self.oty
123  #------------------------------ helpers -----------------------------------------------------  #------------------------------ helpers -----------------------------------------------------
# Line 134  Line 165 
165              n1 = fty.get_vecLength(ityp1)              n1 = fty.get_vecLength(ityp1)
166              k=ityp1.k              k=ityp1.k
167              if(not checkNd(ityp1)):              if(not checkNd(ityp1)):
168                  return(false, ("curl not supported for this type", op1))                  return(false, ("curl not supported for this type: "+ityp1.name))
169              if((n1==2)):              if((n1==2)):
170                  return (true,fty.convertTy(ty_scalarF_d2,k-1))                  return (true,fty.convertTy(ty_scalarF_d2,k-1))
171              elif(n1==3):              elif(n1==3):
172                  return (true,fty.convertTy(ty_vec3F_d3,k-1))                  return (true,fty.convertTy(ty_vec3F_d3,k-1))
173          else:          else:
174              return(false, ("curl not supported for this type", op1))              return(false, ("curl not supported for this type: "+ityp1.name))
175      elif(op_jacob==op1):            # apply op_jacob      elif(op_jacob==op1):            # apply op_jacob
176          if(fty.is_Vector(ityp1)):          if(fty.is_Vector(ityp1)):
177              n1 = fty.get_vecLength(ityp1)              n1 = fty.get_vecLength(ityp1)
178              k=ityp1.k              k=ityp1.k
179              if(not checkNd(ityp1)):              if(not checkNd(ityp1)):
180                  return(false, ("jacob not supported for this type", op1))                  return(false, ("jacob not supported for this type: "+ityp1.name))
181              if((n1==2)):              if((n1==2)):
182                  return (true,fty.convertTy(ty_mat2x2F_d2, k-1))                  return (true,fty.convertTy(ty_mat2x2F_d2, k-1))
183              elif(n1==3):              elif(n1==3):
184                  return (true,fty.convertTy(ty_mat3x3F_d3, k-1))                  return (true,fty.convertTy(ty_mat3x3F_d3, k-1))
185          else:          else:
186              return (false, ("curl not supported for this type", op1))              return (false, ("curl not supported for this type: "+ityp1.name))
   
187      else:      else:
188          return (false,"operation not represented in unary operations")          raise Exception("operation not represented in unary operations:"+op1.name)
189    
190  #type of field after operation is applied  #type of field after operation is applied
191  def applyBinaryOp(op1,ityps):  def applyBinaryOp(op1,ityps):
192        # print "iypes:",len(ityps),"-", ityps
193    
194      ityp1=ityps[0]      ityp1=ityps[0]
195    
196      ityp2=ityps[1]      ityp2=ityps[1]
197      fldty = find_field(ityp1,ityp2) #assures same dimension for both fields  
198        #print "checking",op1.name,"-",ityp1.name,"-",ityp2.name
199        (tf, fldty) = find_field(ityp1,ityp2) #assures same dimension for both fields
200    
201        if(not tf):
202            return (false, "not the same dimension")
203    
204      k = fldty.k      k = fldty.k
205      dim = fldty.dim      dim = fldty.dim
206    
207      if (op_add==op1) or (op_subtract==op1):      if (op_add==op1) or (op_subtract==op1):
208            if(ityp1.id==ityp2.id):
209          return (true, ityp1) #type unaffected by operation          return (true, ityp1) #type unaffected by operation
210            else:
211                return (false, "not the right shape")
212      elif(op_scale==op1):      elif(op_scale==op1):
213    
214          if(fty.is_Scalar(ityp1) or fty.is_Scalar(ityp2)):          if(fty.is_Scalar(ityp1) or fty.is_Scalar(ityp2)):
215              shape1 = fty.get_shape(ityp1)              shape1 = fty.get_shape(ityp1)
216              shape2 = fty.get_shape(ityp2)              shape2 = fty.get_shape(ityp2)
# Line 174  Line 218 
218              rtn2 = fty.convertTy(rty1, k)              rtn2 = fty.convertTy(rty1, k)
219              return (true, rtn2)              return (true, rtn2)
220          else:          else:
221              (false, "scaling applied to non scalar fields")              return (false, "scaling applied to non scalar fields")
222      elif(op_outer==op1):      elif(op_outer==op1):
223            if(fty.is_Scalar(ityp1) or fty.is_Scalar(ityp2)):
224                return  (false,  "outer product applied to scalar fields")
225          shape1 = fty.get_shape(ityp1)          shape1 = fty.get_shape(ityp1)
226          shape2 = fty.get_shape(ityp2)          shape2 = fty.get_shape(ityp2)
227          rty = shapeToTy(shape1+shape2, dim)          beta = shape1+shape2
228            if (len(beta)>3):
229                return (false,"output to bigfor outer product"+ityp1.name+ityp2.name)
230            n1 = fty.get_last_ix(ityp1)
231            n2= fty.get_first_ix(ityp2)
232            if(n1!=n2): #must have equal vector lengths
233                return  (false, ("must have equal vector length:"+ityp1.name+ityp2.name))
234    
235    
236            rty = shapeToTy(beta, dim)
237          return (true, fty.convertTy(rty, k))          return (true, fty.convertTy(rty, k))
238      elif(op_inner==op1):      elif(op_inner==op1):
239          if(fty.is_Scalar(ityp1) or fty.is_Scalar(ityp2)):          if(fty.is_Scalar(ityp1) or fty.is_Scalar(ityp2)):
# Line 194  Line 249 
249              rty = shapeToTy(shape1+shape2, dim)              rty = shapeToTy(shape1+shape2, dim)
250              return (true, fty.convertTy(rty, k))              return (true, fty.convertTy(rty, k))
251      elif(op_cross==op1):      elif(op_cross==op1):
252            if(fty.is_Scalar(ityp1) or fty.is_Scalar(ityp2)):
253                return  (false,  "cross product applied to scalar fields")
254          if(fty.is_Vector(ityp1) and fty.is_Vector(ityp2)):          if(fty.is_Vector(ityp1) and fty.is_Vector(ityp2)):
255              n1 = fty.get_vecLength(ityp1)              n1 = fty.get_vecLength(ityp1)
256              n2 = fty.get_vecLength(ityp2)              n2 = fty.get_vecLength(ityp2)
# Line 206  Line 263 
263                  return  (false, ("cross product needs to be applied to vectors of the same size", op1.name))                  return  (false, ("cross product needs to be applied to vectors of the same size", op1.name))
264          return  (false, ("cross product needs to be applied to vectors", op1.name))          return  (false, ("cross product needs to be applied to vectors", op1.name))
265      else:      else:
266          return  (false, ("operation not represented in binary operations", op1.name))          raise Exception("operation not represented in binary operations"+ op1.name)
267    
268    
269  # create apply, operator and field objects  # create apply, operator and field objects
# Line 224  Line 281 
281          (F, finfo1, coeff1) = mk_Field(index1, ishape, k, inputfile, dim1)          (F, finfo1, coeff1) = mk_Field(index1, ishape, k, inputfile, dim1)
282          (tf, otype) = applyUnaryOp(opr,[finfo1])          (tf, otype) = applyUnaryOp(opr,[finfo1])
283          if (tf==false):          if (tf==false):
284              raise "can not apply unary operator"              raise Exception(otype)
285          z1 = apply(name, opr,F, None, otype, true)          z1 = apply(name, opr,F, None, otype, true, true)
286          return (z1, [coeff1], dim1, otype)          return (z1, [coeff1], dim1, otype)
287      elif (arity==2): # binary operator      elif (arity==2): # binary operator
288          index2=1          index2=1
# Line 234  Line 291 
291          (F, finfo1, coeff1) = mk_Field(index1, ishape, k, inputfile, dim1)          (F, finfo1, coeff1) = mk_Field(index1, ishape, k, inputfile, dim1)
292          (G, finfo2, coeff2) = mk_Field(index2, ishape, k, inputfile, dim2)          (G, finfo2, coeff2) = mk_Field(index2, ishape, k, inputfile, dim2)
293          finfos= [finfo1, finfo2]          finfos= [finfo1, finfo2]
294          (_,otype) = applyBinaryOp(opr, finfos)          #print "opr",opr.name,"finfos",len(finfos)
295          z = apply(name, opr, F,G, otype,true)          (tf,otype) = applyBinaryOp(opr, finfos)
296            if (tf==false):
297                raise Exception(otype)
298            z = apply(name, opr, F,G, otype,true, true)
299          coeffs = [coeff1,coeff2]          coeffs = [coeff1,coeff2]
300          fldty = find_field(ity1, ity2) # dimension for field involved          (_,fldty) = find_field(ity1, ity2) # dimension for field involved
301          dimF= fldty.dim          dimF= fldty.dim
302          return (z, coeffs, dimF, otype)          return (z, coeffs, dimF, otype)
303      else:      else:
304          raise Exception ("arity is not supported: "+str(arity))          raise Exception ("arity is not supported: "+str(arity))
305    
306  # embed inside a unary operator  # embed inside a unary operator
307  def mkApply_opr(name,opr, z1,otype1):  def mkApply_opr(name, opr, lhs, rhs, otypes):
308      arity=opr.arity      arity=opr.arity
309      if (arity==1): # unary operator      if (arity==1): # unary operator
310          (tf, otype2) = applyUnaryOp(opr, [otype1])          otype1 = otypes[0]
311            (tf, otypertn) = applyUnaryOp(opr, [otype1])
312          if (tf==false):          if (tf==false):
313              raise "can not apply unary operator"              raise "can not apply unary operator"
314          z2 = apply(name, opr,z1,None, otype2, false)          z2 = apply(name, opr,lhs, rhs, otypertn, false, true)
315            return z2
316        elif (arity==2): # binary operator
317            otype1 = otypes[0]
318            otype2 = otypes[1]
319            (tf, otypertn) = applyBinaryOp(opr, [otype1,otype2])
320            if (tf==false):
321                raise "can not apply unary operator"
322            z2 = apply(name, opr,lhs, rhs, otypertn, false, true)
323          return z2          return z2
324      else:      else:
325          raise Exception ("arity is not supported: "+str(arity))          raise Exception ("arity is not supported: "+str(arity))
326    
327  def mkApply_twice(name,opr_inner,opr_outer, ishape, inputfile):  def mkApply_twice(name,opr_inner,opr_outer, ishape, inputfile):
328      arity=opr_outer.arity      arity=opr_outer.arity
329        #print "mkapply twice"
330        #print "ishape:",len(ishape),"-", ishape
331      if (arity==1): # unary operator      if (arity==1): # unary operator
332          (z1, coeffs, dim1, otype1) =  mkApply_fld(name,opr_inner,ishape, inputfile)          (z1, coeffs, dim1, otype1) =  mkApply_fld(name,opr_inner,ishape, inputfile)
333          z2 = mkApply_opr(name,opr_outer, z1,otype1)          z2 = mkApply_opr(name, opr_outer, z1, None, [otype1])
334          #apply.toStr(z1)          #apply.toStr(z1)
335          #apply.toStr(z2)          #apply.toStr(z2)
336            # print "coeff1",coeffs
337            return (z2, coeffs, dim1)
338        elif (arity==2): # binary operator
339            # first is an application
340    
341            (z1, coeff1, dim1, otype1) =  mkApply_fld(name, opr_inner, ishape, inputfile)
342            # second is just a field
343            id = 2 # index of field exp
344            ity2 =ishape[id]
345            k = 2
346            dim2  = fty.get_dim(ity2)
347    
348            (F2, finfo2, coeff2) = mk_Field(id, ishape, k, inputfile+"cat", dim2)
349            #print "coeff2",coeff2
350            #print "coeff1",len(coeff1),"-",coeff1
351            #print "coeff1-a",ishape[0].name,"-",len(coeff1[0]),"-",coeff1[0]
352            #print "coeff1-b",ishape[1].name,"-",len(coeff1[1]),"-",coeff1[1]
353            #print "coeff2",ishape[2].name,"-",len(coeff2),"-",coeff2
354            coeffs = coeff1+[coeff2]
355            #print "coeffs",coeffs
356            rhs = F2
357    
358            z2 = mkApply_opr(name,opr_outer, z1, rhs, [otype1, finfo2])
359            #apply.toStr(z1)
360            #apply.toStr(z2)
361    
362          return (z2, coeffs, dim1)          return (z2, coeffs, dim1)
363      else:      else:
364          raise Exception ("arity is not supported: "+str(arity))          raise Exception ("arity is not supported: "+str(arity))

Legend:
Removed from v.3945  
changed lines
  Added in v.3946

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0