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

SCM Repository

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

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

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

revision 4482, Thu Sep 1 20:43:01 2016 UTC revision 4483, Fri Sep 2 05:07:01 2016 UTC
# Line 31  Line 31 
31    
32  # shape to matching field type  # shape to matching field type
33  def shape_to_field(shape1, d1):  def shape_to_field(shape1, d1):
34      print " shape_to_field "," shape: ",shape1.name, " dim: ",d1      #print " shape_to_field "," shape: ",shape1.name, " dim: ",d1
35      shapeout = shape1.shape      shapeout = shape1.shape
36      return shapeToTy(shapeout, d1)      return shapeToTy(shapeout, d1)
37    
38    def is_nrrd(shape):
39        if((shape == ty_scalarT)):
40            return True
41        elif((shape == ty_vec2T) or (shape == ty_vec3T) or (shape == ty_vec4T)):
42            return True
43        elif((shape  == ty_mat2x2T) or  (shape == ty_mat3x3T)):
44            return True
45        else:
46            return False
47    
48  # convert shape tensor to fty  # convert shape tensor to fty
49  # expected that list is order of dimension [Tensor, d1, d2, d3]  # expected that list is order of dimension [Tensor, d1, d2, d3]
50  # only those that can data generated  # only those that can data generated
51  # ty_noneT used  # ty_noneT used
52  def shape_to_fty(shape):  def shape_to_fty(shape):
53      #print "shape", shape      #print "shape", shape
54      def get():      if(is_nrrd(shape)):
55          return [shape_to_tensor(shape), shape_to_field(shape, 1), shape_to_field(shape, 2), shape_to_field(shape, 3)]          return [shape_to_tensor(shape), shape_to_field(shape, 1), shape_to_field(shape, 2), shape_to_field(shape, 3)]
     if((shape == ty_scalarT)):  
         return get()  
     elif((shape == ty_vec2T) or (shape == ty_vec3T) or (shape == ty_vec4T)):  
         return get()  
     elif((shape  == ty_mat2x2T) or  (shape == ty_mat3x3T)):  
         return get()  
56      else:      else:
57          return [shape_to_tensor(shape), None, None,  None]          return [shape_to_tensor(shape), None, None,  None]
58    
   
59  # converst rty to tshape1.  # converst rty to tshape1.
60  # shape to type  # shape to type
61    # if we want to force the result ot be supportedtypes
62    isNrrd = True # does it need to be supported by a nrrd file
63  def convert_rst_binary(fty1, fty2, rty, dim):  def convert_rst_binary(fty1, fty2, rty, dim):
64      if(fty.is_Field(fty1)):      def cvt():
65          return shape_to_field(rty, dim)         if((fty.is_Field(fty1)) or (fty.is_Field(fty2))):
     elif(fty.is_Field(fty2)):  
66          return shape_to_field(rty, dim)          return shape_to_field(rty, dim)
67      else:      else:
68          return shape_to_tensor(rty)          return shape_to_tensor(rty)
69        if(isNrrd and (not (is_nrrd(rty)))):
70           return None
71        else:
72           return cvt()
73    
74  def convert_rst_unary(fty1, rty, dim):  def convert_rst_unary(fty1, rty, dim):
75        def cvt():
76      if(fty.is_Field(fty1)):      if(fty.is_Field(fty1)):
77          return shape_to_field(rty, dim)          return shape_to_field(rty, dim)
78      else:      else:
79          return shape_to_tensor(rty)          return shape_to_tensor(rty)
80        if(isNrrd and (not (is_nrrd(rty)))):
81            return None
82        else:
83            return cvt()
84    
85  ##################################################################################################  ##################################################################################################
86  # actual type and dimension  # actual type and dimension
# Line 75  Line 88 
88  def check_tshape(op1,  tshape1, tshape2_shape, dim):  def check_tshape(op1,  tshape1, tshape2_shape, dim):
89      if(op1 == op_gradient):      if(op1 == op_gradient):
90          if(fty.is_Field(tshape1)):          if(fty.is_Field(tshape1)):
91              if(dim ==2):              if(dim ==1):
92                    return ty_scalarF_d1
93                elif(dim ==2):
94                  return ty_vec2F_d2                  return ty_vec2F_d2
95              elif(dim ==3):              elif(dim ==3):
96                  return ty_vec3F_d3                  return ty_vec3F_d3
# Line 96  Line 111 
111      if((op1 == op_negation) or  (op1==op_copy)):      if((op1 == op_negation) or  (op1==op_copy)):
112          return shape_same          return shape_same
113      elif(op1==op_norm):      elif(op1==op_norm):
114          return shape_same          return ty_scalarT
115      elif(op1==op_normalize):      elif(op1==op_normalize):
116            if(tty.isSca(tty1_orig)):
117               return None
118          return shape_same          return shape_same
119      elif((op1==op_trace or op1 ==op_det)):      elif((op1==op_trace or op1 ==op_det)):
120            if(tty.isMat(tty1_orig)):
121          return ty_scalarT          return ty_scalarT
122            return None
123      elif(op1==op_transpose):      elif(op1==op_transpose):
124          [i, j] = b.shape          if(tty.isMat(tty1_orig)):
125                [i, j] = tty1_orig.shape
126          return nToshape([j,i])          return nToshape([j,i])
127            return None
128        elif(op1==op_slicem0):
129            if(tty.isMat(tty1_orig)):
130                [i, j] = tty1_orig.shape
131                return nToshape([j])
132            return None
133        elif(op1==op_slicem1):
134            if(tty.isMat(tty1_orig)):
135                [i, j] = tty1_orig.shape
136                return nToshape([i])
137            return None
138        elif((op1==op_slicev0) or (op1==op_slicev1)):
139            if(tty.isVec(tty1_orig)):
140               return ty_scalarT
141            return None
142        elif(op1==op_slicet0 ):
143            if(tty.isTen3(tty1_orig)):
144                [i, j, k] = tty1_orig.shape
145                return nToshape([i,k])
146            return None
147        elif(op1==op_slicet1):
148            if(tty.isTen3(tty1_orig)):
149                [i, j, k] = tty1_orig.shape
150                return nToshape([k])
151            return None
152        elif(op1==op_inverse):
153            if(tty.isMat(tty1_orig)):
154                return tty1_orig
155            return None
156        elif((op1==op_cosine) or  (op1==op_sine) or (op1==op_tangent)):
157            if(tty.isSca(tty1_orig)):
158                return tty1_orig
159            return None
160        elif((op1==op_acosine) or  (op1==op_asine) or (op1==op_atangent) or (op1==op_sqrt)):
161            if(tty.isSca(tty1_orig)):
162                return ty_scalarT
163            return None
164      elif(op1==op_gradient):      elif(op1==op_gradient):
165            if(tty.isSca (tty1_orig)):
166                return ty_noneT
167            return None
168        elif((op1==op_divergence) or  (op1==op_curl) or  (op1==op_jacob)):
169            if(tty.isVec (tty1_orig)):
170                return ty_noneT
171          return None          return None
   
172      else:      else:
173          raise Exception ("unsupported operator: "+op1.name)          raise Exception ("unsupported operator: "+op1.name)
174    
175  def op_to_shape(op1, tty1_orig):  def op2_to_shape(op1, tty1_orig):
176      n = [2,3,4]      n = [2,3,4]
177      # transformations to term      # transformations to term
178      def term_id(term):      def term_id(term):
# Line 122  Line 184 
184      # term: base+[d]      # term: base+[d]
185      # bshape =  transform term with f()      # bshape =  transform term with f()
186      def grow(ashape, es, f):      def grow(ashape, es, f):
187          print "grow"          #print "grow"
188          rtn = []          rtn = []
189          for i in es:          for i in es:
190              (b, _ ) = i              (b, _ ) = i
191              base = b.shape              base = b.shape
192              print "b:", b.name              #print "b:", b.name
193              for d in n:              for d in n:
194                  print "d:", d                  #print "d:", d
195                  # second argument to operator                  # second argument to operator
196                  term = base +[d]                  term = base +[d]
197                  bshape = f(term)                  bshape = f(term)
# Line 137  Line 199 
199                  rshape = ashape +  bshape                  rshape = ashape +  bshape
200                  ty2  = nToshape(term)                  ty2  = nToshape(term)
201                  tshape = nToshape(rshape)                  tshape = nToshape(rshape)
202                  print "Created: ty2 ", ty2 .name, " tshape: ", tshape.name                  #print "Created: ty2 ", ty2 .name, " tshape: ", tshape.name
203                  rtn.append((ty2 , tshape))                  rtn.append((ty2 , tshape))
204          return rtn          return rtn
205      # add index to argument, and make result type      # add index to argument, and make result type
# Line 231  Line 293 
293          else:          else:
294              # limit output size              # limit output size
295              if(tty.isTen3(tty1)):              if(tty.isTen3(tty1)):
296                  print "marka"                  #print "marka"
297                  (ashape, vec) = rtnvec_double(tty1)                  (ashape, vec) = rtnvec_double(tty1)
298                  print "markb"                  #print "markb"
299                  mat = grow_inner(ashape, vec)                  mat = grow_inner(ashape, vec)
300                  print "markc"                  #print "markc"
301                  #ten = grow_inner(ashape, mat)                  #ten = grow_inner(ashape, mat)
302                  return  mat#+ten                  return  mat#+ten
303              elif(tty.isMat(tty1)):              elif(tty.isMat(tty1)):
304                  print "mark-a"                  #print "mark-a"
305                  (ashape, vec) = rtnvec_double(tty1)                  (ashape, vec) = rtnvec_double(tty1)
306                  print "mark-b"                  #print "mark-b"
307                  mat = grow_inner(ashape, vec)                  mat = grow_inner(ashape, vec)
308                  print "mark-c"                  #print "mark-c"
309                      #ten = grow_inner(ashape, mat)                      #ten = grow_inner(ashape, mat)
310                  return  mat#+ten                  return  mat#+ten
311              else:              else:
# Line 303  Line 365 
365      else:      else:
366          raise Exception ("op1 not supported:"+op1.name)          raise Exception ("op1 not supported:"+op1.name)
367    
368    #########################################################################################################
369    #given a field of a particular dim, return all types with that dimension.
370    def d_tofield(dim):
371        if(dim==1):
372            return [ty_scalarF_d1, ty_vec2F_d1, ty_vec3F_d1, ty_vec4F_d1, ty_mat2x2F_d1, ty_mat3x3F_d1]
373        elif(dim==2):
374            return  [ty_scalarF_d2, ty_vec2F_d2, ty_vec3F_d2, ty_vec4F_d2, ty_mat2x2F_d2, ty_mat3x3F_d2]
375        elif(dim==3):
376            return  [ty_scalarF_d3, ty_vec2F_d3, ty_vec3F_d3, ty_vec4F_d3, ty_mat2x2F_d3, ty_mat3x3F_d3]
377        else:
378            return l_all1
379    
380  # moving backwards  # moving backwards
381    # given opr_outer what are the allowed result types
382    def expected_inshape(op1):
383        # (op1) input type -> output type
384        if(op1 == op_gradient):
385            d1 = [(ty_scalarF_d1, ty_scalarF_d1)]
386            d2 = [(ty_scalarF_d2, ty_vec2F_d2)]
387            d3 =[(ty_scalarF_d3, ty_vec3F_d3)]
388            return d1+d2+d3
389        elif(op1 == op_divergence):
390            d2 = [(ty_vec2F_d2, ty_scalarF_d2)]
391            d3 =[(ty_vec3F_d3, ty_scalarF_d3)]
392            return d2+d3
393        elif(op1 == op_curl):
394            d2 = [(ty_vec2F_d2, ty_scalarF_d2)]
395            d3 =[(ty_vec3F_d3, ty_vec3F_d3)]
396            return d2+d3
397        elif(op1 == op_jacob):
398            d2 = [(ty_vec2F_d2, ty_mat2x2F_d2),(ty_mat2x2F_d2, ty_ten2x2x2F_d2)]
399            d3 =[(ty_vec3F_d3, ty_mat3x3F_d3), (ty_mat3x3F_d3, ty_ten3x3x3F_d3)]
400            return d2+d3
401        elif(op1==op_normalize):
402            m =[]
403            for i in l_all1:
404                if(not (tty.isSca(i))):
405                    m.append([i, i])
406            return m
407        elif((op1 ==op_norm)):
408            m =[]
409            for i in l_all1:
410                m.append((i, shape_to_fty(ty_scalarT)))
411            return m
412    
413        elif((op1 == op_negation) or (op1==op_copy)):
414            m =[]
415            for i in l_all1:
416                m.append([i, i])
417            return m
418        elif((op1==op_trace)):
419            d1 = [(ty_mat2x2F_d1, ty_scalarF_d1), (ty_mat3x3F_d1, ty_scalarF_d1), (ty_mat4x4F_d1, ty_scalarF_d1)]
420            d2 = [(ty_mat2x2F_d2, ty_scalarF_d2), (ty_mat3x3F_d2, ty_scalarF_d2), (ty_mat4x4F_d2, ty_scalarF_d2)]
421            d3 = [(ty_mat2x2F_d3, ty_scalarF_d3), (ty_mat3x3F_d3, ty_scalarF_d3), (ty_mat4x4F_d3, ty_scalarF_d3)]
422            return d1+d2+d3
423        elif((op1==op_det)):
424            d1 = [(ty_mat2x2F_d1, ty_scalarF_d1), (ty_mat3x3F_d1, ty_scalarF_d1)]
425            d2 = [(ty_mat2x2F_d2, ty_scalarF_d2), (ty_mat3x3F_d2, ty_scalarF_d2)]
426            d3 = [(ty_mat2x2F_d3, ty_scalarF_d3), (ty_mat3x3F_d3, ty_scalarF_d3)]
427            return d1+d2+d3
428        else:
429            raise Exception ("op1:"+op1.name)
430    
431    #given tshape, and arg1, what is arg2 (shape)
432    def backwards_op2_arg1(op1, tshape, arg1):
433        # inner product operation
434        #print "op1:",op1.name, "tshape:", tshape.name, "arg1:",arg1.name
435        if(op1  == op_inner):
436            #(arg1, _) -> tshape
437            if(fty.is_ScalarField(tshape)):
438                if(fty.is_VectorField(arg1)):
439                    return arg1.tensorType
440                else:
441                    # no other types
442                    return None
443            else:
444                #print "notrightthsape", tshape.name,"-",ty_vec2F_d2.name
445                return None
446        elif((op1  == op_add) or  (op1  == op_subtract)):
447            if(tshape==arg1):
448                return arg1.tensorType
449            else:
450                return None
451        else:
452            #print "not rightoperator"
453            raise Exception ("op1:"+op1.name)
454    
455    #given tshape, and arg1, what is arg2 (shape)
456    def backwards_op2_arg2(op1, tshape, arg2):
457        # inner product operation
458        #print "op1:",op1.name, "tshape:", tshape.name, "arg1:",arg1.name
459        if(op1  == op_inner):
460            #(arg1, _) -> tshape
461            if(fty.is_ScalarField(tshape)):
462                if(fty.is_VectorField(arg2)):
463                    return arg2.tensorType
464                else:
465                    # no other types
466                    return None
467            else:
468                #print "notrightthsape", tshape.name,"-",ty_vec2F_d2.name
469                return None
470        elif((op1  == op_add) or  (op1  == op_subtract)):
471            if(tshape==arg2):
472                return arg2.tensorType
473            else:
474                return None
475        else:
476            #print "not rightoperator"
477            raise Exception ("op1:"+op1.name)

Legend:
Removed from v.4482  
changed lines
  Added in v.4483

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