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

# SCM Repository

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

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

revision 2450, Thu Oct 3 20:17:08 2013 UTC revision 2838, Tue Nov 25 03:40:24 2014 UTC
# Line 4  Line 4
4      local      local
5
6      structure E = Ein      structure E = Ein
7    (* structure P=Printer*)      structure P=Printer
8        structure F=Filter
9        structure G=EpsHelpers
10
11      in      in
12
13    fun err str=raise Fail (String.concat["Ill-formed EIN Operator",str])
14    val testing=1
15
16  (*Flattens Add constructor: change, expression *)  fun flatProd [e]=e
17  fun mkAdd [e]=(1,e)  | flatProd e=E.Prod e
| mkAdd(e)=let
fun flatten((i, (E.Add l)::l'))= flatten(1,l@l')
|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)
| es => (b,E.Add es)
(* 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 c<0.0) then
if (c>1.0 orelse c<1.0) then let
val(b,a)=flatten(i,l') in (b,[E.Const c]@a) end
else flatten(1,l')
else (3, [E.Const(0.0)])
| 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 if(b=3) then (1,E.Const(0.0))
else case a
of [] => (1,E.Const(0.0))
| [e] => (1,e)
| es => (b, E.Prod es)
(* end case *)
end

18
fun mkEps(e)= (case e
of E.Apply(E.Partial [E.V a], E.Prod( e2::m ))=> (0,e)
| E.Apply(E.Partial [E.V a,E.V b], E.Prod( (E.Epsilon(i,j,k))::m ))=>
(if(a=i andalso b=j) then (1,E.Const(0.0))
else if(a=i andalso b=k) then (1,E.Const(0.0))
else if(a=j andalso b=i) then (1,E.Const(0.0))
else if(a=j andalso b=k) then (1,E.Const(0.0))
else if(a=k andalso b=j) then (1,E.Const(0.0))
else if(a=k andalso b=i) then (1,E.Const(0.0))
else (0,e))
|_=> (0,e)
(*end case*))
19
20  fun mkApply(E.Apply(d, e)) = (case e  fun prodAppPartial(es,p1)=(case es
21      of E.Tensor(a,[])=> (0,E.Const(0.0))      of []      => raise Fail "Empty App Partial"
22       | E.Tensor _=> (0,E.Apply(d,e))      | [e1]     => E.Apply(E.Partial p1,e1)
23       | E.Const _=> (1,E.Const(0.0))      | (e1::e2) => let
24       | E.Add l => (1,E.Add(List.map (fn e => E.Apply(d, e)) l))          val l= prodAppPartial(e2,p1)
25       | E.Sub(e2, e3) =>(1, E.Sub(E.Apply(d, e2), E.Apply(d, e3)))          val (_,e2')= F.mkProd[e1,l]
26       | E.Prod((E.Epsilon c)::e2)=> mkEps(E.Apply(d,e))          val (_,e1')=F.mkProd(e2@ [E.Apply(E.Partial p1, e1)])
27       | E.Prod[E.Tensor(a,[]), e2]=>  (0, E.Prod[ E.Tensor(a,[]), E.Apply(d, e2)]  )          in
28       | E.Prod((E.Tensor(a,[]))::e2)=>  (0, E.Prod[E.Tensor(a,[]), E.Apply(d, E.Prod e2)] )              E.Add[e1',e2']
29       | E.Prod es =>    (let          end
fun prod [e] = (E.Apply(d, e))
| prod(e1::e2)=(let val l= prod(e2) val m= E.Prod[e1,l]
val lr=e2 @[E.Apply(d,e1)]   val(b,a) =mkProd lr
in ( E.Add[ a, m] )
end)
| prod _= (E.Const(1.0))
in (1,prod es)
end)
| _=> (0,E.Apply(d,e))
30               (*end case*))               (*end case*))
31
32  fun mkSumApply(E.Sum(c,E.Apply(d, e))) = (case e  (*rewritten Sum*)
33      of E.Tensor(a,[])=> (0,E.Const(0.0))  fun mkSum(c1,e1)=(case e1
34      | E.Tensor _=> (0,E.Sum(c,E.Apply(d,e)))      of E.Conv _   => (0,E.Sum(c1,e1))
35      | E.Field _ =>(0, E.Sum(c, E.Apply(d,e)))      | E.Field _   => (0,E.Sum(c1,e1))
36      | E.Const _=> (1,E.Const(0.0))      | E.Probe _   => (0,E.Sum(c1,e1))
37      | E.Add l => (1,E.Add(List.map (fn e => E.Sum(c,E.Apply(d, e))) l))      | E.Apply _   => (0,E.Sum(c1,e1))
38      | E.Sub(e2, e3) =>(1, E.Sub(E.Sum(c,E.Apply(d, e2)), E.Sum(c,E.Apply(d, e3))))      | E.Delta _   => (0,E.Sum(c1,e1))
39      | E.Prod((E.Epsilon c)::e2)=> mkEps(E.Apply(d,e))      | E.Epsilon _ => (0,E.Sum(c1,e1))
40      | E.Prod[E.Tensor(a,[]), e2]=>  (0, E.Prod[ E.Tensor(a,[]), E.Sum(c,E.Apply(d, e2))]  )      (*| E.Tensor []  => (1,e1)*)
41      | E.Prod((E.Tensor(a,[]))::e2)=>  (0, E.Prod[E.Tensor(a,[]), E.Sum(c,E.Apply(d, E.Prod e2))] )      | E.Tensor _  => (0,E.Sum(c1,e1))
42      | E.Prod es =>   (let      | E.Neg e2    => (1,E.Neg(E.Sum(c1,e2)))
43          fun prod [e] = (E.Apply(d, e))      | E.Sub (a,b) => (1,E.Sub(E.Sum(c1,a),E.Sum(c1,b)))
44          | prod(e1::e2)=(let val l= prod(e2) val m= E.Prod[e1,l]      | E.Add e     => (1,E.Add (List.map (fn(a)=>E.Sum(c1,a)) e))
45              val lr=e2 @[E.Apply(d,e1)]   val(b,a) =mkProd lr      | E.Div (a,b) => (1,E.Div(E.Sum(c1,a),E.Sum(c1,b)))
46              in ( E.Add[ a, m] ) end)      | E.Lift e    => (1,E.Lift(E.Sum(c1,e)))
47          | prod _= (E.Const(1.0))      | E.Sum(c2,e2)=> (1,E.Sum(c1@c2,e2))
48              in (1, E.Sum(c,prod es))  end)      | E.Prod p     =>F.filterSca(c1,p)
49      | _=> (0,E.Sum(c,E.Apply(d,e)))      | E.Const _   => err("Sum of Const")
50        | E.Partial _ => err("Sum of Partial")
51        | E.Krn _     => err("Krn used before expand")
52        | E.Value _   => err("Value used before expand")
53        | E.Img _     => err("Probe used before expand")
54      (*end case*))      (*end case*))
55
56    (*rewritten Apply*)
57    fun mkapply(d1,e1)=(case e1
58  (* Identity: (Epsilon ijk Epsilon ilm) e => (Delta jl Delta km - Delta jm Delta kl) e      of E.Lift e   => (1,E.Const 0)
59      The epsToDels Function searches for Epsilons in the expression, checks for this identity in all adjacent Epsilons and if needed, does the transformation.      | E.Prod []   => err("Apply of empty product")
60       The Function returns two separate list, 1 is the remaining list of Epsilons that have not be changed to deltas, and the second is the Product of the remaining expression.      | E.Add []    => err("Apply of empty Addition")
61    Ex:(Epsilon_ijk Epsilon_ilm) Epsilon_stu e =>([Epsilon_stu], [Delta_jl,Delta_km,e -Delta_jm Delta_kl, e] )      | E.Conv(v, alpha, h, d2)    =>let
62     This is useful since we can normalize the second list without having to normalize the epsilons again.                          val E.Partial d3=d1
63          4(Eps Eps)                          in (1,E.Conv(v,alpha,h,d2@d3)) end
64         3( Delta_liDelta mj- Delta_mi Delta_lj)      | E.Field _   => (0,E.Apply(d1,e1))
65           Ai-      | E.Probe _   => (0,E.Apply(d1,e1)) (*FIX ME, Should be error actually apply of a tensor result*)
66          *)      | E.Apply(E.Partial d2,e2)  => let
67                            val E.Partial d3=d1
68                            in (1,E.Apply(E.Partial(d3@d2),e2)) end
69                     *)      | E.Apply _   => err" Apply of non-Partial expression"
70        | E.Sum(c2,e2)=> (1,E.Sum(c2,E.Apply(d1,e2)))
71        | E.Neg e2    => (1,E.Neg(E.Apply(d1,e2)))
72  fun epsToDels(E.Sum(count,E.Prod e))= let      | E.Add e     => (1,E.Add (List.map (fn(a)=>E.Apply(d1,a)) e))
73      fun doubleEps((E.Epsilon (a,b,c))::(E.Epsilon(d,e,f))::es,eps,e3)=      | E.Sub (a,b) => (1,E.Sub(E.Apply(d1,a),E.Apply(d1,b)))
74          let      | E.Div (g,b) => 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*)

fun rmIndex(_,_,[])=[]
| rmIndex([],[],cs)=cs
| rmIndex([],m ,e1::cs)=[e1]@rmIndex(m,[],cs)
| rmIndex(i::ix,rest ,(E.V c)::cs)=
if(i=c) then rmIndex(rest@ix,[],cs)
else rmIndex(ix,rest@[i],(E.V c)::cs)

val s'= rmIndex([i,s,t,u,v],[],count)
val s''=[E.V s, E.V t ,E.V u, E.V v]
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,s')
of ([],[]) =>(1,deltas)
|([],_)=>(1,E.Sum(s',deltas))
|(_,[])=>(1,E.Prod(eps@[deltas]))
|(_,_) =>(1, E.Sum(s', E.Prod(eps@[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

val (es,rest)=findeps([],e)

75      in      in
76          dist(es,[],rest)          (case F.filterField[b]
77      end          of (_,[]) => (1,E.Div(E.Apply(d1,g),b)) (*Division by a real*)
78            | (pre,h) => let
79  (*              val g'=E.Apply(d1,g)
80                val h'=E.Apply(d1,flatProd(h))
81                val num=E.Sub(E.Prod([g']@h),E.Prod[g,h'])
82                val denom=E.Prod(pre@h@h)
83                in (1,E.Div(num,denom))
(*The Deltas then need to be distributed over to the tensors in the expression e.
Ex.:Delta ij ,Tensor_j, e=> Tensor_i,e. The mkDelts function compares every Delta in the expression to the tensors in the expressions while keeping the results in the correct order.
This also returns a list of deltas and a list of the remaining expression.
*)

fun mkDel(e) = let
fun Del(i, [],x)= (i,[],x)
| Del(i, d,[])=(i, d,[])
| Del(i, (E.Delta(d1,d2))::d, (E.Tensor(id,[x]))::xs)=
if(x=d2) then (let
val(i',s,t)= Del(i+1,d, xs)
in Del(i',s, [E.Tensor(id, [d1])] @t) end)
else (let val (i',s,t)= Del(i,[E.Delta(d1,d2)],xs)
val(i2,s2,t2)= Del(i',d,[E.Tensor(id,[x])]@t)
in (i2,s@s2, t2) end )
| Del(i, (E.Delta(d1,d2))::d, (E.Field(id,[x]))::xs)=
if(x=d2) then (let
val(i',s,t)= Del(i+1,d, xs)
in Del(i',s, [E.Field(id, [d1])] @t) end)
else (let val (i',s,t)= Del(i,[E.Delta(d1,d2)],xs)
val(i2,s2,t2)= Del(i',d,[E.Field(id,[x])]@t)
in (i2,s@s2, t2) end )

| Del(i, d, t)= (i,d,t)
fun findels(e,[])= (e,[])
| findels(e,es)= let val del1= hd(es)
in (case del1
of E.Delta _=> findels(e@[del1],tl(es))
|_=> (e, es))
84              end              end
85      val(a,b)= findels([], e)          (*end case*))
in
Del(0, a, b)
86      end      end
87
88        | E.Prod p =>let
89            val (pre, post)= F.filterField p
90            val E.Partial d3=d1
91            in F.mkProd(pre@[prodAppPartial(post,d3)])
92            end
93        | E.Const _   => err("Const without Lift")
94        | E.Tensor _  => err("Tensor without Lift")
95        | E.Delta _   => err("Apply of Delta")
96        | E.Epsilon _ => err("Apply of Eps")
97        | E.Partial _ => err("Apply of Partial")
98        | E.Krn _     => err("Krn used before expand")
99        | E.Value _   => err("Value used before expand")
100        | E.Img _     => err("Probe used before expand")
101        (*end case*))
102
(*The Deltas are distributed over to the tensors in the expression e.
This function checks for instances of the dotProduct.
Sum_2 (Delta_ij (A_i B_j D_k))=>Sum_1(A_i B_i) D_k
*)
fun checkDot(E.Sum(s,E.Prod e))= let
fun dot(i,d,r, (E.Tensor(ida,[a]))::(E.Tensor(idb,[b]))::ts)=
if (a=b) then
dot(i-1,d@[E.Sum(1,E.Prod[(E.Tensor(ida,[a])), (E.Tensor(idb,[b]))])], [],r@ts)
else dot(i,d, r@[E.Tensor(idb,[b])],(E.Tensor(ida,[a]))::ts)
|dot(i, d,r, [t])=dot(i,d@[t], [], r)
|dot(i,d, [],[])= (i,d, [],[])
|dot(i,d, r, [])= dot(i,d, [], r)
|dot(i, d, r, (E.Prod p)::t)= dot (i, d, r, p@t)
|dot(i,d, r, e)= (i,d@r@e, [], [])

val(i,d,r,c)= dot(s,[],[], e)
val soln= (case d of [d1]=>d1
|_=> E.Prod d)
in E.Sum(i,soln) end
|checkDot(e)= (e)

*)

fun reduceDelta(E.Sum(c,E.Prod p))=let

fun findDeltas(dels,rest,E.Delta d::es)= findDeltas(dels@[E.Delta d], rest, es)
| findDeltas(dels,rest,E.Epsilon eps::es)=findDeltas(dels,rest@[E.Epsilon eps],es)
| findDeltas(dels,rest,es)=  (dels,rest,es)

fun rmIndex(_,_,[])=[]
| rmIndex([],[],cs)=cs
| rmIndex([],m ,e1::cs)=[e1]@rmIndex(m,[],cs)
| rmIndex(i::ix,rest ,c::cs)=
if(i=c) then rmIndex(rest@ix,[],cs)
else rmIndex(ix,rest@[i],c::cs)

fun distribute(change,d,dels,[],done)=(change,dels@d,done)
| distribute(change,[],[],e,done)=(change,[],done@e)
| distribute(change,E.Delta(i,j)::ds,dels,E.Tensor(id,[tx])::es,done)=
if(j=tx) then distribute(change@[j],dels@ds,[] ,es ,done@[E.Tensor(id,[i])])
else distribute(change,ds,dels@[E.Delta(i,j)],E.Tensor(id,[tx])::es,done)
| distribute(change,d,dels,e::es,done)=distribute(change,dels@d,[],es,done@[e])
103
104      val (dels,eps,es)=findDeltas([],[],p)  (*rewritten probe*)
105      val (change,dels',done)=distribute([],dels,[],es,[])  fun mkprobe(e1,x)=(case e1
106      val index=rmIndex(change,[],c)      of E.Lift e   => (1,e)
107        | E.Prod []   => err("Probe of empty product")
108        | E.Prod p    => (1,E.Prod (List.map (fn(a)=>E.Probe(a,x)) p))
109        | E.Apply _   => (0,E.Probe(e1,x))
110        | E.Conv _    => (0,E.Probe(e1,x))
111        | E.Field _   => (0,E.Probe(e1,x))
112        | E.Sum(c,e') => (1,E.Sum(c,E.Probe(e',x)))
113        | E.Add e     => (1,E.Add (List.map (fn(a)=>E.Probe(a,x)) e))
114        | E.Sub (a,b) => (1,E.Sub(E.Probe(a,x),E.Probe(b,x)))
115        | E.Neg e'    => (1,E.Neg(E.Probe(e',x)))
116        | E.Div (a,b) => (1,E.Div(E.Probe(a,x),E.Probe(b,x)))
117        | E.Const _   => err("Const without Lift")
118        | E.Tensor _  => err("Tensor without Lift")
119        | E.Delta _   => err("Probe of Delta")
120        | E.Epsilon _ => err("Probe of Eps")
121        | E.Partial _ => err("Probe Partial")
122        | E.Probe _   => err("Probe of a Probe")
123        | E.Krn _     => err("Krn used before expand")
124        | E.Value _   => err("Value used before expand")
125        | E.Img _     => err("Probe used before expand")
126    (*end case*))
127
in
(change, E.Sum(index,E.Prod (eps@dels'@done)))
end
128
129
130
131  (*Apply normalize to each term in product list  (*Apply normalize to each term in product list
132  or Apply normalize to tail of each list*)  or Apply normalize to tail of each list*)
133  fun normalize (Ein.EIN{params, index, body}) = let  fun normalize (ee as Ein.EIN{params, index, body}) = let
134        val changed = ref false        val changed = ref false
135
136        fun rewriteBody body = (case body        fun rewriteBody body = (case body
137               of E.Const _=> body               of E.Const _=> body
138                | E.Tensor _ =>body                | E.Tensor _ =>body
139                | E.Field _=> body                | E.Field _=> body
| E.Kernel _ =>body
140                | E.Delta _ => body                | E.Delta _ => body
| E.Value _ =>body
141                | E.Epsilon _=>body                | E.Epsilon _=>body
142                | E.Conv _      => body
143                | E.Partial _   => body
144                | E.Krn _       => raise Fail"Krn before Expand"
145                | E.Img _       => raise Fail"Img before Expand"
146                | E.Value _     => raise Fail"Value before Expand"
147
148                    (*************Algebraic Rewrites **************)
149                | E.Neg(E.Neg e)    => rewriteBody e
150                | E.Neg e => E.Neg(rewriteBody e)                | E.Neg e => E.Neg(rewriteBody e)
151                | E.Add es => let val (b,a)= mkAdd(List.map rewriteBody es)              | E.Lift e          => E.Lift(rewriteBody e)
152                     in if (b=1) then ( changed:=true;a) else a end              | E.Add es          => let val (change,body')= F.mkAdd(List.map rewriteBody es)
153                       in if (change=1) then ( changed:=true;body') else body' end
154                | E.Sub(a, E.Field f)=> (changed:=true;E.Add[a, E.Neg(E.Field(f))])
155                | E.Sub(E.Sub(a,b),E.Sub(c,d))  => rewriteBody(E.Sub(E.Add[a,d],E.Add[b,c]))
156                | E.Sub(E.Sub(a,b),e2)          => rewriteBody (E.Sub(a,E.Add[b,e2]))
157                | E.Sub(e1,E.Sub(c,d))          => rewriteBody(E.Add([E.Sub(e1,c),d]))
158                | E.Sub (a,b)=>  E.Sub(rewriteBody a, rewriteBody b)                | E.Sub (a,b)=>  E.Sub(rewriteBody a, rewriteBody b)
159                | E.Div(E.Div(a,b),E.Div(c,d))  => rewriteBody(E.Div(E.Prod[a,d],E.Prod[b,c]))
160                | E.Div(E.Div(a,b),c)           => rewriteBody (E.Div(a, E.Prod[b,c]))
161                | E.Div(a,E.Div(b,c))           => rewriteBody (E.Div(E.Prod[a,c],b))
162                | 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)
163
164                  (*Product*)                  (**************Apply, Sum, Probe**************)
165                | E.Apply(E.Partial [],e)   => e
166                | E.Apply(E.Partial d1, e1) =>
167                    let
168                    val e2 = rewriteBody e1
169                    val (c,e3)=mkapply(E.Partial d1,e2)
170                    in (case c of 1=>(changed:=true;e3)| _ =>e3 (*end case*))
171                    end
172                | E.Apply _                 => raise Fail" Not well-formed Apply expression"
173                | E.Sum([],e)               => (changed:=true;rewriteBody e)
174                | E.Sum(c,e)                => let
175                    val (c,e')=mkSum(c,rewriteBody e)
176                    in (case c of 0 => e'|_ => (changed:=true;e'))
177                    end
178                | E.Probe(u,v)              =>
179                    let
180                    val (c',b')=mkprobe(rewriteBody u,rewriteBody v)
181                    in (case c'
182                        of 1=> (changed:=true;b')
183                        |_=> b'
184                        (*end case*))
185                    end
186                    (*************Product**************)
187                  | E.Prod [] => raise Fail"missing elements in product"
188                | E.Prod [e1] => rewriteBody e1                | E.Prod [e1] => rewriteBody e1
189                | E.Prod(e1::(E.Add(e2))::e3)=>                | E.Prod((E.Add(e2))::e3)=>
190                       (changed := true; E.Add(List.map (fn e=> E.Prod([e]@e3)) e2))
191                  | E.Prod((E.Sub(e2,e3))::e4)=>
192                    (changed :=true; E.Sub(E.Prod([e2]@e4), E.Prod([e3]@e4 )))
193                  | E.Prod((E.Div(e2,e3))::e4)=> (changed :=true; E.Div(E.Prod([e2]@e4), e3 ))
194                  | E.Prod(e1::E.Add(e2)::e3)=>
195                     (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))
196                | E.Prod(e1::(E.Sub(e2,e3))::e4)=>                | E.Prod(e1::E.Sub(e2,e3)::e4)=>
197                     (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; E.Prod([E.Conv(f,deltas@r1)]@ps))
198
199
200                | E.Prod(e::es)=>let                  (*************Product EPS **************)
val e'=rewriteBody e
val e2=rewriteBody(E.Prod es)
in(case e2 of E.Prod p'=> E.Prod([e']@p')
|_=>E.Prod [e',e2])
end
201
202                (*Apply*)                | E.Prod(E.Epsilon(i,j,k)::E.Apply(E.Partial d,e)::es)=>let
203                | E.Apply(e1,e2)=>E.Apply(rewriteBody e1, rewriteBody e2)                   val change= G.matchEps(0,d,[],[i,j,k])
204                     in case (change,es)
205                        of (1,_) =>(changed:=true; E.Const 0)
206                        | (_,[]) =>E.Prod[E.Epsilon(i,j,k),rewriteBody (E.Apply(E.Partial d,e))]
207                        |(_,_)=> let
208                            val a=rewriteBody(E.Prod([E.Apply(E.Partial d,e)]@ es))
209                            val (_,b)=F.mkProd [E.Epsilon(i,j,k),a]
210                            in b end
211                    end
212                  | E.Prod(E.Epsilon(i,j,k)::E.Conv(V,alpha, h, d)::es)=>let
213                        val change= G.matchEps(0,d,[],[i,j,k])
214                        in case (change,es)
215                            of (1,_) =>(changed:=true; E.Const 0)
216                            | (_,[]) =>E.Prod[E.Epsilon(i,j,k),E.Conv(V,alpha, h, d)]
217                            | (_,_) =>let
218                                val a=rewriteBody(E.Prod([E.Conv(V,alpha, h, d)]@ es))
219                                val (_,b) = F.mkProd [E.Epsilon(i,j,k),a]
220                                in b end
221                        end
222
223                  | E.Prod[(E.Epsilon(e1,e2,e3)), E.Tensor(_,[E.V i1,E.V i2])]=>
224                        if(e2=i1 andalso e3=i2) then (changed :=true;E.Const(0))
225                        else body
226
227                | E.Prod(E.Epsilon eps1::ps)=> (case (G.epsToDels(E.Epsilon eps1::ps))
228                    of (1,e,[],_,_)      =>(changed:=true;e)(* Changed to Deltas *)
229                    | (1,e,sx,_,_)      =>(changed:=true;E.Sum(sx,e))(* Changed to Deltas *)
230                    | (_,_,_,_,[])   =>  body
231                    | (_,_,_,epsAll,rest) => let
232                            val p'=rewriteBody(E.Prod rest)
233                            val(_,b)= F.mkProd(epsAll@[p'])
234                            in b end
235                    (*end case*))
236
237                | E.Prod(E.Sum(c1,E.Prod(E.Epsilon e1::es1))::E.Sum(c2,E.Prod(E.Epsilon e2::es2))::es) =>
238                    (case G.epsToDels([E.Epsilon e1, E.Epsilon e2]@es1@es2@es)
239                    of (1,e,sx,_,_)=> (changed:=true; E.Sum(c1@c2@sx,e))
240                    | (_,_,_,_,_)=>let
241                        val eA=rewriteBody(E.Sum(c1,E.Prod(E.Epsilon e1::es1)))
242                        val eB=rewriteBody(E.Prod(E.Sum(c2,E.Prod(E.Epsilon e2::es2))::es))
243                        val (_,e)=F.mkProd([eA,eB])
244                        in e
245                        end
246                    (*end case*))
247
248                | E.Prod[E.Delta d, E.Neg e]=> (changed:=true;E.Neg(E.Prod[E.Delta d, e]))
249                | E.Prod(E.Delta d::es)=>let
250                    val (pre',eps, dels,post)= F.filterGreek(E.Delta d::es)
251                    val (change,a)=G.reduceDelta(eps, dels, post)
252                    in (case (change,a)
253                        of (0, _)=> E.Prod [E.Delta d,rewriteBody(E.Prod es)]
254                        | (_, E.Prod p)=>let
255                            val (_, p') = F.mkProd p
256                            in (changed:=true;p') end
257                        | _ => (changed:=true;a )
258                        (*end case*))
259                        end
260
261                (* Sum *)                | E.Prod[e1,e2]=> let val (_,b)=F.mkProd[rewriteBody e1, rewriteBody e2] in b end
262                | E.Sum([],e)=> rewriteBody e                | E.Prod(e::es)=>let
263                     | E.Sum(_,E.Const c)=>(changed:=true;E.Const c)                      val e'=rewriteBody e
264                | E.Sum(c,(E.Add l))=> (changed:=true;E.Add(List.map (fn e => E.Sum(c,e)) l))                      val e2=rewriteBody(E.Prod es)
265                | E.Sum(c,E.Prod(E.Epsilon eps1::E.Epsilon eps2::ps))=>                      val(_,b)=(case e2
266                     let val (i,e,rest)=epsToDels(body)                          of E.Prod p'=> F.mkProd([e']@p')
267                     in (case (i, e,rest)                          |_=>F.mkProd [e',e2])
268                     of (1,[e1],_) =>(changed:=true;e1)                  in b
269                          |(0,eps,[])=>body                     end
|(0,eps,rest)=>(let
val p'=rewriteBody(E.Prod rest)
val p''= (case p' of E.Prod p=>p |e=>[e])
in E.Sum(c, E.Prod (eps@p'')) end
)
|_=>body
) end
| E.Sum(c, E.Prod(E.Delta d::es))=>let
val (change,body')=reduceDelta(body)
in (case change of []=>body'|_=>(changed:=true;body')) end
| E.Sum(c,e)=>E.Sum(c,rewriteBody e)
270
271              (*end case*))              (*end case*))
272
273        fun loop body = let              fun loop(body ,count) = let
274                    val _ =print(String.concat["\n\n N =>",Int.toString(count),"--",P.printbody(body)])
275              val body' = rewriteBody body              val body' = rewriteBody body
276
277              in              in
278                if !changed                if !changed
279                     then (changed := false; (*print " \n \t => \n \t ";print( P.printbody body');print "\n";*)loop body')                  then  (changed := false ;loop(body',count+1))
280                  else body'                  else (body',count)
281              end              end
282      val b = loop body      val _ =print(String.concat["\n ******************* \n Start Normalize \n\n "])
283        val (b,count) = loop(body,0)
284        val _ =print(String.concat["\n Out of normalize \n",P.printbody(b),"\n    Final CounterXX:",Int.toString(count),"\n\n"])
285      in      in
286      ((Ein.EIN{params=params, index=index, body=b}))                  (Ein.EIN{params=params, index=index, body=b},count)
287      end      end
288  end  end
289
290
291
292  end (* local *)  end (* local *)

Legend:
 Removed from v.2450 changed lines Added in v.2838

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