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 2414, Mon Aug 19 05:02:14 2013 UTC revision 2795, Tue Nov 4 21:58:11 2014 UTC
# Line 2  Line 2
2  structure NormalizeEin = struct  structure NormalizeEin = struct
3
4      local      local
structure G = GenericEin
structure E = Ein
structure S = Specialize
structure R = Rewrite

5
6        structure E = Ein
7        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    fun flatProd [e]=e
17  (*  | flatProd e=E.Prod e
If changed is true then I know the expression will run through the funciton again.
However, if not, then I want to make sure that every expression in the Product is examined, and not just individually but as a group.
Prod[t1,t2,(t3+t4)] indivually=> same
Prod[t1] @ Prod[t2,(t3+t4)]=> Notice rule here
Prod[t1] @ Add(Prod (t2, t3), Prod (t2, t4))

*)

(*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 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

fun mkEps(e)= (case e
of E.Apply(E.Partial [a], E.Prod( e2::m ))=> (0,e)
| E.Apply(E.Partial [a,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*))

fun mkApply(E.Apply(d, e)) = (case e
of E.Tensor(a,[])=> (0,E.Const(0.0))
| E.Tensor _=> (0,E.Apply(d,e))
| E.Const _=> (1,E.Const(0.0))
| E.Sub(e2, e3) =>(1, E.Sub(E.Apply(d, e2), E.Apply(d, e3)))
| E.Prod((E.Epsilon c)::e2)=> mkEps(E.Apply(d,e))
| E.Prod[E.Tensor(a,[]), e2]=>  (0, E.Prod[ E.Tensor(a,[]), E.Apply(d, e2)]  )
| E.Prod((E.Tensor(a,[]))::e2)=>  (0, E.Prod[E.Tensor(a,[]), E.Apply(d, E.Prod e2)] )
| E.Prod es =>    (let
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))
(*end case*))

fun mkSumApply(E.Sum(c,E.Apply(d, e))) = (case e
of E.Tensor(a,[])=> (0,E.Const(0.0))
| E.Tensor _=> (0,E.Sum(c,E.Apply(d,e)))
| E.Field _ =>(0, E.Sum(c, E.Apply(d,e)))
| E.Const _=> (1,E.Const(0.0))
| E.Sub(e2, e3) =>(1, E.Sub(E.Sum(c,E.Apply(d, e2)), E.Sum(c,E.Apply(d, e3))))
| E.Prod((E.Epsilon c)::e2)=> mkEps(E.Apply(d,e))
| E.Prod[E.Tensor(a,[]), e2]=>  (0, E.Prod[ E.Tensor(a,[]), E.Sum(c,E.Apply(d, e2))]  )
| E.Prod((E.Tensor(a,[]))::e2)=>  (0, E.Prod[E.Tensor(a,[]), E.Sum(c,E.Apply(d, E.Prod e2))] )
| E.Prod es =>   (let
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, E.Sum(c,prod es))  end)
| _=> (0,E.Sum(c,E.Apply(d,e)))
(*end case*))

(* Identity: (Epsilon ijk Epsilon ilm) e => (Delta jl Delta km - Delta jm Delta kl) e
The epsToDels Function searches for Epsilons in the expression, checks for this identity in all adjacent Epsilons and if needed, does the transformation.
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.
Ex:(Epsilon_ijk Epsilon_ilm) Epsilon_stu e =>([Epsilon_stu], [Delta_jl,Delta_km,e -Delta_jm Delta_kl, e] )
This is useful since we can normalize the second list without having to normalize the epsilons again.*)

fun epsToDels(E.Sum(count,E.Prod e))= let
fun doubleEps((E.Epsilon (a,b,c))::(E.Epsilon(d,e,f))::es,e3)=
let
fun createDeltas(s,t,u,v, e3)=
(1,  E.Sub(E.Sum(2,E.Prod([E.Delta(s,u), E.Delta(t,v)] @e3)),
E.Sum(2,E.Prod([E.Delta(s,v), E.Delta(t,u)]@e3))))
in if(a=d) then createDeltas(b,c,e,f, e3)
else if(a=e) then createDeltas(b,c,f,d, e3)
else if(a=f) then createDeltas(b,c,d,e, e3)
else if(b=d) then createDeltas(c,a,e,f, e3)
else if(b=e) then createDeltas(c,a,f,d,e3)
else if(b=f) then createDeltas(c,a,d,e,e3)
else if(c=d) then createDeltas(a,b,e,f,e3)
else if(c=e) then createDeltas(a,b,f,d,e3)
else if(c=f) then createDeltas(a,b,d,e,e3)
else (0,(E.Prod((E.Epsilon (a,b,c))::(E.Epsilon(d,e,f))::e3)))
end
fun findeps(e,[])= (e,[])
| findeps(e,(E.Epsilon eps)::es)=  findeps(e@[E.Epsilon eps],es)
| findeps(e,es)= (e, es)
fun distribute([], s)=(0, [],s)
| distribute([e1], s)=(0, [e1], s)
| distribute(e1::es, s)= let val(i, exp)=doubleEps(e1::es, s)
in if(i=1) then (1, tl(es), [exp])
else let val(a,b,c)= distribute(es, s)
in (a, [e1]@b, c) end
end
val (change, eps,rest)= distribute(findeps([], e))
in (change, eps,rest) end

18
19
20    fun prodAppPartial(es,p1)=(case es
21  (*The Deltas then need to be distributed over to the tensors in the expression e.      of []      => raise Fail "Empty App Partial"
22  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.      | [e1]     => E.Apply(E.Partial p1,e1)
23     This also returns a list of deltas and a list of the remaining expression.      | (e1::e2) => let
24    *)          val l= prodAppPartial(e2,p1)
25            val (_,e2')= F.mkProd[e1,l]
26  fun mkDel(e) = let          val (_,e1')=F.mkProd(e2@ [E.Apply(E.Partial p1, e1)])
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))
end
val(a,b)= findels([], e)
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  (*The Deltas are distributed over to the tensors in the expression e.  (*rewritten Apply*)
56   This function checks for instances of the dotProduct.  fun mkapply(d1,e1)=(case e1
57  Sum_2 (Delta_ij (A_i B_j D_k))=>Sum_1(A_i B_i) D_k      of E.Lift e   => (1,E.Const 0)
58  *)      | E.Prod []   => err("Apply of empty product")
59     fun checkDot(E.Sum(s,E.Prod e))= let      | E.Add []    => err("Apply of empty Addition")
60         fun dot(i,d,r, (E.Tensor(ida,[a]))::(E.Tensor(idb,[b]))::ts)=      | E.Conv(v, alpha, h, d2)    =>let
61                     if (a=b) then                          val E.Partial d3=d1
62                          dot(i-1,d@[E.Sum(1,E.Prod[(E.Tensor(ida,[a])), (E.Tensor(idb,[b]))])], [],r@ts)                          in (1,E.Conv(v,alpha,h,d2@d3)) end
63                     else dot(i,d, r@[E.Tensor(idb,[b])],(E.Tensor(ida,[a]))::ts)      | E.Field _   => (0,E.Apply(d1,e1))
64            |dot(i, d,r, [t])=dot(i,d@[t], [], r)      | E.Probe _   => (0,E.Apply(d1,e1)) (*FIX ME, Should be error actually apply of a tensor result*)
65            |dot(i,d, [],[])= (i,d, [],[])      | E.Apply(E.Partial d2,e2)  => let
66            |dot(i,d, r, [])= dot(i,d, [], r)                          val E.Partial d3=d1
67            |dot(i, d, r, (E.Prod p)::t)= dot (i, d, r, p@t)                          in (1,E.Apply(E.Partial(d3@d2),e2)) end
68            |dot(i,d, r, e)= (i,d@r@e, [], [])      | E.Apply _   => err" Apply of non-Partial expression"
69        | E.Sum(c2,e2)=> (1,E.Sum(c2,E.Apply(d1,e2)))
70          val(i,d,r,c)= dot(s,[],[], e)      | E.Neg e2    => (1,E.Neg(E.Apply(d1,e2)))
71          val soln= (case d of [d1]=>d1      | E.Add e     => (1,E.Add (List.map (fn(a)=>E.Apply(d1,a)) e))
72                     |_=> E.Prod d)      | E.Sub (a,b) => (1,E.Sub(E.Apply(d1,a),E.Apply(d1,b)))
73          in E.Sum(i,soln) end      | E.Div (g,b) => let
74        |checkDot(e)= (e)          in
75            (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
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
102
103    (*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
127
128
# Line 235  Line 131
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
# Line 243  Line 140
140                | E.Epsilon _=>body                | E.Epsilon _=>body
141                | E.Conv _=> body                | E.Conv _=> body
142                | E.Partial _=>body                | E.Partial _=>body
143                            | E.Add es => let val (b,a)= mkAdd(List.map rewriteBody es)              | E.Krn _       => raise Fail"Krn before Expand"
144                      in if (b=1) then ( changed:=true;a) else a end              | 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)
150                | E.Lift e          => E.Lift(rewriteBody e)
152                       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.Probe(u,v)=> (  E.Probe(rewriteBody u, v))
| E.Sum(0, e)=>e
| E.Sum(_, (E.Const c))=> E.Const c

| E.Sum(c,E.Prod((E.Delta d)::es))=>(
let val (i,dels, e)= mkDel((E.Delta d)::es)
val rest=(case e of [e1]=> rewriteBody e1
|_=> rewriteBody(E.Prod(e)))
val soln= (case rest of E.Prod r=> E.Sum(c-i, E.Prod(dels@r))
|_=>E.Sum(c-i, E.Prod(dels@[rest])))
val q= checkDot(soln)
in if (i=0) then q
else (changed :=true;q)
end )
162
163                | E.Sum(c,E.Prod((E.Epsilon e1 )::(E.Epsilon e2)::xs))=>                  (**************Apply, Sum, Probe**************)
164                     let val (i,eps, e)= epsToDels(body)              | E.Apply(E.Partial [],e)   => e
165                     in              | E.Apply(E.Partial d1, e1) =>
if (i=0) then let val e'=rewriteBody(E.Prod(e)) in (case e'
of E.Prod m=> let val (i2, p)= mkProd(eps @ m)
in E.Sum(c, p) end
|_=>E.Sum(c, E.Prod(eps@ [e']))) end
else(let val [list]=e
val ans=rewriteBody(list)
val soln=(case ans
of E.Sub (E.Sum(c1,(E.Prod s1)),E.Sum(c2,(E.Prod s2))) =>
E.Sum(c-3+c1, E.Sub(E.Prod(eps@s1),E.Prod(eps@s2)))
| E.Sub (E.Sum(c1,s1),E.Sum(c2,s2)) =>
E.Sum(c-3+c1, E.Prod(eps@ [E.Sub(s1,s2)]))
|_=> E.Prod(eps@ [ans]))
in (changed :=true;soln) end
) end

| E.Sum(c, E.Apply(E.Partial p,   E.Prod((E.Delta(i,j))::e3 )))=>

let fun part([], e2, counter)=([], e2, counter)
| part(p1::ps, [E.Delta(i,j)],counter)=if (p1=j) then ([i]@ps,[],counter-1)
else (let val (a,b,counter)=part(ps, [E.Delta(i,j)],counter)
in ([p1]@a, b,counter )  end)
val (e1,e2,counter)= part(p, [E.Delta(i,j)],c)

in   (E.Sum(counter, E.Apply(E.Partial e1, E.Prod(e2@e3)))) end

| E.Sum(c, E.Apply(p, e))=>let
val e'= rewriteBody(E.Sum(c, e))
val p'= rewriteBody p
val (i, e2)= (case e'
of E.Sum(c',exp)=> mkSumApply(E.Sum(c', E.Apply(p', exp)))
|_=>mkApply( E.Apply(p', e')))
in if(i=1) then (changed :=true;e2) else e2 end

| E.Sum(c, e)=> E.Sum(c, rewriteBody e)

| E.Prod([e1])=>(rewriteBody e1 )
(changed := true;
E.Add(List.map (fn e=> E.Prod([e1, e]@e3)) e2))
| E.Prod(e1::(E.Sub(e2,e3))::e4)=>
( changed :=true; E.Sub(E.Prod([e1, e2]@e4), E.Prod([e1,e3]@e4 )))
| E.Prod[E.Partial r1,E. Conv(E.Field(id,[i]), deltas)]=>
(changed:=true; (
166                     let                     let
167                      val j1= List.map (fn(x)=> (i,x))  r1                  val e2 = rewriteBody e1
168                     in E.Conv(E.Field(id,[i]), j1@deltas) end ))                  val (c,e3)=mkapply(E.Partial d1,e2)
169                | E.Prod((E.Partial r1)::(E.Partial r2)::e) =>                  in (case c of 1=>(changed:=true;e3)| _ =>e3 (*end case*))
170                      (changed := true; E.Prod([E.Partial (r1@r2)] @ e)  )                  end
171                | E.Prod[(E.Epsilon(e1,e2,e3)), E.Tensor(_,[i1,i2])]=>              | E.Apply _                 => raise Fail" Not well-formed Apply expression"
172                      if(e2=i1 andalso e3=i2) then (changed :=true;E.Const(0.0))              | 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
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))
195                  | E.Prod(e1::E.Sub(e2,e3)::e4)=>
196                    (changed :=true; E.Sub(E.Prod([e1,e2]@e4), E.Prod([e1,e3]@e4 )))
197
198
199                    (*************Product EPS **************)
200
201                  | E.Prod(E.Epsilon(i,j,k)::E.Apply(E.Partial d,e)::es)=>let
202                     val change= G.matchEps(0,d,[],[i,j,k])
203                     in case (change,es)
204                        of (1,_) =>(changed:=true; E.Const 0)
205                        | (_,[]) =>E.Prod[E.Epsilon(i,j,k),rewriteBody (E.Apply(E.Partial d,e))]
206                        |(_,_)=> 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                    end
211                  | E.Prod(E.Epsilon(i,j,k)::E.Conv(V,alpha, h, d)::es)=>let
212                        val change= G.matchEps(0,d,[],[i,j,k])
213                        in case (change,es)
214                            of (1,_) =>(changed:=true; E.Const 0)
215                            | (_,[]) =>E.Prod[E.Epsilon(i,j,k),E.Conv(V,alpha, h, d)]
216                            | (_,_) =>let
217                                val a=rewriteBody(E.Prod([E.Conv(V,alpha, h, d)]@ es))
218                                val (_,b) = F.mkProd [E.Epsilon(i,j,k),a]
219                                in b end
220                        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                      else body
| E.Prod((E.Epsilon eps1)::es)=> (let
val rest=(case es of [e1] => rewriteBody e1
|_=> rewriteBody(E.Prod(es)))
val (i, solution)=(case rest
of E.Prod m=> mkProd ([E.Epsilon eps1] @m )
|_=>  mkProd([E.Epsilon eps1]@ [rest]))
in if (i=1) then (changed:=true;solution)
else solution end)

| E.Prod (e::es) => (let val r=rewriteBody(E.Prod es)
val (i,solution)= (case r of E.Prod m => mkProd([e]@m )
|_=> mkProd([e]@ [r]))
in if (i=1) then (changed:=true;solution)
else solution end)
| E.Apply(E.Const _,_) => (E.Const(0.0))
| E.Apply(E.Partial p, E.Prod((E.Delta(i,j))::e3))=>
let fun part([], e2)=([], e2)
| part(p1::ps, [E.Delta(i,j)])=if (p1=j) then ([i]@ps,[])
else (let val (a,b)=part(ps, [E.Delta(i,j)])
in ([p1]@a, b )  end)
val (e1,e2)= part(p, [E.Delta(i,j)])
in   E.Apply(E.Partial e1, E.Prod(e2@e3)) end

| E.Apply(d,e)=> ( let val (t1,t2)= mkApply(E.Apply(rewriteBody d, rewriteBody e))
in if (t1=1) then (changed :=true;t2) else t2 end )
|_=> body
225
226                | 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*))
235
236                | 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
245              (*end case*))              (*end case*))
246
247        fun loop body = let              | 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.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*))
270
271                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              end                          of 1=> (print(String.concat["\nN =>",Int.toString(count),"--",P.printbody(body')]);1)
279      val b = loop body                          | _=> 1)
280      in      in
281      ((Ein.EIN{params=params, index=index, body=b}))                          (changed := false ;loop(body',count+1))
282      end      end
283                    else (body',count)
284    end    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
292                    (Ein.EIN{params=params, index=index, body=b},count)
293        end
294    end
295
296
297

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