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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4261 - (download) (as text) (annotate)
Mon Jul 25 20:14:18 2016 UTC (2 years, 11 months ago) by cchiw
File size: 4251 byte(s)
added cases to apply
 # -*- coding: utf-8 -*-

from __future__ import unicode_literals

class operator:
    def __init__(self, id, name, arity, symb, placement):
        self.id=id
        self.name=name
        self.arity=arity
        self.symb=symb
        self.placement=placement
    def toStr(self):
        self.name, str(arity)
    def get_name(self):
        return self.name
    def isEq_id(a,b):
        return (a.id==b.id)
    def isEq_idNum(a,b):
        return (a.id==b)
    def toStr(self,s):
        if(self.arity==1):
            return self.name+"("+s+"0"+")"
        elif(self.arity==2):
            return self.name+"("+s+"0,"+s+ "1"+")"

#------------------------------ constants -----------------------------------------------------
#  Constants
# id, name, arity, symbol
#unay operators
place_left = "left"
place_right = "right"
place_middle = "middle"
place_split = "split"


id=0
#op_none= operator(id,"none", 1,"", place_left)
op_negation = operator(id,"neg", 1,"-", place_left)
op_norm = operator(id+1,"norm", 1, u'|', place_split)
op_normalize = operator(id+2,"normalize", 1, u'normalize', place_left)
op_trace = operator(id+3,"trace", 1, u'trace', place_left)
op_transpose = operator(id+4,"transpose", 1, u'transpose', place_left)
op_det = operator(id+5,"det", 1, u'det', place_left)
op_probe= operator(id+6,"probe", 1,"", place_left)

op_reg = [ op_negation, op_norm, op_normalize, op_trace, op_transpose, op_det,op_probe]

id=id+7
#differentiation
op_gradient = operator(id, "grad", 1, u'∇', place_left)
op_divergence = operator(id+1, "div", 1, u'∇•', place_left)
op_curl= operator(id+2, "curl", 1, u'∇×',place_left)
op_jacob= operator(id+3, "jacob", 1, u'∇⊗', place_left)
op_diff =[op_gradient, op_divergence, op_curl, op_jacob]

id=id+4
#slicing
op_slicem0 = operator(id,"slicem0", 1, u'[1,:]', place_right)
op_slicem1 = operator(id+1,"slicem1", 1, u'[:,0]', place_right)
op_slicev0 = operator(id+2,"slicev0", 1, u'[0]', place_right)
op_slicev1 = operator(id+3,"slicev1", 1, u'[1]', place_right)
op_slicet0 = operator(id+4,"slicet0", 1, u'[:,1,:]', place_right)
op_slicet1 = operator(id+5,"slicet1", 1, u'[1,0,:]', place_right)
op_slice = [op_slicem0, op_slicem1, op_slicev0, op_slicev1, op_slicet0, op_slicet1]
op_unary= op_reg+ op_diff+op_slice

#binary operators
id=id+6
op_add = operator(id,"addition", 2,"+", place_middle)
op_subtract = operator(id+1,"subtraction", 2, "-", place_middle)
op_cross = operator(id+2,"cross product", 2, u'×', place_middle)
op_outer = operator(id+3,"outer product", 2, u'⊗', place_middle)
op_inner = operator(id+4,"inner product", 2, u'•', place_middle)
op_scale = operator(id+5,"multiplication", 2, u'*', place_middle)
op_division = operator(id+6,"division", 2, u'/', place_middle)
op_modulate = operator(id+7,"modulate", 2, "modulate",  place_left)
op_doubledot= operator(id+8,"op_doubledot", 2, u':', place_middle)
op_binary = [op_add, op_subtract, op_cross, op_outer, op_inner, op_scale, op_division, op_modulate, op_doubledot]

#Trig
id=id+9
op_cosine = operator(id, "cosine", 1, u'cosF', place_left)
op_sine = operator(id+1, "sine", 1, u'sinF', place_left)
op_tangent = operator(id+2, "tangent", 1, u'tanF', place_left)
op_acosine = operator(id+3, "arccosine", 1, u'acosF', place_left)
op_asine = operator(id+4, "arcsine", 1, u'asinF', place_left)
op_atangent = operator(id+5, "arctangent", 1, u'atanF', place_left)
op_sqrt = operator(id+7, "sqrt", 1, u'sqrt', place_left)
op_trig=[op_cosine, op_sine, op_tangent, op_acosine, op_asine , op_atangent, op_sqrt]

op_all = op_unary+op_binary

i=0
for op1 in op_all:
    x= "-"+op1.name+"("+str(op1.id)+") "
    print (x)
    if(not (op1.id==i)):
        raise Exception(x+" does not match placement "+str(i))
    i+=1

def idToStr(n):
    for i in  (op_unary+op_binary):
        if (n==i.id):
            return i.name
    return "no match"
def idToStrBinary(n):
    n =n+6
    for i in  (op_binary):
        if (n==i.id):
            return i.name
    else:
        return "no match"

# get outer operator
def id_toOpr(id):
    return op_all[id]


# length of operators
def getN():
    # opr max
    return len(op_all)


# do we need to create an extra field type
def needextratype(ex_inner, ex_outer):
    return (ex_outer.arity==2)

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