Skip to content

Commit

Permalink
added rules for sin, cos, tan, cot
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@22378 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Vitalij Ruge committed Sep 21, 2014
1 parent 76e345f commit 3475fc4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Compiler/BackEnd/Differentiate.mo
Expand Up @@ -1568,6 +1568,17 @@ algorithm
(DAE.BINARY(
DAE.BINARY(DAE.RCONST(2.0), DAE.MUL(tp), exp_1), DAE.DIV(tp),
DAE.BINARY(exp_2, DAE.ADD(tp), DAE.RCONST(1.0))), funcs);
// diff(cot(x)) = (2*der(x)/(cos(2*x)-1))
case ("cot",_,_,_,_,_)
equation
tp = Expression.typeof(exp);
(exp_1, funcs) = differentiateExp(exp, inDiffwrtCref, inInputData,inDiffType,inFuncs);
exp_2 = Expression.makePureBuiltinCall("cos", {DAE.BINARY(DAE.RCONST(2.0),DAE.MUL(tp),exp)}, tp);
then
(DAE.BINARY(
DAE.BINARY(DAE.RCONST(2.0), DAE.MUL(tp), exp_1), DAE.DIV(tp),
DAE.BINARY(exp_2, DAE.SUB(tp), DAE.RCONST(1.0))), funcs);


// der(arcsin(x)) = der(x)/sqrt(1-x^2)
case ("asin",_,_,_,_,_)
Expand Down
40 changes: 40 additions & 0 deletions Compiler/FrontEnd/ExpressionSimplify.mo
Expand Up @@ -3526,6 +3526,25 @@ algorithm
true = Expression.expEqual(e1,e2);
then DAE.RCONST(1.0);

// tan(e)*cot(e) = 1.0
case(DAE.MUL(tp),DAE.CALL(path=Absyn.IDENT("tan"),expLst={e1}),DAE.CALL(path=Absyn.IDENT("cot"),expLst={e2}))
equation
true = Expression.expEqual(e1,e2);
e = Expression.makeConstOne(tp);
then e;

// tan(e)*cos(e) = sin(e)
case(DAE.DIV(tp),DAE.CALL(path=Absyn.IDENT("tan"),expLst={e1}),DAE.CALL(path=Absyn.IDENT("cos"),expLst={e2}))
equation
true = Expression.expEqual(e1,e2);
then Expression.makePureBuiltinCall("sin",{e1},tp);

// cot(e)*sin(e) = cos(e)
case(DAE.DIV(tp),DAE.CALL(path=Absyn.IDENT("cot"),expLst={e1}),DAE.CALL(path=Absyn.IDENT("sin"),expLst={e2}))
equation
true = Expression.expEqual(e1,e2);
then Expression.makePureBuiltinCall("cos",{e1},tp);

// a+(-b)
case (DAE.ADD(ty = tp),e1,DAE.UNARY(operator = DAE.UMINUS(ty = _),exp = e2))
equation
Expand Down Expand Up @@ -4186,6 +4205,27 @@ algorithm
case (_,DAE.POW(ty = _),DAE.BINARY(e1,DAE.POW(ty = _),e2),e3,_,_)
then DAE.BINARY(e1,DAE.POW(DAE.T_REAL_DEFAULT),DAE.BINARY(e2,DAE.MUL(DAE.T_REAL_DEFAULT),e3));

// sin(e)/cos(e) => tan(e)
case(_,DAE.DIV(ty),DAE.CALL(path=Absyn.IDENT("sin"),expLst={e1}),DAE.CALL(path=Absyn.IDENT("cos"),expLst={e2}),_,_)
equation
true = Expression.expEqual(e1,e2);
then Expression.makePureBuiltinCall("tan",{e1},ty);
// cos(e)/sin(e) => cot(e)
case(_,DAE.DIV(ty),DAE.CALL(path=Absyn.IDENT("cos"),expLst={e1}),DAE.CALL(path=Absyn.IDENT("sin"),expLst={e2}),_,_)
equation
true = Expression.expEqual(e1,e2);
then Expression.makePureBuiltinCall("cot",{e1},ty);
// e1/cot(e2) => e1*tan(e2)
case(_,DAE.DIV(ty),e1,DAE.CALL(path=Absyn.IDENT("cot"),expLst={e2}),_,_)
equation
e = Expression.makePureBuiltinCall("tan",{e2},ty);
then DAE.BINARY(e1,DAE.MUL(ty), e);
// e1/tan(e2) => e1*cot(e2)
case(_,DAE.DIV(ty),e1,DAE.CALL(path=Absyn.IDENT("tan"),expLst={e2}),_,_)
equation
e= Expression.makePureBuiltinCall("cot",{e2},ty);
then DAE.BINARY(e1,DAE.MUL(ty), e);

// e1 -e2 => -e1 e2
// Note: This rule is *not* commutative
case (_,DAE.MUL(ty = ty),e1,DAE.UNARY(operator = DAE.UMINUS(ty = _),exp = e2),_,_)
Expand Down

0 comments on commit 3475fc4

Please sign in to comment.