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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4483 - (download) (as text) (annotate)
Fri Sep 2 05:07:01 2016 UTC (2 years, 8 months ago) by cchiw
File size: 15813 byte(s)
shape based iteration
import sys
import re
import os

from obj_apply import *
from obj_ex import  *
from obj_field import *
from obj_counter import *
from obj_frame import  *

from test_createField import createField
from test_writeDiderot import writeDiderot
from test_eval import eval
from test_compare import compare
from test_observed import observed

from core import *
from write import *
from var_ty import *
from frame import  *



##################################################################################################
# helper function for intermediate types
# shape to matching tensor type
def shape_to_tensor(shape1):
    d1 = nonefield_dim
    shapeout = shape1.shape
    return shapeToTy(shapeout, d1)

# shape to matching field type
def shape_to_field(shape1, d1):
    #print " shape_to_field "," shape: ",shape1.name, " dim: ",d1
    shapeout = shape1.shape
    return shapeToTy(shapeout, d1)

def is_nrrd(shape):
    if((shape == ty_scalarT)):
        return True
    elif((shape == ty_vec2T) or (shape == ty_vec3T) or (shape == ty_vec4T)):
        return True
    elif((shape  == ty_mat2x2T) or  (shape == ty_mat3x3T)):
        return True
    else:
        return False

# convert shape tensor to fty
# expected that list is order of dimension [Tensor, d1, d2, d3]
# only those that can data generated
# ty_noneT used
def shape_to_fty(shape):
    #print "shape", shape
    if(is_nrrd(shape)):
        return [shape_to_tensor(shape), shape_to_field(shape, 1), shape_to_field(shape, 2), shape_to_field(shape, 3)]
    else:
        return [shape_to_tensor(shape), None, None,  None]

# converst rty to tshape1.
# shape to type
# if we want to force the result ot be supportedtypes
isNrrd = True # does it need to be supported by a nrrd file
def convert_rst_binary(fty1, fty2, rty, dim):
    def cvt():
       if((fty.is_Field(fty1)) or (fty.is_Field(fty2))):
            return shape_to_field(rty, dim)
       else:
            return shape_to_tensor(rty)
    if(isNrrd and (not (is_nrrd(rty)))):
       return None
    else:
       return cvt()

def convert_rst_unary(fty1, rty, dim):
    def cvt():
        if(fty.is_Field(fty1)):
            return shape_to_field(rty, dim)
        else:
            return shape_to_tensor(rty)
    if(isNrrd and (not (is_nrrd(rty)))):
        return None
    else:
        return cvt()

##################################################################################################
# actual type and dimension
# does it still pass?
def check_tshape(op1,  tshape1, tshape2_shape, dim):
    if(op1 == op_gradient):
        if(fty.is_Field(tshape1)):
            if(dim ==1):
                return ty_scalarF_d1
            elif(dim ==2):
                return ty_vec2F_d2
            elif(dim ==3):
                return ty_vec3F_d3
            else:
                return None
        return None
    else:
        return  convert_rst_unary(tshape1, tshape2_shape, dim)


##################################################################################################
# given operator and input arg, what are the possibilities for second argument
# op1(shape1, shape2)=> tshape
# op1*shape1 -> (shape2, tshape)..
# tshape1 is fty1
def op1_to_shape(op1, tty1_orig):
    shape_same = tty1_orig
    if((op1 == op_negation) or  (op1==op_copy)):
        return shape_same
    elif(op1==op_norm):
        return ty_scalarT
    elif(op1==op_normalize):
        if(tty.isSca(tty1_orig)):
           return None
        return shape_same
    elif((op1==op_trace or op1 ==op_det)):
        if(tty.isMat(tty1_orig)):
            return ty_scalarT
        return None
    elif(op1==op_transpose):
        if(tty.isMat(tty1_orig)):
            [i, j] = tty1_orig.shape
            return nToshape([j,i])
        return None
    elif(op1==op_slicem0):
        if(tty.isMat(tty1_orig)):
            [i, j] = tty1_orig.shape
            return nToshape([j])
        return None
    elif(op1==op_slicem1):
        if(tty.isMat(tty1_orig)):
            [i, j] = tty1_orig.shape
            return nToshape([i])
        return None
    elif((op1==op_slicev0) or (op1==op_slicev1)):
        if(tty.isVec(tty1_orig)):
           return ty_scalarT
        return None
    elif(op1==op_slicet0 ):
        if(tty.isTen3(tty1_orig)):
            [i, j, k] = tty1_orig.shape
            return nToshape([i,k])
        return None
    elif(op1==op_slicet1):
        if(tty.isTen3(tty1_orig)):
            [i, j, k] = tty1_orig.shape
            return nToshape([k])
        return None
    elif(op1==op_inverse):
        if(tty.isMat(tty1_orig)):
            return tty1_orig
        return None
    elif((op1==op_cosine) or  (op1==op_sine) or (op1==op_tangent)):
        if(tty.isSca(tty1_orig)):
            return tty1_orig
        return None
    elif((op1==op_acosine) or  (op1==op_asine) or (op1==op_atangent) or (op1==op_sqrt)):
        if(tty.isSca(tty1_orig)):
            return ty_scalarT
        return None
    elif(op1==op_gradient):
        if(tty.isSca (tty1_orig)):
            return ty_noneT
        return None
    elif((op1==op_divergence) or  (op1==op_curl) or  (op1==op_jacob)):
        if(tty.isVec (tty1_orig)):
            return ty_noneT
        return None
    else:
        raise Exception ("unsupported operator: "+op1.name)

def op2_to_shape(op1, tty1_orig):
    n = [2,3,4]
    # transformations to term
    def term_id(term):
        return term 
    def term_cut_front(term):
         return term [1:]
    # es is  a list of (types,_)
    # for each shape we add a single index to base
    # term: base+[d]
    # bshape =  transform term with f()
    def grow(ashape, es, f):
        #print "grow"
        rtn = []
        for i in es:
            (b, _ ) = i
            base = b.shape
            #print "b:", b.name
            for d in n:
                #print "d:", d
                # second argument to operator
                term = base +[d]
                bshape = f(term)
                #apend shapes
                rshape = ashape +  bshape
                ty2  = nToshape(term)
                tshape = nToshape(rshape)
                #print "Created: ty2 ", ty2 .name, " tshape: ", tshape.name
                rtn.append((ty2 , tshape))
        return rtn
    # add index to argument, and make result type
    def grow_scale(ashape, es):
        # grow bshape -> bshape
        return grow(ashape, es, term_id)
    def grow_inner(ashape, es):
        # grow bshape -> cut bshape[1:]
        return grow(ashape, es, term_cut_front)
    # create list of vector types
    def rtnvec_scale(tty1):
        shape =  tty1.shape
        # starting to build from index
        inner_index = []
        # ashape T_ashape
        ashape = shape
        #shape to type
        # second argument
        ty2 = nToshape(inner_index)
        # result to 1*2 arguements
        tshape = nToshape(ashape)
        sca = [(ty2, tshape)]
        # grow by a single dimension
        vec = grow_scale(ashape, sca)
        return vec
    def rtnvec_inner(tty1):
        shape =  tty1.shape
        # starting to build from index
        # shared inner index
        ix = len(shape)-1
        inner_index =  [shape[ix]]
        # ashape T_ashape
        ashape =  shape[:ix]
        #shape to type
        # second argument
        ty2 = nToshape(inner_index)
        # result to 1*2 arguements
        tshape = nToshape(ashape)
        vec = [(ty2, tshape)]
        return (ashape, vec)
    def rtnvec_double(tty1):
        shape =  tty1.shape
        # starting to build from index
        # shared inner index
        ix = len(shape)-2
        jx = len(shape)-1
        inner_index =  [shape[ix], shape[jx]]
        # ashape T_ashape
        ashape =  shape[:ix]
        #shape to type
        # second argument
        ty2 = nToshape(inner_index)
        # result to 1*2 arguements
        tshape = nToshape(ashape)
        vec = [(ty2, tshape)]
        return (ashape, vec)
    
    #####################  operation specific functions   #####################
    # apply inner product to shape
    def shape_inner(tty1):
        # add index to argument, and make result type
        # first index in second argument is [2]
         # must have matching inner index
        # smallest argumment is a vector
        if(tty.isSca(tty1)):
            return []
        else:
            if(tty.isTen3(tty1)):
                (ashape, vec) = rtnvec_inner(tty1)
                # grow by a single dimension
                mat = grow_inner(ashape, vec)
                return  vec+mat
            elif(tty.isMat(tty1)):
                (ashape, vec) = rtnvec_inner(tty1)
                # grow by a single dimension
                mat = grow_inner(ashape, vec)
                ten = grow_inner(ashape, mat)
                return  vec +mat+ten
            elif(tty.isVec(tty1)):
                (ashape, vec) = rtnvec_inner(tty1)
                # grow by a single dimension
                mat = grow_inner(ashape, vec)
                ten = grow_inner(ashape, mat)
                return  vec +mat+ten
            else:
                return []
    # double dot
    def shape_doubledot(tty1):
        if(tty.isSca(tty1) or  tty.isVec(tty1)):
            return []
        else:
            # limit output size
            if(tty.isTen3(tty1)):
                #print "marka"
                (ashape, vec) = rtnvec_double(tty1)
                #print "markb"
                mat = grow_inner(ashape, vec)
                #print "markc"
                #ten = grow_inner(ashape, mat)
                return  mat#+ten
            elif(tty.isMat(tty1)):
                #print "mark-a"
                (ashape, vec) = rtnvec_double(tty1)
                #print "mark-b"
                mat = grow_inner(ashape, vec)
                #print "mark-c"
                    #ten = grow_inner(ashape, mat)
                return  mat#+ten
            else:
                return []
    # cross product
    def shape_cross(shape):
        if(shape == ty_vec2T):
            i1 = (ty_vec2T, ty_scalarT)
            return [i1]
        elif(shape == ty_vec3T):
            i1 = (ty_vec3T, ty_vec3T)
            return [i1]
        else:
            return []
    # scaling
    def shape_scale(tty1):
        if(tty.isSca(tty1)):
            ashape =  tty1.shape
            # everything is possible
            vec = rtnvec_scale(tty1)
            mat = grow_scale(ashape, vec)
            ten = grow_scale(ashape, mat)
            l = [(tty1,tty1)]+vec+mat+ten
            return  l
        else:
            return [(ty_scalarT, tty1)]
    # apply outer product to shape
    def shape_outer(tty1):
        if(tty.isVec(tty1)):
            vec = rtnvec_scale(tty1)
            ashape= tty1.shape
            mat = grow_scale(ashape, vec)
            return  vec+mat
        elif(tty.isMat(tty1)):
            vec = rtnvec_scale(tty1)
            return  vec
        else:# too large
            return []
     #####################  handle cases  #####################
     # inline simple ones
    if((op1==op_add) or  (op1==op_subtract) or  (op1==op_modulate)):
         # all arguments must have the same shape
        return [(tty1_orig, tty1_orig)]
    elif(op1==op_division):
        # second argument must be a scalar
        return [(ty_scalarT, tty1_orig)]
    elif(op1==op_cross):
        return shape_cross(tty1_orig)
    elif(op1==op_outer):
        return shape_outer(tty1_orig)
    elif(op1==op_inner):
        return shape_inner(tty1_orig)
    elif(op1==op_scale):
        return shape_scale(tty1_orig)
    elif(op1 == op_doubledot):
        return shape_doubledot(tty1_orig)
    else:
        raise Exception ("op1 not supported:"+op1.name)

#########################################################################################################
#given a field of a particular dim, return all types with that dimension.
def d_tofield(dim):
    if(dim==1):
        return [ty_scalarF_d1, ty_vec2F_d1, ty_vec3F_d1, ty_vec4F_d1, ty_mat2x2F_d1, ty_mat3x3F_d1]
    elif(dim==2):
        return  [ty_scalarF_d2, ty_vec2F_d2, ty_vec3F_d2, ty_vec4F_d2, ty_mat2x2F_d2, ty_mat3x3F_d2]
    elif(dim==3):
        return  [ty_scalarF_d3, ty_vec2F_d3, ty_vec3F_d3, ty_vec4F_d3, ty_mat2x2F_d3, ty_mat3x3F_d3]
    else:
        return l_all1

# moving backwards
# given opr_outer what are the allowed result types
def expected_inshape(op1):
    # (op1) input type -> output type
    if(op1 == op_gradient):
        d1 = [(ty_scalarF_d1, ty_scalarF_d1)]
        d2 = [(ty_scalarF_d2, ty_vec2F_d2)]
        d3 =[(ty_scalarF_d3, ty_vec3F_d3)]
        return d1+d2+d3
    elif(op1 == op_divergence):
        d2 = [(ty_vec2F_d2, ty_scalarF_d2)]
        d3 =[(ty_vec3F_d3, ty_scalarF_d3)]
        return d2+d3
    elif(op1 == op_curl):
        d2 = [(ty_vec2F_d2, ty_scalarF_d2)]
        d3 =[(ty_vec3F_d3, ty_vec3F_d3)]
        return d2+d3
    elif(op1 == op_jacob):
        d2 = [(ty_vec2F_d2, ty_mat2x2F_d2),(ty_mat2x2F_d2, ty_ten2x2x2F_d2)]
        d3 =[(ty_vec3F_d3, ty_mat3x3F_d3), (ty_mat3x3F_d3, ty_ten3x3x3F_d3)]
        return d2+d3
    elif(op1==op_normalize):
        m =[]
        for i in l_all1:
            if(not (tty.isSca(i))):
                m.append([i, i])
        return m
    elif((op1 ==op_norm)):
        m =[]
        for i in l_all1:
            m.append((i, shape_to_fty(ty_scalarT)))
        return m

    elif((op1 == op_negation) or (op1==op_copy)):
        m =[]
        for i in l_all1:
            m.append([i, i])
        return m
    elif((op1==op_trace)):
        d1 = [(ty_mat2x2F_d1, ty_scalarF_d1), (ty_mat3x3F_d1, ty_scalarF_d1), (ty_mat4x4F_d1, ty_scalarF_d1)]
        d2 = [(ty_mat2x2F_d2, ty_scalarF_d2), (ty_mat3x3F_d2, ty_scalarF_d2), (ty_mat4x4F_d2, ty_scalarF_d2)]
        d3 = [(ty_mat2x2F_d3, ty_scalarF_d3), (ty_mat3x3F_d3, ty_scalarF_d3), (ty_mat4x4F_d3, ty_scalarF_d3)]
        return d1+d2+d3
    elif((op1==op_det)):
        d1 = [(ty_mat2x2F_d1, ty_scalarF_d1), (ty_mat3x3F_d1, ty_scalarF_d1)]
        d2 = [(ty_mat2x2F_d2, ty_scalarF_d2), (ty_mat3x3F_d2, ty_scalarF_d2)]
        d3 = [(ty_mat2x2F_d3, ty_scalarF_d3), (ty_mat3x3F_d3, ty_scalarF_d3)]
        return d1+d2+d3
    else:
        raise Exception ("op1:"+op1.name)

#given tshape, and arg1, what is arg2 (shape)
def backwards_op2_arg1(op1, tshape, arg1):
    # inner product operation
    #print "op1:",op1.name, "tshape:", tshape.name, "arg1:",arg1.name
    if(op1  == op_inner):
        #(arg1, _) -> tshape
        if(fty.is_ScalarField(tshape)):
            if(fty.is_VectorField(arg1)):
                return arg1.tensorType
            else:
                # no other types
                return None
        else:
            #print "notrightthsape", tshape.name,"-",ty_vec2F_d2.name
            return None
    elif((op1  == op_add) or  (op1  == op_subtract)):
        if(tshape==arg1):
            return arg1.tensorType
        else:
            return None
    else:
        #print "not rightoperator"
        raise Exception ("op1:"+op1.name)

#given tshape, and arg1, what is arg2 (shape)
def backwards_op2_arg2(op1, tshape, arg2):
    # inner product operation
    #print "op1:",op1.name, "tshape:", tshape.name, "arg1:",arg1.name
    if(op1  == op_inner):
        #(arg1, _) -> tshape
        if(fty.is_ScalarField(tshape)):
            if(fty.is_VectorField(arg2)):
                return arg2.tensorType
            else:
                # no other types
                return None
        else:
            #print "notrightthsape", tshape.name,"-",ty_vec2F_d2.name
            return None
    elif((op1  == op_add) or  (op1  == op_subtract)):
        if(tshape==arg2):
            return arg2.tensorType
        else:
            return None
    else:
        #print "not rightoperator"
        raise Exception ("op1:"+op1.name)

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