Home My Page Projects Code Snippets Project Openings diderot

# SCM Repository

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

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

Tue Mar 17 19:40:06 2015 UTC (6 years, 3 months ago) by cchiw
File size: 20240 byte(s)
det 3x3 field
(* creates EIN operators
*
* COPYRIGHT (c) 2012 The Diderot Project (http://diderot-language.cs.uchicago.edu)
*)

structure MkOperators = struct

local

structure E = Ein
structure P=Printer
in

fun specialize(alpha,inc)=  List.tabulate(length(alpha), (fn(x)=>E.V (x+inc)))
fun sumIds(n,inc,alpha)=let
val vs=List.tabulate(n, (fn v=>E.V (v+inc)))
in ListPair.map  (fn(v,i)=>(v, 0, i-1))   (vs, alpha) end
fun sumIds2(n,i)=List.tabulate(n, (fn v=>(E.V v, 0, i)))

params = [E.TEN(1,[]), E.TEN(1,[])] ,
index = [],
body = E.Add[ E.Tensor(0, []), E.Tensor(1, [])]
}

(* Adding tensors : < X{\alpha} + Y_{\alpha}>_{\alpha} *)
val expindex= specialize(alpha,0)
in
E.EIN{
params = [E.TEN(1,alpha), E.TEN(1,alpha)],
index = alpha,
body = E.Add[E.Tensor(0, expindex), E.Tensor(1, expindex)]
}
end

(* mkField functions*)
(*Adding Fields : < F{\alpha} + G_{\alpha}>_{\alpha} *)
val expindex= specialize(shape,0)
in E.EIN{
params = [E.FLD(dim),E.FLD(dim)],
index = shape,
}
end

(*Tensor and Fields*)
val expindex= specialize(shape,0)
in E.EIN{
params = [E.TEN(1,shape),E.FLD(dim)],
index = shape,
}
end
(********************************* Sub **************************************)
(* Subtract Scalars*)
val subRR = E.EIN{
params = [E.TEN(1,[]), E.TEN(1,[])],
index = [],
body = E.Sub( E.Tensor(0, []), E.Tensor(1, []))
}

fun subTT alpha=let
val expindex= specialize(alpha,0)
in
E.EIN{
params = [E.TEN(1,alpha), E.TEN(1,alpha)],
index = alpha,
body = E.Sub(E.Tensor(0, expindex), E.Tensor(1, expindex))
}
end

fun subTF(dim,shape) =let
val expindex= specialize(shape,0)
in
E.EIN{
params = [E.TEN(1,shape),E.FLD(dim)],
index = shape,
}
end

fun subFT(dim,shape) = let
val expindex= specialize(shape,0)
in
E.EIN{
params = [E.TEN(1,[]),E.FLD(dim)],
index = shape,
body = E.Sub(E.Field(1, expindex),E.Lift(E.Tensor(0, expindex)))
}
end

fun subFF(dim,shape) =let
val expindex= specialize(shape,0)
in E.EIN{
params = [E.FLD(dim),E.FLD(dim)],
index = shape,
body = E.Sub(E.Field(0, expindex),E.Field(1, expindex))
}
end
(********************************** mul *************************************)
(* Product Scalars*)
val mulRR = E.EIN{
params =[E.TEN(1,[]), E.TEN(1,[])],
index = [],
body = E.Prod[ E.Tensor(0, []), E.Tensor(1, [])]
}

(* scalar times tensor product: <s * T_{\alpha}>_{\alpha} *)
fun mulRT alpha = let
val expindex= specialize(alpha,0)
in
E.EIN{
params = [E.TEN(1,[]), E.TEN(1,alpha)],
index = alpha,
body = E.Prod[ E.Tensor(0, []),  E.Tensor(1, expindex)]
}
end

fun mulRF(dim,shape) =let
val  expindex= specialize(shape,0)
in E.EIN{
params = [E.TEN(1,[]),E.FLD(dim)],
index = shape,
body = E.Prod[E.Lift( E.Tensor(0,[])), E.Field(1,expindex)]
}
end

fun mulST(dim,shape) =let
val  expindex= specialize(shape,0)
in E.EIN{
params = [E.TEN(1,shape),E.FLD(dim)],
index = shape,
body = E.Prod[E.Lift( E.Tensor(0,expindex)), E.Field(1,[])]
}
end

fun mulSS dim = E.EIN{
params = [E.FLD(dim),E.FLD(dim)],
index = [],
body = E.Prod[E.Field(0, []),E.Field(1, [])]
}

fun mulSF(dim,shape) =let
val  expindex= specialize(shape,0)
in E.EIN{
params = [E.FLD(dim),E.FLD(dim)],
index = shape,
body = E.Prod[E.Field(0, []),E.Field(1, expindex)]
}
end

(************************************div ************************************)
(* Divide Scalars*)
val divRR = E.EIN{
params = [E.TEN(1,[]), E.TEN(1,[])],
index = [],
body = E.Div( E.Tensor(0, []), E.Tensor(1, []))
}

fun divTR alpha =let
val expindex= specialize(alpha,0)
in
E.EIN{
params = [E.TEN(1,alpha), E.TEN(1,[])],
index = alpha,
body = E.Div(E.Tensor(0, expindex), E.Tensor(1,[]))
}
end

fun divFR(dim,shape) = let
val expindex= specialize(shape,0)
in E.EIN{
params = [E.FLD(dim),E.TEN(1,[])],
index = shape,
body = E.Div(E.Field(0, expindex),E.Lift(  E.Tensor(1, [])))
}
end

fun divSS dim = E.EIN{
params = [E.FLD(dim),E.FLD(dim)],
index = [],
body = E.Div(E.Field(0, []),E.Field(1, []))
}

fun divFS(dim,shape) = let
val  expindex= specialize(shape,0)
in  E.EIN{
params = [E.FLD(dim),E.FLD(dim)],
index = shape,
body = E.Prod[E.Field(0, expindex),E.Div(E.Const 1,E.Field(1, []))]
}
end
(************************************* Neg **********************************)
fun negTT alpha=let
val  expindex= specialize(alpha,0)
in
E.EIN{
params = [E.TEN(1,alpha)],index = alpha,
body = E.Neg(E.Tensor(0, expindex))
}
end

fun negFF(dim,shape) = let
val expindex = specialize(shape,0)
in E.EIN{
params = [E.FLD(dim)],index = shape,
body = E.Neg(E.Field(0, expindex))
}
end

(****************************** cross ***********************************)
(*crossProduct is on 3D vectors ..vec3 t8=t0 × t1; *)
val cross3TT = E.EIN{
params = [E.TEN(1,[3]), E.TEN(1,[3])],
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 ]) ])
}

(*2-d cross product Eps_{ij}U_i V_j*)
val cross2TT = E.EIN{
params = [E.TEN(1,[2]), E.TEN(1,[2])],
index= [],
body=E.Sum([(E. V 0,0,1),(E.V 1,0,1)],
E.Prod[ E.Eps2(0, 1), E.Tensor(0, [E.V 0]),  E.Tensor(1, [E.V 1])])
}

(*Field Cross Product*)
val cross2FF = E.EIN{
params = [E.FLD(2), E.FLD(2)],index= [],
body=E.Sum([(E. V 0,0,1),(E.V 1,0,1)],
E.Prod[ E.Eps2(0, 1), E.Field(0, [E.V 0]),  E.Field(1, [E.V 1]) ])
}

(*Field Cross Product*)
val cross3FF = E.EIN{
params = [E.FLD(3), E.FLD(3)],index= [3],
body=E.Sum([(E. V 1,0,2),(E.V 2,0,2)],
E.Prod[ E.Epsilon(0, 1, 2),
E.Field(0, [E.V 1]),  E.Field(1, [E.V 2 ]) ])
}
(********************  outerProduct ********************************)
(*Vector Examples : <T_i * T_j>_ij..t0⊗t1*)
fun outerTT(dimA,dimB) =
E.EIN{
params = [E.TEN(1,[dimA]), E.TEN(1,[dimB])],
index= [dimA,dimB],
body= E.Prod[E.Tensor(0, [E.V 0]), E.Tensor(1, [E.V 1])]
}

(*Assumes same dimension vector field *)
fun outerFF(dim,i,j) =
E.EIN{
params = [E.FLD(dim),E.FLD(dim)],
index = [i,j],
body = E.Prod[E.Field(0, [E.V 0]),E.Field(1, [E.V 1])]
}

(*************************** inner product **********************************)
(* generic inner product: <T_{\alpha i} * T_{i \beta}>_{\alpha \beta} *)
fun innerTT(shape1,i::beta) = let
val alpha= List.take(shape1,length(shape1)-1)
val expindexA= specialize(alpha,0)
val 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,shape1) ,E.TEN(1,i::beta)],
index = alpha@beta,
body = E.Sum(s'', E.Prod[
E.Tensor(0, expindexA@[s']),   (* T_{\alpha i} *)
E.Tensor(1, [s']@expindexB )  (* T'_{i \beta} *)])
}
end
| innerTT _ = raise Fail "Wrong shape for inner product"

(* generic inner product: <T_{\alpha i} * T_{i \beta}>_{\alpha \beta} *)
fun innerFF(shape1,dim,i::beta) = let
val alpha= List.take(shape1,length(shape1)-1)
val expindexA= specialize(alpha,0)
val expindexB= specialize(beta,(length(alpha)))
val sid=E.V(length(alpha)+ length(beta))
in E.EIN{
params = [E.FLD(dim) ,E.FLD(dim)],index = alpha@beta,
body = E.Sum([(sid,0,i-1)],
E.Prod[
E.Field(0, expindexA@[sid]),   (* F_{\alpha i} *)
E.Field(1, [sid]@expindexB )  (* F'_{i \beta} *)
])}
end
| innerFF _ = raise Fail "Wrong shape for innerProductField"

fun innerFT(shape1,dim,i::beta) = let
val alpha= List.take(shape1,length(shape1)-1)
val expindexA= specialize(alpha,0)
val expindexB= specialize(beta,(length(alpha)))
val sid=E.V(length(alpha)+ length(beta))
in E.EIN{
params = [E.FLD(dim),E.TEN(1,i::beta)],
index = alpha@beta,
body = E.Sum([(sid,0,i-1)],
E.Prod[
E.Field(0, expindexA@[sid]),   (* F_{\alpha i} *)
E.Lift(E.Tensor(1, [sid]@expindexB ))  (* F'_{i \beta} *)
])}
end
| innerFT _ = raise Fail "Wrong shape for innerProductFieldTensor"

fun innerTF(shape1,dim,i::beta) = let
val alpha= List.take(shape1,length(shape1)-1)
val expindexA= specialize(alpha,0)
val expindexB= specialize(beta,(length(alpha)))
val sid=E.V(length(alpha)+ length(beta))
in E.EIN{
params = [E.TEN(1,shape1),E.FLD(dim)],index = alpha@beta,
body = E.Sum([(sid,0,i-1)],
E.Prod[E.Lift(E.Tensor(0, expindexA@[sid])),   (* F_{\alpha i} *)
E.Field(1, [sid]@expindexB )  (* F'_{i \beta} *)
])}
end
| innerTF _ = raise Fail "Wrong shape for innerProductTensorField"

(*************************** colon product **********************************)
(*<T_{\alpha i j} * B{i j \beta }>_\alpha \beta*)
fun colonTT(shape1,i::j::beta) = let
val alpha= List.take(shape1,length(shape1)-2)
val expindexA= specialize(alpha,0)
val expindexB= specialize(beta,(length(alpha)))
val sumi=length(alpha)+ length(beta)
val s'=[E.V sumi,E.V(sumi+1)]
val sx=[(E.V sumi,0,i-1),(E.V(sumi+1),0,j-1)]
in
E.EIN{
params = [E.TEN(1,shape1),E.TEN(1,i::j::beta)],
index = alpha@beta,
body = E.Sum(sx,E.Prod[ E.Tensor(0, expindexA@s'), E.Tensor(1,s'@expindexB)])
}
end

(*<F_{\alpha i j} * G{i j \beta }>_\alpha \beta*)
fun colonFF(shape1,i::j::beta) = let
val alpha= List.take(shape1,length(shape1)-2)
val expindexA= specialize(alpha,0)
val expindexB= specialize(beta,(length(alpha)))
val sumi=length(alpha)+ length(beta)
val s'=[E.V sumi,E.V(sumi+1)]
val sx=[(E.V sumi,0,i-1),(E.V(sumi+1),0,j-1)]
in
E.EIN{
params = [E.TEN(1,shape1),E.TEN(1,i::j::beta)],
index = alpha@beta,
body = E.Sum(sx,E.Prod[E.Field(0, expindexA@s'), E.Field(1,s'@expindexB)])
}
end

(******************** Norm  ********************************)

(*get norm, but without the sqrt
* implemented as a summation over a modulate
*)
fun magnitudeTT alpha =let
val expindex= specialize(alpha,0)
val sx= sumIds(length(alpha),0,alpha)
val e= E.EIN{
params = [E.TEN(1,alpha), E.TEN(1,alpha)],
index = [],
body = E.Sum(sx,
E.Prod[E.Tensor(0, expindex), E.Tensor(1, expindex)])
}
(*val _=print(String.concat["\ntesting mag tt",P.printerE e ])*)
in e
end

fun magnitudeFF(dim,[]) =E.EIN{params = [E.FLD(dim),E.FLD(dim)],index = [],body =  E.Field(0, [])}
| magnitudeFF(dim,alpha) =let
val expindex= specialize(alpha,0)
val sx= sumIds(length(alpha),0,alpha)
val e=
E.EIN{
params = [E.FLD(dim),E.FLD(dim)],
index = [],
body = E.Sqrt(E.Sum(sx, E.Prod[E.Field(0, expindex),E.Field(1,expindex)]))
}
(*val _=print(String.concat["\ntesting mag FF",P.printerE e ])*)
in e
end

fun normalizeFF(dim,[]) =E.EIN{params = [E.FLD(dim)],index = [],body =  E.Field(0, [])}
| normalizeFF(dim,alpha) =let
val expindex= specialize(alpha,0)
(*shift indices in the inner product*)
val expindexDot= specialize(alpha,length(alpha))
val i=List.hd alpha
val sx= sumIds(length(alpha),length(alpha),alpha)
val f=E.Field(0, expindex)
val g=E.Field(1, expindexDot)
val e=
E.EIN{
params = [E.FLD(dim) ,E.FLD(dim)],
index = [dim],
body = E.Prod[f,E.Div(E.Const 1,E.Sqrt(E.Sum(sx, E.Prod[g,g])))]
}
(*val _=print(String.concat["\ntesting normalize FF",P.printerE e ])*)
in e
end

(************************* trace *************************)
(* Trace: <M_{i, i}>  This one Sx represents both i's*)
fun traceT dim = E.EIN{
params = [E.TEN(1,[dim,dim])],index = [],
body = E.Sum([(E.V 0,0,dim-1)],E.Tensor(0, [E.V 0, E.V 0]))
}

(* Trace: <Sigma_i F_{\alpha i, i}>  This one Sx represents both i's*)
fun traceF(dim,alpha) =let
val expindex= specialize(alpha,0)
val s=E.V(length(alpha))
in
E.EIN{
params = [E.FLD(dim)],
index = alpha,
body = E.Sum([(s,0,dim-1)],E.Field(0, expindex@[s,s]))
}
end

(************************* tranpose *************************)
fun transposeT alpha =E.EIN{
params = [E.TEN(1,alpha)],
index= List.rev alpha,
body= E.Tensor(0, [E.V 1,E.V 0])
}

(*Transpose Field F_{ji}*)
fun transposeF(dim,i,j) =E.EIN{
params = [E.FLD(dim)], index= [i,j],
body= E.Field(0, [E.V 1,E.V 0])
}
(************************* determinant *************************)
val detT2 =E.EIN{
params = [E.TEN(1,[2,2])],
index= [],
body= E.Sub(E.Prod[E.Tensor(0, [E.C 0,E.C 0]),E.Tensor(0, [E.C 1,E.C 1])],
E.Prod[E.Tensor(0, [E.C 0,E.C 1]),E.Tensor(0, [E.C 1,E.C 0])])
}

val detF2  =E.EIN{
params = [E.FLD 2], index= [],
body= E.Sub(E.Prod[E.Field(0, [E.C 0,E.C 0]),E.Field(0, [E.C 1,E.C 1])],
E.Prod[E.Field(0, [E.C 0,E.C 1]),E.Field(0, [E.C 1,E.C 0])])
}

val detF3  =E.EIN{
params = [E.FLD(3)], index= [],
body=
E.Prod[E.Field(0, [E.C 0,E.C 0]),E.Field(0, [E.C 1,E.C 1]),E.Field(0, [E.C 2,E.C 2])],
E.Prod[E.Field(0, [E.C 0,E.C 1]),E.Field(0, [E.C 1,E.C 2]),E.Field(0, [E.C 2,E.C 0])],
E.Prod[E.Field(0, [E.C 0,E.C 2]),E.Field(0, [E.C 1,E.C 0]),E.Field(0, [E.C 2,E.C 1])],
E.Neg(E.Prod[E.Field(0, [E.C 0,E.C 2]),E.Field(0, [E.C 1,E.C 1]),E.Field(0, [E.C 2,E.C 0])]),
E.Neg(E.Prod[E.Field(0, [E.C 0,E.C 1]),E.Field(0, [E.C 1,E.C 0]),E.Field(0, [E.C 2,E.C 2])]),
E.Neg(E.Prod[E.Field(0, [E.C 0,E.C 0]),E.Field(0, [E.C 1,E.C 2]),E.Field(0, [E.C 2,E.C 1])])
]
}

(************************* other tensor ops *************************)
fun modulate dim =E.EIN{
params = [E.TEN(1,[dim]), E.TEN(1,[dim])],
index = [dim],
body = E.Prod[E.Tensor(0, [E.V 0]), E.Tensor(1, [E.V 0])]
}

fun identity dim =E.EIN{
params = [],index = [dim,dim], body = E.Delta(E.V(0), E.V(1))
}

fun iter ([],_,cnt)=[]
| iter(true::es,c::cs,cnt)=[E.C c]@iter(es,cs,cnt)
|iter(false::es,cs,cnt)=[E.V cnt]@iter(es,cs,cnt+1)
in
E.EIN{  params = [E.TEN(1,argTy)],index = rstTy,body = E.Tensor(0, ix)}
end

(******************** other field ops  ********************************)
(*FLD here is bounded to image field, and dimension of h*)
fun conv(dim,shape) =let
val expindex= specialize(shape,0)
in E.EIN{
params = [E.IMG(dim,shape),E.KRN],
index = shape,
body= E.Conv(0,expindex,1,[])
}
end

(* Probe: <F(x)>_{\alpha}   *)
fun probe(alpha,dim) = let
val  expindex= specialize(alpha,0)
in E.EIN{
params = [E.FLD(dim),E.TEN(0,[])], index= alpha,
body= E.Probe(E.Field(0, expindex), E.Tensor(1,[]))
}
end
(***************************** derivative ****************************)
(*\EinExp{\sum_{ij}\mathcal{E}_{ij} \frac{ F_j}{\partial x_i}*)
val curl2d=E.EIN{
params = [E.FLD 2],
index = [],
body = E.Sum([(E.V 0,0,1), (E.V 1,0,1)],
E.Prod[E.Eps2(0, 1),
E.Apply( E.Partial([E.V 0]), E.Field(0,[E.V 1]))])
}

val curl3d=E.EIN{
params = [E.TEN(1,[3])],
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]))])
}

(*< d F /  d_i>_i  *)
val a=List.hd(alpha)
val  expindex= specialize(alpha,0)
in E.EIN{
params = [E.FLD(a)],
index =alpha,
body = E.Apply(E.Partial(expindex),E.Field(0,[]))
}
end

(*< Sigma d F_alpha /  d x_i>ALpha  i CHANGE HERE *)
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)
in E.EIN{
params = [E.FLD(dim)], index =alpha@[dim],
body = E.Apply(E.Partial [E.V n] ,E.Field(0,i'))
}
end

(*  <d F_i /d_i> *)
fun divergence(dim,alpha)=let
val  expindex= specialize(alpha,0)
val sumI=length(alpha)
val sumIndex=E.V(sumI)
val sumIndexL=[sumIndex]
val S=expindex@sumIndexL
in E.EIN{
params = [E.FLD(dim)],
index = alpha,
body = E.Sum([(sumIndex,0,dim-1)],
E.Apply(E.Partial(sumIndexL),E.Field(0,S)))
}
end

end; (* local *)

end (* local *)