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 4243 - (download) (as text) (annotate)
Thu Jul 21 17:54:34 2016 UTC (3 years, 2 months ago) by cchiw
File size: 12279 byte(s)
added modulate
# -*- coding: utf-8 -*-

from __future__ import unicode_literals

#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)

# 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==0):
            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  (self.dim>0)
    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 tyToStr(s):
    n=len(s)
    if(n==0):
        return "sc"
    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_"+tyToStr(shape)
    return tty(id, name, shape)

ty_noneT = mkTensor(id,[])
def mkNoneField(id, shape):
    #print "id",str(id)
    name = "N_"+tyToStr(shape)
    return fty(id, name, 0, shape, ty_noneT, -1)

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_"+tyToStr(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+1,[])
ty_vec1T = mkTensor(id+2,[1])
ty_vec2T = mkTensor(id+3,[2])
ty_vec3T = mkTensor(id+4,[3])
ty_mat1x1T = mkTensor(id+5,[1,1])
ty_mat2x2T = mkTensor(id+6,[2,2])
ty_mat3x3T = mkTensor(id+7,[3,3])
ty_ten2x2x2T = mkTensor(id+8,[2,2,2])
ty_ten3x3x3T = mkTensor(id+9,[3,3,3])

id=0
#lift tensor to field level
ty_scalarFT = mkNoneField(id,[] )
ty_vec1FT = mkNoneField(id+1, [2] )
ty_vec2FT = mkNoneField(id+2, [2] )
ty_vec3FT = mkNoneField(id+3, [3] )
ty_mat2x2FT = mkNoneField(id+4, [2,2] )
ty_mat3x3FT =mkNoneField(id+5, [3,3] )
ty_mat2x2x2FT = mkNoneField(id+6, [2,2,2] )
ty_mat3x3x3FT = mkNoneField(id+7, [3,3,3] )

#dimension 1
id=10
dim = 1
ty_scalarF_d1 = mkField(id+1, dim, ty_scalarT)
ty_vec1F_d1 = mkField(id+2, dim, ty_vec1T)
ty_vec2F_d1 = mkField(id+3, dim, ty_vec2T)
ty_vec3F_d1 = mkField(id+4, dim, ty_vec3T)
ty_mat2x2F_d1 = mkField(id+5, dim, ty_mat2x2T)
ty_mat3x3F_d1 = mkField(id+6, dim, ty_mat3x3T)
ty_ten2x2x2F_d1 = mkField(id+7, dim, ty_ten2x2x2T)
ty_ten3x3x3F_d1 = mkField(id+8, dim, ty_ten3x3x3T)
#dimension 2
id=20
dim=2
ty_scalarF_d2 = mkField(id+1, dim, ty_scalarT)
ty_vec2F_d2 = mkField(id+3, dim, ty_vec2T)
ty_vec3F_d2 = mkField(id+4, dim, ty_vec3T)
ty_mat2x2F_d2 = mkField(id+5, dim, ty_mat2x2T)
ty_mat3x3F_d2 = mkField(id+6, dim, ty_mat3x3T)
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+1, dim, ty_scalarT)
ty_vec2F_d3 = mkField(id+3, dim, ty_vec2T)
ty_vec3F_d3 = mkField(id+4, dim, ty_vec3T)
ty_mat2x2F_d3 = mkField(id+5, dim, ty_mat2x2T)
ty_mat3x3F_d3 = mkField(id+6, dim, ty_mat3x3T)
ty_ten2x2x2F_d3 = mkField(id+7, dim, ty_ten2x2x2T)
ty_ten3x3x3F_d3 = mkField(id+8, dim, ty_ten3x3x3T)

#list of types
#fields that we can create data
#ty_vec2F_d1,
l_all_F= [ty_scalarF_d1, ty_scalarF_d2, ty_vec2F_d2, ty_vec3F_d2, ty_mat2x2F_d2, ty_scalarF_d3, ty_vec2F_d3,  ty_vec3F_d3,ty_mat2x2F_d3]#, ty_mat3x3F_d3]
l_all_FT = [ty_scalarFT, ty_vec2FT, ty_vec3FT, ty_mat2x2FT, ty_mat3x3FT, ty_mat2x2x2FT, ty_mat3x3x3FT]
l_all= l_all_F + l_all_FT


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

def get_scaF():
    rtn = []
    # binary operator
    for f in l_all_F:
        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():
    rtn = []
    # binary operator
    for f in l_all_F:
        if(fty.is_Vector(f)):
            rtn.append(f)
    return rtn

#list of matrix fields
def get_matF():
    rtn = []
    # binary operator
    for f in l_all_F:
        if(fty.is_Matrix(f)):
            rtn.append(f)
    return rtn

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




# types for multiplication
#one has to be a scalar, and one has to a be a field
def get_mul():
    rtn = []
    # binary operator
    for sf in l_all_F:
        for a in l_all:
            # check equal dim
            if(fty.is_Field(a) and (not check_dim(sf, a))):
                continue
            # one arg needs to be a scalar
            elif (fty.is_Scalar(sf) or fty.is_Scalar(a)):
                #print ("["+sf.name+","+a.name+"]")
                rtn.append([sf,a])
                if(sf.id!=a.id):
                    rtn.append([a,sf])
    return rtn

# types for division
#one has to be a scalar, and one has to a be a field
def get_division():
    rtn = []
    # binary operator
    for sf in l_all_F:
        for a in l_all:
            # check equal dim
            if(fty.is_Field(a) and (not check_dim(sf, a))):
                continue
            # one arg needs to be a scalar
            elif (fty.is_Scalar(sf)):
                rtn.append([a, sf])
            elif (fty.is_Scalar(a)):
                rtn.append([sf,a])
    return rtn


# types for addition, subtraction, modulate
# one has to be a field in list nonscalarsflds
# must have same type.
def get_modulate(fs):
    rtn = []
    # binary operator
    for f in fs:
        for a in l_all:
            # check equal dim if seecond arg is a field
            if(fty.is_Field(a) and (not check_dim(f, a))):
                continue
            # needs to have same type
            elif (fty.get_shape(f) == fty.get_shape(a)):
                if(f.id==a.id):
                    rtn.append([f, a])
                else:
                    rtn.append([f, a])
                    rtn.append([a, f])
    return rtn

# list of fields by type
scalarFlds = get_scaF()
scalarFldsnotd1 = get_scaFnotd1()
vectorFlds = get_vecF()
matrixFlds = get_matF()
ten3Flds = get_Ten3()

#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==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,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,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,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):
    (_, shape) = shapeToTyhelper(shapeout, dim)
    return 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