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

SCM Repository

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

View of /branches/ein16/synth/d2/test_eval.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, 4 months ago) by cchiw
File size: 6914 byte(s)
added some binary operators for 2-d
import sys
import re
from test_examples import value_probe
from test_examples import value_negation
from test_examples import value_gradient
from test_examples import value_add
from test_examples import value_subtract
from test_examples import value_outer
from test_examples import value_inner
from test_examples import value_scalarFd2
from test_examples import value_vec2Fd2
from test_examples import value_mat2x2Fd2
from test_examples import value_scalarT
from test_examples import value_vec2T
from test_examples import value_mat2x2T

#fixed variables
op_probe = value_probe()
op_negation = value_negation()
op_gradient = value_gradient()
op_add = value_add()
op_subtract = value_subtract()
op_outer = value_outer()
op_inner = value_inner()
ty_scalarF_d2 = value_scalarFd2()
ty_vec2F_d2 = value_vec2Fd2()
ty_mat2x2F_d2=value_mat2x2Fd2()
ty_scalarT = value_scalarT()
ty_vec2T = value_vec2T()
ty_mat2x2T=value_mat2x2T()

def digest(coeff):
    (base, xsq, ysq, diag)=coeff
    b = base[0]
    c = base[1]
    d = xsq[0]
    g = xsq[1]
    f = ysq[0]
    h = ysq[1]
    a = diag[0]
    e = diag[1]
    i = diag[2]
    return (a,b,c,d,e,f,g,h,i)

def printFunc(name, coeff):
    (a,b,c,d,e,f,g,h,i)= digest(coeff)
    exp= name+"="
    if(b!=0):
        exp+= str(b)+ "*x"
    if(c!=0):
        exp+= " +"+ str(c)+"y"
    if(d!=0):
        exp+= " +"+ str(d)+ "x^2"
    if(g!=0):
        exp+= " + "+ str(g)+ "y*x^2"
    if(f!=0):
        exp+= " + "+ str(f)+ "y^2"
    if(h!=0):
        exp+= " + "+ str(h)+ "x*y^2"
    if(a!=0):
        exp+= " + "+ str(a)
    if(e!=0):
        exp+= " + "+ str(e)+ "x*y"
    if(i!=0):
        exp+= " + "+ str(i)+ "x^2*y^2"
    print exp

#gradient of field defined by coefficients at position
def grad(x,y,coeff):
    (a,b,c,d,e,f,g,h,i)= digest(coeff)
    x0 = b
    x1 = d*2*x + g*y*2*x
    x2 = h*y*y
    x3 = e*y + i*(y*y)*(2*x)
    x4= x0+x1+x2+x3 #xaxis
    y0 = c
    y1 = g*x*x
    y2 = f*2*y + h*x*2*y
    y3 = e*x + i*(y*2)*(x*x)
    y4= y0+y1+y2+y3 #yaxis
    return [x4,y4]

#evaluate field defined by coefficients at position
def eval(x,y,coeff):
    (a,b,c,d,e,f,g,h,i)= digest(coeff)
    #evaluate field defined by coefficients at position
    t0 = b*x + c*y
    t1 = d*x*x + g*y*x*x
    t2 = f*y*y + h*x*y*y
    t3 = a + e*x*y + i*(y*y)*(x*x)
    t4= t0+t1+t2+t3
    return t4

# create a vector field
def mkVectorField(a):
    # hard-coded augmentation
    return [a,2*a]

#evaluate vector field
def evalv2(x,y,coeff):
    return mkVectorField(eval(x,y,coeff))

def negation(x,y,coeff):
    return (-1*eval(x,y,coeff))
def negationv2(x,y,coeff):
    return  mkVectorField(-1*eval(x,y,coeff))


#evaluate addition of scalar fields
def add(x,y,coeff1,coeff2):
    return eval(x,y,coeff1) + eval(x,y,coeff2)
def addv2(x,y,coeff1,coeff2):
    return mkVectorField(eval(x,y,coeff1) + eval(x,y,coeff2))


#evaluate subtraction of scalar fields
def subtract(x,y,coeff1,coeff2):
    return eval(x,y,coeff1) - eval(x,y,coeff2)
def subtractv2(x,y,coeff1,coeff2):
    return mkVectorField (eval(x,y,coeff1) - eval(x,y,coeff2))

#evaluate outer product
def outerv2(x,y,coeff1,coeff2):
    # coeff same for each axis
    a = mkVectorField(eval(x,y,coeff1))
    b = mkVectorField(eval(x,y,coeff2))
    rtn=[]
    for i in  range(2):
        for j in range(2):
            rtn.append(a[i]*b[j])
    return rtn

#evaluate inner product
def innerv2(x,y,coeff1,coeff2):
    # coeff same for each axis
    a = mkVectorField(eval(x,y,coeff1))
    b = mkVectorField(eval(x,y,coeff2))
    rtn = 0
    for i in  range(2):
        rtn+= a[i]*b[i]
    return rtn


def iter(k, pos, coeff):
    corr = []
    printFunc("F0",coeff)
    for p in pos:
        x=p[0]
        y=p[1]
        val = k(x,y,coeff)
        corr.append(val)
    return corr

def iter2(k, pos, coeff1,coeff2):
    corr = []
    printFunc("F0",coeff1)
    printFunc("F1",coeff2)
    for p in pos:
        x=p[0]
        y=p[1]
        val = k(x,y,coeff1,coeff2)
        corr.append(val)
    return corr


# binary operator with scalar field and vector field
def unary(opr, itypes, pos, coeffs):
    (op_name, arity) = opr
    if (len(coeffs)!=arity):
        n=len(coeffs)
        raise Exception("coeffs: "+str(n)+" arity: "+str(arity))
    if (arity==1):
        # unary operator
        coeff1=coeffs[0]
        ityp1=itypes[0]
        if(opr==op_probe): #probing
            if (ityp1==ty_scalarF_d2): # input is a scalar field
                return iter(eval,pos,coeff1)
            elif(ityp1==ty_vec2F_d2): # input is a vector field
                return iter(evalv2,pos,coeff1)
        elif(opr==op_negation): #negation
            if (ityp1==ty_scalarF_d2): # input is a scalar field
                return iter(negation,pos,coeff1)
            elif(ityp1==ty_vec2F_d2):
                return iter(negationv2,pos,coeff1)
        elif(opr==op_gradient): #gradient
            if (ityp1==ty_scalarF_d2): # input is a scalar field
                return iter(grad,pos,coeff1)
            else:
                raise Exception("vector field: no operator value for "+str(op_name))
        else:
            raise Exception("unsupported unary operator:",op_name)
    elif (arity==2):
        # binary operator
        coeff1=coeffs[0]
        coeff2=coeffs[1]
        ityp1=itypes[0]
        ityp2=itypes[1]
        if(opr==op_add): #adding
            if (ityp1==ty_scalarF_d2) and (ityp1==ityp2): #input is a scalar field
                return iter2(add,pos,coeff1,coeff2)
            elif (ityp1==ty_vec2F_d2) and (ityp1==ityp2): #input is a vector field
                return iter2(addv2,pos,coeff1,coeff2)
            else:
                raise ValueError("unsupported type for this operation: ",(op_name))
        elif(opr==op_subtract): #subtraction
            if (ityp1==ty_scalarF_d2) and (ityp1==ityp2): #input is a scalar field
                return iter2(subtract,pos,coeff1,coeff2)
            elif (ityp1==ty_vec2F_d2) and (ityp1==ityp2): #input is a vector field
                return iter2(subtractv2,pos,coeff1,coeff2)
            else:
                raise ValueError("unsupported type for this operation: ",(op_name))
        elif(opr==op_outer): #outer product
            if (ityp1==ty_vec2F_d2) and (ityp1==ityp2): #input is a vector field
                return iter2(outerv2,pos,coeff1,coeff2)
            else:
                raise ValueError("unsupported type: ", ityp1," for this operation: ",(op_name))
        elif(opr==op_inner): # inner product
            if (ityp1==ty_vec2F_d2) and (ityp1==ityp2): #input is a vector field
                return iter2(innerv2,pos,coeff1,coeff2)
            else:
                raise ValueError("unsupported type: ", ityp1," for this operation: ",(op_name))
        else:
                raise RuntimeError("binary operator is not yet supported ",(op_name))
    else:
        raise Exception ("arity is not supported: "+str(arity))


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