Skip to content

Commit

Permalink
- implement pow operator for matrix
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@5286 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Apr 9, 2010
1 parent 0d5d932 commit 8f5cebc
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
92 changes: 92 additions & 0 deletions Compiler/Exp.mo
Expand Up @@ -2039,6 +2039,15 @@ algorithm
res = simplifyVectorBinary(e1, DAE.DIV(tp), e2);
then
res;
case (e1,DAE.POW_ARR(ty = _),e2)
equation
tp = typeof(e1);
e1 = simplify1(e1);
e2 = simplify1(e2);
a1 = simplifyMatrixPow(e1, tp, e2);
res = simplify1(a1);
then
res;
case (e1,DAE.POW_ARR2(ty = _),e2)
equation
tp = typeof(e1);
Expand Down Expand Up @@ -2562,6 +2571,89 @@ algorithm
end matchcontinue;
end simplifyMatrixBinary1;

protected function simplifyMatrixPow
"function: simplifyMatrixPow
author: Frenkel TUD
Simplifies matrix powers."
input Exp inExp1;
input Type inType;
input Exp inExp2;
output Exp outExp;
algorithm
outExp:=
matchcontinue (inExp1,inType,inExp2)
local
list<list<tuple<Exp, Boolean>>> expl_1,expl1,expl2;
list<tuple<Exp, Boolean>> el;
Type tp1,tp;
Integer size1,i,i_1;
list<Integer> range;
Exp e,m,res;
/* A^0=I */
case (m as DAE.MATRIX(ty = tp1,integer = size1,scalar = expl1),tp,
DAE.ICONST(integer = i))
equation
0=i;
el = Util.listFill((DAE.RCONST(0.0),true),size1);
expl2 = Util.listFill(el,size1);
range = Util.listIntRange2(0,size1-1);
expl_1 = simplifyMatrixPow1(range,expl2,(DAE.RCONST(1.0),true));
then
DAE.MATRIX(tp1,size1,expl_1);
/* A^1=A */
case (m as DAE.MATRIX(ty = tp1,integer = size1,scalar = expl1),tp,
DAE.ICONST(integer = i))
equation
1=i;
then
m;
/* A^i */
case (m as DAE.MATRIX(ty = tp1,integer = size1,scalar = expl1),tp,
DAE.ICONST(integer = i))
equation
true = 1 < i;
i_1 = i - 1;
e = simplifyMatrixPow(m,tp1,DAE.ICONST(i_1));
res = simplifyMatrixProduct(m,e);
then
res;
end matchcontinue;
end simplifyMatrixPow;

protected function simplifyMatrixPow1
"function: simplifyMatrixPow1
author: Frenkel TUD
Simplifies matrix powers."
input list<Integer> inRange;
input list<list<tuple<Exp, Boolean>>> inMatrix;
input tuple<Exp, Boolean> inValue;
output list<list<tuple<Exp, Boolean>>> outMatrix;
algorithm
outMatrix:=
matchcontinue (inRange,inMatrix,inValue)
local
list<list<tuple<Exp, Boolean>>> rm,rm1;
list<tuple<Exp, Boolean>> row,row1;
tuple<Exp, Boolean> e;
Integer i;
list<Integer> rr;
case ({},{},e)
then
{};
case (i::{},row::{},e)
equation
row1 = Util.listReplaceAt(e,i,row);
then
{row1};
case (i::rr,row::rm,e)
equation
row1 = Util.listReplaceAt(e,i,row);
rm1 = simplifyMatrixPow1(rr,rm,e);
then
row1::rm1;
end matchcontinue;
end simplifyMatrixPow1;

protected function simplifyMatrixProduct
"function: simplifyMatrixProduct
author: PA
Expand Down
9 changes: 9 additions & 0 deletions Compiler/Static.mo
Expand Up @@ -12212,6 +12212,15 @@ algorithm
Error.addMessage(Error.INCOMPATIBLE_TYPES, {"vector elementwise division",t1_str,t2_str});
then
fail();
/* Matrix[n,m]^i */
case (DAE.POW_ARR(ty = _),{typ1,typ2},_)
equation
2 = nDims(typ1);
n = dimSize(typ1, 1);
m1 = dimSize(typ1, 2);
equality(n = m1);
then
typ1;

case (DAE.POW_ARR2(ty = _),{typ1,typ2},rtype)
equation
Expand Down

0 comments on commit 8f5cebc

Please sign in to comment.