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 2605, Wed Apr 30 01:46:09 2014 UTC revision 2608, Fri May 2 18:04:54 2014 UTC
# Line 11  Line 11
11      in      in
12
13  fun err str=raise Fail (String.concat["Ill-formed EIN Operator",str])  fun err str=raise Fail (String.concat["Ill-formed EIN Operator",str])
14  val testing=0  val testing=1
15
16    fun flatProd [e]=e
17    | flatProd e=E.Prod e
18
19
20  fun prodAppPartial(es,p1)=(case es  fun prodAppPartial(es,p1)=(case es
# Line 37  Line 39
39      | E.Epsilon _ => (0,E.Sum(c1,e1))      | E.Epsilon _ => (0,E.Sum(c1,e1))
40      | E.Tensor _  => (0,E.Sum(c1,e1))      | E.Tensor _  => (0,E.Sum(c1,e1))
41      | E.Neg e2    => (1,E.Neg(E.Sum(c1,e2)))      | E.Neg e2    => (1,E.Neg(E.Sum(c1,e2)))
| E.Lift e    => (1,E.Lift(E.Sum(c1,e)))
| E.Sum(c2,e2)=> (1,E.Sum(c1@c2,e2))
42      | E.Sub (a,b) => (1,E.Sub(E.Sum(c1,a),E.Sum(c1,b)))      | E.Sub (a,b) => (1,E.Sub(E.Sum(c1,a),E.Sum(c1,b)))
| E.Div (a,b) => (1,E.Div(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)      | E.Prod p     =>F.filterSca(c1,p)
48      | E.Const _   => err("Sum of Const")      | E.Const _   => err("Sum of Const")
49      | E.Partial _ => err("Sum of Partial")      | E.Partial _ => err("Sum of Partial")
# Line 63  Line 65
65      | E.Apply(E.Partial d2,e2)  => let      | E.Apply(E.Partial d2,e2)  => let
66                          val E.Partial d3=d1                          val E.Partial d3=d1
67                          in (1,E.Apply(E.Partial(d3@d2),e2)) end                          in (1,E.Apply(E.Partial(d3@d2),e2)) end
68      | E.Apply _   => raise Fail" Apply of non-Partial expression"      | E.Apply _   => err" Apply of non-Partial expression"
| E.Neg e2    => (1,E.Neg(E.Apply(d1,e2)))
69      | E.Sum(c2,e2)=> (1,E.Sum(c2,E.Apply(d1,e2)))      | E.Sum(c2,e2)=> (1,E.Sum(c2,E.Apply(d1,e2)))
70        | E.Neg e2    => (1,E.Neg(E.Apply(d1,e2)))
72      | E.Sub (a,b) => (1,E.Sub(E.Apply(d1,a),E.Apply(d1,b)))      | E.Sub (a,b) => (1,E.Sub(E.Apply(d1,a),E.Apply(d1,b)))
73      | E.Div (a,b) => (1,E.Div(E.Apply(d1,a),E.Apply(d1,b)))      | E.Div (g,b) => let
74            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      | E.Prod p =>let
88          val (pre, post)= F.filterField p          val (pre, post)= F.filterField p
89          val E.Partial d3=d1          val E.Partial d3=d1
# Line 93  Line 108
108      | E.Apply _   => (0,E.Probe(e1,x))      | E.Apply _   => (0,E.Probe(e1,x))
109      | E.Conv _    => (0,E.Probe(e1,x))      | E.Conv _    => (0,E.Probe(e1,x))
110      | E.Field _   => (0,E.Probe(e1,x))      | E.Field _   => (0,E.Probe(e1,x))
111      | E.Sum(c,e') => (1,E.Sum(c,E.Probe(e',x)))      | E.Sum(c,e') => (1,E.Sum(c,E.Probe(e',x)))
112      | E.Div (a,b) => (1,E.Div(E.Probe(a,x),E.Probe(b,x)))      | E.Add e     => (1,E.Add (List.map (fn(a)=>E.Probe(a,x)) e))
113      | E.Sub (a,b) => (1,E.Sub(E.Probe(a,x),E.Probe(b,x)))      | 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)))      | 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")      | E.Const _   => err("Const without Lift")
117      | E.Tensor _  => err("Tensor without Lift")      | E.Tensor _  => err("Tensor without Lift")
118      | E.Delta _   => err("Probe of Delta")      | E.Delta _   => err("Probe of Delta")
# Line 184  Line 199
199                  (*************Product**************)                  (*************Product**************)
200                | E.Prod [] => raise Fail"missing elements in product"                | E.Prod [] => raise Fail"missing elements in product"
201                | E.Prod [e1] => rewriteBody e1                | E.Prod [e1] => rewriteBody e1
| E.Prod((E.Div(e2,e3))::e4)=>
(changed :=true; E.Div(E.Prod([e2]@e4), e3 ))
203                     (changed := true; E.Add(List.map (fn e=> E.Prod([e]@e3)) e2))                     (changed := true; E.Add(List.map (fn e=> E.Prod([e]@e3)) e2))
(changed := true; E.Add(List.map (fn e=> E.Prod([e1,e]@e3)) e2))
204                | E.Prod((E.Sub(e2,e3))::e4)=>                | E.Prod((E.Sub(e2,e3))::e4)=>
205                  (changed :=true; E.Sub(E.Prod([e2]@e4), E.Prod([e3]@e4 )))                  (changed :=true; E.Sub(E.Prod([e2]@e4), E.Prod([e3]@e4 )))
206                  | E.Prod((E.Div(e2,e3))::e4)=> (changed :=true; E.Div(E.Prod([e2]@e4), e3 ))