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

SCM Repository

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

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

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

revision 3873, Wed May 18 13:38:23 2016 UTC revision 3874, Wed May 18 16:45:54 2016 UTC
# Line 3  Line 3 
3  from test_examples import value_probe  from test_examples import value_probe
4  from test_examples import value_negation  from test_examples import value_negation
5  from test_examples import value_gradient  from test_examples import value_gradient
6    from test_examples import value_add
7    from test_examples import value_subtract
8    from test_examples import value_outer
9    from test_examples import value_inner
10  from test_examples import value_scalarFd2  from test_examples import value_scalarFd2
11  from test_examples import value_vec2Fd2  from test_examples import value_vec2Fd2
12    from test_examples import value_mat2x2Fd2
13  from test_examples import value_scalarT  from test_examples import value_scalarT
14  from test_examples import value_vec2T  from test_examples import value_vec2T
15    from test_examples import value_mat2x2T
16    
17  #fixed variables  #fixed variables
18  op_probe = value_probe()  op_probe = value_probe()
19  op_negation = value_negation()  op_negation = value_negation()
20  op_gradient = value_gradient()  op_gradient = value_gradient()
21    op_add = value_add()
22    op_subtract = value_subtract()
23    op_outer = value_outer()
24    op_inner = value_inner()
25  ty_scalarF_d2 = value_scalarFd2()  ty_scalarF_d2 = value_scalarFd2()
26  ty_vec2F_d2 = value_vec2Fd2()  ty_vec2F_d2 = value_vec2Fd2()
27    ty_mat2x2F_d2=value_mat2x2Fd2()
28  ty_scalarT = value_scalarT()  ty_scalarT = value_scalarT()
29  ty_vec2T = value_vec2T()  ty_vec2T = value_vec2T()
30    ty_mat2x2T=value_mat2x2T()
31    
32  def printFunc(a,b,c,d,e,f,g,h,i):  def digest(coeff):
33        (base, xsq, ysq, diag)=coeff
34        b = base[0]
35        c = base[1]
36        d = xsq[0]
37        g = xsq[1]
38        f = ysq[0]
39        h = ysq[1]
40        a = diag[0]
41        e = diag[1]
42        i = diag[2]
43        return (a,b,c,d,e,f,g,h,i)
44    
45    def printFunc(name, coeff):
46        (a,b,c,d,e,f,g,h,i)= digest(coeff)
47        exp= name+"="
48      if(b!=0):      if(b!=0):
49          print b, "*x"          exp+= str(b)+ "*x"
50      if(c!=0):      if(c!=0):
51          print " +", c,"y"          exp+= " +"+ str(c)+"y"
52      if(d!=0):      if(d!=0):
53          print " +", d, "x^2"          exp+= " +"+ str(d)+ "x^2"
54      if(g!=0):      if(g!=0):
55          print " + ", g, "y*x^2"          exp+= " + "+ str(g)+ "y*x^2"
56      if(f!=0):      if(f!=0):
57          print " + ", f, "y^2"          exp+= " + "+ str(f)+ "y^2"
58      if(h!=0):      if(h!=0):
59          print " + ", h, "x*y^2"          exp+= " + "+ str(h)+ "x*y^2"
60      if(a!=0):      if(a!=0):
61          print " + ", a          exp+= " + "+ str(a)
62      if(e!=0):      if(e!=0):
63              print " + ", e, "x*y"          exp+= " + "+ str(e)+ "x*y"
64      if(i!=0):      if(i!=0):
65          print " + ", i, "x^2*y^2"          exp+= " + "+ str(i)+ "x^2*y^2"
66        print exp
 #evaluate field defined by coefficients at position  
 def eval(x,y,a,b,c,d,e,f,g,h,i):  
     #evaluate field defined by coefficients at position  
     t0 = b*x + c*y  
     t1 = d*x*x + g*y*x*x  
     t2 = f*y*y + h*x*y*y  
     t3 = a + e*x*y + i*(y*y)*(x*x)  
     t4= t0+t1+t2+t3  
     return t4  
   
 def negation(x,y,a,b,c,d,e,f,g,h,i):  
     return (-1*eval(x,y,a,b,c,d,e,f,g,h,i))  
67    
68  #gradient of field defined by coefficients at position  #gradient of field defined by coefficients at position
69  def grad(x,y,a,b,c,d,e,f,g,h,i):  def grad(x,y,coeff):
70        (a,b,c,d,e,f,g,h,i)= digest(coeff)
71      x0 = b      x0 = b
72      x1 = d*2*x + g*y*2*x      x1 = d*2*x + g*y*2*x
73      x2 = h*y*y      x2 = h*y*y
# Line 64  Line 80 
80      y4= y0+y1+y2+y3 #yaxis      y4= y0+y1+y2+y3 #yaxis
81      return [x4,y4]      return [x4,y4]
82    
83  def iter(k, pos, base, xsq, ysq, diag):  #evaluate field defined by coefficients at position
84    def eval(x,y,coeff):
85        (a,b,c,d,e,f,g,h,i)= digest(coeff)
86        #evaluate field defined by coefficients at position
87        t0 = b*x + c*y
88        t1 = d*x*x + g*y*x*x
89        t2 = f*y*y + h*x*y*y
90        t3 = a + e*x*y + i*(y*y)*(x*x)
91        t4= t0+t1+t2+t3
92        return t4
93    
94    # create a vector field
95    def mkVectorField(a):
96        # hard-coded augmentation
97        return [a,2*a]
98    
99    #evaluate vector field
100    def evalv2(x,y,coeff):
101        return mkVectorField(eval(x,y,coeff))
102    
103    def negation(x,y,coeff):
104        return (-1*eval(x,y,coeff))
105    def negationv2(x,y,coeff):
106        return  mkVectorField(-1*eval(x,y,coeff))
107    
108    
109    #evaluate addition of scalar fields
110    def add(x,y,coeff1,coeff2):
111        return eval(x,y,coeff1) + eval(x,y,coeff2)
112    def addv2(x,y,coeff1,coeff2):
113        return mkVectorField(eval(x,y,coeff1) + eval(x,y,coeff2))
114    
115    
116    #evaluate subtraction of scalar fields
117    def subtract(x,y,coeff1,coeff2):
118        return eval(x,y,coeff1) - eval(x,y,coeff2)
119    def subtractv2(x,y,coeff1,coeff2):
120        return mkVectorField (eval(x,y,coeff1) - eval(x,y,coeff2))
121    
122    #evaluate outer product
123    def outerv2(x,y,coeff1,coeff2):
124        # coeff same for each axis
125        a = mkVectorField(eval(x,y,coeff1))
126        b = mkVectorField(eval(x,y,coeff2))
127        rtn=[]
128        for i in  range(2):
129            for j in range(2):
130                rtn.append(a[i]*b[j])
131        return rtn
132    
133    #evaluate inner product
134    def innerv2(x,y,coeff1,coeff2):
135        # coeff same for each axis
136        a = mkVectorField(eval(x,y,coeff1))
137        b = mkVectorField(eval(x,y,coeff2))
138        rtn = 0
139        for i in  range(2):
140            rtn+= a[i]*b[i]
141        return rtn
142    
143    
144    def iter(k, pos, coeff):
145      corr = []      corr = []
146      b = base[0]      printFunc("F0",coeff)
     c = base[1]  
     d = xsq[0]  
     g = xsq[1]  
     f = ysq[0]  
     h = ysq[1]  
     a = diag[0]  
     e = diag[1]  
     i = diag[2]  
147      for p in pos:      for p in pos:
148          x=p[0]          x=p[0]
149          y=p[1]          y=p[1]
150          val = k(x,y,a,b,c,d,e,f,g,h,i)          val = k(x,y,coeff)
151          corr.append(val)          corr.append(val)
152      return corr      return corr
153    
154    def iter2(k, pos, coeff1,coeff2):
155  def unary(ex, pos,base,xsq,ysq,diag):      corr = []
156      if(ex==op_probe): #probing      printFunc("F0",coeff1)
157          return iter(eval,pos,base,xsq,ysq,diag)      printFunc("F1",coeff2)
158      elif(ex==op_negation): #negation      for p in pos:
159          return iter(negation,pos,base,xsq,ysq,diag)          x=p[0]
160      elif(ex==op_gradient): #gradient          y=p[1]
161          return iter(grad,pos,base,xsq,ysq,diag)          val = k(x,y,coeff1,coeff2)
162      else:          corr.append(val)
163          raise ("no operator value for "+str(ex))      return corr
164    
165    
166    # binary operator with scalar field and vector field
167    def unary(opr, itypes, pos, coeffs):
168        (op_name, arity) = opr
169        if (len(coeffs)!=arity):
170            n=len(coeffs)
171            raise Exception("coeffs: "+str(n)+" arity: "+str(arity))
172        if (arity==1):
173            # unary operator
174            coeff1=coeffs[0]
175            ityp1=itypes[0]
176            if(opr==op_probe): #probing
177                if (ityp1==ty_scalarF_d2): # input is a scalar field
178                    return iter(eval,pos,coeff1)
179                elif(ityp1==ty_vec2F_d2): # input is a vector field
180                    return iter(evalv2,pos,coeff1)
181            elif(opr==op_negation): #negation
182                if (ityp1==ty_scalarF_d2): # input is a scalar field
183                    return iter(negation,pos,coeff1)
184                elif(ityp1==ty_vec2F_d2):
185                    return iter(negationv2,pos,coeff1)
186            elif(opr==op_gradient): #gradient
187                if (ityp1==ty_scalarF_d2): # input is a scalar field
188                    return iter(grad,pos,coeff1)
189                else:
190                    raise Exception("vector field: no operator value for "+str(op_name))
191            else:
192                raise Exception("unsupported unary operator:",op_name)
193        elif (arity==2):
194            # binary operator
195            coeff1=coeffs[0]
196            coeff2=coeffs[1]
197            ityp1=itypes[0]
198            ityp2=itypes[1]
199            if(opr==op_add): #adding
200                if (ityp1==ty_scalarF_d2) and (ityp1==ityp2): #input is a scalar field
201                    return iter2(add,pos,coeff1,coeff2)
202                elif (ityp1==ty_vec2F_d2) and (ityp1==ityp2): #input is a vector field
203                    return iter2(addv2,pos,coeff1,coeff2)
204                else:
205                    raise ValueError("unsupported type for this operation: ",(op_name))
206            elif(opr==op_subtract): #subtraction
207                if (ityp1==ty_scalarF_d2) and (ityp1==ityp2): #input is a scalar field
208                    return iter2(subtract,pos,coeff1,coeff2)
209                elif (ityp1==ty_vec2F_d2) and (ityp1==ityp2): #input is a vector field
210                    return iter2(subtractv2,pos,coeff1,coeff2)
211                else:
212                    raise ValueError("unsupported type for this operation: ",(op_name))
213            elif(opr==op_outer): #outer product
214                if (ityp1==ty_vec2F_d2) and (ityp1==ityp2): #input is a vector field
215                    return iter2(outerv2,pos,coeff1,coeff2)
216                else:
217                    raise ValueError("unsupported type: ", ityp1," for this operation: ",(op_name))
218            elif(opr==op_inner): # inner product
219                if (ityp1==ty_vec2F_d2) and (ityp1==ityp2): #input is a vector field
220                    return iter2(innerv2,pos,coeff1,coeff2)
221                else:
222                    raise ValueError("unsupported type: ", ityp1," for this operation: ",(op_name))
223            else:
224                    raise RuntimeError("binary operator is not yet supported ",(op_name))
225        else:
226            raise Exception ("arity is not supported: "+str(arity))
227    

Legend:
Removed from v.3873  
changed lines
  Added in v.3874

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