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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3874 - (download) (as text) (annotate)
Wed May 18 16:45:54 2016 UTC (3 years, 3 months ago) by cchiw
File size: 4889 byte(s)
added some binary operators for 2-d
# -*- coding: utf-8 -*-

from __future__ import unicode_literals
#------------------------------ Examples -----------------------------------------------------
#  Constants
#type of operation
#name and arity

#unay operators
op_probe=("probe", 1)
op_negation=("neg", 1)
op_gradient=("grad", 1)
#binary operators
op_add=("addition", 2)
op_subtract=("subtraction", 2)
op_outer=("outerproduct", 2)
op_inner=("innerproduct", 2)


#field types
ty_scalarF_d2 = "scalarfield"
ty_vec2F_d2 = "vec2field"
ty_mat2x2F_d2 = "mat2x2field"
#tensor types
ty_scalarT = "scalartensor"
ty_vec2T = "vec2tensor"
ty_mat2x2T = "mat2x2tensor"


#type of field after operation is applied
def applyUnaryOp(op1,ityps,k):
    ityp1=ityps[0]
    same = (ityp1,k) #type unaffected by operation
    if (op_probe==op1) or (op_negation==op1):
        return same
    elif (op_gradient==op1):
        if (ty_scalarF_d2==ityp1):
            return (ty_vec2F_d2,k-1)
        else:
            raise "wrong type for gradient operation"
    else:
        print "operation not represented in unary operations:"
        raise "operation not represented in unary operations:"

#type of field after operation is applied
def applyBinaryOp(op1,ityps,k):
    ityp1=ityps[0]
    ityp2=ityps[1]
    same = (ityp1,k) #type unaffected by operation
    if (op_add==op1) or (op_subtract==op1):
        return same
    elif(op_outer==op1):
        if(ityp1==ty_vec2F_d2) and (ityp2==ty_vec2F_d2):
            return (ty_mat2x2F_d2, k)
        else:
            raise Exception ("types not supported for outer product", op1)
    elif(op_inner==op1):
        if(ityp1==ty_vec2F_d2) and (ityp2==ty_vec2F_d2):
            return (ty_scalarF_d2, k)
        else:
            raise Exception ("types not supported for inner product", op1)
    else:
        raise Exception ("operation not represented in binary operations", op1)

#probe field type returns tensor type
def tensorType(a):
    (otyp1,k)=a
    if (ty_scalarF_d2==otyp1):
        return ty_scalarT
    elif (ty_vec2F_d2==otyp1):
        return ty_vec2T
    elif(ty_mat2x2F_d2==otyp1):
        return ty_mat2x2T
    else:
        raise Exception ("field type not represented:", otyp1)

#operation to string
def optostr(op1):
    if(op1==op_probe):
        return ""
    elif(op1==op_negation):
        return "-"
    elif(op1==op_gradient):
        return u'∇'
    elif(op1==op_add):
        return "+"
    elif(op1==op_subtract):
        return "-"
    elif(op1==op_outer):
        return u'⊗'
    elif(op1==op_inner):
        return u'•'
    else:
        raise Exception("unsupported operation: "+op1)

# returning operator names
def value_probe():
    return op_probe
def value_negation():
    return op_negation
def value_gradient():
    return op_gradient
def value_add():
    return op_add
def value_subtract():
    return op_subtract
def value_outer():
    return op_outer
def value_inner():
    return op_inner
def value_scalarFd2():
    return ty_scalarF_d2
def value_vec2Fd2():
    return ty_vec2F_d2
def value_mat2x2Fd2():
    return ty_mat2x2F_d2
def value_scalarT():
   return ty_scalarT
def value_vec2T():
    return ty_vec2T
def value_mat2x2T():
    return ty_mat2x2T
# list of examples
#ex_num=[probe, negation, gradient]
#name, operator, input field, output tensor
examples1=[
    ("probe of 2-d scalar field",op_probe,[ty_scalarF_d2]),         #0
    ("negation of 2-d scalar field",op_negation,[ty_scalarF_d2]),   #1
    ("gradient of 2-d scalar field",op_gradient,[ty_scalarF_d2]),   #2
    ("probe of 2-d vector field",op_probe,[ty_vec2F_d2]),           #3
    ("negation of 2-d vector field",op_negation,[ty_vec2F_d2]),      #4
    ("addition of two 2-d scalar field",op_add,[ty_scalarF_d2,ty_scalarF_d2]),     #5
    ("subtraction of two 2-d scalar field",op_subtract,[ty_scalarF_d2,ty_scalarF_d2]),  #6
    ("addition of two 2-d vector field",op_add,[ty_vec2F_d2,ty_vec2F_d2]),     #7
    ("subtraction of two 2-d vector field",op_subtract,[ty_vec2F_d2,ty_vec2F_d2]),     #8
    ("outer product of two 2-d vector field",op_outer,[ty_vec2F_d2,ty_vec2F_d2]),     #9
    ("inner product of two 2-d vector field",op_inner,[ty_vec2F_d2,ty_vec2F_d2]),     #10
           ]

def getUnaryEx(op1,itype):
    k=1 #hardcoded for now
    otype1=tensorType(applyUnaryOp(op1,itype,k))
    return otype1

def getBinaryEx(op1,itype):
    k=1 #hardcoded for now
    otype1=tensorType(applyBinaryOp(op1,itype,k))
    return otype1


def getEx(n):
    (name,op1,itype)= examples1[int(n)]
    (op_name,arity)=op1
    if(len(itype)!=arity):
        raise Exception("itype: "+str(len(itype))+"arity:"+str(arity))
    if(arity==1):
        otyp1 = getUnaryEx(op1,itype)
        return   (name,op1,itype,otyp1)
    elif(arity==2):
        otyp1 = getBinaryEx(op1,itype)
        return   (name,op1,itype,otyp1)
    else:
        raise Exception ("arity not supported"+str(arity))

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