Skip to content

Commit 6e0b98f

Browse files
author
Vitalij Ruge
committed
ExpressionSolve:
- update expand-strategy - improved cases for pow - added and update tests git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@23610 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent 0a2fdde commit 6e0b98f

File tree

1 file changed

+65
-7
lines changed

1 file changed

+65
-7
lines changed

Compiler/BackEnd/ExpressionSolve.mo

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ preprocessing for solve1,
345345
list<DAE.Exp> lhsWithX, rhsWithX, lhsWithoutX, rhsWithoutX, eWithX, factorWithX, factorWithoutX;
346346
DAE.Exp lhsX, rhsX, lhsY, rhsY, x, y, N;
347347
DAE.ComponentRef cr;
348-
DAE.Boolean con, new_x;
348+
DAE.Boolean con, new_x, expand := true;
349349
Integer iter;
350350

351351
algorithm
@@ -389,10 +389,20 @@ preprocessing for solve1,
389389
end if;
390390

391391
if con then
392-
(lhsX, lhsY) := preprocessingSolve5(x, inExp3,false);
393-
(rhsX, rhsY) := preprocessingSolve5(y, inExp3,false);
392+
(lhsX, lhsY) := preprocessingSolve5(x, inExp3, true);
393+
(rhsX, rhsY) := preprocessingSolve5(y, inExp3, true);
394394
x := Expression.expSub(lhsX, rhsX);
395395
y := Expression.expSub(rhsY, lhsY);
396+
expand := true;
397+
elseif expand then
398+
(lhsX, lhsY) := preprocessingSolve5(x, inExp3, expand);
399+
(rhsX, rhsY) := preprocessingSolve5(y, inExp3, expand);
400+
x := Expression.expSub(lhsX, rhsX);
401+
y := Expression.expSub(rhsY, lhsY);
402+
(x,_) := ExpressionSimplify.simplify1(x);
403+
expand := con;
404+
con := not con;
405+
iter := iter + 50;
396406
end if;
397407

398408
iter := iter + 1;
@@ -569,7 +579,7 @@ algorithm
569579
then
570580
(e1, inExp2, true);
571581

572-
// f(a)^n = c => f(a) = c^(1/x)
582+
// f(a)^n = c => f(a) = c^(1/n)
573583
// where n is odd
574584
case (DAE.BINARY(e1,DAE.POW(_),e2 as DAE.RCONST(r)), _, _)
575585
equation
@@ -581,6 +591,15 @@ algorithm
581591
then
582592
(e1, res, true);
583593

594+
// sqrt(f(a)) = f(a)^n = c => f(a) = c^(1/n)
595+
case (DAE.BINARY(e1,DAE.POW(_),e2 as DAE.RCONST(0.5)), _, _)
596+
equation
597+
false = expHasCref(inExp2, inExp3);
598+
true = expHasCref(e1, inExp3);
599+
res = Expression.expPow(inExp2,DAE.RCONST(2.0));
600+
then
601+
(e1, res, true);
602+
584603
// abs(x) = 0
585604
case (DAE.CALL(path = Absyn.IDENT(name = "abs"),expLst = {e1}), DAE.RCONST(0.0),_)
586605
then (e1,inExp2,true);
@@ -623,7 +642,7 @@ algorithm
623642
local
624643
String s1,s2;
625644
DAE.Operator op;
626-
DAE.Exp e1,e2;
645+
DAE.Exp e1,e2,e3,e4, e, e_1, e_2;
627646
DAE.Type tp;
628647

629648
// exp(f(x)) - exp(g(x)) = 0
@@ -661,6 +680,18 @@ algorithm
661680
true = expHasCref(e2,inExp3);
662681
then (e1, Expression.expPow(e2, DAE.RCONST(2.0)), true);
663682

683+
// f(x)^n - g(x)^n = 0 -> (f(x)/g(x))^n = 1
684+
case(DAE.BINARY(DAE.BINARY(e1, DAE.POW(), e2), DAE.SUB(tp), DAE.BINARY(e3, DAE.POW(), e4)), DAE.RCONST(0.0),_)
685+
equation
686+
true = Expression.expEqual(e2,e4);
687+
true = expHasCref(e1,inExp3);
688+
true = expHasCref(e3,inExp3);
689+
e = Expression.expPow(Expression.makeDiv(e1,e3),e2);
690+
(e_1, e_2, _) = preprocessingSolve3(e, Expression.makeConstOne(tp), inExp3);
691+
then (e_1, e_2, true);
692+
693+
694+
664695
else (inExp1, inExp2, false);
665696

666697
end matchcontinue;
@@ -754,7 +785,7 @@ algorithm
754785

755786
f2 := Expression.expandFactors(inExp2);
756787
(factorWithX2, factorWithoutX2) := List.split1OnTrue(f2, expHasCref, inExp3);
757-
pWithX2 := Expression.makeProductLst(factorWithX2);
788+
(pWithX2,_) := ExpressionSimplify.simplify1(Expression.makeProductLst(factorWithX2));
758789
pWithoutX2 := Expression.makeProductLst(factorWithoutX2);
759790
//print("\nf1 =");print(ExpressionDump.printExpListStr(f1));
760791
//print("\nf2 =");print(ExpressionDump.printExpListStr(f2));
@@ -812,7 +843,7 @@ algorithm
812843
//can be improve with Expression.getTermsContainingX ???
813844

814845
if expHasCref(inExp1, inExp3) then
815-
resTerms := if expand then Expression.allTerms(inExp1) else Expression.terms(inExp1);
846+
resTerms := Expression.terms(inExp1);
816847
// split
817848
(lhs, rhs) := List.split1OnTrue(resTerms, expHasCref, inExp3);
818849
//print("\nlhs =");print(ExpressionDump.printExpListStr(lhs));
@@ -828,6 +859,33 @@ algorithm
828859
outRhs := Expression.makeSum(rhs);
829860
(outRhs,_) := ExpressionSimplify.simplify1(outRhs);
830861

862+
if expand then
863+
resTerms := Expression.terms(Expression.expand(outLhs));
864+
(lhs, rhs) := List.split1OnTrue(resTerms, expHasCref, inExp3);
865+
outLhs := DAE.RCONST(0.0);
866+
// sort
867+
// a*f(x)*b -> c*f(x)
868+
for e in lhs loop
869+
outLhs := expAddX(e, outLhs, inExp3); // special add
870+
end for;
871+
//rhs
872+
outRhs := Expression.expAdd(outRhs,Expression.makeSum(rhs));
873+
(outRhs,_) := ExpressionSimplify.simplify1(outRhs);
874+
875+
resTerms := Expression.allTerms(outLhs);
876+
(lhs, rhs) := List.split1OnTrue(resTerms, expHasCref, inExp3);
877+
// sort
878+
// a*f(x)*b -> c*f(x)
879+
outLhs := DAE.RCONST(0.0);
880+
for e in lhs loop
881+
outLhs := expAddX(e, outLhs, inExp3); // special add
882+
end for;
883+
//rhs
884+
outRhs := Expression.expAdd(outRhs,Expression.makeSum(rhs));
885+
(outRhs,_) := ExpressionSimplify.simplify1(outRhs);
886+
887+
end if;
888+
831889
else
832890
outRhs := inExp1;
833891
end if;

0 commit comments

Comments
 (0)