Skip to content

Commit 2aed853

Browse files
author
Vitalij Ruge
committed
- improved expAdd,expSub, expPow
- added test for expAdd, expSub, expPow git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@23459 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent 2ddef83 commit 2aed853

File tree

1 file changed

+60
-42
lines changed

1 file changed

+60
-42
lines changed

Compiler/FrontEnd/Expression.mo

Lines changed: 60 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3208,7 +3208,7 @@ algorithm
32083208
Operator op;
32093209
Real r1,r2;
32103210
Integer i1,i2;
3211-
DAE.Exp e;
3211+
DAE.Exp e, x, y, z;
32123212
case(_,_)
32133213
equation
32143214
true = isZero(e1);
@@ -3234,13 +3234,42 @@ algorithm
32343234
case (_,DAE.UNARY(operator=DAE.UMINUS_ARR(),exp=e))
32353235
then
32363236
expSub(e1,e);
3237+
// x +(-y)*z
3238+
case (_,DAE.BINARY(DAE.UNARY(DAE.UMINUS(),x),op as DAE.MUL(),y))
3239+
then
3240+
expSub(e1,DAE.BINARY(x,op,y));
3241+
case (_,DAE.BINARY(DAE.UNARY(DAE.UMINUS_ARR(),x), op as DAE.MUL_ARR(),y))
3242+
then
3243+
expSub(e1,DAE.BINARY(x,op,y));
3244+
// x +(-y)/z
3245+
case (_,DAE.BINARY(DAE.UNARY(DAE.UMINUS(),x),op as DAE.DIV(),y))
3246+
then
3247+
expSub(e1,DAE.BINARY(x,op,y));
3248+
case (_,DAE.BINARY(DAE.UNARY(DAE.UMINUS_ARR(),x), op as DAE.DIV_ARR(),y))
3249+
then
3250+
expSub(e1,DAE.BINARY(x,op,y));
32373251
/* -b + a = a - b */
32383252
case (DAE.UNARY(operator=DAE.UMINUS(),exp=e),_)
32393253
then
32403254
expSub(e2,e);
32413255
case (DAE.UNARY(operator=DAE.UMINUS_ARR(),exp=e),_)
32423256
then
32433257
expSub(e2,e);
3258+
// (-y)*z+x
3259+
case (DAE.BINARY(DAE.UNARY(DAE.UMINUS(),x),op as DAE.MUL(),y),_)
3260+
then
3261+
expSub(e2,DAE.BINARY(x,op,y));
3262+
case (DAE.BINARY(DAE.UNARY(DAE.UMINUS_ARR(),x), op as DAE.MUL_ARR(),y),_)
3263+
then
3264+
expSub(e2,DAE.BINARY(x,op,y));
3265+
// (-y)/z + x
3266+
case (DAE.BINARY(DAE.UNARY(DAE.UMINUS(),x),op as DAE.DIV(),y),_)
3267+
then
3268+
expSub(e2,DAE.BINARY(x,op,y));
3269+
case (DAE.BINARY(DAE.UNARY(DAE.UMINUS_ARR(),x), op as DAE.DIV_ARR(),y),_)
3270+
then
3271+
expSub(e2,DAE.BINARY(x,op,y));
3272+
32443273
case (_,_)
32453274
equation
32463275
tp = typeof(e1);
@@ -3272,7 +3301,7 @@ algorithm
32723301
Operator op;
32733302
Real r1,r2;
32743303
Integer i1,i2;
3275-
DAE.Exp e;
3304+
DAE.Exp e, x,y;
32763305
case(_,_)
32773306
equation
32783307
true = isZero(e1);
@@ -3300,6 +3329,20 @@ algorithm
33003329
case (_,DAE.UNARY(operator=DAE.UMINUS_ARR(),exp=e))
33013330
then
33023331
expAdd(e1,e);
3332+
// x -(-y)*z
3333+
case (_,DAE.BINARY(DAE.UNARY(DAE.UMINUS(),x),op as DAE.MUL(),y))
3334+
then
3335+
expAdd(e1,DAE.BINARY(x,op,y));
3336+
case (_,DAE.BINARY(DAE.UNARY(DAE.UMINUS_ARR(),x), op as DAE.MUL_ARR(),y))
3337+
then
3338+
expAdd(e1,DAE.BINARY(x,op,y));
3339+
// x -(-y)/z
3340+
case (_,DAE.BINARY(DAE.UNARY(DAE.UMINUS(),x),op as DAE.DIV(),y))
3341+
then
3342+
expAdd(e1,DAE.BINARY(x,op,y));
3343+
case (_,DAE.BINARY(DAE.UNARY(DAE.UMINUS_ARR(),x), op as DAE.DIV_ARR(),y))
3344+
then
3345+
expAdd(e1,DAE.BINARY(x,op,y));
33033346
/* - a - b = -(a + b) */
33043347
case (DAE.UNARY(operator=DAE.UMINUS(),exp=e),_)
33053348
equation
@@ -3517,8 +3560,10 @@ algorithm
35173560
outExp := matchcontinue(e1,e2)
35183561
local
35193562
Type tp;
3520-
DAE.Exp e,e3,e4;
3563+
DAE.Exp e,e3,e4,e5;
35213564
Real r1,r2;
3565+
Boolean b;
3566+
Operator op;
35223567

35233568
// e1^1 = e1
35243569
case(_,_) equation
@@ -3542,58 +3587,31 @@ algorithm
35423587
false = isZero(e2);
35433588
then makeConstZero(typeof(e1));
35443589

3545-
// (r1*e1)^r2 = c*e1^r2
3546-
case (DAE.BINARY(DAE.RCONST(real = r1),DAE.MUL(_),exp2 = e3) , DAE.RCONST(real = r2)) equation
3547-
e = expPow(DAE.RCONST(r1), DAE.RCONST(r2));
3548-
e = expMul(e,expPow(e3,e2));
3549-
then e;
3550-
3551-
// (e1*r1)^r2 = c*e1^r2
3552-
case (DAE.BINARY(e3, DAE.MUL(_), DAE.RCONST(real = r1)) , DAE.RCONST(real = r2)) equation
3553-
e = expPow(DAE.RCONST(r1), DAE.RCONST(r2));
3554-
e = expMul(e,expPow(e3,e2));
3555-
then e;
3556-
3557-
// (r1/e1)^r2 = c/e1^r2
3558-
case (DAE.BINARY(DAE.RCONST(real = r1),DAE.DIV(_),exp2 = e3) , DAE.RCONST(real = r2)) equation
3559-
e = expPow(DAE.RCONST(r1), DAE.RCONST(r2));
3560-
e = makeDiv(e, expPow(e3,e2));
3561-
then e;
3562-
3563-
// (e1/r1)^r2 = c*e1^r2 TODO! FIXME! SAME AS 2 cases ABOVE??!!
3564-
case (DAE.BINARY(e3, DAE.DIV(_), DAE.RCONST(real = r1)) , DAE.RCONST(real = r2)) equation
3565-
e = expPow(DAE.RCONST(r1), DAE.RCONST(r2));
3566-
e = makeDiv(DAE.RCONST(1.0), e);
3567-
e = expMul(e, expPow(e3,e2));
3568-
then e;
3569-
35703590
// (-e)^r = e^r if r is even
3571-
case (DAE.UNARY(exp=e,operator=DAE.UMINUS()), DAE.RCONST(real = r1)) equation
3572-
r2 = realMod(r1,2.0);
3573-
true = realEq(r2,0.0);
3591+
case (DAE.UNARY(DAE.UMINUS(),e), DAE.RCONST(r1)) equation
3592+
0.0 = realMod(r1,2.0);
35743593
e = expPow(e,DAE.RCONST(r1));
35753594
then e;
35763595

3596+
// (x/y)^(-z) = (y/x)^z
3597+
case (DAE.BINARY(e3, DAE.DIV(), e4), DAE.UNARY(DAE.UMINUS(),e5)) equation
3598+
e = makeDiv(e4,e3);
3599+
e = expPow(e,e5);
3600+
then e;
3601+
35773602
// (e1/e2)^(-r) = (e2/e1)^r
3578-
case (DAE.BINARY(e3, DAE.DIV(_), e4) , DAE.RCONST(r2)) equation
3603+
case (DAE.BINARY(e3, DAE.DIV(), e4) , DAE.RCONST(r2)) equation
35793604
true = realLt(r2, 0.0);
35803605
r2 = realNeg(r2);
35813606
e = makeDiv(e4,e3);
35823607
e = expPow(e,DAE.RCONST(r2));
35833608
then e;
35843609

3585-
// e ^ -r1 = 1/(e^r1)
3586-
case (_, DAE.RCONST(real = r1)) equation
3587-
true = realLt(r1, 0.0);
3588-
r1 = realNeg(r1);
3589-
e = DAE.RCONST(1.0);
3590-
e3 = expPow(e1, DAE.RCONST(r1));
3591-
e = makeDiv(e,e3);
3592-
then e;
3593-
35943610
else equation
35953611
tp = typeof(e1);
3596-
then DAE.BINARY(e1,DAE.POW(tp),e2);
3612+
b = DAEUtil.expTypeArray(tp);
3613+
op = if b then DAE.POW_ARR(tp) else DAE.POW(tp);
3614+
then DAE.BINARY(e1,op,e2);
35973615

35983616
end matchcontinue;
35993617
end expPow;

0 commit comments

Comments
 (0)