241 |
| E.Delta _ =>(1,E.Const 0.0) |
| E.Delta _ =>(1,E.Const 0.0) |
242 |
| E.Value _ =>(1,E.Const 0.0) |
| E.Value _ =>(1,E.Const 0.0) |
243 |
| E.Epsilon _ =>(1,E.Const 0.0) |
| E.Epsilon _ =>(1,E.Const 0.0) |
244 |
| E.Conv (fid,alpha,tid, delta)=> let |
| E.Conv (fid,alpha,tid, delta)=> (1, E.Conv(fid,alpha, tid, delta@d)) |
|
val e'=E.Conv(fid,alpha, tid, delta@d) |
|
|
in (1,e') end |
|
245 |
| E.Add l => (1,E.Add(List.map (fn e => E.Apply(E.Partial d, e)) l)) |
| E.Add l => (1,E.Add(List.map (fn e => E.Apply(E.Partial d, e)) l)) |
246 |
| E.Sub(e2, e3) =>(1, E.Sub(E.Apply(E.Partial d, e2), E.Apply(E.Partial d, e3))) |
| E.Sub(e2, e3) =>(1, E.Sub(E.Apply(E.Partial d, e2), E.Apply(E.Partial d, e3))) |
247 |
| E.Div(e2, e3) =>(1, E.Div(E.Apply(E.Partial d, e2), e3)) |
| E.Div(e2, e3) =>(1, E.Div(E.Apply(E.Partial d, e2), e3))(********FIXX******) |
248 |
| E.Apply(E.Partial d2,e2)=>(1,E.Apply(E.Partial(d@d2), e2)) |
| E.Apply(E.Partial d2,e2)=>(1,E.Apply(E.Partial(d@d2), e2)) |
249 |
| E.Prod [e1]=>(1,E.Apply(E.Partial d,e1)) |
| E.Prod [e1]=>(1,E.Apply(E.Partial d,e1)) |
250 |
| E.Prod es=> let |
| E.Prod es=> let |
271 |
|
|
272 |
|
|
273 |
|
|
274 |
|
|
275 |
|
(*need to add filter here *) |
276 |
fun mkSumApply(E.Sum(c,E.Apply(E.Partial d,e)))=(case e |
fun mkSumApply(E.Sum(c,E.Apply(E.Partial d,e)))=(case e |
277 |
of E.Const _=>(1,E.Const 0.0) |
of E.Const _=>(1,E.Const 0.0) |
278 |
| E.Tensor(_,[])=> (1,E.Const 0.0) |
| E.Tensor(_,[])=> (1,E.Const 0.0) |
279 |
| E.Delta _ =>(1,E.Const 0.0) |
| E.Delta _ =>(1,E.Const 0.0) |
280 |
| E.Value _ =>(1,E.Const 0.0) |
| E.Value _ =>(1,E.Const 0.0) |
281 |
| E.Epsilon _ =>(1,E.Const 0.0) |
| E.Epsilon _ =>(1,E.Const 0.0) |
282 |
| E.Conv (fid,alpha,tid, delta)=> let |
| E.Conv (fid,alpha,tid, delta)=> (1,E.Sum(c,E.Conv(fid,alpha, tid, delta@d))) |
|
val e'=E.Conv(fid,alpha, tid, delta@d) |
|
|
in (1,E.Sum(c,e')) end |
|
283 |
| E.Apply(E.Partial d1,e2)=>(1,E.Sum(c,E.Apply(E.Partial(d@d1),e2))) |
| E.Apply(E.Partial d1,e2)=>(1,E.Sum(c,E.Apply(E.Partial(d@d1),e2))) |
284 |
| E.Add l => (1,E.Add(List.map (fn e => E.Sum(c,E.Apply(E.Partial d, e))) l)) |
| E.Add l => (1,E.Add(List.map (fn e => E.Sum(c,E.Apply(E.Partial d, e))) l)) |
285 |
| E.Sub(e1, e2) => (1, E.Sub(E.Sum(c,E.Apply(E.Partial d, e1)), E.Sum(c,E.Apply(E.Partial d, e2)))) |
| E.Sub(e1, e2) => (1, E.Sub(E.Sum(c,E.Apply(E.Partial d, e1)), E.Sum(c,E.Apply(E.Partial d, e2)))) |
336 |
| E.Value _ =>body |
| E.Value _ =>body |
337 |
| E.Epsilon _=>body |
| E.Epsilon _=>body |
338 |
| E.Conv _=>body |
| E.Conv _=>body |
|
|
|
339 |
| E.Neg e => E.Neg(rewriteBody e) |
| E.Neg e => E.Neg(rewriteBody e) |
340 |
| E.Add es => let val (change,body')= mkAdd(List.map rewriteBody es) |
| E.Add es => let val (change,body')= mkAdd(List.map rewriteBody es) |
341 |
in if (change=1) then ( changed:=true;body') else body' end |
in if (change=1) then ( changed:=true;body') else body' end |
342 |
| E.Sub(a, E.Field f)=> (changed:=true;E.Add[a, E.Neg(E.Field(f))]) |
| E.Sub(a, E.Field f)=> (changed:=true;E.Add[a, E.Neg(E.Field(f))]) |
|
(*| E.Sub(a,E.Sub(b,c))=> (changed:=true;E.Add[E.Sub(a,b),c])*) |
|
343 |
| E.Sub (a,b)=> E.Sub(rewriteBody a, rewriteBody b) |
| E.Sub (a,b)=> E.Sub(rewriteBody a, rewriteBody b) |
344 |
| E.Div (a, b) => E.Div(rewriteBody a, rewriteBody b) |
| E.Div (a, b) => E.Div(rewriteBody a, rewriteBody b) |
345 |
| E.Partial _=>body |
| E.Partial _=>body |
349 |
|
|
350 |
(*************Product**************) |
(*************Product**************) |
351 |
| E.Prod [e1] => rewriteBody e1 |
| E.Prod [e1] => rewriteBody e1 |
|
| E.Prod((E.Add(e2))::e3)=> |
|
|
(changed := true; E.Add(List.map (fn e=> E.Prod([e]@e3)) e2)) |
|
|
| E.Prod((E.Sub(e2,e3))::e4)=> |
|
|
(changed :=true; E.Sub(E.Prod([e2]@e4), E.Prod([e3]@e4 ))) |
|
|
|
|
352 |
| E.Prod((E.Div(e2,e3))::e4)=> |
| E.Prod((E.Div(e2,e3))::e4)=> |
353 |
(changed :=true; E.Div(E.Prod([e2]@e4), e3 )) |
(changed :=true; E.Div(E.Prod([e2]@e4), e3 )) |
354 |
|
| E.Prod((E.Add(e2))::e3)=> |
355 |
|
(changed := true; E.Add(List.map (fn e=> E.Prod([e]@e3)) e2)) |
356 |
| E.Prod(e1::E.Add(e2)::e3)=> |
| E.Prod(e1::E.Add(e2)::e3)=> |
357 |
(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)) |
358 |
|
| E.Prod((E.Sub(e2,e3))::e4)=> |
359 |
|
(changed :=true; E.Sub(E.Prod([e2]@e4), E.Prod([e3]@e4 ))) |
360 |
| E.Prod(e1::E.Sub(e2,e3)::e4)=> |
| E.Prod(e1::E.Sub(e2,e3)::e4)=> |
361 |
(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 ))) |
362 |
|
|
363 |
|
| E.Prod [E.Partial r1,E.Partial r2]=> |
364 |
|
(changed:=true;E.Partial(r1@r2)) |
365 |
|
| E.Prod(E.Partial r1::E.Partial r2::p)=> |
366 |
|
(changed:=true;E.Prod([E.Partial(r1@r2)]@p)) |
367 |
|
|
368 |
|
|
369 |
|
(*************Product EPS **************) |
370 |
|
|
371 |
|
(* Apply (d, e) shoudl be convereted to Conv operator *) |
372 |
| E.Prod(E.Epsilon(i,j,k)::E.Apply(E.Partial d,e)::es)=>let |
| E.Prod(E.Epsilon(i,j,k)::E.Apply(E.Partial d,e)::es)=>let |
373 |
val change= matchEps(0,d,[],[i,j,k]) |
val change= matchEps(0,d,[],[i,j,k]) |
374 |
in case (change,es) |
in case (change,es) |
375 |
of (1,_) =>(changed:=true; E.Const 0.0) |
of (1,_) =>(changed:=true; E.Const 0.0) |
376 |
| (_,[]) =>E.Prod[E.Epsilon(i,j,k), |
| (_,[]) =>E.Prod[E.Epsilon(i,j,k),rewriteBody (E.Apply(E.Partial d,e))] |
|
(rewriteBody (E.Apply(E.Partial d,e)))] |
|
377 |
|(_,_)=> let |
|(_,_)=> let |
378 |
val a=rewriteBody(E.Prod([E.Apply(E.Partial d,e)]@ es)) |
val a=rewriteBody(E.Prod([E.Apply(E.Partial d,e)]@ es)) |
379 |
val (_,b)=mkProd [E.Epsilon(i,j,k),a] |
val (_,b)=mkProd [E.Epsilon(i,j,k),a] |
380 |
in b end |
in b end |
381 |
end |
end |
382 |
|
| E.Prod(E.Epsilon(i,j,k)::E.Conv(V,alpha, h, d)::es)=>let |
383 |
|
val change= matchEps(0,d,[],[i,j,k]) |
384 |
|
in case (change,es) |
385 |
|
of (1,_) =>(changed:=true; E.Const 0.0) |
386 |
|
| (_,[]) =>E.Prod[E.Epsilon(i,j,k),E.Conv(V,alpha, h, d)] |
387 |
|
| (_,_) =>let |
388 |
|
val a=rewriteBody(E.Prod([E.Conv(V,alpha, h, d)]@ es)) |
389 |
|
val (_,b) = mkProd [E.Epsilon(i,j,k),a] |
390 |
|
in b end |
391 |
|
end |
392 |
|
|
393 |
|
|
394 |
| E.Prod[(E.Epsilon(e1,e2,e3)), E.Tensor(_,[E.V i1,E.V i2])]=> |
| E.Prod[(E.Epsilon(e1,e2,e3)), E.Tensor(_,[E.V i1,E.V i2])]=> |
395 |
if(e2=i1 andalso e3=i2) then (changed :=true;E.Const(0.0)) |
if(e2=i1 andalso e3=i2) then (changed :=true;E.Const(0.0)) |
396 |
else body |
else body |
|
| E.Prod [E.Partial r1, E.Tensor(_,[])]=> (changed:=true;E.Const(0.0)) |
|
|
| E.Prod [E.Partial r1,E.Partial r2]=> |
|
|
(changed:=true;E.Partial(r1@r2)) |
|
397 |
|
|
|
| E.Prod(E.Partial r1::E.Partial r2::p)=> |
|
|
(changed:=true;E.Prod([E.Partial(r1@r2)]@p)) |
|
398 |
| E.Prod(E.Sum(c1,E.Prod(E.Epsilon e1::es1))::E.Sum(c2,E.Prod(E.Epsilon e2::es2))::es)=>let |
| E.Prod(E.Sum(c1,E.Prod(E.Epsilon e1::es1))::E.Sum(c2,E.Prod(E.Epsilon e2::es2))::es)=>let |
399 |
val (change,e,rest)=epsToDels(E.Sum(c1@c2, E.Prod([E.Epsilon e1, E.Epsilon e2]@es1@es2@es))) |
val (change,e,rest)=epsToDels(E.Sum(c1@c2, E.Prod([E.Epsilon e1, E.Epsilon e2]@es1@es2@es))) |
400 |
in (case (change,e, rest) |
in (case (change,e, rest) |
409 |
end |
end |
410 |
(*end case*)) |
(*end case*)) |
411 |
end |
end |
412 |
|
(*************Product **************) |
413 |
| E.Prod[e1,e2]=> let val (_,b)=mkProd[rewriteBody e1, rewriteBody e2] in b end |
| E.Prod[e1,e2]=> let val (_,b)=mkProd[rewriteBody e1, rewriteBody e2] in b end |
414 |
| E.Prod(e::es)=>let |
| E.Prod(e::es)=>let |
415 |
val e'=rewriteBody e |
val e'=rewriteBody e |
434 |
|_=> e' |
|_=> e' |
435 |
(*end case*)) |
(*end case*)) |
436 |
end |
end |
|
|
|
437 |
| E.Apply(E.Partial [],e)=> e |
| E.Apply(E.Partial [],e)=> e |
438 |
| E.Apply(E.Partial p,E.Probe(E.Conv(fid,alpha,tid,d),x))=> |
| E.Apply(E.Partial p,E.Probe(E.Conv(fid,alpha,tid,d),x))=> |
439 |
(changed:=true;E.Probe(E.Conv(fid,alpha,tid,d@p),x)) |
(changed:=true;E.Probe(E.Conv(fid,alpha,tid,d@p),x)) |
440 |
| E.Apply(E.Partial p,E.Conv(fid,alpha,tid,d))=> |
| E.Apply(E.Partial p,E.Conv(fid,alpha,tid,d))=> |
441 |
(changed:=true;E.Conv(fid,alpha,tid,d@p)) |
(changed:=true;E.Conv(fid,alpha,tid,d@p)) |
442 |
| E.Apply(E.Partial p, e)=>let |
| E.Apply(E.Partial p, e)=>let |
|
|
|
443 |
val body'=E.Apply(E.Partial p, rewriteBody e) |
val body'=E.Apply(E.Partial p, rewriteBody e) |
444 |
val (c, e')=mkApply(body') |
val (c, e')=mkApply(body') |
445 |
in (case c |
in (case c |
446 |
of 1=>(changed:=true;e') |
of 1=>(changed:=true;e') |
447 |
| _ =>e') end |
| _ =>e') end |
448 |
|
| E.Apply(e1,e2)=>E.Apply(rewriteBody e1, rewriteBody e2) |
|
| E.Apply(e1,e2)=>((E.Apply(rewriteBody e1, rewriteBody e2)) |
|
|
) |
|
|
|
|
449 |
|
|
450 |
(************** Sum *****************) |
(************** Sum *****************) |
451 |
| E.Sum([],e)=> (changed:=true;rewriteBody e) |
| E.Sum([],e)=> (changed:=true;rewriteBody e) |
501 |
|
|
502 |
| E.Sum(c,e)=>E.Sum(c,rewriteBody e) |
| E.Sum(c,e)=>E.Sum(c,rewriteBody e) |
503 |
|
|
504 |
(*Probe*) |
(*******************Probe*****************) |
505 |
| E.Probe(E.Sum(c,s),x)=>(changed:=true;E.Sum(c,E.Probe(s,x))) |
| E.Probe(E.Sum(c,s),x)=>(changed:=true;E.Sum(c,E.Probe(s,x))) |
506 |
|
| E.Probe(E.Tensor t,_)=> E.Tensor t |
507 |
|
|
508 |
| E.Probe(E.Neg e1,x)=>(changed:=true;E.Neg(E.Probe(e1,x))) |
| E.Probe(E.Neg e1,x)=>(changed:=true;E.Neg(E.Probe(e1,x))) |
509 |
| E.Probe(E.Add es,x) => |
| E.Probe(E.Add es,x) => |
510 |
(changed:=true;E.Add(List.map (fn(e1)=>E.Probe(e1,x)) es)) |
(changed:=true;E.Add(List.map (fn(e1)=>E.Probe(e1,x)) es)) |
552 |
|
|
553 |
fun loop(body ,count) = let |
fun loop(body ,count) = let |
554 |
val body' = rewriteBody body |
val body' = rewriteBody body |
|
|
|
|
(* val y=(print "Counter:";print(Int.toString(count));print"\n")*) |
|
555 |
in |
in |
556 |
if !changed |
if !changed |
557 |
then (changed := false ;loop(body',count+1)) |
then (changed := false ;loop(body',count+1)) |
559 |
end |
end |
560 |
|
|
561 |
val (b,count) = loop(body,0) |
val (b,count) = loop(body,0) |
562 |
|
|
563 |
(*val j=(print "Final Counter:";print(Int.toString(count));print"\n")*) |
(*val j=(print "Final Counter:";print(Int.toString(count));print"\n")*) |
564 |
in |
in |
565 |
(Ein.EIN{params=params, index=index, body=b},count) |
(Ein.EIN{params=params, index=index, body=b},count) |