Skip to content

Commit

Permalink
Added builtin cross function (elabBuiltinCross)
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@2565 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Peter Aronsson committed Oct 5, 2006
1 parent b4a3068 commit 3656007
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 21 deletions.
72 changes: 58 additions & 14 deletions Compiler/Derive.mo
Expand Up @@ -59,7 +59,7 @@ public import OpenModelica.Compiler.DAELow;

public import OpenModelica.Compiler.Exp;

protected import OpenModelica.Compiler.Absyn;
public import OpenModelica.Compiler.Absyn;

protected import OpenModelica.Compiler.Util;

Expand Down Expand Up @@ -417,14 +417,18 @@ algorithm
Exp.BINARY(Exp.BINARY(e1,Exp.MUL(tp),e2_1),Exp.ADD(tp),
Exp.BINARY(e1_1,Exp.MUL(tp),e2));

case (Exp.BINARY(exp1 = e1,operator = Exp.DIV(ty = tp),exp2 = e2),tv) /* (f\'g - fg\' ) / g^2 */
case (Exp.BINARY(exp1 = e1,operator = Exp.DIV(ty = tp),exp2 = e2),tv) /* (f'g - fg' ) / g^2 */
equation
e1_1 = differentiateExp(e1, tv);
e2_1 = differentiateExp(e2, tv);
then
Exp.BINARY(
Exp.BINARY(Exp.BINARY(e1_1,Exp.MUL(tp),e2),Exp.SUB(tp),
Exp.BINARY(e1,Exp.MUL(tp),e2_1)),Exp.DIV(tp),Exp.BINARY(e2,Exp.MUL(tp),e2));
Exp.BINARY(
Exp.BINARY(e1_1,Exp.MUL(tp),e2),
Exp.SUB(tp),
Exp.BINARY(e1,Exp.MUL(tp),e2_1)),
Exp.DIV(tp),
Exp.BINARY(e2,Exp.MUL(tp),e2));

case (Exp.UNARY(operator = op,exp = e),tv)
equation
Expand Down Expand Up @@ -632,7 +636,7 @@ algorithm
end matchcontinue;
end differentiateExp;

protected function isTanh
public function isTanh
input Absyn.Path inPath;
algorithm
_:=
Expand All @@ -642,7 +646,7 @@ algorithm
end matchcontinue;
end isTanh;

protected function isCosh
public function isCosh
input Absyn.Path inPath;
algorithm
_:=
Expand All @@ -652,7 +656,47 @@ algorithm
end matchcontinue;
end isCosh;

protected function isSinh
public function isACos
input Absyn.Path inPath;
algorithm
_:=
matchcontinue (inPath)
case (Absyn.IDENT(name = "acos")) then ();
case (Absyn.QUALIFIED(name = "Modelica",path = Absyn.QUALIFIED(name = "Math",path = Absyn.IDENT(name = "acos")))) then ();
end matchcontinue;
end isACos;

public function isASin
input Absyn.Path inPath;
algorithm
_:=
matchcontinue (inPath)
case (Absyn.IDENT(name = "asin")) then ();
case (Absyn.QUALIFIED(name = "Modelica",path = Absyn.QUALIFIED(name = "Math",path = Absyn.IDENT(name = "asin")))) then ();
end matchcontinue;
end isASin;

public function isATan
input Absyn.Path inPath;
algorithm
_:=
matchcontinue (inPath)
case (Absyn.IDENT(name = "atan")) then ();
case (Absyn.QUALIFIED(name = "Modelica",path = Absyn.QUALIFIED(name = "Math",path = Absyn.IDENT(name = "atan")))) then ();
end matchcontinue;
end isATan;

public function isATan2
input Absyn.Path inPath;
algorithm
_:=
matchcontinue (inPath)
case (Absyn.IDENT(name = "atan2")) then ();
case (Absyn.QUALIFIED(name = "Modelica",path = Absyn.QUALIFIED(name = "Math",path = Absyn.IDENT(name = "atan2")))) then ();
end matchcontinue;
end isATan2;

public function isSinh
input Absyn.Path inPath;
algorithm
_:=
Expand All @@ -662,7 +706,7 @@ algorithm
end matchcontinue;
end isSinh;

protected function isSin
public function isSin
input Absyn.Path inPath;
algorithm
_:=
Expand All @@ -672,7 +716,7 @@ algorithm
end matchcontinue;
end isSin;

protected function isCos
public function isCos
input Absyn.Path inPath;
algorithm
_:=
Expand All @@ -682,7 +726,7 @@ algorithm
end matchcontinue;
end isCos;

protected function isExp
public function isExp
input Absyn.Path inPath;
algorithm
_:=
Expand All @@ -692,7 +736,7 @@ algorithm
end matchcontinue;
end isExp;

protected function isLog
public function isLog
input Absyn.Path inPath;
algorithm
_:=
Expand All @@ -702,7 +746,7 @@ algorithm
end matchcontinue;
end isLog;

protected function isLog10
public function isLog10
input Absyn.Path inPath;
algorithm
_:=
Expand All @@ -712,7 +756,7 @@ algorithm
end matchcontinue;
end isLog10;

protected function isSqrt
public function isSqrt
input Absyn.Path inPath;
algorithm
_:=
Expand All @@ -722,7 +766,7 @@ algorithm
end matchcontinue;
end isSqrt;

protected function isTan
public function isTan
input Absyn.Path inPath;
algorithm
_:=
Expand Down
32 changes: 25 additions & 7 deletions Compiler/Exp.mo
Expand Up @@ -2908,7 +2908,7 @@ algorithm
end matchcontinue;
end inverseFactors;

protected function makeProduct "function: makeProduct
public function makeProduct "function: makeProduct
Takes a list of expressions an makes a product expression multiplying all
elements in the list.
Expand Down Expand Up @@ -2980,6 +2980,24 @@ algorithm
end matchcontinue;
end checkIfOther;

public function makeDiff "Takes two expressions and create the difference between them"
input Exp e1;
input Exp e2;
output Exp res;
algorithm
res := matchcontinue(e1,e2)
local
Type etp;
Boolean scalar;
Operator op;
case(e1,e2) equation
etp = typeof(e1);
scalar = typeBuiltin(etp);
op = Util.if_(scalar,SUB(etp),SUB_ARR(etp));
then BINARY(e1,op,e2);
end matchcontinue;
end makeDiff;

public function makeSum "function: makeSum
Takes a list of expressions an makes a sum expression adding all
Expand Down Expand Up @@ -4977,12 +4995,12 @@ algorithm
case (MUL(ty = _)) then " * ";
case (DIV(ty = _)) then " / ";
case (POW(ty = _)) then " ^ ";
case (ADD_ARR(ty = _)) then " + ";
case (SUB_ARR(ty = _)) then " - ";
case (MUL_SCALAR_ARRAY(ty = _)) then " * ";
case (MUL_ARRAY_SCALAR(ty = _)) then " * ";
case (MUL_SCALAR_PRODUCT(ty = _)) then " * ";
case (MUL_MATRIX_PRODUCT(ty = _)) then " * ";
case (ADD_ARR(ty = _)) then " +ARR ";
case (SUB_ARR(ty = _)) then " -ARR ";
case (MUL_SCALAR_ARRAY(ty = _)) then " *ARR ";
case (MUL_ARRAY_SCALAR(ty = _)) then " *ARR ";
case (MUL_SCALAR_PRODUCT(ty = _)) then " *ARR ";
case (MUL_MATRIX_PRODUCT(ty = _)) then " *ARR ";
case (DIV_ARRAY_SCALAR(ty = _)) then " / ";
end matchcontinue;
end binopSymbol1;
Expand Down
77 changes: 77 additions & 0 deletions Compiler/Static.mo
Expand Up @@ -3892,6 +3892,81 @@ algorithm
end matchcontinue;
end elabBuiltinScalar;

protected function elabBuiltinCross "
author: PA
This function handles the built in cross operator.
For example, scalar({x[1],x[2],x[3]},{y[1],y[2],y[3]})
=> {x[2]*y[3]-x[3]*y[2],x[3]*y[1]-x[1]*y[3],x[1]*y[2]-x[2]*y[1]}
"
input Env.Cache inCache;
input Env.Env inEnv;
input list<Absyn.Exp> inAbsynExpLst;
input Boolean inBoolean;
output Env.Cache outCache;
output Exp.Exp outExp;
output Types.Properties outProperties;
algorithm
(outCache,outExp,outProperties):=
matchcontinue (inCache,inEnv,inAbsynExpLst,inBoolean)
local
Exp.Exp e1,e2;
tuple<Types.TType, Option<Absyn.Path>> tp1,tp2;
Types.Const c1,c2,c;
Boolean scalar1,scalar2;
list<Env.Frame> env;
Boolean impl;
Env.Cache cache;
Absyn.Exp v1,v2;
list<Exp.Exp> expl1,expl2,expl3;
Exp.Type etp1,etp2,etp,etp3;
Types.Type eltTp;

//First, try symbolic simplification
case (cache,env,{v1,v2},impl) equation
(cache,Exp.ARRAY(etp1,scalar1,expl1),Types.PROP(tp1,c1),_) = elabExp(cache,env, v1, impl, NONE);
(cache,Exp.ARRAY(etp2,scalar2,expl2),Types.PROP(tp2,c2),_) = elabExp(cache,env, v2, impl, NONE);
{3} = Types.getDimensionSizes(tp1);
{3} = Types.getDimensionSizes(tp2);
expl3 = elabBuiltinCross2(expl1,expl2);
c = Types.constAnd(c1,c2);
etp3 = Types.elabType(tp1);
then
(cache,Exp.ARRAY(etp3,scalar1,expl3),Types.PROP(tp1,c));

//Fallback, use builtin function cross
case (cache,env,{v1,v2},impl) equation
(cache,e1,Types.PROP(tp1,c1),_) = elabExp(cache,env, v1, impl, NONE);
(cache,e2,Types.PROP(tp2,c2),_) = elabExp(cache,env, v2, impl, NONE);
{3} = Types.getDimensionSizes(tp1);
{3} = Types.getDimensionSizes(tp2);
etp = Exp.typeof(e1);
eltTp = Types.arrayElementType(tp1);
then (cache,Exp.CALL(Absyn.IDENT("cross"),{e1,e2},false,true,Exp.T_ARRAY(etp,{SOME(3)})),
Types.PROP((Types.T_ARRAY(Types.DIM(SOME(3)),eltTp),NONE),Types.C_VAR()));
end matchcontinue;
end elabBuiltinCross;

protected function elabBuiltinCross2 "help function to elabBuiltinCross"
input list<Exp.Exp> v1;
input list<Exp.Exp> v2;
output list<Exp.Exp> res;
algorithm
res := matchcontinue(v1,v2)
local Exp.Exp x1,x2,x3,y1,y2,y3,p1,p2,r1,r2,r3;

// {x[2]*y[3]-x[3]*y[2],x[3]*y[1]-x[1]*y[3],x[1]*y[2]-x[2]*y[1]}
case({x1,x2,x3},{y1,y2,y3}) equation
p1 = Exp.makeProduct({x2,y2});
p2 = Exp.makeProduct({x3,y2});
r1 = Exp.makeDiff(p1,p2);
r2 = Exp.makeDiff(Exp.makeProduct({x3,y1}),Exp.makeProduct({x1,y3}));
r3 = Exp.makeDiff(Exp.makeProduct({x1,y2}),Exp.makeProduct({x2,y1}));
then {r1,r2,r3};
end matchcontinue;
end elabBuiltinCross2;


protected function elabBuiltinVector "function: elabBuiltinVector
author: PA
Expand Down Expand Up @@ -4109,6 +4184,8 @@ algorithm
case "vector" then elabBuiltinVector;

case "scalar" then elabBuiltinScalar;

case "cross" then elabBuiltinCross;

case "dymTableTimeIni" then elabBuiltinDymtabletimeini;
end matchcontinue;
Expand Down

0 comments on commit 3656007

Please sign in to comment.