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 4483, Fri Sep 2 05:07:01 2016 UTC revision 4484, Fri Sep 2 20:10:56 2016 UTC
# Line 1  Line 1 
 import sys  
 import re  
 import os  
   
 from obj_apply import *  
 from obj_ex import  *  
1  from obj_field import *  from obj_field import *
2  from obj_counter import *  from obj_operator import *
 from obj_frame import  *  
   
 from test_createField import createField  
 from test_writeDiderot import writeDiderot  
 from test_eval import eval  
 from test_compare import compare  
 from test_observed import observed  
   
 from core import *  
 from write import *  
 from var_ty import *  
 from frame import  *  
   
   
3    
4  ##################################################################################################  ##################################################################################################
5  # helper function for intermediate types  # helper function for intermediate types
# Line 27  Line 7 
7  def shape_to_tensor(shape1):  def shape_to_tensor(shape1):
8      d1 = nonefield_dim      d1 = nonefield_dim
9      shapeout = shape1.shape      shapeout = shape1.shape
10      return shapeToTy(shapeout, d1)      (tf, m)= shapeToTyhelper(shapeout, d1)
11        if(tf):
12            return m
13        else:
14            return None
15    
16  # shape to matching field type  # shape to matching field type
17  def shape_to_field(shape1, d1):  def shape_to_field(shape1, d1, k):
18      #print " shape_to_field "," shape: ",shape1.name, " dim: ",d1      #print " shape_to_field "," shape: ",shape1.name, " dim: ",d1
19      shapeout = shape1.shape      shapeout = shape1.shape
20      return shapeToTy(shapeout, d1)      (tf, m)= shapeToTyhelper(shapeout, d1)
21        if(tf):
22            return fty.convertTy(m, k)
23        else:
24            return None
25    
26  def is_nrrd(shape):  def is_nrrd(shape):
27      if((shape == ty_scalarT)):      if((shape == ty_scalarT)):
# Line 49  Line 37 
37  # expected that list is order of dimension [Tensor, d1, d2, d3]  # expected that list is order of dimension [Tensor, d1, d2, d3]
38  # only those that can data generated  # only those that can data generated
39  # ty_noneT used  # ty_noneT used
40  def shape_to_fty(shape):  def shape_to_fty(shape, k):
41      #print "shape", shape      #print "shape", shape
42      if(is_nrrd(shape)):      if(is_nrrd(shape)):
43          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, k), shape_to_field(shape, 2, k), shape_to_field(shape, 3, k)]
44      else:      else:
45          return [shape_to_tensor(shape), None, None,  None]          return [shape_to_tensor(shape), None, None,  None]
46    
# Line 62  Line 50 
50  isNrrd = True # does it need to be supported by a nrrd file  isNrrd = True # does it need to be supported by a nrrd file
51  def convert_rst_binary(fty1, fty2, rty, dim):  def convert_rst_binary(fty1, fty2, rty, dim):
52      def cvt():      def cvt():
53         if((fty.is_Field(fty1)) or (fty.is_Field(fty2))):          if(fty.is_Field(fty1)):
54              return shape_to_field(rty, dim)              k = fty1.k
55                return shape_to_field(rty, dim, k)
56            elif(fty.is_Field(fty2)):
57                k = fty2.k
58                return shape_to_field(rty, dim, k)
59         else:         else:
60              return shape_to_tensor(rty)              return shape_to_tensor(rty)
61      if(isNrrd and (not (is_nrrd(rty)))):      if(isNrrd and (not (is_nrrd(rty)))):
# Line 74  Line 66 
66  def convert_rst_unary(fty1, rty, dim):  def convert_rst_unary(fty1, rty, dim):
67      def cvt():      def cvt():
68          if(fty.is_Field(fty1)):          if(fty.is_Field(fty1)):
69              return shape_to_field(rty, dim)              k = fty1.k
70                return shape_to_field(rty, dim, k)
71          else:          else:
72              return shape_to_tensor(rty)              return shape_to_tensor(rty)
73      if(isNrrd and (not (is_nrrd(rty)))):      if(isNrrd and (not (is_nrrd(rty)))):
# Line 85  Line 78 
78  ##################################################################################################  ##################################################################################################
79  # actual type and dimension  # actual type and dimension
80  # does it still pass?  # does it still pass?
81  def check_tshape(op1,  tshape1, tshape2_shape, dim):  def check1_tshape(op1,  ity, out_shape, dim):
82    
83        def dek(rty1):
84            k = ity.k
85            if(k==0):
86                print "nothing"
87                return None
88            return fty.convertTy(rty1, k-1)
89      if(op1 == op_gradient):      if(op1 == op_gradient):
90          if(fty.is_Field(tshape1)):          if (fty.is_ScalarField(ity)):
91              if(dim ==1):              if(dim ==1):
92                  return ty_scalarF_d1                  return dek(ty_scalarF_d1)
93              elif(dim ==2):              elif(dim ==2):
94                  return ty_vec2F_d2                  return dek(ty_vec2F_d2)
95              elif(dim ==3):              elif(dim ==3):
96                  return ty_vec3F_d3                  return dek(ty_vec3F_d3)
97              else:              else:
98                  return None                  return None
99        elif(op1 == op_divergence):
100            print "mark a"
101            if (ity.id  == ty_vec2F_d2.id):
102                print "mark b"
103                return dek(ty_scalarF_d2)
104            elif(ity.id == ty_vec3F_d3.id):
105                return dek(ty_scalarF_d3)
106          return None          return None
107        elif(op1 == op_curl):
108            if (ity.id  == ty_vec2F_d2.id):
109                return dek(ty_scalarF_d2)
110            elif(ity.id == ty_vec3F_d3.id):
111                    return dek(ty_vec3F_d3)
112      else:      else:
113          return  convert_rst_unary(tshape1, tshape2_shape, dim)              return None
114        elif(op1 == op_jacob):
115            if (ity.id  == ty_vec2F_d2.id):
116                return dek(ty_mat2x2F_d2)
117            elif(ity.id == ty_vec3F_d3.id):
118                return dek(ty_mat3x3F_d3)
119            else:
120                return None
121        else:
122            return  convert_rst_unary(ity, out_shape, dim)
123    
124    # same as above except for binary operations
125    def check2_tshape(op1,  exp1, exp2, rty, dim):
126        def f(e):
127            k = exp1.k
128            return fty.convertTy(e, k)
129        if(op1==op_cross):
130            if(exp1 == ty_vec2F_d2.id):
131                if(exp2 == ty_vec2F_d2.id):
132                    return f(ty_scalarF_d2)
133                elif(exp2 == ty_vec2FT.id):
134                    return f(ty_scalarF_d2)
135                return None
136            elif(exp1 == ty_vec3F_d3.id):
137                if(exp2 == ty_vec3F_d3.id):
138                    return f(ty_vec3F_d3)
139                elif(exp2 == ty_vec3FT.id):
140                    return f(ty_scalarF_d3)
141            elif(exp1 == ty_vec3FT.id and exp2 == ty_vec3FT.id):
142                return (ty_vec3FT)
143            elif(exp1 == ty_vec2FT.id and exp2 == ty_vec2FT.id):
144                return (ty_vec2FT)
145            else:
146                return None
147                        # scaling
148        else:
149            return convert_rst_binary(exp1, exp2, rty, dim)
150    
151    
152  ##################################################################################################  ##################################################################################################
# Line 107  Line 155 
155  # op1*shape1 -> (shape2, tshape)..  # op1*shape1 -> (shape2, tshape)..
156  # tshape1 is fty1  # tshape1 is fty1
157  def op1_to_shape(op1, tty1_orig):  def op1_to_shape(op1, tty1_orig):
158        print "op1: ", op1.name, " tty1: ", tty1_orig.name
159      shape_same = tty1_orig      shape_same = tty1_orig
160      if((op1 == op_negation) or  (op1==op_copy)):      if((op1 == op_negation) or  (op1==op_copy)):
161          return shape_same          return shape_same
# Line 365  Line 414 
414      else:      else:
415          raise Exception ("op1 not supported:"+op1.name)          raise Exception ("op1 not supported:"+op1.name)
416    
 #########################################################################################################  
 #given a field of a particular dim, return all types with that dimension.  
 def d_tofield(dim):  
     if(dim==1):  
         return [ty_scalarF_d1, ty_vec2F_d1, ty_vec3F_d1, ty_vec4F_d1, ty_mat2x2F_d1, ty_mat3x3F_d1]  
     elif(dim==2):  
         return  [ty_scalarF_d2, ty_vec2F_d2, ty_vec3F_d2, ty_vec4F_d2, ty_mat2x2F_d2, ty_mat3x3F_d2]  
     elif(dim==3):  
         return  [ty_scalarF_d3, ty_vec2F_d3, ty_vec3F_d3, ty_vec4F_d3, ty_mat2x2F_d3, ty_mat3x3F_d3]  
     else:  
         return l_all1  
   
 # moving backwards  
 # given opr_outer what are the allowed result types  
 def expected_inshape(op1):  
     # (op1) input type -> output type  
     if(op1 == op_gradient):  
         d1 = [(ty_scalarF_d1, ty_scalarF_d1)]  
         d2 = [(ty_scalarF_d2, ty_vec2F_d2)]  
         d3 =[(ty_scalarF_d3, ty_vec3F_d3)]  
         return d1+d2+d3  
     elif(op1 == op_divergence):  
         d2 = [(ty_vec2F_d2, ty_scalarF_d2)]  
         d3 =[(ty_vec3F_d3, ty_scalarF_d3)]  
         return d2+d3  
     elif(op1 == op_curl):  
         d2 = [(ty_vec2F_d2, ty_scalarF_d2)]  
         d3 =[(ty_vec3F_d3, ty_vec3F_d3)]  
         return d2+d3  
     elif(op1 == op_jacob):  
         d2 = [(ty_vec2F_d2, ty_mat2x2F_d2),(ty_mat2x2F_d2, ty_ten2x2x2F_d2)]  
         d3 =[(ty_vec3F_d3, ty_mat3x3F_d3), (ty_mat3x3F_d3, ty_ten3x3x3F_d3)]  
         return d2+d3  
     elif(op1==op_normalize):  
         m =[]  
         for i in l_all1:  
             if(not (tty.isSca(i))):  
                 m.append([i, i])  
         return m  
     elif((op1 ==op_norm)):  
         m =[]  
         for i in l_all1:  
             m.append((i, shape_to_fty(ty_scalarT)))  
         return m  
   
     elif((op1 == op_negation) or (op1==op_copy)):  
         m =[]  
         for i in l_all1:  
             m.append([i, i])  
         return m  
     elif((op1==op_trace)):  
         d1 = [(ty_mat2x2F_d1, ty_scalarF_d1), (ty_mat3x3F_d1, ty_scalarF_d1), (ty_mat4x4F_d1, ty_scalarF_d1)]  
         d2 = [(ty_mat2x2F_d2, ty_scalarF_d2), (ty_mat3x3F_d2, ty_scalarF_d2), (ty_mat4x4F_d2, ty_scalarF_d2)]  
         d3 = [(ty_mat2x2F_d3, ty_scalarF_d3), (ty_mat3x3F_d3, ty_scalarF_d3), (ty_mat4x4F_d3, ty_scalarF_d3)]  
         return d1+d2+d3  
     elif((op1==op_det)):  
         d1 = [(ty_mat2x2F_d1, ty_scalarF_d1), (ty_mat3x3F_d1, ty_scalarF_d1)]  
         d2 = [(ty_mat2x2F_d2, ty_scalarF_d2), (ty_mat3x3F_d2, ty_scalarF_d2)]  
         d3 = [(ty_mat2x2F_d3, ty_scalarF_d3), (ty_mat3x3F_d3, ty_scalarF_d3)]  
         return d1+d2+d3  
     else:  
         raise Exception ("op1:"+op1.name)  
   
 #given tshape, and arg1, what is arg2 (shape)  
 def backwards_op2_arg1(op1, tshape, arg1):  
     # inner product operation  
     #print "op1:",op1.name, "tshape:", tshape.name, "arg1:",arg1.name  
     if(op1  == op_inner):  
         #(arg1, _) -> tshape  
         if(fty.is_ScalarField(tshape)):  
             if(fty.is_VectorField(arg1)):  
                 return arg1.tensorType  
             else:  
                 # no other types  
                 return None  
         else:  
             #print "notrightthsape", tshape.name,"-",ty_vec2F_d2.name  
             return None  
     elif((op1  == op_add) or  (op1  == op_subtract)):  
         if(tshape==arg1):  
             return arg1.tensorType  
         else:  
             return None  
     else:  
         #print "not rightoperator"  
         raise Exception ("op1:"+op1.name)  
   
 #given tshape, and arg1, what is arg2 (shape)  
 def backwards_op2_arg2(op1, tshape, arg2):  
     # inner product operation  
     #print "op1:",op1.name, "tshape:", tshape.name, "arg1:",arg1.name  
     if(op1  == op_inner):  
         #(arg1, _) -> tshape  
         if(fty.is_ScalarField(tshape)):  
             if(fty.is_VectorField(arg2)):  
                 return arg2.tensorType  
             else:  
                 # no other types  
                 return None  
         else:  
             #print "notrightthsape", tshape.name,"-",ty_vec2F_d2.name  
             return None  
     elif((op1  == op_add) or  (op1  == op_subtract)):  
         if(tshape==arg2):  
             return arg2.tensorType  
         else:  
             return None  
     else:  
         #print "not rightoperator"  
         raise Exception ("op1:"+op1.name)  

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

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