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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4257 - (download) (as text) (annotate)
Mon Jul 25 15:23:32 2016 UTC (2 years, 11 months ago) by cchiw
File size: 9295 byte(s)
fixed image shape issue
# -*- coding: utf-8 -*-

from __future__ import unicode_literals

from obj_ty import *
from obj_operator import *

class example:
    def __init__(self, opr, tys):
        self.opr=opr
        self.tys=tys
    def toStr(self, i):
        name = self.opr.name+" ( "
        curty = self.tys[i]
        for j in curty:
            name+=j.name+","
        name+=" )"
        return name
    def get_oprname(self):
        return self.opr.name
    def get_opr(self):
        return self.opr
    def get_ty(self, i):
        if (i >= len(self.tys)):
            raise Exception("unsupported type("+str(i)+") for "+ self.opr.name)
        return self.tys[i]

#--------------  put list in parameter form  ----------------------
# make list of unary args
def get_unu(fall):
    rtn = []
    # binary operator
    for f in fall:
        rtn.append([f])
        #print ("["+f.name+"]")
    return rtn

# binary operator
def get_bin_same(fall):
    rtn = []
    for f in fall:
        rtn.append([f,f])
        #print ("["+f.name+","+g.name+"]")
    return rtn

#is type represented in list of fields
def outsupported(f,b):
    bshape = fty.get_shape(f)+fty.get_shape(b)
    if (len(bshape)>4):
        return False
    dim = f.dim
    (tf, _) = shapeToTyhelper(bshape, dim)
    return tf

#binary operators between flds fld (limited in some way)
# and higher order tensor/tensor field
def get_inner_n(fld, lb, ub):
    rtn = []
    for f in fld:
        for b in l_all:
            n = len(fty.get_shape(b))
            if((fty.is_Field(b)) and (not check_dim(f,b))): # check equal dim
                continue
            elif(n>ub):
                continue
            elif(n<lb):
                continue
            if(not  outsupported(f,b)):
                continue
            if((fty.get_last_ix(f)==fty.get_first_ix(b))):
                #print ("["+f.name+","+b.name+"]")
                rtn.append([f,b])
            if(not fty.isEq_id(f,b)):
                if((fty.get_last_ix(b)==fty.get_first_ix(f))):
                    #print ("["+b.name+","+f.name+"]")
                    rtn.append([b,f])
    return rtn


#binary operators between flds fld (limited in some way)
# and higher order tensor/tensor field
def get_outer_n(fld, lb, ub):
    rtn = []
    for f in fld:
        for b in l_all:
            if(not outsupported(f,b)):
                continue
            n = len(fty.get_shape(b))
            if((fty.is_Field(b)) and (not check_dim(f,b))): # check equal dim
                continue
            elif(n>ub):
                continue
            elif(n<lb):
                continue
            
            rtn.append([f,b])
            if(not fty.isEq_id(f,b)):
                rtn.append([b,f])
    return rtn

#binary operators between flds fld (limited in some way)
# and higher order tensor/tensor field
def get_cross_n(fld, lb, ub):
    rtn = []
    for f in fld:
        [n] = (fty.get_shape(f))
        if(not (n == f.dim)):
            continue
        for b in l_all:
            if(not outsupported(f,b)):
                continue
            if((fty.is_Field(b)) and (not check_dim(f,b))): # check equal dim
                continue
            if(not (fty.is_Vector(b))):
                continue
            [m]= fty.get_shape(b)
            if(not (m==n)):
                continue
            rtn.append([f,b])
            if(not fty.isEq_id(f,b)):
                rtn.append([b,f])
    return rtn
#-------------- binary operators  ----------------------
def writeex(e):
    #print (e)
    f = open("rst/results_ex_1.txt", 'a+')
    f.write(e)
    f.close()


def itertys(op1, pre, f):
    #print pre
    #print len(f)
    i = 0
    writeex(pre)
    x="\n\n operator: "+op1.name+ "("+str(op1.id) +") "+"length #"+str(len(f))+"\n"
    for j in f:
        x+= "\n cmd 1 2 : "+str(op1.id)+" "+str(i)+ " ("
        for k in j:
            x+= k.name+ ","
        x+=")"
        i+=1
    writeex(x)


#--------------unary operators  ----------------------
# ps_..  list of unary args   [[a],[b],..]
# unary args
ps_unu_all =        get_unu(l_all_F)    # all fields
ps_unu_s =          get_unu(scalarFlds) # all scalar fields
ps_unu_s_notd1 =    get_unu(scalarFldsnotd1) # all scalar fields
ps_unu_vec =        get_unu(vectorFlds) # all vector fields
ps_unu_vec_samedim =  get_unu(vectorFlds_samedim)
ps_unu_mat =        get_unu(matrixFlds) # all matrix fields
ps_unu_ten3 =       get_unu(ten3Flds)
# binary args
nonscalarsflds = vectorFlds+matrixFlds
# modulate make sure each arg has the same shape
ps_bin_add        = get_modulate(l_all_F)
ps_bin_nonsca     = get_modulate(nonscalarsflds) # same shapes and non-scalars
ps_bin_mat        = get_modulate(matrixFlds)

#binary ops between a vector field and higher-order tensors with specific shape
# special case
ps_bin_vec_cross  = get_cross_n(vectorFlds, 1,1)
ps_bin_vec_outer  = get_outer_n(nonscalarsflds,1,3)
ps_bin_vec_inner  = get_inner_n(nonscalarsflds,1,3)
ps_bin_sca_all    = get_mul()                  # one arg is a scalar
ps_bin_sca_second = get_division()          # second arg is a scalar
# same arg for both terms

def getEx_binsamenons(op1):
    args = ps_bin_nonsca
    itertys(op1, op1.name, args)
    return  example(op1, args)

def getEx_all(op1):
    args = ps_unu_all
    itertys(op1, op1.name, args)
    return  example(op1, args)
def getEx_vec(op1):
    args = ps_unu_vec
    itertys(op1, op1.name, args)
    return  example(op1,  args)
def getEx_vec_samedim (op1):
    args = ps_unu_vec_samedim
    itertys(op1, op1.name, args)
    return  example(op1,  args)
def getEx_mat(op1):
    args = ps_unu_mat
    itertys(op1, op1.name, args)
    return  example(op1, args)
def getEx_ten3(op1):
    args= ps_unu_ten3
    itertys(op1, op1.name, args)
    return  example(op1, args)

def getEx_binmat(op1):
    args = ps_bin_mat
    itertys(op1, op1.name, args)
    return example(op1, args)

def getEx_binall(op1):
    # binary operator types are the same shape
    args =  ps_bin_add
    itertys(op1, op1.name, args)
    return example(op1,  args)
def getEx_bincross(op1):
    args = ps_bin_vec_cross
    itertys(op1, op1.name, args)
    return example(op1, args)
def getEx_binouter(op1):
    args =  ps_bin_vec_outer
    itertys(op1, op1.name, args)
    return example(op1, args)
def getEx_bininner(op1):
    args = ps_bin_vec_inner
    itertys(op1, op1.name, args)
    return example(op1, args)
def getEx_binscale(op1):
    args = ps_bin_sca_all
    itertys(op1, op1.name, args)
    return example(op1, args)
def getEx_bindiv(op1):
    args= ps_bin_sca_second
    itertys(op1, op1.name, args)
    return example(op1, args)

#-------------- examples with operators and types  ----------------------
# returns example object
def oprToEx(op1):
    if((op1.id==op_sine.id) or (op1.id==op_cosine.id) or (op1.id== op_tangent.id)):
        return example(op1, ps_unu_s)
    elif((op1.id==op_asine.id) or (op1.id==op_acosine.id) or (op1.id== op_atangent.id)):
        return example(op1, ps_unu_s)
    elif((op1.id==op_gradient.id) or (op1.id== op_sqrt.id)):
        return example(op1, ps_unu_s)
    elif((op1.id==op_slicev0.id) or (op1.id==op_slicev1.id)) :
        return getEx_vec(op1)                   # slice vector
    elif((op1.id==op_divergence.id) or (op1.id==op_curl.id)  or (op1.id==op_jacob.id)):
        return getEx_vec_samedim(op1)           # divergence, curl, jacobian
    elif((op1.id==op_trace.id) or (op1.id==op_transpose.id) or (op1.id==op_det.id)):
        return getEx_mat(op1)                   # trace, transpose,det
    elif((op1.id==op_slicem0.id) or (op1.id==op_slicem1.id)):
        return getEx_mat(op1)                   # slice matrix
    elif((op1.id==op_slicet0.id) or (op1.id==op_slicet1.id)):
        return getEx_ten3(op1)                  # slice third order tensor
    elif((op1.id==op_probe.id) or (op1.id==op_negation.id) or (op1.id==op_norm.id) or (op1.id==op_normalize.id)):
       return  getEx_all(op1)                   # probe
    elif((op1.id==op_add.id) or (op1.id==op_subtract.id)):
        return getEx_binall( op1)               # addition
    elif(op1.id==op_cross.id):
        return getEx_bincross(op1)              # cross product
    elif(op1.id==op_outer.id):
       return getEx_binouter(op1)               # outer product
    elif(op1.id==op_inner.id):
       return getEx_bininner(op1)               # inner product
    elif(op1.id==op_scale.id):
        return getEx_binscale (op1)             # scaling
    elif(op1.id==op_division.id):
       return getEx_bindiv (op1)                # division
    elif(op1.id==op_modulate.id):
        return  getEx_binsamenons(op1)          # modulate
    elif(op1.id==op_doubledot.id) :
        return getEx_binmat(op1)                # doubledot
    else:
        raise Exception("no built in example of operator"+op1.name)

# gets a single examples
def get_single_example(opr, ty_num):
    ex = oprToEx(opr)
    print "current operator "+opr.name+ "("+str(opr.id)+ ") # "+str(ty_num)+"|"+str(len(ex.tys)-1)
    i = 0
    for t in ex.tys:
        x=""
        if(i==ty_num):
            x+="->"
        x += str(i)+". "
        for j in t:
            x+= j.name+","
        i+=1
        print x
    name = example.toStr(ex, ty_num)
    ty = example.get_ty(ex, ty_num)
    return (name, opr,ty)



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