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 3998 - (download) (as text) (annotate)
Sun Jun 19 17:12:03 2016 UTC (3 years ago) by cchiw
File size: 4467 byte(s)
added modulate, normalize,.. to mkops and testing
# -*- 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)
        print ("unsupported type("+str(i)+") for "+ "l"+str(len(self.tys))+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

#binary operators between vector field
# and higher order tensor/tensor field
def get_vF_n(lb, ub):
    rtn = []
    for f in vectorFlds:
        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((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

#-------------- make list of type args  ----------------------
# 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_vec =        get_unu(vectorFlds) # all vector fields
ps_unu_mat =        get_unu(matrixFlds) # all matrix fields
# unary operators
ex_probe = example(op_probe, ps_unu_all)            # probe
ex_negation = example(op_negation,ps_unu_all)       # negation
ex_gradient = example(op_gradient, ps_unu_s)        # gradient
ex_divergence = example(op_divergence, ps_unu_vec)  # divergence
ex_curl =  example(op_curl, ps_unu_vec)             # curl
ex_jacobian = example(op_jacob, ps_unu_vec)         # jacobian
ex_norm = example(op_norm, ps_unu_all)              # norm
ex_normalize = example(op_normalize, ps_unu_all)    # normalize
ex_trace = example(op_trace, ps_unu_mat)            # trace
ex_transpose = example(op_transpose, ps_unu_mat)    # tranpose
ex_det = example(op_det, ps_unu_mat)            # det

# binary args
# makes sure one arg is a field and the other has the same shape
ps_bin_add = get_add()
#binary ops between a vector field and higher-order tensors with specific shape
ps_bin_vec_cross =  get_vF_n(1,1)           # cross product
ps_bin_vec_outer =  get_vF_n(1,2)           # outer product
ps_bin_vec_inner =  get_vF_n(1,3)           # inner product
ps_bin_sca_all =    get_mul()                  # one arg is a scalar
ps_bin_sca_second = get_division()          # second arg is a scalar
#binary opertaors
ex_add = example(op_add, ps_bin_add) #     addition
ex_subtract = example(op_subtract, ps_bin_add) # subtraction
ex_cross = example(op_cross, ps_bin_vec_cross) # cross product
ex_outer = example(op_outer, ps_bin_vec_outer) # outer product
ex_inner = example(op_inner, ps_bin_vec_inner) # inner product
ex_scale = example (op_scale, ps_bin_sca_all)   # scaling
ex_division= example (op_division, ps_bin_sca_second)   # 11 division

# order of edxamples
exs_unary = [ex_probe, ex_negation, ex_gradient, ex_divergence, ex_curl, ex_jacobian, ex_norm, ex_normalize, ex_trace, ex_transpose, ex_det]
exs_binary = [ex_add, ex_subtract, ex_cross, ex_outer, ex_inner, ex_scale, ex_division]

exs = exs_unary+exs_binary
       
# returns example object
def get_ex(opr_num):
    return exs[opr_num]

# gets a single examples
def get_single_example(opr_num, ty_num):
    ex = get_ex(opr_num)
    ty = example.get_ty(ex, ty_num)
    name = example.toStr(ex, ty_num)
    opr = example.get_opr(ex)
    return (name, opr,ty)

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