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

Commit 3db7ba0

Browse files
committed
Fix for DAE.RSUB with Expression.factorsWork
1 parent 1d668ee commit 3db7ba0

2 files changed

Lines changed: 47 additions & 133 deletions

File tree

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
@@ -2935,123 +2935,35 @@ public function factors
29352935
algorithm
29362936
// TODO: Remove this listReverse as it is pointless.
29372937
// It transforms a*b to b*a, but the testsuite expects this :(
2938-
outExpLst := listReverse(factorsWork(inExp,{},false,false));
2938+
outExpLst := listReverse(factorsWork(inExp,{},false));
29392939
end factors;
29402940

29412941
protected function factorsWork
29422942
"Returns the factors of the expression if any as a list of expressions"
29432943
input DAE.Exp inExp;
2944-
input list<DAE.Exp> inAcc;
2945-
input Boolean noFactors "Decides if the default is the empty list or not";
2944+
input output list<DAE.Exp> acc;
29462945
input Boolean doInverseFactors "Decides if a factor e should be 1/e instead";
2947-
output list<DAE.Exp> outExpLst;
29482946
algorithm
2949-
outExpLst := match (inExp,inAcc,noFactors,doInverseFactors)
2947+
acc := match inExp
29502948
local
29512949
DAE.Exp e1,e2,e;
2952-
ComponentRef cr;
2953-
Real r;
2954-
Boolean b;
2955-
list<DAE.Exp> acc;
29562950

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

@@ -3106,53 +3018,51 @@ algorithm
31063018
// TODO: Remove this listReverse as it is pointless.
31073019
// It transforms a*b to b*a, but the testsuite expects this :(
31083020
// issue with expEqual(a*b,b*a) return false
3109-
outExpLst := listReverse(expandFactorsWork(inExp,{},false,false));
3021+
outExpLst := listReverse(expandFactorsWork(inExp,{},false));
31103022
end expandFactors;
31113023

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

3121-
outExpLst := match (inExp,inAcc,noFactors,doInverseFactors)
3031+
acc := match inExp
31223032
local
31233033
DAE.Exp e1,e2,e3,e;
31243034
Type tp;
3125-
list<DAE.Exp> acc, pow_acc, pow_acc2;
3035+
list<DAE.Exp> pow_acc, pow_acc2;
31263036

31273037
// (x*y)^n = x^n*y^n
3128-
case (DAE.BINARY(DAE.BINARY(e1,DAE.MUL(),e2), DAE.POW(), e3),acc,_,_)
3038+
case DAE.BINARY(DAE.BINARY(e1,DAE.MUL(),e2), DAE.POW(), e3)
31293039
equation
3130-
pow_acc = expandFactorsWork(e1,{},noFactors,doInverseFactors);
3040+
pow_acc = expandFactorsWork(e1,{},doInverseFactors);
31313041
pow_acc = expPowLst(pow_acc, e3);
31323042

3133-
pow_acc2 = expandFactorsWork(e2,{},noFactors,doInverseFactors);
3043+
pow_acc2 = expandFactorsWork(e2,{},doInverseFactors);
31343044
pow_acc2 = expPowLst(pow_acc2, e3);
31353045

31363046
acc = listAppend(pow_acc, acc);
31373047
acc = listAppend(pow_acc2, acc);
31383048
then acc;
31393049
// (x/y)^n = x^n*y^(-n)
3140-
case (DAE.BINARY(DAE.BINARY(e1,DAE.DIV(),e2), DAE.POW(), e3),acc,_,_)
3050+
case DAE.BINARY(DAE.BINARY(e1,DAE.DIV(),e2), DAE.POW(), e3)
31413051
equation
3142-
pow_acc = expandFactorsWork(e1,{},noFactors,doInverseFactors);
3052+
pow_acc = expandFactorsWork(e1,{},doInverseFactors);
31433053
pow_acc = expPowLst(pow_acc, e3);
31443054

3145-
pow_acc2 = expandFactorsWork(e2,{},noFactors,doInverseFactors);
3055+
pow_acc2 = expandFactorsWork(e2,{},doInverseFactors);
31463056
pow_acc2 = expPowLst(pow_acc2, negate(e3));
31473057

31483058
acc = listAppend(pow_acc, acc);
31493059
acc = listAppend(pow_acc2, acc);
31503060
then acc;
31513061
// (x^n)^m = x^(n*m)
3152-
case (DAE.BINARY(DAE.BINARY(e1,DAE.POW(),e2), DAE.POW(), e3),acc,_,_)
3062+
case DAE.BINARY(DAE.BINARY(e1,DAE.POW(),e2), DAE.POW(), e3)
31533063
equation
31543064
e = expMul(e2,e3);
3155-
pow_acc = expandFactorsWork(e1,{},noFactors,doInverseFactors);
3065+
pow_acc = expandFactorsWork(e1,{},doInverseFactors);
31563066
pow_acc = expPowLst(pow_acc, e);
31573067

31583068
acc = listAppend(pow_acc, acc);
@@ -3164,31 +3074,30 @@ algorithm
31643074
// abs(x/y) = abs(x)/abs(y);
31653075

31663076
// -(x) = -1*x
3167-
case(DAE.UNARY(DAE.UMINUS(tp),e1),acc,_,_)
3077+
case DAE.UNARY(DAE.UMINUS(tp),e1)
31683078
equation
31693079
e = makeConstOne(tp);
3170-
acc = expandFactorsWork(e1,acc,true,doInverseFactors);
3080+
acc = expandFactorsWork(e1,acc,doInverseFactors);
31713081
e = negate(e);
31723082
then e::acc;
3173-
case(DAE.UNARY(DAE.UMINUS_ARR(tp),e1),acc,_,_)
3083+
case DAE.UNARY(DAE.UMINUS_ARR(tp),e1)
31743084
equation
31753085
e = makeConstOne(tp);
3176-
acc = expandFactorsWork(e1,acc,true,doInverseFactors);
3086+
acc = expandFactorsWork(e1,acc,doInverseFactors);
31773087
e = negate(e);
31783088
then e::acc;
31793089

31803090
else
31813091
equation
3182-
acc = factorsWork(inExp,inAcc,noFactors,doInverseFactors);
3183-
then expandFactorsWork2(acc, noFactors, doInverseFactors);
3092+
acc = factorsWork(inExp,acc,doInverseFactors);
3093+
then expandFactorsWork2(acc,doInverseFactors);
31843094

31853095
end match;
31863096

31873097
end expandFactorsWork;
31883098

31893099
protected function expandFactorsWork2
31903100
input list<DAE.Exp> inAcc;
3191-
input Boolean noFactors "Decides if the default is the empty list or not";
31923101
input Boolean doInverseFactors "Decides if a factor e should be 1/e instead";
31933102
output list<DAE.Exp> outExpLst = {};
31943103
protected
@@ -3197,11 +3106,11 @@ algorithm
31973106

31983107
for elem in inAcc loop
31993108
tmpExpLst := match(elem)
3200-
case(DAE.BINARY(DAE.BINARY(_,DAE.DIV(),_), DAE.POW(), _)) then expandFactorsWork(elem,{},noFactors,doInverseFactors);
3201-
case(DAE.BINARY(DAE.BINARY(_,DAE.MUL(),_), DAE.POW(), _)) then expandFactorsWork(elem,{},noFactors,doInverseFactors);
3202-
case(DAE.BINARY(DAE.BINARY(_,DAE.POW(),_), DAE.POW(), _)) then expandFactorsWork(elem,{},noFactors,doInverseFactors);
3203-
case(DAE.UNARY(DAE.UMINUS(),_)) then expandFactorsWork(elem,{},noFactors,doInverseFactors);
3204-
case(DAE.UNARY(DAE.UMINUS_ARR(),_)) then expandFactorsWork(elem,{},noFactors,doInverseFactors);
3109+
case(DAE.BINARY(DAE.BINARY(_,DAE.DIV(),_), DAE.POW(), _)) then expandFactorsWork(elem,{},doInverseFactors);
3110+
case(DAE.BINARY(DAE.BINARY(_,DAE.MUL(),_), DAE.POW(), _)) then expandFactorsWork(elem,{},doInverseFactors);
3111+
case(DAE.BINARY(DAE.BINARY(_,DAE.POW(),_), DAE.POW(), _)) then expandFactorsWork(elem,{},doInverseFactors);
3112+
case(DAE.UNARY(DAE.UMINUS(),_)) then expandFactorsWork(elem,{},doInverseFactors);
3113+
case(DAE.UNARY(DAE.UMINUS_ARR(),_)) then expandFactorsWork(elem,{},doInverseFactors);
32053114
else {elem};
32063115
end match;
32073116
outExpLst := listAppend(tmpExpLst, outExpLst);
@@ -7119,6 +7028,13 @@ algorithm
71197028
then
71207029
(DAE.ASUB(e1, expl), arg);
71217030

7031+
case e1 as DAE.RSUB()
7032+
equation
7033+
(e2, arg) = traverseExpBidir(e1.exp, inEnterFunc, inExitFunc, inArg);
7034+
e1.exp = e2;
7035+
then
7036+
(e1, arg);
7037+
71227038
case DAE.TSUB(e1, i, ty)
71237039
equation
71247040
(e1, arg) = traverseExpBidir(e1, inEnterFunc, inExitFunc, inArg);

0 commit comments

Comments
 (0)