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

SCM Repository

[diderot] View of /branches/charisee/src/compiler/ein/mkoperators.sml
ViewVC logotype

View of /branches/charisee/src/compiler/ein/mkoperators.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2521 - (download) (annotate)
Thu Jan 9 02:17:07 2014 UTC (5 years, 7 months ago) by cchiw
File size: 10001 byte(s)
Added type Checker
(* examples.sml
 *
 * COPYRIGHT (c) 2012 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 *)

structure mkOperators = struct

    local

    structure E = Ein
    structure P=Printer
    in


    fun expIndex(n,inc)=List.tabulate(n, (fn(x)=>E.V (x+inc)))
     fun expIndexP(n,inc)=List.tabulate(n, (fn(x)=> (x+inc)))
 
    fun specialize(alpha,inc)=(alpha,expIndex(length(alpha),inc))
    fun specializeP(alpha,inc)=(alpha,expIndexP(length(alpha),inc))
 
    (*mkTensor functions*)
    (* Adding tensors : < X{\alpha} + Y_{\alpha}>_{\alpha} *)
    fun addTen(alpha)=let
        val (index', expindex')= specialize(alpha,0)
        in 
            E.EIN{
            params = [E.TEN 1, E.TEN 1], index = index',
            body = E.Add[E.Tensor(0, expindex'), E.Tensor(1, expindex')]
            }
        end
        
    fun createVec(dim)=E.EIN{
        params = [E.TEN 1], index = [dim],
        body = E.Tensor(0, [E.V 0])
        }

      
     val zero=E.EIN{
        params = [], index = [],
        body = E.Const(0.0)
        }
        

        
     (* Subtracting tensors *)
     fun subTen(alpha)=let
        val (index', expindex')= specialize(alpha,0)
        in
            E.EIN{
            params = [E.TEN 1, E.TEN 1], index = index',
            body = E.Sub(E.Tensor(0, expindex'), E.Tensor(1, expindex'))}
        end
    
    fun divTen(alpha) =let
        val (index', expindex')= specialize(alpha,0)
        in  E.EIN{
            params = [E.TEN 1, E.TEN 1], index = index',
            body = E.Div(E.Tensor(0, expindex'), E.Tensor(1,[]))
        }
        end
        
    (* Trace: <M_{i, i}>  This one Sx represents both i's*)
    fun trace(dim) = E.EIN{
        params = [E.TEN 1],         (* M *)
        index = [],           (* i *)
        body = E.Sum([(E.V 0,1,dim-1)],E.Tensor(0, [E.V 0, E.V 0]))
    }
        
    fun negTen(alpha)=let
        val (index', expindex')= specialize(alpha,0)
        in E.EIN{
            params = [E.TEN 1],
            index = index',
            body = E.Neg(E.Tensor(0, expindex'))
        }
        end
        
    (* scalar times tensor product: <s * T_{\alpha}>_{\alpha} *)
    fun scaleTen(alpha) = let
        val (index', expindex')= specialize(alpha,0)
        in E.EIN{
            params = [E.TEN 1, E.TEN 1],    (* s and T *)
            index = index',           (* \alpha *)
            body = E.Prod[ E.Tensor(0, []),  E.Tensor(1, expindex')]
        }
        end 

    (* generic inner product: <T_{\alpha i} * T_{i \beta}>_{\alpha \beta} *)
    fun innerProduct(shape1,i::beta) = let
        val alpha= List.take(shape1,length(shape1)-1)
        val (indexA, expindexA)= specialize(alpha,0)
        val (indexB, expindexB)= specialize(beta,(length(alpha)))
        val s'=E.V(length(alpha)+ length(beta))
        val s''=[(s',0,i-1)]
        in E.EIN{
            params = [E.TEN 1, E.TEN 1],              (* T and T' *)
            index = indexA@indexB,   (* \alpha \beta, i *)
            body = E.Sum(s'', E.Prod[
                E.Tensor(0, expindexA@[s']),   (* T_{\alpha i} *)
                E.Tensor(1, [s']@expindexB )  (* T'_{i \beta} *)
                ])
        }
        end 

    (*<T_{\alpha i j} * B{i j \beta }>_\alpha \beta*)
    fun doubleDot(shape1,i::j::beta) = let
        val alpha= List.take(shape1,length(shape1)-2)
        val (indexA, expindexA)= specialize(alpha,0)
        val (indexB, expindexB)= specialize(beta,(length(alpha)))
        val sumi=length(alpha)+ length(beta)
        val s'=[E.V sumi,E.V(sumi+1)]
        val s''=[(E.V sumi,0,i-1),(E.V(sumi+1),0,j-1)]
        in E.EIN{
            params = [E.TEN 1,E.TEN 1],
            index = indexA@indexB,
            body = E.Sum(s'',E.Prod[
                E.Tensor(0, expindexA@s'),
                E.Tensor(1,s'@expindexB)
                ])
        }
        end
        
    (*Vector Examples : <T_i * T_j>_ij..t0⊗t1*)
    fun outerProduct(dimA,dimB) =E.EIN{
        params = [E.TEN 1, E.TEN 1],
        index= [dimA,dimB],
        body= E.Prod[E.Tensor(0, [E.V 0]), E.Tensor(1, [E.V 1])]
    }
       
        
    fun transpose([i,j]) =E.EIN{
        params = [E.TEN 1], index= [i,j],
        body= E.Tensor(0, [E.V 1,E.V 0])
      }
        

    fun modulate(dim) =E.EIN{
        params = [E.TEN 1, E.TEN 1],
        index = [dim],
        body = E.Prod[E.Tensor(0, [E.V 0]), E.Tensor(1, [E.V 0])]
        }
        
        
    (*crossProduct is on 3D vectors ..vec3 t8=t0 × t1; *)
    val crossProduct = E.EIN{
        params = [E.TEN 1, E.TEN 1],
        index= [3],
        body=E.Sum([(E. V 1,0,2),(E.V 2,0,2)],
            E.Prod[ E.Epsilon(0, 1, 2), E.Tensor(0, [E.V 1]),  E.Tensor(1, [E.V 2 ]) ])
    }

    (* Identiy: <\delta_{i j}>_{i j}  *)
    fun identity(dim) =E.EIN{
        params = [],
        index = [dim,dim],
        body = E.Delta(E.V(0), E.V(1))
    }
            
    (*Tensor and Fields*)
    fun addTenField(dim) = E.EIN{
        params = [E.TEN 1,E.FLD(dim)],
        index = [],
        body = E.Add[E.Tensor(0, []),E.Field(1, [])]
    }
        
    fun subTenField(dim) = E.EIN{
        params = [E.TEN 1,E.FLD(dim)],
        index = [],
        body = E.Add[E.Tensor(0, []),E.Neg(E.Field(1, []))]
    }
    
    fun subFieldTen(dim) = E.EIN{
        params = [E.TEN 1,E.FLD(dim)],
        index = [],
        body = E.Sub(E.Field(1, []),E.Tensor(0, []))
    }
    
    (* mkField functions*)
    (*Adding Fields : < F{\alpha} + G_{\alpha}>_{\alpha} *)
    fun addField(dim,shape) =let
        val (index', expindex')= specialize(shape,0)
        in E.EIN{
            params = [E.FLD(dim),E.FLD(dim)],
            index = index',
            body = E.Add[E.Field(0, expindex'),E.Field(1, expindex')]
        }
        end
        
    fun subField(dim,shape) =let
        val (index', expindex')= specialize(shape,0)
        in E.EIN{
            params = [E.FLD(dim),E.FLD(dim)],
            index = index',
            body = E.Sub(E.Field(0, expindex'),E.Field(1, expindex'))
        }
        end
    
    fun scaleField(dim,shape) =let
        val (index', expindex')= specialize(shape,0)
        in E.EIN{
            params = [E.TEN 1,E.FLD(dim)],
            index = index',
            body = E.Prod[ E.Tensor(0,[]), E.Field(1,expindex')]
        }
        end
    
    fun divideField(dim,shape) = let
        val (index', expindex')= specialize(shape,0)
        in E.EIN{
            params = [E.FLD(dim),E.TEN 1],
            index = index',
            body = E.Div(E.Field(0, expindex'), E.Tensor(1, []))
        }
        end
         
    fun negField(dim,shape) = let
        val (index', expindex')= specialize(shape,0)
        in E.EIN{
            params = [E.FLD(dim)],
            index = index',
            body = E.Neg(E.Field(0, expindex'))
        }
        end

    (*< d F /  d_i>_i  *)
        fun grad(a::alpha)=let
        val (index', expindex')= specialize(a::alpha,0)
        in E.EIN{
            params = [E.FLD(a)],
            index =index',
            body = E.Apply(E.Partial(expindex'),E.Field(0,[]))
        }
        end
        
        (*< Sigma d F_alpha /  d x_i>iALpha *)
        fun dotimes(dim,alpha)= let
             val n=length(alpha)
             fun expIndex(n,inc)=List.tabulate(n, (fn(x)=>E.V (x+inc)))
             val i'=expIndex(n,0)
             val z=length i'
            
        in E.EIN{
            params = [E.FLD(dim)], index =[dim]@alpha,
            body = E.Apply(E.Partial [E.V (z)] ,E.Field(0,i'))
        }
        end
        
    (*  <d F_i /d_i> *)
        fun divergence(dim,alpha)=let
        val (index', expindex')= specialize(alpha,0)
        in E.EIN{
            params = [E.FLD(dim)],
            index = [],
            body = E.Sum([(E.V 0,0,dim-1)],E.Apply(E.Partial([E.V(0)]),E.Field(0,[E.V(0)])))
        }
        end
        
    (*FLD here is bounded to image field, and dimension of h*)
    fun conv(dim,shape) =let
        val (index', expindex')= specialize(shape,0)
        in E.EIN{
            params = [E.IMG(dim),E.KRN],
            index = index',
            body= E.Conv(0,expindex',1,[])
        }
        end

    (* Probe: <F(x)>_{\alpha}   *)
    fun probe(alpha,dim) = let
        val (indexT, expindexT)= specialize(alpha,0)
 
        in E.EIN{
            params = [E.FLD(dim),E.TEN 0],
            index= indexT,
            body= E.Probe(E.Field(0, expindexT), E.Tensor(1,[]))
        }
        end

    (*(F_y/dx - F_x/dy )k*)
    val curl2d=E.EIN{
        params = [E.FLD 2],
        index = [],
        body = E.Sub(E.Apply(E.Partial([E.C 0]), E.Field(0,[E.C 1])),
                     E.Apply(E.Partial([E.C 1]), E.Field(0,[E.C 0])))
    }

    val curl3d=E.EIN{
        params = [E.TEN 1],
        index = [3],
        body = E.Sum([(E.V 1,0,2), (E.V 2,0,2)],E.Prod[E.Epsilon(0, 1, 2),E.Apply( E.Partial([E.V 1]), E.Field(0,[E.V 2]))])
    }
    
    (*Scalars*)
    (* Add Scalars*)
    val addScalar = E.EIN{
        params = [E.TEN 1, E.TEN 1] ,
        index = [],
        body = E.Add[ E.Tensor(0, []), E.Tensor(1, [])]
    }
        
    (* Subtract Scalars*)
    val subScalar = E.EIN{
        params = [E.TEN 1, E.TEN 1],
        index = [],
        body = E.Sub( E.Tensor(0, []), E.Tensor(1, []))
    }

    (* Divide Scalars*)
    val divScalar = E.EIN{
        params = [E.TEN 1, E.TEN 1],
        index = [],
        body = E.Div( E.Tensor(0, []), E.Tensor(1, []))
    }
        
        
    (* Product Scalars*)
    val prodScalar = E.EIN{
        params = [E.TEN 1, E.TEN 1],
        index = [],
        body = E.Prod[ E.Tensor(0, []), E.Tensor(1, [])]
    }
        
    (*Transform M_ij x_j+T*)
    fun transform(i, j) = E.EIN{
        params = [E.TEN 1, E.TEN 1, E.TEN 1],
        index = [i],
        body = E.Add
            [E.Sum([(E.V 1, 0,j-1)],E.Prod[E.Tensor(0, [E.V 0, E.V 1]), E.Tensor(1, [E.V 1])]),
              E.Tensor(1,[E.V 0])]
        }


  end; (* local *)

    end (* local *)

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