Home My Page Projects Code Snippets Project Openings diderot

# SCM Repository

[diderot] Diff of /branches/charisee/src/compiler/high-il/normalize-ein.sml
 [diderot] / branches / charisee / src / compiler / high-il / normalize-ein.sml

# Diff of /branches/charisee/src/compiler/high-il/normalize-ein.sml

revision 2460, Wed Oct 9 19:09:26 2013 UTC revision 2795, Tue Nov 4 21:58:11 2014 UTC
# Line 5  Line 5
5
6      structure E = Ein      structure E = Ein
7      structure P=Printer      structure P=Printer
8      structure O =OrderEin      structure F=Filter
9      in      structure G=EpsHelpers

(*Flattens Add constructor: change, expression *)
|flatten(i,((E.Const c):: l'))=
if (c>0.0 orelse c<0.0) then let
val(b,a)=flatten(i,l') in (b,[E.Const c]@a) end
else flatten(1,l')
| flatten(i,[])=(i,[])
| flatten (i,e::l') =  let
val(b,a)=flatten(i,l') in (b,[e]@a) end

val (b,a)=flatten(0,e)
in case a
of [] => (1,E.Const(1.0))
| [e] => (1,e)
(* end case *)
end

fun mkProd [e]=(1,e)
| mkProd(e)=let
fun flatten(i,((E.Prod l)::l'))= flatten(1,l@l')
|flatten(i,((E.Const c)::l'))=
if(c>0.0 orelse  0.0>c) then (3,[E.Const 0.0])
else flatten(i,l')
| flatten(i,[])=(i,[])
| flatten (i,e::l') =  let val(a,b)=flatten(i,l') in (a,[e]@b) end
val (change,a)=flatten(0,e)
in if(change=3) then (1,E.Const(0.0))
else case a
of [] => (1,E.Const(0.0))
| [e] => (1,e)
| es => (change, E.Prod es)
(* end case *)
end

10
11        in
12
13  fun rmEpsIndex(_,_,[])=[]  fun err str=raise Fail (String.concat["Ill-formed EIN Operator",str])
14  | rmEpsIndex([],[],cs)=cs  val testing=1
| rmEpsIndex([],m ,e1::cs)=[e1]@rmEpsIndex(m,[],cs)
| rmEpsIndex(i::ix,rest ,(E.V c)::cs)=
if(i=c) then rmEpsIndex(rest@ix,[],cs)
else rmEpsIndex(ix,rest@[i],(E.V c)::cs)

(* Transform eps to deltas*)
fun epsToDels(E.Sum(count,E.Prod e))= let
fun doubleEps((E.Epsilon (a,b,c))::(E.Epsilon(d,e,f))::es,eps,e3)=
let

(*Function is called when eps are being changed to deltas*)
fun createDeltas(i,s,t,u,v, e3)= let

(*remove index from original index list*)
(*currrent, left, sumIndex*)
15
16              val s'= rmEpsIndex([i,s,t,u,v],[],count)  fun flatProd [e]=e
17              val s''=[E.V s, E.V t ,E.V u, E.V v]  | flatProd e=E.Prod e
val deltas= E.Sub(
E.Sum(s'',E.Prod([E.Delta(E.V s,E.V u), E.Delta(E.V t,E.V v)] @e3)),
E.Sum(s'',E.Prod([E.Delta(E.V s,E.V v), E.Delta(E.V t,E.V u)]@e3)))

in (case (eps,es,s')
of ([],[],[]) =>(1,deltas)
|([],_,[]) =>(1,E.Prod( es@[deltas]))
|([],[],_)=>(1,E.Sum(s',deltas))
|([],_,_)=>(1,E.Sum(s',E.Prod(es@[deltas])))
|(_,_,[])=>(1,E.Prod(eps@es@[deltas]))
|_ =>(1, E.Sum(s', E.Prod(eps@es@[deltas])))
)
end

in if(a=d) then createDeltas(a,b,c,e,f, e3)
else if(a=e) then createDeltas(a,b,c,f,d, e3)
else if(a=f) then createDeltas(a,b,c,d,e, e3)
else if(b=d) then createDeltas(b,c,a,e,f, e3)
else if(b=e) then createDeltas(b,c,a,f,d,e3)
else if(b=f) then createDeltas(b,c,a,d,e,e3)
else if(c=d) then createDeltas(c,a,b,e,f,e3)
else if(c=e) then createDeltas(c,a,b,f,d,e3)
else if(c=f) then createDeltas(c,a,b,d,e,e3)
else (0,E.Const 0.0)
end
fun findeps(e,[])= (e,[])
| findeps(e,(E.Epsilon eps)::es)=  findeps(e@[E.Epsilon eps],es)
| findeps(e,es)= (e, es)

fun dist([],eps,rest)=(0,eps,rest)
| dist([e],eps,rest)=(0,eps@[e],rest)
| dist(c1::current,eps,rest)=let
val(i, exp)= doubleEps(c1::current,eps,rest)
in  (case i of 1=>(i,[exp],[E.Const 2.0])
|_=> dist(current, eps@[c1],rest))
end
18
val (es,rest)=findeps([],e)
19
20    fun prodAppPartial(es,p1)=(case es
21        of []      => raise Fail "Empty App Partial"
22        | [e1]     => E.Apply(E.Partial p1,e1)
23        | (e1::e2) => let
24            val l= prodAppPartial(e2,p1)
25            val (_,e2')= F.mkProd[e1,l]
26            val (_,e1')=F.mkProd(e2@ [E.Apply(E.Partial p1, e1)])
27      in      in
29      end      end
30        (* end case *))
31
32    (*rewritten Sum*)
33    fun mkSum(c1,e1)=(case e1
34        of E.Conv _   => (0,E.Sum(c1,e1))
35        | E.Field _   => (0,E.Sum(c1,e1))
36        | E.Probe _   => (0,E.Sum(c1,e1))
37        | E.Apply _   => (0,E.Sum(c1,e1))
38        | E.Delta _   => (0,E.Sum(c1,e1))
39        | E.Epsilon _ => (0,E.Sum(c1,e1))
40        | E.Tensor _  => (0,E.Sum(c1,e1))
41        | E.Neg e2    => (1,E.Neg(E.Sum(c1,e2)))
42        | E.Sub (a,b) => (1,E.Sub(E.Sum(c1,a),E.Sum(c1,b)))
44        | E.Div (a,b) => (1,E.Div(E.Sum(c1,a),E.Sum(c1,b)))
45        | E.Lift e    => (1,E.Lift(E.Sum(c1,e)))
46        | E.Sum(c2,e2)=> (1,E.Sum(c1@c2,e2))
47        | E.Prod p     =>F.filterSca(c1,p)
48        | E.Const _   => err("Sum of Const")
49        | E.Partial _ => err("Sum of Partial")
50        | E.Krn _     => err("Krn used before expand")
51        | E.Value _   => err("Value used before expand")
52        | E.Img _     => err("Probe used before expand")
53        (*end case*))
54
55  fun rmIndex(_,_,[])=[]  (*rewritten Apply*)
56      | rmIndex([],[],cs)=cs  fun mkapply(d1,e1)=(case e1
57      | rmIndex([],m ,e1::cs)=[e1]@rmIndex(m,[],cs)      of E.Lift e   => (1,E.Const 0)
58      | rmIndex(i::ix,rest ,c::cs)=      | E.Prod []   => err("Apply of empty product")
59          if(i=c) then rmIndex(rest@ix,[],cs)      | E.Add []    => err("Apply of empty Addition")
60          else rmIndex(ix,rest@[i],c::cs)      | E.Conv(v, alpha, h, d2)    =>let
61                            val E.Partial d3=d1
62  (* Apply deltas to tensors/fields*)                          in (1,E.Conv(v,alpha,h,d2@d3)) end
63  fun reduceDelta(E.Sum(c,E.Prod p))=let      | E.Field _   => (0,E.Apply(d1,e1))
64        | E.Probe _   => (0,E.Apply(d1,e1)) (*FIX ME, Should be error actually apply of a tensor result*)
65      fun findDeltas(dels,rest,E.Delta d::es)= findDeltas(dels@[E.Delta d], rest, es)      | E.Apply(E.Partial d2,e2)  => let
66      | findDeltas(dels,rest,E.Epsilon eps::es)=findDeltas(dels,rest@[E.Epsilon eps],es)                          val E.Partial d3=d1
67      | findDeltas(dels,rest,es)=  (dels,rest,es)                          in (1,E.Apply(E.Partial(d3@d2),e2)) end
68        | E.Apply _   => err" Apply of non-Partial expression"
69        | E.Sum(c2,e2)=> (1,E.Sum(c2,E.Apply(d1,e2)))
70      fun distribute(change,d,dels,[],done)=(change,dels@d,done)      | E.Neg e2    => (1,E.Neg(E.Apply(d1,e2)))
72      | distribute(change,E.Delta(i,j)::ds,dels,E.Tensor(id,[tx])::es,done)=      | E.Sub (a,b) => (1,E.Sub(E.Apply(d1,a),E.Apply(d1,b)))
73          if(j=tx) then distribute(change@[j],dels@ds,[] ,es ,done@[E.Tensor(id,[i])])      | E.Div (g,b) => let
else distribute(change,ds,dels@[E.Delta(i,j)],E.Tensor(id,[tx])::es,done)
| distribute(change,E.Delta(i,j)::ds,dels,E.Field(id,[tx])::es,done)=
if(j=tx) then distribute(change@[j],dels@ds,[] ,es ,done@[E.Field(id,[i])])
else distribute(change,ds,dels@[E.Delta(i,j)],E.Field(id,[tx])::es,done)
| distribute(change,d,dels,e::es,done)=distribute(change,dels@d,[],es,done@[e])

val (dels,eps,es)=findDeltas([],[],p)
val (change,dels',done)=distribute([],dels,[],es,[])
val index=rmIndex(change,[],c)

74    in    in
75         (change, E.Sum(index,E.Prod (eps@dels'@done)))          (case F.filterField[b]
76            of (_,[]) => (1,E.Div(E.Apply(d1,g),b)) (*Division by a real*)
77            | (pre,h) => let
78                val g'=E.Apply(d1,g)
79                val h'=E.Apply(d1,flatProd(h))
80                val num=E.Sub(E.Prod([g']@h),E.Prod[g,h'])
81                val denom=E.Prod(pre@h@h)
82                in (1,E.Div(num,denom))
83                end
84            (*end case*))
85    end    end
86
87        | E.Prod p =>let
88            val (pre, post)= F.filterField p
89            val E.Partial d3=d1
90            in F.mkProd(pre@[prodAppPartial(post,d3)])
91            end
92        | E.Const _   => err("Const without Lift")
93        | E.Tensor _  => err("Tensor without Lift")
94        | E.Delta _   => err("Apply of Delta")
95        | E.Epsilon _ => err("Apply of Eps")
96        | E.Partial _ => err("Apply of Partial")
97        | E.Krn _     => err("Krn used before expand")
98        | E.Value _   => err("Value used before expand")
99        | E.Img _     => err("Probe used before expand")
100        (*end case*))
101
fun mkApplySum(E.Apply(E.Partial d,E.Sum(c,e)))=(print "apply sum";case e
of E.Tensor(a,[])=>(1,E.Const 0.0)
| E.Const _ =>(1,E.Const 0.0)
| E.Add l => (1,E.Add(List.map (fn e => E.Apply(E.Partial d, E.Sum(c,e))) l))
| E.Sub(e2, e3) =>(1, E.Sub(E.Apply(E.Partial d, E.Sum(c,e2)), E.Apply(E.Partial d, E.Sum(c,e3))))

| E.Prod [e1]=>(1,E.Apply(E.Partial d,E.Sum(c,e1)))
| E.Prod(E.Tensor(a,[])::e1::[])=>(1,E.Prod[E.Tensor(a,[]),E.Apply(E.Partial d,E.Sum(c,e1))])

| E.Prod(E.Tensor(a,[])::e2)=>(1,E.Prod[E.Tensor(a,[]),E.Apply(E.Partial d,E.Sum(c,E.Prod e2))])

| E.Prod es=>(let
fun prod [e1] =E.Apply(E.Partial d,e1)
| prod (E.Epsilon eps1::es) = (E.Apply(E.Partial d, E.Prod (E.Epsilon eps1::es)))
| prod (E.Delta e1::es) = (E.Apply(E.Partial d, E.Prod (E.Delta e1::es)))
| prod (E.Prod e1::es)=prod(e1@es)
| prod(e1::e2)=(let
val l= prod(e2)
val (_, a)= mkProd[e1,l]
val lr=e2 @[E.Apply(E.Partial d,e1)]
val(_,b) =mkProd lr
end)
val chainrule=prod es
in (1,E.Sum(c, chainrule)) end)
|_=>(0,E.Apply(E.Partial d,E.Sum(c,e)))
(* end case*))

fun mkApply2(E.Apply(E.Partial d,e))=(print "aa";case e
of E.Tensor(a,[])=>(1,E.Const 0.0)
| E.Const _ =>(1,E.Const 0.0)
| E.Add l => (1,E.Add(List.map (fn e => E.Apply(E.Partial d, e)) l))
| E.Sub(e2, e3) =>(1, E.Sub(E.Apply(E.Partial d, e2), E.Apply(E.Partial d, e3)))
| E.Apply(E.Partial e1,e2)=>(1,E.Apply(E.Partial(d@e1), e2))
| E.Prod [e1]=>(1,E.Apply(E.Partial d,e1))
| E.Prod(E.Tensor(a,[])::e1::[])=>(1,E.Prod[E.Tensor(a,[]),E.Apply(E.Partial d,e1)])
| E.Prod(E.Tensor(a,[])::e2)=>(1,E.Prod[E.Tensor(a,[]),E.Apply(E.Partial d,E.Prod e2)])
| E.Prod es=> (let
fun prod [e1] =(0,E.Apply(E.Partial d,e1))
| prod (E.Epsilon eps1::es) = (0,E.Apply(E.Partial d, E.Prod (E.Epsilon eps1::es)))
| prod (E.Delta e1::es) = (0,E.Apply(E.Partial d, E.Prod (E.Delta e1::es)))
| prod (E.Prod e1::es)=prod(e1@es)
| prod(E.Tensor t::e2)=(let
val (_,l)= prod(e2) val m= E.Prod[E.Tensor t,l]
val lr=e2 @[E.Apply(E.Partial d,E.Tensor t)] val(b,a) =mkProd lr
end)
| prod(E.Field f::e2)=(let
val (_,l)= prod(e2) val m= E.Prod[E.Field f,l]
val lr=e2 @[E.Apply(E.Partial d,E.Field f)] val(b,a) =mkProd lr
end)
| prod e = (0,E.Apply(E.Partial d, E.Prod e))

val (a,b)= prod es

in (a, b) end)
|_=>(0,E.Apply(E.Partial d,e))
(* end case*))

fun mkSumApply2(E.Sum(c,E.Apply(E.Partial d,e)))=(print "in here ";case e
of E.Const _=>(1,E.Const 0.0)
| E.Tensor(_,[])=> (1,E.Const 0.0)
| E.Field _=>(0,E.Sum(c,E.Apply(E.Partial d,e)))
| E.Apply(E.Partial e1,e2)=>(1,E.Sum(c,E.Apply(E.Partial(d@e1),e2)))

| E.Add l => (1,E.Add(List.map (fn e => E.Sum(c,E.Apply(E.Partial d, e))) l))
| E.Sub(e2, e3) =>
(*(0,E.Sub(e2,e3))
*)
(print "sub";(1, E.Sub(E.Sum(c,E.Apply(E.Partial d, e2)), E.Sum(c,E.Apply(E.Partial d, e3)))))

| E.Prod [e1]=>(print "one";(1,E.Sum(c,E.Apply(E.Partial d,e1))))

| E.Prod(E.Tensor(a,[])::e2::[])=>("in scalar";(1, E.Prod[E.Tensor(a,[]),E.Sum(c,E.Apply(E.Partial d,e2))]))

| E.Prod(E.Tensor(a,[])::e2)=>("in scalar";(1, E.Prod[E.Tensor(a,[]),E.Sum(c,E.Apply(E.Partial d,E.Prod e2))]))

| E.Prod es =>(print "in prod";let
fun prod (change,rest, sum,partial,[]) = (change,E.Sum(sum,E.Apply(E.Partial partial,E.Prod rest)))
| prod (change,rest, sum,partial,E.Epsilon(i,j,k)::ps)= let
fun matchprod(2,_,_,_)= 1 (*matched 2*)
| matchprod(num,_,_,[])=0
| matchprod(0,_,_,[eps])=0
| matchprod(num,[],rest,eps::epsx)=
matchprod(num,rest,[],epsx)
| matchprod(num,E.V p::px,rest,eps::epsx)=
if(p=eps) then (matchprod(num+1,rest@px,[],epsx))
else matchprod(num,px,rest@[E.V p], eps::epsx)
| matchprod(num,p::px,rest,eps)=
matchprod(num,px,rest,eps)

val change'= matchprod(0,d,[],[i,j,k])
in (case change'
of 1 => (1,E.Const 0.0)
| _ =>prod(change,rest@[E.Epsilon(i,j,k)],sum,partial,ps)
(*end case*))
end
| prod (change,rest, sum,partial,E.Delta(i,j)::ps)=let
fun applyDelPartial([],_)=(0,[])
| applyDelPartial(p::px,r)=
if(j=p) then (1,r@[i]@px)
else  applyDelPartial(px,r@[p])

val (change',px)=applyDelPartial(d,[])

in (case change'
of 1 => (let val index=rmIndex([j],[],sum)
in prod(1,rest, index,px, ps) end )
| _ => prod(change,rest@[E.Delta(i,j)], sum,partial, ps)
(*end case*)) end
102
103          | prod (change,rest,sum, partial,e::es)= prod(change,rest@[e],sum,partial,es)  (*rewritten probe*)
104    fun mkprobe(e1,x)=(case e1
105        of E.Lift e   => (1,e)
106        | E.Prod []   => err("Probe of empty product")
107        | E.Prod p    => (1,E.Prod (List.map (fn(a)=>E.Probe(a,x)) p))
108        | E.Apply _   => (0,E.Probe(e1,x))
109        | E.Conv _    => (0,E.Probe(e1,x))
110        | E.Field _   => (0,E.Probe(e1,x))
111        | E.Sum(c,e') => (1,E.Sum(c,E.Probe(e',x)))
113        | E.Sub (a,b) => (1,E.Sub(E.Probe(a,x),E.Probe(b,x)))
114        | E.Neg e'    => (1,E.Neg(E.Probe(e',x)))
115        | E.Div (a,b) => (1,E.Div(E.Probe(a,x),E.Probe(b,x)))
116        | E.Const _   => err("Const without Lift")
117        | E.Tensor _  => err("Tensor without Lift")
118        | E.Delta _   => err("Probe of Delta")
119        | E.Epsilon _ => err("Probe of Eps")
120        | E.Partial _ => err("Probe Partial")
121        | E.Probe _   => err("Probe of a Probe")
122        | E.Krn _     => err("Krn used before expand")
123        | E.Value _   => err("Value used before expand")
124        | E.Img _     => err("Probe used before expand")
125    (*end case*))
126
val (change,exp) = prod(0,[],c, d, es)
127
in
(change,exp)
end)
| _=>(print "nope";(0,E.Sum(c,E.Apply(E.Partial d,e))))
(* end case*))
128
(*
E.Sum(c,Apply(d,e))
try E.Sum(c,e)=> E.Sum(c',e')
==>    E.Sum(c',E.Apply(d,e'))
E.Apply(d,e')=> E.Apply(d',e'')
==>E.Sum(c',E.Apply(d',e'')
*)
129
130  (*Apply normalize to each term in product list  (*Apply normalize to each term in product list
131  or Apply normalize to tail of each list*)  or Apply normalize to tail of each list*)
132  fun normalize (Ein.EIN{params, index, body}) = let  fun normalize (Ein.EIN{params, index, body}) = let

133        val changed = ref false        val changed = ref false
134
135        fun rewriteBody body = (case body        fun rewriteBody body = (case body
136               of E.Const _=> body               of E.Const _=> body
137                | E.Tensor _ =>body                | E.Tensor _ =>body
138                | E.Field _=> body                | E.Field _=> body
| E.Kernel _ =>body
139                | E.Delta _ => body                | E.Delta _ => body
| E.Value _ =>body
140                | E.Epsilon _=>body                | E.Epsilon _=>body
141                | E.Conv _      => body
142                | E.Partial _   => body
143                | E.Krn _       => raise Fail"Krn before Expand"
144                | E.Img _       => raise Fail"Img before Expand"
145                | E.Value _     => raise Fail"Value before Expand"
146
147                    (*************Algebraic Rewrites **************)
148                | E.Neg(E.Neg e)    => rewriteBody e
149                | E.Neg e => E.Neg(rewriteBody e)                | E.Neg e => E.Neg(rewriteBody e)
150                | E.Add es => let val (change,body')= mkAdd(List.map rewriteBody es)              | E.Lift e          => E.Lift(rewriteBody e)
152                     in if (change=1) then ( changed:=true;body') else body' end                     in if (change=1) then ( changed:=true;body') else body' end
153                | E.Sub(a, E.Field f)=> (changed:=true;E.Add[a, E.Neg(E.Field(f))])
155                | E.Sub(E.Sub(a,b),e2)          => rewriteBody (E.Sub(a,E.Add[b,e2]))
157                | E.Sub (a,b)=>  E.Sub(rewriteBody a, rewriteBody b)                | E.Sub (a,b)=>  E.Sub(rewriteBody a, rewriteBody b)
158                | E.Div(E.Div(a,b),E.Div(c,d))  => rewriteBody(E.Div(E.Prod[a,d],E.Prod[b,c]))
159                | E.Div(E.Div(a,b),c)           => rewriteBody (E.Div(a, E.Prod[b,c]))
160                | E.Div(a,E.Div(b,c))           => rewriteBody (E.Div(E.Prod[a,c],b))
161                | E.Div (a, b) => E.Div(rewriteBody a, rewriteBody b)                | E.Div (a, b) => E.Div(rewriteBody a, rewriteBody b)
| E.Partial _=>body
| E.Conv (V, alpha)=> E.Conv(rewriteBody V, alpha)
| E.Probe(u,v)=>  E.Probe(rewriteBody u, rewriteBody v)
| E.Image es => E.Image(List.map rewriteBody es)
162
163                  (*Product*)                  (**************Apply, Sum, Probe**************)
164                | E.Apply(E.Partial [],e)   => e
165                | E.Apply(E.Partial d1, e1) =>
166                    let
167                    val e2 = rewriteBody e1
168                    val (c,e3)=mkapply(E.Partial d1,e2)
169                    in (case c of 1=>(changed:=true;e3)| _ =>e3 (*end case*))
170                    end
171                | E.Apply _                 => raise Fail" Not well-formed Apply expression"
172                | E.Sum([],e)               => (changed:=true;rewriteBody e)
173                | E.Sum(c,e)                => let
174                    val (c,e')=mkSum(c,rewriteBody e)
175                    in (case c of 0 => e'|_ => (changed:=true;e'))
176                    end
177                | E.Probe(u,v)              =>
178                    let
179                    val (c',b')=mkprobe(rewriteBody u,rewriteBody v)
180                    in (case c'
181                        of 1=> (changed:=true;b')
182                        |_=> b'
183                        (*end case*))
184                    end
185                    (*************Product**************)
186                  | E.Prod [] => raise Fail"missing elements in product"
187                | E.Prod [e1] => rewriteBody e1                | E.Prod [e1] => rewriteBody e1
189                       (changed := true; E.Add(List.map (fn e=> E.Prod([e]@e3)) e2))
190                  | E.Prod((E.Sub(e2,e3))::e4)=>
191                    (changed :=true; E.Sub(E.Prod([e2]@e4), E.Prod([e3]@e4 )))
192                  | E.Prod((E.Div(e2,e3))::e4)=> (changed :=true; E.Div(E.Prod([e2]@e4), e3 ))
194                     (changed := true; E.Add(List.map (fn e=> E.Prod([e1, e]@e3)) e2))                     (changed := true; E.Add(List.map (fn e=> E.Prod([e1, e]@e3)) e2))
195                | E.Prod(e1::(E.Sub(e2,e3))::e4)=>                | E.Prod(e1::E.Sub(e2,e3)::e4)=>
196                     (changed :=true; E.Sub(E.Prod([e1, e2]@e4), E.Prod([e1,e3]@e4 )))                     (changed :=true; E.Sub(E.Prod([e1, e2]@e4), E.Prod([e1,e3]@e4 )))
| E.Prod [E.Partial r1,E.Conv(f,deltas)]=>
(changed:=true;E.Conv(f,deltas@r1))
| E.Prod (E.Partial r1::E.Conv(f,deltas)::ps)=>
(changed:=true;
let val (change,e)=mkProd([E.Conv(f,deltas@r1)]@ps)
in e end)
| E.Prod[(E.Epsilon(e1,e2,e3)), E.Tensor(_,[E.V i1,E.V i2])]=>
if(e2=i1 andalso e3=i2) then (changed :=true;E.Const(0.0))
else body
| E.Prod [E.Partial r1, E.Tensor(_,[])]=> (changed:=true;E.Const(0.0))
| E.Prod(E.Partial r1::E.Partial r2::p)=>
(changed:=true;E.Prod([E.Partial(r1@r2)]@p))
| E.Prod [E.Partial _, _] =>body

| E.Prod (E.Partial p1::es)=> (let
fun prod [e1] =E.Apply(E.Partial p1,e1)
| prod(e1::e2)=(let
val l= prod(e2) val m= E.Prod[e1,l]
val lr=e2 @[E.Apply(E.Partial p1,e1)] val(b,a) =mkProd lr
end)
in (changed:=true;prod es) end)
197
| E.Prod(e::es)=>let
val e'=rewriteBody e
val e2=rewriteBody(E.Prod es)
val(a,b)=(case e2 of E.Prod p'=> mkProd([e']@p')
|_=>mkProd [e',e2])
in b
end
198
199                (*Apply*)                  (*************Product EPS **************)

| E.Apply(E.Partial d,E.Sum(c,e))=>let
val(c,e')=mkApplySum(E.Apply(E.Partial d,E.Sum(c, rewriteBody e)))
val e''=(case e'
of E.Apply(d,E.Sum s)=>E.Apply(d,rewriteBody(E.Sum s))
|_=> e')
in (print "bb";case c of 1=>(changed:=true;e'')
|_=> e'')end
| E.Apply(E.Partial [],e)=> e
200
201                | E.Apply(E.Partial p, e)=>let                | E.Prod(E.Epsilon(i,j,k)::E.Apply(E.Partial d,e)::es)=>let
202                      val body'=E.Apply(E.Partial p, rewriteBody e)                   val change= G.matchEps(0,d,[],[i,j,k])
203                      val (c, e')=mkApply2(body')                   in case (change,es)
204                  in (case c of 1=>(changed:=true;e')                      of (1,_) =>(changed:=true; E.Const 0)
205                      | _ =>e') end                      | (_,[]) =>E.Prod[E.Epsilon(i,j,k),rewriteBody (E.Apply(E.Partial d,e))]
206                | E.Apply(e1,e2)=>E.Apply(rewriteBody e1, rewriteBody e2)                      |(_,_)=> let
207                            val a=rewriteBody(E.Prod([E.Apply(E.Partial d,e)]@ es))
208                            val (_,b)=F.mkProd [E.Epsilon(i,j,k),a]
209                            in b end
210                (* Sum *)                  end
211                | E.Sum([],e)=> (changed:=true;rewriteBody e)                | E.Prod(E.Epsilon(i,j,k)::E.Conv(V,alpha, h, d)::es)=>let
212                | E.Sum(_,E.Const c)=>(changed:=true;E.Const c)                      val change= G.matchEps(0,d,[],[i,j,k])
213                | E.Sum(c,(E.Add l))=> (changed:=true;E.Add(List.map (fn e => E.Sum(c,e)) l))                      in case (change,es)
214                | E.Sum(c,E.Sub(e1,e2))=>(changed:=true; E.Sub(E.Sum(c,e1),E.Sum(c,e2)))                          of (1,_) =>(changed:=true; E.Const 0)
215                | E.Sum(c,E.Prod(E.Epsilon eps1::E.Epsilon eps2::ps))=>                          | (_,[]) =>E.Prod[E.Epsilon(i,j,k),E.Conv(V,alpha, h, d)]
216                     let val (i,e,rest)=epsToDels(body)                          | (_,_) =>let
217                  in (print "eps to dels \n ";case (i, e,rest)                              val a=rewriteBody(E.Prod([E.Conv(V,alpha, h, d)]@ es))
218                  of (1,[e1],r) =>(print "changed\n";changed:=true;e1)                              val (_,b) = F.mkProd [E.Epsilon(i,j,k),a]
219                  |(0,eps,[])=>(print "non";body)                              in b end
|(0,eps,rest)=>(let
val p'=rewriteBody(E.Prod rest)
val p''= (case p' of E.Prod p=>p |e=>[e])
val(a,b)= mkProd (eps@p'')
in E.Sum(c,b) end
)
|_=>body)
220                     end                     end
| E.Sum(c, E.Prod(E.Delta d::es))=>let
val (change,a)=reduceDelta(body)
val (change',body')=(case a
of E.Prod p=> mkProd p
|_=> (0,a))
in (case change of []=>body'|_=>(changed:=true;body')) end

| E.Sum(c,E.Apply(E.Partial _,e))=>let
val (change,exp)=mkSumApply2(body)
val exp'=(case exp
of  E.Const c => E.Const c
| E.Sum(c',E.Apply(d',e'))  => (let
val s'=rewriteBody(E.Sum(c',e'))
in (case s'
of E.Sum([],e'')=> rewriteBody (E.Apply(d',e''))
| E.Sum(s'',e'') => E.Sum(s'',rewriteBody(E.Apply(d',e'')))
| _ => E.Apply(d',s'))

end)
221
222                  | E.Prod[(E.Epsilon(e1,e2,e3)), E.Tensor(_,[E.V i1,E.V i2])]=>
223                        if(e2=i1 andalso e3=i2) then (changed :=true;E.Const(0))
224                        else body
225
226                          | _ =>exp              | E.Prod(E.Epsilon eps1::ps)=> (case (G.epsToDels(E.Epsilon eps1::ps))
227                    of (1,e,[],_,_)      =>(changed:=true;e)(* Changed to Deltas *)
228                    | (1,e,sx,_,_)      =>(changed:=true;E.Sum(sx,e))(* Changed to Deltas *)
229                    | (_,_,_,_,[])   =>  body
230                    | (_,_,_,epsAll,rest) => let
231                            val p'=rewriteBody(E.Prod rest)
232                            val(_,b)= F.mkProd(epsAll@[p'])
233                            in b end
234                          (* end case *))                          (* end case *))
235
236                  in (case change of 1=>(changed:=true;exp') |_=>exp')              | E.Prod(E.Sum(c1,E.Prod(E.Epsilon e1::es1))::E.Sum(c2,E.Prod(E.Epsilon e2::es2))::es) =>
237                    (case G.epsToDels([E.Epsilon e1, E.Epsilon e2]@es1@es2@es)
238                    of (1,e,sx,_,_)=> (changed:=true; E.Sum(c1@c2@sx,e))
239                    | (_,_,_,_,_)=>let
240                        val eA=rewriteBody(E.Sum(c1,E.Prod(E.Epsilon e1::es1)))
241                        val eB=rewriteBody(E.Prod(E.Sum(c2,E.Prod(E.Epsilon e2::es2))::es))
242                        val (_,e)=F.mkProd([eA,eB])
243                        in e
244                  end                  end
245                    (*end case*))
246
247                | E.Prod(E.Delta d::es)=>let
248                    val (pre',eps, dels,post)= F.filterGreek(E.Delta d::es)
249                    val (change,a)=G.reduceDelta(eps, dels, post)
250                    in (case (change,a)
251                        of (0, _)=> E.Prod [E.Delta d,rewriteBody(E.Prod es)]
252                        | (_, E.Prod p)=>let
253                            val (_, p') = F.mkProd p
254                            in (changed:=true;p') end
255                        | _ => (changed:=true;a )
256                        (*end case*))
257                        end
258
259                | E.Sum(c,e)=>E.Sum(c,rewriteBody e)                | E.Prod[e1,e2]=> let val (_,b)=F.mkProd[rewriteBody e1, rewriteBody e2] in b end
260                  | E.Prod(e::es)=>let
261                        val e'=rewriteBody e
262                        val e2=rewriteBody(E.Prod es)
263                        val(_,b)=(case e2
264                            of E.Prod p'=> F.mkProd([e']@p')
265                            |_=>F.mkProd [e',e2])
266                    in b
267                       end
268
269              (*end case*))              (*end case*))
270
271        fun loop body = let              fun loop(body ,count) = let
272              val body' = rewriteBody body              val body' = rewriteBody body
273
274              in              in
275                if !changed                if !changed
276                     then (changed := false; loop body')                  then let
277                  else body'                      val _= (case testing
278                            of 1=> (print(String.concat["\nN =>",Int.toString(count),"--",P.printbody(body')]);1)
279                            | _=> 1)
280                        in
281                            (changed := false ;loop(body',count+1))
282              end              end
283      val b = loop body                  else (body',count)
284                end
285
286        val (b,count) = loop(body,0)
287        val _ =(case testing
288            of 1 => (print(String.concat["\n out of normalize \n",P.printbody(b),"\n    Final CounterXX:",Int.toString(count),"\n\n"]);1)
289            | _=> 1
290            (*end case*))
291      in      in
292      ((Ein.EIN{params=params, index=index, body=b}))                  (Ein.EIN{params=params, index=index, body=b},count)
293      end      end
294  end  end
295
296
297
298  end (* local *)  end (* local *)

Legend:
 Removed from v.2460 changed lines Added in v.2795