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

Commit bb49baa

Browse files
perostOpenModelica-Hudson
authored andcommitted
Fix expression simplification rules.
- Fixes several expression simplifications rules that changed Integer expressions into Real expressions. Belonging to [master]: - #2330 - OpenModelica/OpenModelica-testsuite#908
1 parent 9edd1f5 commit bb49baa

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

Compiler/FrontEnd/Expression.mo

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4493,6 +4493,17 @@ algorithm
44934493
end match;
44944494
end makeConstZero;
44954495

4496+
function makeConstNumber
4497+
input DAE.Type ty;
4498+
input Integer n;
4499+
output DAE.Exp exp;
4500+
algorithm
4501+
exp := match ty
4502+
case DAE.T_INTEGER() then DAE.ICONST(n);
4503+
else DAE.RCONST(n);
4504+
end match;
4505+
end makeConstNumber;
4506+
44964507
public function makeConstZeroE
44974508
"Generates a zero constant, using type from inExp"
44984509
input DAE.Exp iExp;

Compiler/FrontEnd/ExpressionSimplify.mo

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2115,7 +2115,16 @@ public function simplify2
21152115
input Boolean simplifyAddOrSub = true;
21162116
input Boolean simplifyMulOrDiv = true;
21172117
output DAE.Exp outExp;
2118+
protected
2119+
DAE.Type ty;
21182120
algorithm
2121+
ty := Expression.typeof(inExp);
2122+
2123+
if not Expression.isIntegerOrReal(ty) then
2124+
outExp := inExp;
2125+
return;
2126+
end if;
2127+
21192128
outExp := match(inExp)
21202129
local
21212130
DAE.Exp e1,e2,exp_2,exp_3, resConst;
@@ -2125,13 +2134,13 @@ algorithm
21252134

21262135
// global simplify ADD and SUB
21272136
case DAE.BINARY(operator = op) /* multiple terms simplifications */
2128-
guard simplifyAddOrSub and Expression.isIntegerOrReal(Expression.typeof(inExp)) and Expression.isAddOrSub(op)
2137+
guard simplifyAddOrSub and Expression.isAddOrSub(op)
21292138
equation
21302139
/* Sorting constants, 1+a+2+b => 3+a+b */
21312140
lstExp = Expression.terms(inExp);
21322141
(lstConstExp, lstExp) = List.splitOnTrue(lstExp, Expression.isConstValue);
21332142
hasConst = not listEmpty(lstConstExp);
2134-
resConst = if hasConst then simplifyBinaryAddConstants(lstConstExp) else DAE.RCONST(0.0);
2143+
resConst = if hasConst then simplifyBinaryAddConstants(lstConstExp) else Expression.makeConstZero(ty);
21352144
exp_2 = if hasConst then Expression.makeSum1(lstExp) else inExp;
21362145
/* Merging coefficients 2a+4b+3a+b => 5a+5b */
21372146
exp_3 = simplifyBinaryCoeff(exp_2);
@@ -2142,7 +2151,7 @@ algorithm
21422151
//locked global simplify ADD and SUB
21432152
//unlocked global simplify MUL and DIV
21442153
case DAE.BINARY(exp1 = e1,operator = op,exp2 = e2) /* multiple factors simplifications */
2145-
guard Expression.isIntegerOrReal(Expression.typeof(inExp)) and Expression.isAddOrSub(op)
2154+
guard Expression.isAddOrSub(op)
21462155
equation
21472156
e1 = simplify2(e1, false, true);
21482157
e2 = simplify2(e2, false, true);
@@ -2151,7 +2160,7 @@ algorithm
21512160

21522161
//global simplify MUL and DIV
21532162
case DAE.BINARY(operator = op) /* multiple factors simplifications */
2154-
guard simplifyMulOrDiv and Expression.isIntegerOrReal(Expression.typeof(inExp)) and Expression.isMulOrDiv(op)
2163+
guard simplifyMulOrDiv and Expression.isMulOrDiv(op)
21552164
equation
21562165
/* Sorting constants, 1+a+2+b => 3+a+b */
21572166
lstExp = Expression.factors(inExp);
@@ -2176,7 +2185,7 @@ algorithm
21762185
//unlocked global simplify ADD and SUB
21772186
//locked global simplify MUL and DIV
21782187
case DAE.BINARY(exp1 = e1,operator = op,exp2 = e2) /* multiple terms simplifications */
2179-
guard Expression.isIntegerOrReal(Expression.typeof(inExp)) and Expression.isMulOrDiv(op)
2188+
guard Expression.isMulOrDiv(op)
21802189
equation
21812190
e1 = simplify2(e1, true, false);
21822191
e2 = simplify2(e2, true, false);
@@ -2185,7 +2194,6 @@ algorithm
21852194

21862195
//others operators
21872196
case DAE.BINARY(exp1 = e1,operator = op,exp2 = e2) /* multiple terms/factor simplifications */
2188-
guard Expression.isIntegerOrReal(Expression.typeof(inExp))
21892197
equation
21902198
e1 = simplify2(e1);
21912199
e2 = simplify2(e2);
@@ -2848,7 +2856,8 @@ algorithm
28482856
DAE.Exp res,e,e1,e2;
28492857
Type tp;
28502858

2851-
case ((e as DAE.BINARY(operator = DAE.MUL())))
2859+
case ((e as DAE.BINARY(operator = DAE.MUL(tp))))
2860+
guard Types.isScalarReal(tp)
28522861
equation
28532862
e_lst = Expression.factors(e);
28542863
e_lst_1 = simplifyMul(e_lst);
@@ -4058,12 +4067,12 @@ algorithm
40584067

40594068
// (e * e1) * e => e1*e^2
40604069
case (DAE.MUL(),DAE.BINARY(e2,op1 as DAE.MUL(ty),e3),e1)
4061-
guard Expression.expEqual(e2,e1)
4070+
guard Types.isScalarReal(ty) and Expression.expEqual(e2,e1)
40624071
then DAE.BINARY(e3,op1,DAE.BINARY(e1,DAE.POW(ty),DAE.RCONST(2.0)));
40634072

40644073
// e * (e1 * e) => e1*e^2
40654074
case (DAE.MUL(),e1,DAE.BINARY(e2,op1 as DAE.MUL(ty),e3))
4066-
guard Expression.expEqual(e1,e3)
4075+
guard Types.isScalarReal(ty) and Expression.expEqual(e1,e3)
40674076
then DAE.BINARY(e2,op1,DAE.BINARY(e1,DAE.POW(ty),DAE.RCONST(2.0)));
40684077

40694078
// r1 * (r2 * e) => (r1*r2)*e
@@ -4097,17 +4106,17 @@ algorithm
40974106
// then
40984107
// DAE.BINARY(e1,op2,DAE.BINARY(e2,op1,e3));
40994108

4100-
// e2 + (e*e2) = (1.0 + e)*e2
4101-
// e2 + (e2*e) = (1.0 + e)*e2;
4102-
case (op1 as DAE.ADD(), e1, DAE.BINARY(e2, op2 as DAE.MUL(),e3))
4109+
// e2 + (e*e2) = (1 + e)*e2
4110+
// e2 + (e2*e) = (1 + e)*e2;
4111+
case (op1 as DAE.ADD(ty), e1, DAE.BINARY(e2, op2 as DAE.MUL(),e3))
41034112
guard not Expression.isConstValue(e1)
41044113
equation
41054114
if Expression.expEqual(e1,e3)
41064115
then
4107-
exp = DAE.BINARY(e1,op2,DAE.BINARY(DAE.RCONST(1.0),op1,e2));
4116+
exp = DAE.BINARY(e1,op2,DAE.BINARY(Expression.makeConstOne(ty),op1,e2));
41084117
else if Expression.expEqual(e1,e2)
41094118
then
4110-
exp = DAE.BINARY(e1,op2,DAE.BINARY(DAE.RCONST(1.0),op1,e3));
4119+
exp = DAE.BINARY(e1,op2,DAE.BINARY(Expression.makeConstOne(ty),op1,e3));
41114120
else
41124121
fail();
41134122
end if;
@@ -4376,7 +4385,7 @@ algorithm
43764385
equation
43774386
true = Types.isRealOrSubTypeReal(ty);
43784387
true = Expression.expEqual(e1,e2);
4379-
e = DAE.RCONST(2.0);
4388+
e = Expression.makeConstNumber(ty, 2);
43804389
then DAE.BINARY(e,DAE.MUL(ty),e1);
43814390

43824391
// a-(-b) = a+b
@@ -4423,6 +4432,7 @@ algorithm
44234432
case (_,DAE.MUL(ty = ty),e1,e2,_,_)
44244433
equation
44254434
false = Expression.isZero(e2);
4435+
true = Types.isRealOrSubTypeReal(ty);
44264436
true = Expression.expEqual(e1,e2);
44274437
res = DAE.BINARY(e1,DAE.POW(ty),DAE.RCONST(2.0));
44284438
then res;

0 commit comments

Comments
 (0)