Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit 059c8c0

Browse files
sjoelundOpenModelica-Hudson
authored andcommitted
Fix for DAE.RSUB with Expression.factorsWork
Belonging to [master]: - #2076 - OpenModelica/OpenModelica-testsuite#810
1 parent 1c428bf commit 059c8c0

File tree

2 files changed

+47
-133
lines changed

2 files changed

+47
-133
lines changed

Compiler/BackEnd/ExpressionSolve.mo

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ algorithm
129129
varexp := Expression.expDer(varexp);
130130
cr := ComponentReference.crefPrefixDer(cr);
131131
end if;
132-
133132
if (Types.isIntegerOrRealOrSubTypeOfEither(Expression.typeof(e1)) and Types.isIntegerOrRealOrSubTypeOfEither(Expression.typeof(e2))) then
134133
(e1, e2) := preprocessingSolve(e1, e2, varexp, SOME(shared.functionTree), NONE(), 0, false);
135134
end if;
@@ -915,7 +914,6 @@ algorithm
915914
(factorWithX1, factorWithoutX1) := List.split1OnTrue(f1, expHasCref, inExp3);
916915
pWithX1 := makeProductLstSort(factorWithX1);
917916
pWithoutX1 := makeProductLstSort(factorWithoutX1);
918-
919917
f2 := Expression.expandFactors(inExp2);
920918
(factorWithX2, factorWithoutX2) := List.split1OnTrue(f2, expHasCref, inExp3);
921919
(pWithX2,_) := ExpressionSimplify.simplify1(makeProductLstSort(factorWithX2));
@@ -989,7 +987,7 @@ algorithm
989987
if expand then
990988
(cr, b) := Expression.expOrDerCref(inExp3);
991989
if b then
992-
(lhs, rhs) := Expression.allTermsForCref(inExp1, cr, Expression.Expression.expHasDerCref);
990+
(lhs, rhs) := Expression.allTermsForCref(inExp1, cr, Expression.expHasDerCref);
993991
else
994992
(lhs, rhs) := Expression.allTermsForCref(inExp1, cr, Expression.expHasCrefNoPreOrStart);
995993
end if;
@@ -1748,7 +1746,7 @@ algorithm
17481746
false = hasOnlyFactors(inExp1,inExp2);
17491747
e = Expression.makeDiff(inExp1,inExp2);
17501748
(e,_) = ExpressionSimplify.simplify1(e);
1751-
//print("\n\ne: ");print(ExpressionDump.printExpStr(e));
1749+
//print("\ne: ");print(ExpressionDump.printExpStr(e));
17521750
dere = Differentiate.differentiateExpSolve(e, cr, functions);
17531751
//print("\nder(e): ");print(ExpressionDump.printExpStr(dere));
17541752
(dere,_) = ExpressionSimplify.simplify(dere);

Compiler/FrontEnd/Expression.mo

Lines changed: 45 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -2936,123 +2936,35 @@ public function factors
29362936
algorithm
29372937
// TODO: Remove this listReverse as it is pointless.
29382938
// It transforms a*b to b*a, but the testsuite expects this :(
2939-
outExpLst := listReverse(factorsWork(inExp,{},false,false));
2939+
outExpLst := listReverse(factorsWork(inExp,{},false));
29402940
end factors;
29412941

29422942
protected function factorsWork
29432943
"Returns the factors of the expression if any as a list of expressions"
29442944
input DAE.Exp inExp;
2945-
input list<DAE.Exp> inAcc;
2946-
input Boolean noFactors "Decides if the default is the empty list or not";
2945+
input output list<DAE.Exp> acc;
29472946
input Boolean doInverseFactors "Decides if a factor e should be 1/e instead";
2948-
output list<DAE.Exp> outExpLst;
29492947
algorithm
2950-
outExpLst := match (inExp,inAcc,noFactors,doInverseFactors)
2948+
acc := match inExp
29512949
local
29522950
DAE.Exp e1,e2,e;
2953-
ComponentRef cr;
2954-
Real r;
2955-
Boolean b;
2956-
list<DAE.Exp> acc;
29572951

2958-
case (DAE.BINARY(exp1 = e1,operator = DAE.MUL(),exp2 = e2),acc,_,_)
2959-
equation
2960-
acc = factorsWork(e1,acc,true,doInverseFactors);
2961-
acc = factorsWork(e2,acc,true,doInverseFactors);
2962-
then acc;
2963-
case (DAE.BINARY(exp1 = e1,operator = DAE.DIV(ty = DAE.T_REAL()),exp2 = e2),acc,_,_)
2964-
equation
2965-
acc = factorsWork(e1,acc,true,doInverseFactors);
2966-
acc = factorsWork(e2,acc,true,not doInverseFactors);
2952+
case DAE.BINARY(exp1 = e1,operator = DAE.MUL(),exp2 = e2)
2953+
algorithm
2954+
acc := factorsWork(e1,acc,doInverseFactors);
2955+
acc := factorsWork(e2,acc,doInverseFactors);
29672956
then acc;
2968-
case (DAE.CREF(),acc,_,_)
2969-
equation
2970-
e = if doInverseFactors then inverseFactors(inExp) else inExp;
2971-
then e::acc;
2972-
case (DAE.BINARY(),acc,_,_)
2973-
equation
2974-
e = if doInverseFactors then inverseFactors(inExp) else inExp;
2975-
then e::acc;
2976-
case (DAE.ICONST(integer = 1),acc,_,_)
2957+
case DAE.BINARY(exp1 = e1,operator = DAE.DIV(ty = DAE.T_REAL()),exp2 = e2)
2958+
algorithm
2959+
acc := factorsWork(e1,acc,doInverseFactors);
2960+
acc := factorsWork(e2,acc,not doInverseFactors);
29772961
then acc;
2978-
case (DAE.ICONST(),acc,_,_)
2979-
equation
2980-
e = if doInverseFactors then inverseFactors(inExp) else inExp;
2981-
then e::acc;
2982-
case (DAE.RCONST(real = r),acc,_,_)
2983-
equation
2984-
b = not realEq(r,1.0);
2985-
e = if b and doInverseFactors then inverseFactors(inExp) else inExp;
2986-
acc = List.consOnTrue(b, e, acc);
2962+
case DAE.ICONST(integer = 1)
29872963
then acc;
2988-
case (DAE.SCONST(),acc,_,_)
2989-
equation
2990-
e = if doInverseFactors then inverseFactors(inExp) else inExp;
2991-
then e::acc;
2992-
case (DAE.UNARY(),acc,_,_) // factor(-(x*y)) is -(x*y) ??
2993-
equation
2994-
e = if doInverseFactors then inverseFactors(inExp) else inExp;
2995-
then e::acc;
2996-
case (DAE.LUNARY(),acc,_,_)
2997-
then inExp::acc;
2998-
case (DAE.IFEXP(),acc,_,_)
2999-
equation
3000-
e = if doInverseFactors then inverseFactors(inExp) else inExp;
3001-
then e::acc;
3002-
case (DAE.CALL(),acc,_,_)
3003-
equation
3004-
e = if doInverseFactors then inverseFactors(inExp) else inExp;
3005-
then e::acc;
3006-
case (DAE.RECORD(),acc,_,_)
3007-
equation
3008-
e = if doInverseFactors then inverseFactors(inExp) else inExp;
3009-
then e::acc;
3010-
case (DAE.RECORD(),acc,_,_)
3011-
equation
3012-
e = if doInverseFactors then inverseFactors(inExp) else inExp;
3013-
then e::acc;
3014-
case (DAE.PARTEVALFUNCTION(),acc,_,_)
3015-
equation
3016-
e = if doInverseFactors then inverseFactors(inExp) else inExp;
3017-
then e::acc;
3018-
case (DAE.ARRAY(),acc,_,_)
3019-
equation
3020-
e = if doInverseFactors then inverseFactors(inExp) else inExp;
3021-
then e::acc;
3022-
case (DAE.MATRIX(),acc,_,_)
3023-
equation
3024-
e = if doInverseFactors then inverseFactors(inExp) else inExp;
3025-
then e::acc;
3026-
case (DAE.RANGE(),acc,_,_)
3027-
equation
3028-
e = if doInverseFactors then inverseFactors(inExp) else inExp;
3029-
then e::acc;
3030-
case (DAE.CAST(),acc,_,_)
3031-
equation
3032-
e = if doInverseFactors then inverseFactors(inExp) else inExp;
3033-
then e::acc;
3034-
case (DAE.ASUB(),acc,_,_)
3035-
equation
3036-
e = if doInverseFactors then inverseFactors(inExp) else inExp;
3037-
then e::acc;
3038-
case (DAE.TSUB(),acc,_,_)
3039-
equation
3040-
e = if doInverseFactors then inverseFactors(inExp) else inExp;
3041-
then e::acc;
3042-
case (DAE.SIZE(),acc,_,_)
3043-
equation
3044-
e = if doInverseFactors then inverseFactors(inExp) else inExp;
3045-
then e::acc;
3046-
case (DAE.REDUCTION(),acc,_,_)
3047-
equation
3048-
e = if doInverseFactors then inverseFactors(inExp) else inExp;
3049-
then e::acc;
3050-
case (e,acc,true,_)
3051-
equation
3052-
e = if doInverseFactors then inverseFactors(e) else e;
3053-
then e::acc;
3054-
case (_,acc,false,_)
2964+
case DAE.RCONST(real = 1.0)
30552965
then acc;
2966+
// case DAE.UNARY() // factor(-(x*y)) is -(x*y) ??
2967+
else (if doInverseFactors then inverseFactors(inExp) else inExp) :: acc;
30562968
end match;
30572969
end factorsWork;
30582970

@@ -3107,53 +3019,51 @@ algorithm
31073019
// TODO: Remove this listReverse as it is pointless.
31083020
// It transforms a*b to b*a, but the testsuite expects this :(
31093021
// issue with expEqual(a*b,b*a) return false
3110-
outExpLst := listReverse(expandFactorsWork(inExp,{},false,false));
3022+
outExpLst := listReverse(expandFactorsWork(inExp,{},false));
31113023
end expandFactors;
31123024

31133025
protected function expandFactorsWork
31143026
"Returns the factors of the expression if any as a list of expressions"
31153027
input DAE.Exp inExp;
3116-
input list<DAE.Exp> inAcc;
3117-
input Boolean noFactors "Decides if the default is the empty list or not";
3028+
input output list<DAE.Exp> acc;
31183029
input Boolean doInverseFactors "Decides if a factor e should be 1/e instead";
3119-
output list<DAE.Exp> outExpLst;
31203030
algorithm
31213031

3122-
outExpLst := match (inExp,inAcc,noFactors,doInverseFactors)
3032+
acc := match inExp
31233033
local
31243034
DAE.Exp e1,e2,e3,e;
31253035
Type tp;
3126-
list<DAE.Exp> acc, pow_acc, pow_acc2;
3036+
list<DAE.Exp> pow_acc, pow_acc2;
31273037

31283038
// (x*y)^n = x^n*y^n
3129-
case (DAE.BINARY(DAE.BINARY(e1,DAE.MUL(),e2), DAE.POW(), e3),acc,_,_)
3039+
case DAE.BINARY(DAE.BINARY(e1,DAE.MUL(),e2), DAE.POW(), e3)
31303040
equation
3131-
pow_acc = expandFactorsWork(e1,{},noFactors,doInverseFactors);
3041+
pow_acc = expandFactorsWork(e1,{},doInverseFactors);
31323042
pow_acc = expPowLst(pow_acc, e3);
31333043

3134-
pow_acc2 = expandFactorsWork(e2,{},noFactors,doInverseFactors);
3044+
pow_acc2 = expandFactorsWork(e2,{},doInverseFactors);
31353045
pow_acc2 = expPowLst(pow_acc2, e3);
31363046

31373047
acc = listAppend(pow_acc, acc);
31383048
acc = listAppend(pow_acc2, acc);
31393049
then acc;
31403050
// (x/y)^n = x^n*y^(-n)
3141-
case (DAE.BINARY(DAE.BINARY(e1,DAE.DIV(),e2), DAE.POW(), e3),acc,_,_)
3051+
case DAE.BINARY(DAE.BINARY(e1,DAE.DIV(),e2), DAE.POW(), e3)
31423052
equation
3143-
pow_acc = expandFactorsWork(e1,{},noFactors,doInverseFactors);
3053+
pow_acc = expandFactorsWork(e1,{},doInverseFactors);
31443054
pow_acc = expPowLst(pow_acc, e3);
31453055

3146-
pow_acc2 = expandFactorsWork(e2,{},noFactors,doInverseFactors);
3056+
pow_acc2 = expandFactorsWork(e2,{},doInverseFactors);
31473057
pow_acc2 = expPowLst(pow_acc2, negate(e3));
31483058

31493059
acc = listAppend(pow_acc, acc);
31503060
acc = listAppend(pow_acc2, acc);
31513061
then acc;
31523062
// (x^n)^m = x^(n*m)
3153-
case (DAE.BINARY(DAE.BINARY(e1,DAE.POW(),e2), DAE.POW(), e3),acc,_,_)
3063+
case DAE.BINARY(DAE.BINARY(e1,DAE.POW(),e2), DAE.POW(), e3)
31543064
equation
31553065
e = expMul(e2,e3);
3156-
pow_acc = expandFactorsWork(e1,{},noFactors,doInverseFactors);
3066+
pow_acc = expandFactorsWork(e1,{},doInverseFactors);
31573067
pow_acc = expPowLst(pow_acc, e);
31583068

31593069
acc = listAppend(pow_acc, acc);
@@ -3165,31 +3075,30 @@ algorithm
31653075
// abs(x/y) = abs(x)/abs(y);
31663076

31673077
// -(x) = -1*x
3168-
case(DAE.UNARY(DAE.UMINUS(tp),e1),acc,_,_)
3078+
case DAE.UNARY(DAE.UMINUS(tp),e1)
31693079
equation
31703080
e = makeConstOne(tp);
3171-
acc = expandFactorsWork(e1,acc,true,doInverseFactors);
3081+
acc = expandFactorsWork(e1,acc,doInverseFactors);
31723082
e = negate(e);
31733083
then e::acc;
3174-
case(DAE.UNARY(DAE.UMINUS_ARR(tp),e1),acc,_,_)
3084+
case DAE.UNARY(DAE.UMINUS_ARR(tp),e1)
31753085
equation
31763086
e = makeConstOne(tp);
3177-
acc = expandFactorsWork(e1,acc,true,doInverseFactors);
3087+
acc = expandFactorsWork(e1,acc,doInverseFactors);
31783088
e = negate(e);
31793089
then e::acc;
31803090

31813091
else
31823092
equation
3183-
acc = factorsWork(inExp,inAcc,noFactors,doInverseFactors);
3184-
then expandFactorsWork2(acc, noFactors, doInverseFactors);
3093+
acc = factorsWork(inExp,acc,doInverseFactors);
3094+
then expandFactorsWork2(acc,doInverseFactors);
31853095

31863096
end match;
31873097

31883098
end expandFactorsWork;
31893099

31903100
protected function expandFactorsWork2
31913101
input list<DAE.Exp> inAcc;
3192-
input Boolean noFactors "Decides if the default is the empty list or not";
31933102
input Boolean doInverseFactors "Decides if a factor e should be 1/e instead";
31943103
output list<DAE.Exp> outExpLst = {};
31953104
protected
@@ -3198,11 +3107,11 @@ algorithm
31983107

31993108
for elem in inAcc loop
32003109
tmpExpLst := match(elem)
3201-
case(DAE.BINARY(DAE.BINARY(_,DAE.DIV(),_), DAE.POW(), _)) then expandFactorsWork(elem,{},noFactors,doInverseFactors);
3202-
case(DAE.BINARY(DAE.BINARY(_,DAE.MUL(),_), DAE.POW(), _)) then expandFactorsWork(elem,{},noFactors,doInverseFactors);
3203-
case(DAE.BINARY(DAE.BINARY(_,DAE.POW(),_), DAE.POW(), _)) then expandFactorsWork(elem,{},noFactors,doInverseFactors);
3204-
case(DAE.UNARY(DAE.UMINUS(),_)) then expandFactorsWork(elem,{},noFactors,doInverseFactors);
3205-
case(DAE.UNARY(DAE.UMINUS_ARR(),_)) then expandFactorsWork(elem,{},noFactors,doInverseFactors);
3110+
case(DAE.BINARY(DAE.BINARY(_,DAE.DIV(),_), DAE.POW(), _)) then expandFactorsWork(elem,{},doInverseFactors);
3111+
case(DAE.BINARY(DAE.BINARY(_,DAE.MUL(),_), DAE.POW(), _)) then expandFactorsWork(elem,{},doInverseFactors);
3112+
case(DAE.BINARY(DAE.BINARY(_,DAE.POW(),_), DAE.POW(), _)) then expandFactorsWork(elem,{},doInverseFactors);
3113+
case(DAE.UNARY(DAE.UMINUS(),_)) then expandFactorsWork(elem,{},doInverseFactors);
3114+
case(DAE.UNARY(DAE.UMINUS_ARR(),_)) then expandFactorsWork(elem,{},doInverseFactors);
32063115
else {elem};
32073116
end match;
32083117
outExpLst := listAppend(tmpExpLst, outExpLst);
@@ -7112,6 +7021,13 @@ algorithm
71127021
then
71137022
(DAE.ASUB(e1, expl), arg);
71147023

7024+
case e1 as DAE.RSUB()
7025+
equation
7026+
(e2, arg) = traverseExpBidir(e1.exp, inEnterFunc, inExitFunc, inArg);
7027+
e1.exp = e2;
7028+
then
7029+
(e1, arg);
7030+
71157031
case DAE.TSUB(e1, i, ty)
71167032
equation
71177033
(e1, arg) = traverseExpBidir(e1, inEnterFunc, inExitFunc, inArg);

0 commit comments

Comments
 (0)