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

Mon Nov 4 21:33:35 2013 UTC (6 years, 10 months ago) by cchiw
File size: 9992 byte(s)
mid-il fixes
(* examples.sml
*
* COPYRIGHT (c) 2012 The Diderot Project (http://diderot-language.cs.uchicago.edu)
*)

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 specialize(alpha,inc)=(alpha,expIndex(length(alpha),inc))

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

fun createVec(dim)=E.EIN{
params = [E.TEN], index = [dim],
body = E.Tensor(0, [E.V 0])
}

(* just to test normalize
val (index', expindex')= specialize(alpha,0)
in
E.EIN{
params = [E.TEN, E.TEN], index = index',
}
end
*)

(* Subtracting tensors *)
fun subTen(alpha)=let
val (index', expindex')= specialize(alpha,0)
in
E.EIN{
params = [E.TEN, E.TEN], 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, E.TEN], 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],         (* M *)
index = [],           (* i *)
body = E.Sum([(E.V 0,1,dim)],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],
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, E.TEN],    (* 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)]
in E.EIN{
params = [E.TEN, E.TEN],              (* 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),(E.V(sumi+1),0,j)]
in E.EIN{
params = [E.TEN,E.TEN],
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*)
fun outerProduct(dimA,dimB) =E.EIN{
params = [E.TEN, E.TEN],
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], index= [i,j],
body= E.Tensor(0, [E.V 1,E.V 0])
}

(* generic mod <T_{\alpha} * T_{ \alpha}>_{\alpha } *)
(*fun modulate(alpha) = let
val (indexA, expindexA)= specialize(alpha,0)
in E.EIN{
params = [E.TEN, E.TEN],
index = indexA,
body = E.Prod[E.Tensor(0, expindexA), E.Tensor(1, expindexA)]
}
end*)
fun modulate(dim) =E.EIN{
params = [E.TEN, E.TEN],
index = [dim],
body = E.Prod[E.Tensor(0, [E.V 0]), E.Tensor(1, [E.V 0])]
}

(*crossProduct is on 3D vectors *)
val crossProduct = E.EIN{
params = [E.TEN, E.TEN],
index= [3],
body=E.Sum([(E. V 1,0,3),(E.V 2,0,3)],
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*)
params = [E.TEN,E.FLD(dim)],
index = [],
}

fun subTenField(dim) = E.EIN{
params = [E.TEN,E.FLD(dim)],
index = [],
}

fun subFieldTen(dim) = E.EIN{
params = [E.TEN,E.FLD(dim)],
index = [],
body = E.Sub(E.Field(1, []),E.Tensor(0, []))
}

(* mkField functions*)
(*Adding Fields : < F{\alpha} + G_{\alpha}>_{\alpha} *)
val (index', expindex')= specialize(shape,0)
in E.EIN{
params = [E.FLD(dim),E.FLD(dim)],
index = index',
}
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,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.TEN,E.FLD(dim)],
index = index',
body = E.Div(E.Field(1, expindex'), E.Tensor(0, []))
}
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 *)
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

(*  <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)],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.FLD(dim),E.TEN],
index = index',
body= E.Conv(0,expindex',1,[])
}
end

(* Probe: <F(x)>_{\alpha}   *)
fun probe(alpha,dim,shape) = let
val (indexT, expindexT)= specialize(alpha,0)
val (indexF, expindexF)= specialize(shape,length(alpha))
in E.EIN{
params = [E.TEN,E.FLD(dim)],
index= (indexT@indexF),
body= E.Probe(E.Field(1, expindexF), E.Tensor(0,expindexT))
}
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],
index = [3],
body = E.Sum([(E.V 1,0,3), (E.V 2,0,3)],E.Apply( E.Partial([E.V 1]), E.Prod[E.Epsilon(0, 1, 2), E.Field(0,[E.V 2])]))
}

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

(* Subtract Scalars*)
val subScalar = E.EIN{
params = [E.TEN, E.TEN],
index = [],
body = E.Sub( E.Tensor(0, []), E.Tensor(1, []))
}

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

(* Product Scalars*)
val prodScalar = E.EIN{
params = [E.TEN, E.TEN],
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, E.TEN, E.TEN],
index = [i],