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

Commit 197ec7a

Browse files
hkielOpenModelica-Hudson
authored andcommitted
fix cosh^2(x)-sinh^2(x)=1 in ExpressionSimplify.simplifyBinary
also remove asin(sin(x))=x and atan(tan(x))=x from simplifyCall Belonging to [master]: - #2023 - OpenModelica/OpenModelica-testsuite#783
1 parent 1fd2008 commit 197ec7a

File tree

1 file changed

+44
-83
lines changed

1 file changed

+44
-83
lines changed

Compiler/FrontEnd/ExpressionSimplify.mo

Lines changed: 44 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ algorithm
376376
DAE.Type tp;
377377
Boolean b2;
378378
Real r1,r2;
379-
String idn;
379+
String idn, idn2;
380380
Integer n,i1,i2;
381381

382382
// homotopy(e, e) => e
@@ -470,16 +470,12 @@ algorithm
470470
then DAE.ARRAY(DAE.T_ARRAY(tp,{DAE.DIM_INTEGER(n),DAE.DIM_INTEGER(n)}),false,matrix);
471471

472472
// arcxxx(xxx(e)) => e; xxx(arcxxx(e)) => e
473-
case (DAE.CALL(path=Absyn.IDENT("sin"),expLst={DAE.CALL(path=Absyn.IDENT("asin"),expLst={e})}))
474-
then e;
475-
case (DAE.CALL(path=Absyn.IDENT("cos"),expLst={DAE.CALL(path=Absyn.IDENT("acos"),expLst={e})}))
476-
then e;
477-
case (DAE.CALL(path=Absyn.IDENT("tan"),expLst={DAE.CALL(path=Absyn.IDENT("atan"),expLst={e})}))
478-
then e;
479-
case (DAE.CALL(path=Absyn.IDENT("asin"),expLst={DAE.CALL(path=Absyn.IDENT("sin"),expLst={e})}))
480-
then e;
481-
case (DAE.CALL(path=Absyn.IDENT("atan"),expLst={DAE.CALL(path=Absyn.IDENT("tan"),expLst={e})}))
473+
case (DAE.CALL(path=Absyn.IDENT(idn),expLst={DAE.CALL(path=Absyn.IDENT(idn2),expLst={e})}))
474+
guard (idn=="tan" and idn2=="atan")
475+
// or (idn=="sin" and idn2=="asin")
476+
// or (idn=="cos" and idn2=="acos")
482477
then e;
478+
483479
// modulo for real values
484480
case (DAE.CALL(path=Absyn.IDENT("mod"),expLst={DAE.RCONST(r1),DAE.RCONST(r2)}))
485481
then DAE.RCONST(r1-floor(r1/r2)*r2);
@@ -3801,10 +3797,11 @@ protected function simplifyBinaryCommutative
38013797
input DAE.Exp rhs;
38023798
output DAE.Exp exp;
38033799
algorithm
3804-
exp := matchcontinue (op,lhs,rhs)
3805-
case (_,_,_) then simplifyBinaryCommutativeWork(op,lhs,rhs);
3806-
case (_,_,_) then simplifyBinaryCommutativeWork(op,rhs,lhs);
3807-
end matchcontinue;
3800+
try
3801+
exp := simplifyBinaryCommutativeWork(op,lhs,rhs);
3802+
else
3803+
exp := simplifyBinaryCommutativeWork(op,rhs,lhs);
3804+
end try;
38083805
end simplifyBinaryCommutative;
38093806

38103807
protected function simplifyBinaryCommutativeWork
@@ -3822,50 +3819,42 @@ algorithm
38223819
Real r1,r2,r3;
38233820
/* sin(2*x) = 2*sin(x)*cos(x) */
38243821
case (DAE.MUL(_),DAE.CALL(path=Absyn.IDENT("sin"),expLst={e1}),DAE.CALL(path=Absyn.IDENT("cos"),expLst={e2}))
3822+
guard Expression.expEqual(e1,e2)
38253823
equation
3826-
true = Expression.expEqual(e1,e2);
38273824
e = DAE.BINARY(DAE.RCONST(2.0),DAE.MUL(DAE.T_REAL_DEFAULT),e1);
38283825
e = Expression.makePureBuiltinCall("sin",{e},DAE.T_REAL_DEFAULT);
38293826
then DAE.BINARY(DAE.RCONST(0.5),DAE.MUL(DAE.T_REAL_DEFAULT),e);
38303827

38313828
/* sin^2(x)+cos^2(x) = 1 */
38323829
case (DAE.ADD(_),DAE.BINARY(DAE.CALL(path=Absyn.IDENT("sin"),expLst={e1}),DAE.POW(DAE.T_REAL()),DAE.RCONST(real = 2.0)),
38333830
DAE.BINARY(DAE.CALL(path=Absyn.IDENT("cos"),expLst={e2}),DAE.POW(DAE.T_REAL()),DAE.RCONST(real = 2.0)))
3834-
equation
3835-
true = Expression.expEqual(e1,e2);
3831+
guard Expression.expEqual(e1,e2)
38363832
then DAE.RCONST(1.0);
38373833

38383834
// tan(e)*cos(e) = sin(e)
38393835
case(DAE.MUL(tp),DAE.CALL(path=Absyn.IDENT("tan"),expLst={e1}),DAE.CALL(path=Absyn.IDENT("cos"),expLst={e2}))
3840-
equation
3841-
true = Expression.expEqual(e1,e2);
3836+
guard Expression.expEqual(e1,e2)
38423837
then Expression.makePureBuiltinCall("sin",{e1},tp);
38433838

3844-
/* sinh^2(x)+cosh^2(x) = 1 */
3845-
case (DAE.ADD(_),DAE.BINARY(DAE.CALL(path=Absyn.IDENT("sinh"),expLst={e1}),DAE.POW(DAE.T_REAL()),DAE.RCONST(real = 2.0)),
3846-
DAE.BINARY(DAE.CALL(path=Absyn.IDENT("cosh"),expLst={e2}),DAE.POW(DAE.T_REAL()),DAE.RCONST(real = 2.0)))
3847-
equation
3848-
true = Expression.expEqual(e1,e2);
3839+
/* cosh^2(x)-sinh^2(x) = 1 */
3840+
case (DAE.ADD(_),DAE.BINARY(DAE.CALL(path=Absyn.IDENT("cosh"),expLst={e1}),DAE.POW(DAE.T_REAL()),DAE.RCONST(real = 2.0)),
3841+
DAE.UNARY(operator = DAE.UMINUS(),exp=DAE.BINARY(DAE.CALL(path=Absyn.IDENT("sinh"),expLst={e2}),DAE.POW(DAE.T_REAL()),DAE.RCONST(real = 2.0))))
3842+
guard Expression.expEqual(e1,e2)
38493843
then DAE.RCONST(1.0);
38503844

38513845
// tanh(e)*cosh(e) = sinh(e)
38523846
case(DAE.MUL(tp),DAE.CALL(path=Absyn.IDENT("tanh"),expLst={e1}),DAE.CALL(path=Absyn.IDENT("cosh"),expLst={e2}))
3853-
equation
3854-
true = Expression.expEqual(e1,e2);
3847+
guard Expression.expEqual(e1,e2)
38553848
then Expression.makePureBuiltinCall("sinh",{e1},tp);
38563849

38573850
// a+(-b)
38583851
case (DAE.ADD(ty = tp),e1,DAE.UNARY(operator = DAE.UMINUS(),exp = e2))
3859-
equation
3860-
e = DAE.BINARY(e1,DAE.SUB(tp),e2);
3861-
then e;
3852+
then DAE.BINARY(e1,DAE.SUB(tp),e2);
38623853

38633854
// a + ((-b) op2 c) = a - (b op2 c)
38643855
case (DAE.ADD(ty = tp),e1,DAE.BINARY(DAE.UNARY(operator = DAE.UMINUS(),exp = e2), op2, e3))
3865-
equation
3866-
true = Expression.isMulOrDiv(op2);
3867-
e = DAE.BINARY(e1, DAE.SUB(tp), DAE.BINARY(e2,op2,e3));
3868-
then e;
3856+
guard Expression.isMulOrDiv(op2)
3857+
then DAE.BINARY(e1, DAE.SUB(tp), DAE.BINARY(e2,op2,e3));
38693858

38703859
// Commutative
38713860
// (-a)+b = b + (-a)
@@ -3876,10 +3865,8 @@ algorithm
38763865

38773866
// 0+e => e
38783867
case (DAE.ADD(),e1,e2)
3879-
equation
3880-
true = Expression.isZero(e1);
3881-
then
3882-
e2;
3868+
guard Expression.isZero(e1)
3869+
then e2;
38833870

38843871
// e1*(e2/e3) => (e1e2)/e3
38853872
case (DAE.MUL(ty = tp),e1,DAE.BINARY(exp1 = e2,operator = DAE.DIV(ty = tp2),exp2 = e3))
@@ -3889,25 +3876,18 @@ algorithm
38893876

38903877
// 0 * a = 0
38913878
case (DAE.MUL(),_,e2)
3892-
equation
3893-
true = Expression.isZero(e2);
3894-
then
3895-
e2;
3879+
guard Expression.isZero(e2)
3880+
then e2;
38963881

38973882
// 1 * a = a
38983883
case (DAE.MUL(),e1,e2)
3899-
equation
3900-
true = Expression.isConstOne(e2);
3901-
then
3902-
e1;
3884+
guard Expression.isConstOne(e2)
3885+
then e1;
39033886

39043887
// -1 * a = -a
39053888
case (DAE.MUL(ty = ty),e1,e2)
3906-
equation
3907-
true = Expression.isConstMinusOne(e2);
3908-
e = DAE.UNARY(DAE.UMINUS(ty),e1);
3909-
then
3910-
e;
3889+
guard Expression.isConstMinusOne(e2)
3890+
then DAE.UNARY(DAE.UMINUS(ty),e1);
39113891

39123892
// done in simplifyBinary
39133893
// -a * -b = a * b
@@ -3919,42 +3899,27 @@ algorithm
39193899

39203900
// (e * e1) * e => e1*e^2
39213901
case (DAE.MUL(),DAE.BINARY(e2,DAE.MUL(ty),e3),e1)
3922-
equation
3923-
true = Expression.expEqual(e2,e1);
3924-
e = DAE.BINARY(e1,DAE.POW(ty),DAE.RCONST(2.0) );
3925-
then
3926-
DAE.BINARY(e3,DAE.MUL(ty),e);
3902+
guard Expression.expEqual(e2,e1)
3903+
then DAE.BINARY(e3,DAE.MUL(ty),DAE.BINARY(e1,DAE.POW(ty),DAE.RCONST(2.0)));
39273904

39283905
// e * (e1 * e) => e1*e^2
39293906
case (DAE.MUL(),e1,DAE.BINARY(e2,DAE.MUL(ty),e3))
3930-
equation
3931-
true = Expression.expEqual(e1,e3);
3932-
e = DAE.BINARY(e1,DAE.POW(ty),DAE.RCONST(2.0) );
3933-
then
3934-
DAE.BINARY(e2,DAE.MUL(ty),e);
3907+
guard Expression.expEqual(e1,e3)
3908+
then DAE.BINARY(e2,DAE.MUL(ty),DAE.BINARY(e1,DAE.POW(ty),DAE.RCONST(2.0)));
39353909

39363910

39373911
// r1 * (r2 * e) => (r1*r2)*e
39383912
case (DAE.MUL(),DAE.RCONST(real = r1),DAE.BINARY(DAE.RCONST(real = r2),DAE.MUL(DAE.T_REAL()),e2))
3939-
equation
3940-
r3 = r1 * r2;
3941-
then
3942-
DAE.BINARY(DAE.RCONST(r3),DAE.MUL(DAE.T_REAL_DEFAULT),e2);
3913+
then DAE.BINARY(DAE.RCONST(r1 * r2),DAE.MUL(DAE.T_REAL_DEFAULT),e2);
39433914

39443915
// r1 * (e * r2) => (r1*r2)*e
39453916
case (DAE.MUL(),DAE.RCONST(real = r1),DAE.BINARY(e2,DAE.MUL(DAE.T_REAL()),DAE.RCONST(real = r2)))
3946-
equation
3947-
r3 = r1 * r2;
3948-
then
3949-
DAE.BINARY(DAE.RCONST(r3),DAE.MUL(DAE.T_REAL_DEFAULT),e2);
3917+
then DAE.BINARY(DAE.RCONST(r1 * r2),DAE.MUL(DAE.T_REAL_DEFAULT),e2);
39503918

39513919
// |e1| /e1 = e1/|e1| => sign(e1)
39523920
case(DAE.DIV(ty),DAE.CALL(path=Absyn.IDENT("abs"),expLst={e1}),e2)
3953-
equation
3954-
true = Expression.expEqual(e1,e2);
3955-
res = Expression.makePureBuiltinCall("sign",{e1},ty);
3956-
then
3957-
res;
3921+
guard Expression.expEqual(e1,e2)
3922+
then Expression.makePureBuiltinCall("sign",{e1},ty);
39583923

39593924
// SUB is not commutative
39603925
// (e*e1) - (e*e2) => e*(e1-e2)
@@ -3973,8 +3938,8 @@ algorithm
39733938
// e2 + (e*e2) = (1.0 + e)*e2
39743939
// e2 + (e2*e) = (1.0 + e)*e2;
39753940
case (op1 as DAE.ADD(), e1, DAE.BINARY(e2, op2 as DAE.MUL(),e3))
3941+
guard not Expression.isConstValue(e1)
39763942
equation
3977-
false = Expression.isConstValue(e1);
39783943
if Expression.expEqual(e1,e3)
39793944
then
39803945
exp = DAE.BINARY(e1,op2,DAE.BINARY(DAE.RCONST(1.0),op1,e2));
@@ -3990,20 +3955,16 @@ algorithm
39903955

39913956
// sqrt(e) * e => e^1.5
39923957
case (DAE.MUL(),DAE.CALL(path=Absyn.IDENT("sqrt"),expLst={e1}),e2)
3993-
equation
3994-
true = Expression.expEqual(e1,e2);
3995-
then
3996-
DAE.BINARY(e1,DAE.POW(DAE.T_REAL_DEFAULT),DAE.RCONST(1.5));
3958+
guard Expression.expEqual(e1,e2)
3959+
then DAE.BINARY(e1,DAE.POW(DAE.T_REAL_DEFAULT),DAE.RCONST(1.5));
39973960
// sqrt(e) * e^r => e^(r+0.5)
39983961
case (DAE.MUL(),DAE.CALL(path=Absyn.IDENT("sqrt"),expLst={e1}),DAE.BINARY(e2,DAE.POW(),e))
3999-
equation
4000-
true = Expression.expEqual(e1,e2);
4001-
then
4002-
DAE.BINARY(e1,DAE.POW(DAE.T_REAL_DEFAULT),DAE.BINARY(e,DAE.ADD(DAE.T_REAL_DEFAULT),DAE.RCONST(0.5)));
3962+
guard Expression.expEqual(e1,e2)
3963+
then DAE.BINARY(e1,DAE.POW(DAE.T_REAL_DEFAULT),DAE.BINARY(e,DAE.ADD(DAE.T_REAL_DEFAULT),DAE.RCONST(0.5)));
40033964
// x*x^y => x^(y+1)
40043965
case (DAE.MUL(),e1,DAE.BINARY(e3, op1 as DAE.POW(tp),e4))
3966+
guard Expression.expEqual(e1,e3)
40053967
equation
4006-
true = Expression.expEqual(e1,e3);
40073968
e = Expression.makeConstOne(tp);
40083969
then
40093970
DAE.BINARY(e1,op1,DAE.BINARY(e,DAE.ADD(tp),e4));

0 commit comments

Comments
 (0)