(* 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,alpha), E.TEN(1,alpha)], index = index', body = E.Add[E.Tensor(0, expindex'), E.Tensor(1, expindex')] } end fun createVec(dim)=E.EIN{ params = [E.TEN(1,[dim])], index = [dim], body = E.Tensor(0, [E.V 0]) } val zero=E.EIN{ params = [], index = [], body = E.Const(0) } (* Subtracting tensors *) fun subTen(alpha)=let val (index', expindex')= specialize(alpha,0) in E.EIN{ params = [E.TEN(1,alpha), E.TEN(1,alpha)], 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,alpha), E.TEN(1,[])], index = index', body = E.Div(E.Tensor(0, expindex'), E.Tensor(1,[])) } end (* Trace: This one Sx represents both i's*) fun trace(dim) = E.EIN{ params = [E.TEN(1,[dim,dim])], (* M *) index = [], (* i *) body = E.Sum([(E.V 0,0,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,alpha)], index = index', body = E.Neg(E.Tensor(0, expindex')) } end (* scalar times tensor product: _{\alpha} *) fun scaleTen(alpha) = let val (index', expindex')= specialize(alpha,0) in E.EIN{ params = [E.TEN(1,[]), E.TEN(1,alpha)], (* s and T *) index = index', (* \alpha *) body = E.Prod[ E.Tensor(0, []), E.Tensor(1, expindex')] } end (* generic inner product: _{\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,shape1) ,E.TEN(1,i::beta)], (* 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 (*_\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,shape1),E.TEN(1,i::j::beta)], index = indexA@indexB, body = E.Sum(s'',E.Prod[ E.Tensor(0, expindexA@s'), E.Tensor(1,s'@expindexB) ]) } end (*Vector Examples : _ij..t0⊗t1*) fun outerProduct(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])] } fun transpose([i,j]) =E.EIN{ params = [E.TEN(1,[i,j])], index= [i,j], body= E.Tensor(0, [E.V 1,E.V 0]) } 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])] } (*crossProduct is on 3D vectors ..vec3 t8=t0 × t1; *) val crossProduct = 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 ]) ]) } (* 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>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 (* *) 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: _{\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,[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]))]) } (*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,[i,j]), E.TEN(1,[j]), E.TEN(1,[j])], 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 *)
Click to toggle
does not end with </html> tag
does not end with </body> tag
The output has ended thus: , [E.V 1])]), E.Tensor(1,[E.V 0])] } end; (* local *) end (* local *)