Skip to content

Commit

Permalink
Support nested Condition.
Browse files Browse the repository at this point in the history
  • Loading branch information
corywalker committed Aug 23, 2017
1 parent 433f62c commit 080c098
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
14 changes: 14 additions & 0 deletions examples/test_rubi.m
@@ -0,0 +1,14 @@
testproblems = ReadList["/Users/cwalker32/Code/gocode/src/github.com/corywalker/expreduce/test_rubi/test_rubi.m"];
testproblems = DeleteCases[testproblems, Null];

testi = 1;

While[testi <= Length[testproblems],
testp = testproblems[[testi]];
(*Print[testp];*)
res = Int[testp[[1]], testp[[2]]];
(*Print[res];*)
(*Print[res === testp[[4]]];*)
If[res === testp[[4]], Print[testi]];
testi = testi+1;
];
24 changes: 18 additions & 6 deletions expreduce/replace.go
Expand Up @@ -102,19 +102,31 @@ func ReplaceAll(this Ex, r *Expression, es *EvalState, pm *PDManager,
return this
}

func tryCondWithMatches(asCond *Expression, matches *PDManager, es *EvalState) (Ex, bool) {
condRes := ReplacePD(asCond.Parts[2], es, matches).Eval(es)
condResSymbol, condResIsSymbol := condRes.(*Symbol)
if condResIsSymbol {
if condResSymbol.Name == "System`True" {
toReturn := ReplacePD(asCond.Parts[1], es, matches)
if rCond, isRCond := HeadAssertion(toReturn, "System`Condition"); isRCond {
return tryCondWithMatches(rCond, matches, es)
}
return toReturn, true
}
}
return nil, false
}

func Replace(this Ex, r *Expression, es *EvalState) (Ex, bool) {
if asCond, isCond := HeadAssertion(r.Parts[2], "System`Condition"); isCond {
mi, cont := NewMatchIter(this, r.Parts[1], EmptyPD(), es)
for cont {
res, matches, done := mi.next()
cont = !done
if res {
condRes := ReplacePD(asCond.Parts[2], es, matches).Eval(es)
condResSymbol, condResIsSymbol := condRes.(*Symbol)
if condResIsSymbol {
if condResSymbol.Name == "System`True" {
return ReplacePD(asCond.Parts[1], es, matches), true
}
toReturn, ok := tryCondWithMatches(asCond, matches, es)
if ok {
return toReturn, true
}
}
}
Expand Down

0 comments on commit 080c098

Please sign in to comment.