Skip to content

Commit

Permalink
Changed builtin functions asin,acos and atan to arcsin, arccos and ar…
Browse files Browse the repository at this point in the history
…ctan (same as in Dymola).

Added eliminationLevel = 3 for removing only equations on the form a=constant.
Added der(arccos),der(arcsin) and der(arctan) to Derive.mo
Fixed performance problem with Exp.simplify.
Fixed bug with matrix operation a*[1,2;3,4] in Exp.simplify.
Fixed bug in tranforming a flat-parsed Absyn to normal Absyn (Interactive.mo).

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@2621 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Peter Aronsson committed Dec 6, 2006
1 parent 5b2ad2a commit 5249348
Show file tree
Hide file tree
Showing 10 changed files with 340 additions and 152 deletions.
6 changes: 3 additions & 3 deletions Compiler/Builtin.mo
Expand Up @@ -2557,9 +2557,9 @@ algorithm
env = Env.extendFrameT(env, "sin", real2real) "Not in the report" ;
env = Env.extendFrameT(env, "cos", real2real);
env = Env.extendFrameT(env, "tan", real2real);
env = Env.extendFrameT(env, "asin", real2real);
env = Env.extendFrameT(env, "acos", real2real);
env = Env.extendFrameT(env, "atan", real2real);
env = Env.extendFrameT(env, "arcsin", real2real);
env = Env.extendFrameT(env, "arccos", real2real);
env = Env.extendFrameT(env, "arctan", real2real);
env = Env.extendFrameT(env, "exp", real2real);
env = Env.extendFrameT(env, "log", real2real);
env = Env.extendFrameT(env, "log10", real2real);
Expand Down
6 changes: 3 additions & 3 deletions Compiler/Ceval.mo
Expand Up @@ -221,9 +221,9 @@ algorithm
case "sin" then cevalBuiltinSin;
case "cos" then cevalBuiltinCos;
case "log" then cevalBuiltinLog;
case "asin" then cevalBuiltinAsin;
case "acos" then cevalBuiltinAcos;
case "atan" then cevalBuiltinAtan;
case "arcsin" then cevalBuiltinAsin;
case "arccos" then cevalBuiltinAcos;
case "arctan" then cevalBuiltinAtan;
case "tan" then cevalBuiltinTan;
case "integer" then cevalBuiltinInteger;
case "mod" then cevalBuiltinMod;
Expand Down
18 changes: 17 additions & 1 deletion Compiler/DAELow.mo
Expand Up @@ -2649,25 +2649,29 @@ algorithm
case (EQUATION(e1 as Exp.CREF(componentRef = _),e2 as Exp.CREF(componentRef = _)),swap)
equation
true = RTOpts.eliminationLevel() > 0;
true = RTOpts.eliminationLevel() <> 3;
then (e1,e2);
// a-b = 0
case (EQUATION(Exp.BINARY(e1 as Exp.CREF(_,_),Exp.SUB(_),e2 as Exp.CREF(_,_)),e),false)
equation
true = RTOpts.eliminationLevel() > 0;
true = RTOpts.eliminationLevel() <> 3;
true = Exp.isZero(e);
then
(e1,e2);
// a-b = 0 swap
case (EQUATION(Exp.BINARY(e1 as Exp.CREF(_,_),Exp.SUB(_),e2 as Exp.CREF(_,_)),e),true)
equation
true = RTOpts.eliminationLevel() > 0;
true = RTOpts.eliminationLevel() <> 3;
true = Exp.isZero(e);
then
(e2,e1);
// 0 = a-b
case (EQUATION(e,Exp.BINARY(e1 as Exp.CREF(_,_),Exp.SUB(_),e2 as Exp.CREF(_,_))),false)
equation
true = RTOpts.eliminationLevel() > 0;
true = RTOpts.eliminationLevel() <> 3;
true = Exp.isZero(e);
then
(e1,e2);
Expand All @@ -2676,78 +2680,90 @@ algorithm
case (EQUATION(e,Exp.BINARY(e1 as Exp.CREF(_,_),Exp.SUB(_),e2 as Exp.CREF(_,_))),false)
equation
true = RTOpts.eliminationLevel() > 0;
true = RTOpts.eliminationLevel() <> 3;
true = Exp.isZero(e);
then
(e2,e1);

// a + b = 0
case (EQUATION(Exp.BINARY(e1 as Exp.CREF(_,_),Exp.ADD(t),e2 as Exp.CREF(_,_)),e),false) equation
true = RTOpts.eliminationLevel() > 1;
true = RTOpts.eliminationLevel() > 1;
true = RTOpts.eliminationLevel() <> 3;
true = Exp.isZero(e);
then (e1,Exp.UNARY(Exp.UMINUS(t),e2));

// a + b = 0 swap
case (EQUATION(Exp.BINARY(e1 as Exp.CREF(_,_),Exp.ADD(t),e2 as Exp.CREF(_,_)),e),true) equation
true = RTOpts.eliminationLevel() > 1;
true = RTOpts.eliminationLevel() <> 3;
true = Exp.isZero(e);
then (e2,Exp.UNARY(Exp.UMINUS(t),e1));

// 0 = a+b
case (EQUATION(e,Exp.BINARY(e1 as Exp.CREF(_,_),Exp.ADD(t),e2 as Exp.CREF(_,_))),false) equation
true = RTOpts.eliminationLevel() > 1;
true = RTOpts.eliminationLevel() <> 3;
true = Exp.isZero(e);
then (e1,Exp.UNARY(Exp.UMINUS(t),e2));

// 0 = a+b swap
case (EQUATION(e,Exp.BINARY(e1 as Exp.CREF(_,_),Exp.ADD(t),e2 as Exp.CREF(_,_))),true) equation
true = RTOpts.eliminationLevel() > 1;
true = RTOpts.eliminationLevel() <> 3;
true = Exp.isZero(e);
then (e2,Exp.UNARY(Exp.UMINUS(t),e1));

// a = -b
case (EQUATION(e1 as Exp.CREF(_,_),e2 as Exp.UNARY(Exp.UMINUS(_),Exp.CREF(_,_))),swap)
equation
true = RTOpts.eliminationLevel() > 1;
true = RTOpts.eliminationLevel() <> 3;
then (e1,e2);

// -a = b => a = -b
case (EQUATION(Exp.UNARY(Exp.UMINUS(t),e1 as Exp.CREF(_,_)),e2 as Exp.CREF(_,_)),swap)
equation
true = RTOpts.eliminationLevel() > 1;
true = RTOpts.eliminationLevel() <> 3;
then (e1,Exp.UNARY(Exp.UMINUS(t),e2));

// -b - a = 0 => a = -b
case (EQUATION(Exp.BINARY(e2 as Exp.UNARY(Exp.UMINUS(_),Exp.CREF(_,_)),Exp.SUB(_),e1 as Exp.CREF(_,_)),e),false)
equation
true = RTOpts.eliminationLevel() > 1;
true = RTOpts.eliminationLevel() <> 3;
true = Exp.isZero(e);
then (e1,e2);

// -b - a = 0 => a = -b swap
case (EQUATION(Exp.BINARY(Exp.UNARY(Exp.UMINUS(t),e2 as Exp.CREF(_,_)),Exp.SUB(_),e1 as Exp.CREF(_,_)),e),true)
equation
true = RTOpts.eliminationLevel() > 1;
true = RTOpts.eliminationLevel() <> 3;
true = Exp.isZero(e);
then (e2,Exp.UNARY(Exp.UMINUS(t),e1));

// 0 = -b - a => a = -b
case (EQUATION(e,Exp.BINARY(e2 as Exp.UNARY(Exp.UMINUS(_),Exp.CREF(_,_)),Exp.SUB(_),e1 as Exp.CREF(_,_))),false)
equation
true = RTOpts.eliminationLevel() > 1;
true = RTOpts.eliminationLevel() <> 3;
true = Exp.isZero(e);
then (e1,e2);

// 0 = -b - a => a = -b swap
case (EQUATION(e,Exp.BINARY(Exp.UNARY(Exp.UMINUS(t),e2 as Exp.CREF(_,_)),Exp.SUB(_),e1 as Exp.CREF(_,_))),true)
equation
true = RTOpts.eliminationLevel() > 1;
true = RTOpts.eliminationLevel() <> 3;
true = Exp.isZero(e);
then (e2,Exp.UNARY(Exp.UMINUS(t),e1));

// -a = -b
case (EQUATION(Exp.UNARY(Exp.UMINUS(_),e1 as Exp.CREF(_,_)),Exp.UNARY(Exp.UMINUS(_),e2 as Exp.CREF(_,_))),swap)
equation
true = RTOpts.eliminationLevel() > 1;
true = RTOpts.eliminationLevel() <> 3;
then (e1,e2);
// a = constant
case (EQUATION(e1 as Exp.CREF(_,_),e),swap) equation
Expand Down
84 changes: 74 additions & 10 deletions Compiler/Derive.mo
Expand Up @@ -83,8 +83,7 @@ algorithm
e1_1 = differentiateExpTime(e1, timevars);
e2_1 = differentiateExpTime(e2, timevars);
e1_2 = Exp.simplify(e1_1);
e2_2 = Exp.simplify(e2_1) "& Exp.simplify(e1\'\') => e1\'\' &
Exp.simplify(e2\'\') => e2\'\'" ;
e2_2 = Exp.simplify(e2_1);
then
DAELow.EQUATION(e1_2,e2_2);
case (DAELow.ALGORITHM(index = _),_)
Expand Down Expand Up @@ -163,6 +162,32 @@ algorithm
then
Exp.UNARY(Exp.UMINUS(Exp.REAL()),Exp.BINARY(e_1,Exp.MUL(Exp.REAL()),
Exp.CALL(Absyn.IDENT("sin"),{e},false,true,Exp.REAL())));

// der(arccos(x)) = -der(x)/sqrt(1-x^2)
case (Exp.CALL(path = fname,expLst = {e}),timevars)
equation
isACos(fname);
e_1 = differentiateExpTime(e, timevars) ;
then
Exp.UNARY(Exp.UMINUS(Exp.REAL()),Exp.BINARY(e_1,Exp.DIV(Exp.REAL()),
Exp.CALL(Absyn.IDENT("sqrt"),{Exp.BINARY(Exp.RCONST(1.0),Exp.SUB(Exp.REAL()),Exp.BINARY(e,Exp.MUL(Exp.REAL()),e))},false,true,Exp.REAL())));

// der(arcsin(x)) = der(x)/sqrt(1-x^2)
case (Exp.CALL(path = fname,expLst = {e}),timevars)
equation
isASin(fname);
e_1 = differentiateExpTime(e, timevars) ;
then
Exp.BINARY(e_1,Exp.DIV(Exp.REAL()),
Exp.CALL(Absyn.IDENT("sqrt"),{Exp.BINARY(Exp.RCONST(1.0),Exp.SUB(Exp.REAL()),Exp.BINARY(e,Exp.MUL(Exp.REAL()),e))},false,true,Exp.REAL()));

// der(arctan(x)) = der(x)/1+x^2
case (Exp.CALL(path = fname,expLst = {e}),timevars)
equation
isATan(fname);
e_1 = differentiateExpTime(e, timevars) ;
then
Exp.BINARY(e_1,Exp.DIV(Exp.REAL()),Exp.BINARY(Exp.RCONST(1.0),Exp.ADD(Exp.REAL()),Exp.BINARY(e,Exp.MUL(Exp.REAL()),e)));

case (Exp.CALL(path = fname,expLst = {e}),timevars)
equation
Expand All @@ -179,7 +204,6 @@ algorithm
then
Exp.BINARY(e_1,Exp.DIV(Exp.REAL()),e);

// *** Addition by JA 20060621
case (Exp.CALL(path = fname,expLst = {e},tuple_ = false,builtin = true),timevars)
equation
isLog(fname);
Expand All @@ -198,7 +222,6 @@ algorithm
then
exp;

// *** End of addition by JA 20060621
case ((e as Exp.CREF(componentRef = cr,ty = tp)),timevars) /* list_member(cr,timevars) => false */ then Exp.RCONST(0.0);
case (Exp.BINARY(exp1 = e1,operator = Exp.ADD(ty = tp),exp2 = e2),tv)
equation
Expand Down Expand Up @@ -329,7 +352,7 @@ algorithm
local
Real rval;
Exp.ComponentRef cr,crx,tv;
Exp.Exp e,e1_1,e2_1,e1,e2,const_one,d_e1,exp,e_1,exp_1,e3_1,e3;
Exp.Exp e,e1_1,e2_1,e1,e2,const_one,d_e1,d_e2,exp,e_1,exp_1,e3_1,e3;
Exp.Type tp;
Absyn.Path a,fname;
Boolean b,c;
Expand Down Expand Up @@ -370,7 +393,7 @@ algorithm

case (Exp.BINARY(exp1 = (e1 as Exp.CREF(componentRef = cr)),operator = Exp.POW(ty = tp),exp2 = e2),tv) /* ax^(a-1) */
equation
true = Exp.crefEqual(cr, tv) "a^x => ax^(a-1)" ;
true = Exp.crefEqual(cr, tv) "x^a => ax^(a-1)" ;
false = Exp.expContains(e2, Exp.CREF(tv,tp));
const_one = differentiateExp(Exp.CREF(tv,tp), tv);
then
Expand All @@ -387,6 +410,17 @@ algorithm
Exp.BINARY(e1,Exp.POW(tp),Exp.BINARY(e2,Exp.SUB(tp),const_one)));
then
exp;

case (e as Exp.BINARY(exp1 = e1,operator = Exp.POW(ty = tp),exp2 = e2),tv) /* a^x => a^x * log(A) */
equation
false = Exp.expContains(e1, Exp.CREF(tv,tp));
true = Exp.expContains(e2,Exp.CREF(tv,tp));
d_e2 = differentiateExp(e2, tv);
exp = Exp.BINARY(d_e2,Exp.MUL(tp),
Exp.BINARY(e,Exp.MUL(tp),Exp.CALL(Absyn.IDENT("log"),{e1},false,true,tp))
);
then
exp;

case (Exp.BINARY(exp1 = (e1 as Exp.CALL(path = (a as Absyn.IDENT(name = "der")),expLst = {(exp as Exp.CREF(componentRef = cr))},tuple_ = b,builtin = c,ty=ctp)),operator = Exp.POW(ty = tp),exp2 = e2),tv) /* ax^(a-1) */
local Exp.Type ctp;
Expand Down Expand Up @@ -477,6 +511,35 @@ algorithm
Exp.UNARY(Exp.UMINUS(Exp.REAL()),
Exp.CALL(Absyn.IDENT("sin"),{exp},b,c,tp)),Exp.MUL(Exp.REAL()),exp_1);

// der(arccos(x)) = -der(x)/sqrt(1-x^2)
case (Exp.CALL(path = fname,expLst = {e}),tv)
equation
isACos(fname);
true = Exp.expContains(e, Exp.CREF(tv,Exp.REAL()));
e_1 = differentiateExp(e, tv) ;
then
Exp.UNARY(Exp.UMINUS(Exp.REAL()),Exp.BINARY(e_1,Exp.DIV(Exp.REAL()),
Exp.CALL(Absyn.IDENT("sqrt"),{Exp.BINARY(Exp.RCONST(1.0),Exp.SUB(Exp.REAL()),Exp.BINARY(e,Exp.MUL(Exp.REAL()),e))},false,true,Exp.REAL())));

// der(arcsin(x)) = der(x)/sqrt(1-x^2)
case (Exp.CALL(path = fname,expLst = {e}),tv)
equation
isASin(fname);
true = Exp.expContains(e, Exp.CREF(tv,Exp.REAL()));
e_1 = differentiateExp(e, tv) ;
then
Exp.BINARY(e_1,Exp.DIV(Exp.REAL()),
Exp.CALL(Absyn.IDENT("sqrt"),{Exp.BINARY(Exp.RCONST(1.0),Exp.SUB(Exp.REAL()),Exp.BINARY(e,Exp.MUL(Exp.REAL()),e))},false,true,Exp.REAL()));

// der(arctan(x)) = der(x)/1+x^2
case (Exp.CALL(path = fname,expLst = {e}),tv)
equation
isATan(fname);
true = Exp.expContains(e, Exp.CREF(tv,Exp.REAL()));
e_1 = differentiateExp(e, tv) ;
then
Exp.BINARY(e_1,Exp.DIV(Exp.REAL()),Exp.BINARY(Exp.RCONST(1.0),Exp.ADD(Exp.REAL()),Exp.BINARY(e,Exp.MUL(Exp.REAL()),e)));

case (Exp.CALL(path = fname,expLst = (exp :: {}),tuple_ = b,builtin = c,ty=tp),tv)
local Exp.Type tp;
equation
Expand Down Expand Up @@ -620,6 +683,7 @@ algorithm
s = Exp.printExpStr(e);
s2 = Exp.printComponentRefStr(cr);
str = Util.stringAppendList({"differentiate_exp ",s," w.r.t:",s2," failed\n"});
//print(str);
Debug.fprint("failtrace", str);
then
fail();
Expand Down Expand Up @@ -651,7 +715,7 @@ public function isACos
algorithm
_:=
matchcontinue (inPath)
case (Absyn.IDENT(name = "acos")) then ();
case (Absyn.IDENT(name = "arccos")) then ();
case (Absyn.QUALIFIED(name = "Modelica",path = Absyn.QUALIFIED(name = "Math",path = Absyn.IDENT(name = "acos")))) then ();
end matchcontinue;
end isACos;
Expand All @@ -661,7 +725,7 @@ public function isASin
algorithm
_:=
matchcontinue (inPath)
case (Absyn.IDENT(name = "asin")) then ();
case (Absyn.IDENT(name = "arcsin")) then ();
case (Absyn.QUALIFIED(name = "Modelica",path = Absyn.QUALIFIED(name = "Math",path = Absyn.IDENT(name = "asin")))) then ();
end matchcontinue;
end isASin;
Expand All @@ -671,7 +735,7 @@ public function isATan
algorithm
_:=
matchcontinue (inPath)
case (Absyn.IDENT(name = "atan")) then ();
case (Absyn.IDENT(name = "arctan")) then ();
case (Absyn.QUALIFIED(name = "Modelica",path = Absyn.QUALIFIED(name = "Math",path = Absyn.IDENT(name = "atan")))) then ();
end matchcontinue;
end isATan;
Expand All @@ -681,7 +745,7 @@ public function isATan2
algorithm
_:=
matchcontinue (inPath)
case (Absyn.IDENT(name = "atan2")) then ();
case (Absyn.IDENT(name = "arctan2")) then ();
case (Absyn.QUALIFIED(name = "Modelica",path = Absyn.QUALIFIED(name = "Math",path = Absyn.IDENT(name = "atan2")))) then ();
end matchcontinue;
end isATan2;
Expand Down
3 changes: 1 addition & 2 deletions Compiler/Env.mo
Expand Up @@ -864,8 +864,7 @@ algorithm
bind_str = Types.printBindingStr(bind);
res = Util.stringAppendList(
{"v:",n," ",s,"(",elt_str,") [",tp_str,"] {",var_str,
"}, binding:",bind_str}) ",\", compframe:\",
frame_str" ;
"}, binding:",bind_str});
then
res;
case ((n,VAR(instantiated = (tv as Types.VAR(attributes = Types.ATTR(parameter_ = var),type_ = tp)),declaration = SOME((elt,_)),instStatus = i,env = {})))
Expand Down

0 comments on commit 5249348

Please sign in to comment.