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

SCM Repository

[diderot] View of /branches/ein16/synth/d2/obj_ty.py
ViewVC logotype

View of /branches/ein16/synth/d2/obj_ty.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4456 - (download) (as text) (annotate)
Thu Aug 25 04:03:27 2016 UTC (2 years, 8 months ago) by cchiw
File size: 14421 byte(s)
added missing cubic
# -*- coding: utf-8 -*-

from __future__ import unicode_literals

nonefield_k= -1
nonefield_dim = 0

#tensor types
class tty:
    def __init__(self, id, name, shape):
        self.id=id
        self.name=name
        self.shape=shape
    def toStr(self):
        return ("tensor"+str(self.name))
    def isEq_id(a,b):
        return (a.id==b.id)
    def psize(self):
        p = 1
        for i in self.shape:
            p = p*i
        print self.name+"psize", p 
        return p
    def tyToStr(self):
        s = self.shape
        n = len(s)
        if(n==0):
            return "s"
        elif(n==1):
            [v] = s
            return "v"+str(v)
        elif(n==2):
            [v,m] = s
            return "m"+str(v)+"x"+str(m)
        elif(n==3):
            [v,m,l] = s
            return "t"+str(v)+"x"+str(m)+"x"+str(l)
        return "t"


# field types
class fty:
    def __init__(self, id, name, dim, shape, tensorType, k):
        self.id=id
        self.name=name
        self.dim=dim
        self.shape=shape
        self.tensorType=tensorType # if we probed a field with this field type
        self.k=k
    def toStr(self):
        if(self.dim==nonefield_dim):
            return "tensor "
        else:
            return ("field #"+str(self.k)+"("+str(self.dim)+")"+str(self.shape))
    def get_tensorType(self):
        return self.tensorType
    def get_dim(self):
        return self.dim
    # get vector length
    def get_shape(ty0):
        return  ty0.shape

    def get_vecLength(ty0):
        shape = ty0.shape
        if  (len(shape)==1):
            return shape[0] # vector length
        else:
            raise "unsupported get_vecLength types"
    def is_Field(self):
        return  (not (self.dim==nonefield_dim))
    def is_Tensor(self):
        return  (self.dim==nonefield_dim)
    def is_ScalarField(self):
        return  ((len(self.shape)==0) and fty.is_Field(self))
    def is_VectorField(self):
        return  ((len(self.shape)==1) and fty.is_Field(self))
    def is_MatrixField(self):
        return  ((len(self.shape)==2) and fty.is_Field(self))
    def is_Scalar(self):
        return  (len(self.shape)==0)
    def is_Vector(self):
        return  (len(self.shape)==1)
    def is_Matrix(self):
        return  (len(self.shape)==2)
    def is_Ten3(self):
        return  (len(self.shape)==3)
    def get_first_ix(self):
        return  self.shape[0]
    def get_last_ix(self):
        return  self.shape[len(self.shape)-1]
    def drop_first(self):
        rtn = []
        for i in range(len(self.shape)-1):
            rtn.append(self.shape[i])
        return rtn
    def drop_last(self):
        rtn = []
        for i in range(len(self.shape)-1):
            rtn.append(self.shape[i+1])
        return rtn

    #compares finfo with fty constant
    def isEq_id(a,b):
        return (a.id==b.id)
    #string for diderot program
    def toDiderot(self):
        if(self.dim==0):
            return "tensor "+str(self.shape)
        else:
            return "field#"+str(self.k)+"("+str(self.dim)+")"+str(self.shape)
    #creates ty object
    def convertTy(const,k):
        return  fty(const.id,const.name, const.dim, const.shape, const.tensorType, k)


# ------------------------------ type name to other properties ------------------------------
# shorthand used to refer to different types
# the helper functions match shorthand to other properties and creates ty object

def shapeToStr(s):
    n= len(s)
    if(n==0):
        return "s"
    elif(n==1):
        [v] = s
        return "v"+str(v)
    elif(n==2):
        [v,m] = s
        return "m"+str(v)+"x"+str(m)
    elif(n==3):
        [v,m,l] = s
        return "t"+str(v)+"x"+str(m)+"x"+str(l)
    return "t"

def mkTensor(id, shape):
    #print "id",str(id)
    name = "t_"+shapeToStr(shape)
    return tty(id, name, shape)

ty_noneT = mkTensor(id,[])
# distinctive features of lifted tensors or NoneFields
# are dim=0 and k=-1
def mkNoneField(id, outputtensor):
    #print "id",str(id)
    shape = outputtensor.shape
    name = "T_"+shapeToStr(shape)
    return fty(id, name,nonefield_dim, shape, outputtensor, nonefield_k)

k_init=2#null k

#fields:  #id,name, dim, shape in string form,probe field type returns tensor type
def mkField(id, dim, outputtensor):
    #print "id",str(id)
    name = "F_"+shapeToStr(outputtensor.shape)+"_d"+str(dim)
    return fty(id, name, dim, outputtensor.shape, outputtensor, k_init)

def get_Tshape(ty):
    return ty.shape

#tensors
id=0
ty_scalarT = mkTensor(id,[])
ty_vec1T = mkTensor(id+1,[1])
ty_vec2T = mkTensor(id+2,[2])
ty_vec3T = mkTensor(id+3,[3])
ty_vec4T = mkTensor(id+3,[4])
ty_mat1x1T = mkTensor(id+4,[1,1])
ty_mat2x2T = mkTensor(id+5,[2,2])
ty_mat3x3T = mkTensor(id+6,[3,3])
ty_mat4x4T = mkTensor(id+6,[4,4])
ty_mat2x3T = mkTensor(id+7,[2,3])
ty_mat3x2T = mkTensor(id+8,[3,2])
ty_ten2x2x2T = mkTensor(id+9,[2,2,2])
ty_ten3x3x3T = mkTensor(id+10,[3,3,3])

ty_ten2x3x2T = mkTensor(id+11,[2,3,2])
ty_ten3x2x2T = mkTensor(id+12,[3,2,2])
ty_ten3x3x2T = mkTensor(id+13,[3,3,2])

ty_ten2x3x3T = mkTensor(id+14,[2,3,3])
ty_ten3x2x3T = mkTensor(id+15,[3,2,3])
ty_ten2x2x3T = mkTensor(id+16,[2,2,3])


id=0
#lift tensor to field level
ty_scalarFT = mkNoneField(id, ty_scalarT)
ty_vec1FT = mkNoneField(id+1, ty_vec1T)
ty_vec2FT = mkNoneField(id+2, ty_vec2T)
ty_vec3FT = mkNoneField(id+3, ty_vec3T)
ty_mat2x2FT = mkNoneField(id+4, ty_mat2x2T)
ty_mat3x3FT =mkNoneField(id+5, ty_mat3x3T)
ty_mat2x3FT = mkNoneField(id+6, ty_mat2x3T)
ty_mat3x2FT = mkNoneField(id+7, ty_mat3x2T)
ty_ten2x2x2FT = mkNoneField(id+8, ty_ten2x2x2T)
ty_ten3x3x3FT = mkNoneField(id+9, ty_ten3x3x3T)

#dimension 1
id=10
dim = 1
ty_scalarF_d1 = mkField(id, dim, ty_scalarT)
ty_vec1F_d1 = mkField(id+1, dim, ty_vec1T)
ty_vec2F_d1 = mkField(id+2, dim, ty_vec2T)
ty_vec3F_d1 = mkField(id+3, dim, ty_vec3T)
ty_mat2x2F_d1 = mkField(id+4, dim, ty_mat2x2T)
ty_mat3x3F_d1 = mkField(id+5, dim, ty_mat3x3T)
ty_mat2x3F_d1 = mkField(id+6, dim, ty_mat2x3T)
ty_mat3x2F_d1 = mkField(id+7, dim, ty_mat3x2T)
ty_ten2x2x2F_d1 = mkField(id+8, dim, ty_ten2x2x2T)
ty_ten3x3x3F_d1 = mkField(id+9, dim, ty_ten3x3x3T)
#dimension 2
id=20
dim=2
ty_scalarF_d2 = mkField(id, dim, ty_scalarT)
ty_vec2F_d2 = mkField(id+1, dim, ty_vec2T)
ty_vec3F_d2 = mkField(id+2, dim, ty_vec3T)
ty_mat2x2F_d2 = mkField(id+3, dim, ty_mat2x2T)
ty_mat3x3F_d2 = mkField(id+4, dim, ty_mat3x3T)
ty_mat2x3F_d2 = mkField(id+5, dim, ty_mat2x3T)
ty_mat3x2F_d2 = mkField(id+6, dim, ty_mat3x2T)
ty_ten2x2x2F_d2 = mkField(id+7, dim, ty_ten2x2x2T)
ty_ten3x3x3F_d2 = mkField(id+8, dim, ty_ten3x3x3T)
#dimension 3
id=30
dim=3
ty_scalarF_d3 = mkField(id, dim, ty_scalarT)
ty_vec2F_d3 = mkField(id+1, dim, ty_vec2T)
ty_vec3F_d3 = mkField(id+2, dim, ty_vec3T)
ty_vec4F_d3 = mkField(id+3, dim, ty_vec4T)
ty_mat2x2F_d3 = mkField(id+4, dim, ty_mat2x2T)
ty_mat3x3F_d3 = mkField(id+5, dim, ty_mat3x3T)
ty_mat4x4F_d3 = mkField(id+6, dim, ty_mat4x4T)
ty_mat2x3F_d3 = mkField(id+7, dim, ty_mat2x3T)
ty_mat3x2F_d3 = mkField(id+8, dim, ty_mat3x2T)
ty_ten2x2x2F_d3 = mkField(id+9, dim, ty_ten2x2x2T)
ty_ten3x3x3F_d3 = mkField(id+10, dim, ty_ten3x3x3T)

#list of types
#fields that we can create data
#ty_vec2F_d1,
l_all_F_d1 = [ty_scalarF_d1, ty_vec2F_d1, ty_vec3F_d1, ty_mat2x2F_d1, ty_mat3x3F_d1]
l_all_F_d2 = [ty_scalarF_d2, ty_vec2F_d2, ty_vec3F_d2, ty_mat2x2F_d2, ty_mat3x3F_d2]
l_all_F_d3 = [ty_scalarF_d3, ty_vec2F_d3, ty_vec3F_d3, ty_mat2x2F_d3, ty_mat3x3F_d3]
l_all_F = l_all_F_d1 +l_all_F_d2 + l_all_F_d3
l_all_T = [ty_scalarFT, ty_vec2FT, ty_vec3FT, ty_mat2x2FT, ty_mat3x3FT, ty_mat2x3FT , ty_mat3x2FT, ty_ten2x2x2FT, ty_ten3x3x3FT]
l_all =  l_all_T+l_all_F


# check equal dim
def check_dim(fld,b):
    if(fty.is_Field(b)):
        return (fld.dim==b.dim)
    return true

def get_scaF(es):
    rtn = []
    # binary operator
    for f in es:
        if(fty.is_Scalar(f)):
            rtn.append(f)
    return rtn

def get_scaFnotd1():
    rtn = []
    # binary operator
    for f in l_all_F:
        dim = f.dim
        if(fty.is_Scalar(f) and (dim!=1)):
            rtn.append(f)
    return rtn


#list of vector fields
def get_vecF(es):
    rtn = []
    # binary operator
    for f in es:
        if(fty.is_Vector(f)):
            rtn.append(f)
    return rtn

#list of vector fields (d)=n.
def get_vecF_samedim(es):
    rtn = []
    # binary operator
    for f in es:
        if(fty.is_Vector(f)):
            [n] =f.shape
            if(f.dim== n):
                rtn.append(f)
    return rtn

#list of vector fields (d)=n.
def get_vecF_matF(es):
    rtn = []
    # binary operator
    for f in es:
        if(fty.is_Vector(f) or fty.is_Matrix(f)):
            rtn.append(f)
    return rtn


#list of matrix fields
def get_matF(es):
    rtn = []
    # binary operator
    for f in es:
        if(fty.is_Matrix(f)):
            rtn.append(f)
            #print "ty", f.name
    return rtn


#list of matrix fields
def get_mat_symmal(es):
    rtn = []
    # binary operator
    for f in es:
        if(fty.is_Matrix(f)):
            [n, m] = f.shape
            if(n==m):
                rtn.append(f)
    return rtn


#list of matrix fields
def get_Ten3(es):
    rtn = []
    # binary operator
    for f in es:
        if(fty.is_Ten3(f)):
            rtn.append(f)
    return rtn

#binary operator so two args
def find_field(ty1, ty2):
    dim1=ty1.dim
    dim2=ty2.dim
    if (dim1==0): # tensors
        return (True , ty2)
    elif(dim2==0):# tensors
        return  (True , ty1)
    elif(dim1==dim2):
        return  (True , ty1)
    else :
        return (False, None)

#shape to type
#shape to type
def shapeToTyhelper(shapeout, dim):
    if (dim==nonefield_dim):
        if (shapeout==[]):
            return (True, ty_scalarFT)
        elif (shapeout==[1]):
            return (True, ty_vec1FT)
        elif (shapeout==[2]):
            return  (True,  ty_vec2FT)
        elif (shapeout==[3]):
            return  (True,  ty_vec3FT)
        elif(shapeout==[1,1]):
            return (True, ty_mat1x1FT)
        elif(shapeout==[2,2]):
            return (True, ty_mat2x2FT)
        elif(shapeout==[3,3]):
            return (True, ty_mat3x3FT)
        elif(shapeout==[2,3]):
            return (True, ty_mat2x3FT)
        elif(shapeout==[3,2]):
            return (True, ty_mat3x2FT)
        elif (shapeout==[2,2,2]):
            return (True, ty_ten2x2x2FT)
        elif(shapeout==[3,3, 3]):
            return (True, ty_ten3x3x3FT)
        else:
            #print "shapeout",shapeout,"dim", dim
            return (False, ("unsupported shapeout dim-1 "+ str(shapeout)))
    elif (dim==1):
        if (shapeout==[]):
            return (True, ty_scalarF_d1)
        elif (shapeout==[1]):
            return (True, ty_vec1F_d1)
        elif (shapeout==[2]):
            return  (True,  ty_vec2F_d1)
        elif (shapeout==[3]):
            return  (True,  ty_vec3F_d1)
        elif(shapeout==[1,1]):
            return (True, ty_mat1x1F_d1)
        elif(shapeout==[2,2]):
            return (True, ty_mat2x2F_d1)
        elif(shapeout==[3,3]):
            return (True, ty_mat3x3F_d1)
        elif(shapeout==[2,3]):
            return (True, ty_mat2x3F_d1)
        elif(shapeout==[3,2]):
            return (True, ty_mat3x2F_d1)
        elif (shapeout==[2,2,2]):
            return (True, ty_ten2x2x2F_d1)
        elif(shapeout==[3,3, 3]):
            return (True, ty_ten3x3x3F_d1)
        else:
            #print "shapeout",shapeout,"dim", dim
            return (False, ("unsupported shapeout dim-1 "+ str(shapeout)))
    elif (dim==2):
        if (shapeout==[]):
            return (True, ty_scalarF_d2)
        elif (shapeout==[2]):
            return (True, ty_vec2F_d2)
        elif(shapeout==[3]):
            return (True, ty_vec3F_d2)
        elif (shapeout==[2,2]):
            return (True, ty_mat2x2F_d2)
        elif (shapeout==[3,3]):
            return (True, ty_mat3x3F_d2)
        elif (shapeout==[2,3]):
            return (True, ty_mat2x3F_d2)
        elif (shapeout==[3,2]):
            return (True, ty_mat3x2F_d2)
        elif (shapeout==[2,2,2]):
            return (True,  ty_ten2x2x2F_d2)
        elif(shapeout==[3,3, 3]):
            return (True, ty_ten3x3x3F_d2)
        else:
            #print "shapeout",shapeout,"dim", dim
            return(False, "unsupported shapeout dim-2 "+str(shapeout))
    elif (dim==3):
        if (shapeout==[]):
            return (True, ty_scalarF_d3)
        elif(shapeout==[3]):
            return (True, ty_vec3F_d3)
        elif(shapeout==[3,3]):
            return (True, ty_mat3x3F_d3)
        elif(shapeout==[3,3, 3]):
            return(True,  ty_ten3x3x3F_d3)
        elif(shapeout==[2]):
            return (True,  ty_vec2F_d3)
        elif(shapeout==[2,2]):
            return (True, ty_mat2x2F_d3)
        elif(shapeout==[2,3]):
            return (True, ty_mat2x3F_d3)
        elif(shapeout==[3,2]):
            return (True, ty_mat3x2F_d3)
        elif(shapeout==[2,2,2]):
            return (True, ty_ten2x2x2F_d3)
        else:
            return (False, "unsupported shapeout dim-3"+str(shapeout))
    else:
        return (False, "unsupported dim")

def shapeToTy(shapeout, dim):
    (tf, shape) = shapeToTyhelper(shapeout, dim)
    if(tf):
        return shape
    else:
        raise Exception ("shapeout",shapeout, "dim", dim, "rtn:",shape)
#concat two types to form a new type
def concatTys(ty1, ty2):
    if (fty.is_Vector(ty1)):
        n1 = fty.get_vecLength(ty1)
        if (fty.is_Vector(ty2)):
            n2 = fty.get_vecLength(ty2)
            fldty = find_field(ty1, ty2)
            k =  fldty.k
            if (n1==2):
                if(n2==2):
                    return fty.convertTy(ty_mat2x2F_d2, k)
                else:
                    raise "unsupported concat types"
            elif (n1==3):
                if (n2==3):
                    return fty.convertTy(ty_mat3x3F_d3, k)
                else:
                    raise "unsupported concat types"
    #    elif (fty.is_Matrix(ty2)):
        else:
            raise "unsupported concat types"
    else:
        raise "unsupported concat types"

#reduce shape of fields
def reduceIndex(ty0):
    # keep current k value
    k=ty0.k
    if (fty.isEq_id(ty0, ty_vec2F_d2)):
        return fty.convertTy(ty_scalarF_d2,k)
    elif (fty.isEq_id(ty0, ty_vec3F_d3)):
        return fty.convertTy(ty_scalarF_d3,k)
    else:
        raise Exception ("unsupported field shape:"+ty0.name)


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