Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- Make linspace simplify better


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@14551 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Dec 27, 2012
1 parent 49e0f97 commit d248ee7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
45 changes: 45 additions & 0 deletions Compiler/FrontEnd/Expression.mo
Expand Up @@ -1466,6 +1466,33 @@ algorithm
DAE.ARRAY(array=es) := e;
end getArrayContents;

public function getArrayOrRangeContents "returns the list of expressions in the array"
input DAE.Exp e;
output list<DAE.Exp> es;
algorithm
es := match e
local
Boolean bstart,bstep,bstop;
Integer istart,istep,istop;
Real rstart,rstep,rstop;
case DAE.ARRAY(array=es) then es;
case DAE.RANGE(DAE.T_BOOL(varLst = _), DAE.BCONST(bstart), NONE(), DAE.BCONST(bstop))
then List.map(ExpressionSimplify.simplifyRangeBool(bstart, bstop), makeBoolExp);

case DAE.RANGE(DAE.T_INTEGER(varLst = _),DAE.ICONST(istart),NONE(),DAE.ICONST(istop))
then List.map(ExpressionSimplify.simplifyRange(istart,1,istop), makeIntegerExp);

case DAE.RANGE(DAE.T_INTEGER(varLst = _),DAE.ICONST(istart),SOME(DAE.ICONST(istep)),DAE.ICONST(istop))
then List.map(ExpressionSimplify.simplifyRange(istart,istep,istop), makeIntegerExp);

case DAE.RANGE(DAE.T_REAL(varLst = _),DAE.RCONST(rstart),NONE(),DAE.RCONST(rstop))
then List.map(ExpressionSimplify.simplifyRangeReal(rstart,1.0,rstop), makeRealExp);

case DAE.RANGE(DAE.T_REAL(varLst = _),DAE.RCONST(rstart),SOME(DAE.RCONST(rstep)),DAE.RCONST(rstop))
then List.map(ExpressionSimplify.simplifyRangeReal(rstart,rstep,rstop), makeRealExp);
end match;
end getArrayOrRangeContents;

public function get2dArrayOrMatrixContent "returns the list of expressions in the array"
input DAE.Exp e;
output list<list<DAE.Exp>> outExps;
Expand Down Expand Up @@ -3362,6 +3389,24 @@ algorithm
e := DAE.ICONST(i);
end makeIntegerExp;

public function makeRealExp
"Creates an integer constant expression given the integer input."
input Real r;
output DAE.Exp e;
annotation(__OpenModelica_EarlyInline = true);
algorithm
e := DAE.RCONST(r);
end makeRealExp;

public function makeBoolExp
"Creates an integer constant expression given the integer input."
input Boolean b;
output DAE.Exp e;
annotation(__OpenModelica_EarlyInline = true);
algorithm
e := DAE.BCONST(b);
end makeBoolExp;

public function makeConstOne
"function makeConstOne
author: PA
Expand Down
7 changes: 4 additions & 3 deletions Compiler/FrontEnd/ExpressionSimplify.mo
Expand Up @@ -1398,7 +1398,7 @@ algorithm
equation
e2 = Util.if_(intGt(i,j),e,e1);
then e2;

end matchcontinue;
end simplifyBuiltinConstantCalls;

Expand Down Expand Up @@ -4535,7 +4535,7 @@ protected function simplifyReduction
algorithm
outValue := matchcontinue (inReduction,b)
local
DAE.Exp expr, cref;
DAE.Exp expr, cref, range;
DAE.Ident iter_name;
list<DAE.Exp> values;
Option<Values.Value> defaultValue;
Expand All @@ -4561,7 +4561,8 @@ algorithm
case (DAE.REDUCTION(reductionInfo = DAE.REDUCTIONINFO(path = Absyn.IDENT(str), exprType = ty, defaultValue = defaultValue, foldExp = foldExp), expr = expr, iterators = iterators),_)
equation
true = listMember(str,{"array","min","max","sum","product"});
{DAE.REDUCTIONITER(id = iter_name, guardExp = NONE(), exp = DAE.ARRAY(array = values))} = iterators;
{DAE.REDUCTIONITER(id = iter_name, guardExp = NONE(), exp = range)} = iterators;
values = Expression.getArrayOrRangeContents(range);
// TODO: Use foldExp
ety = Types.simplifyType(ty);
cref = DAE.CREF(DAE.CREF_IDENT(iter_name, ety, {}), ety);
Expand Down
5 changes: 3 additions & 2 deletions Compiler/FrontEnd/ModelicaBuiltin.mo
Expand Up @@ -304,11 +304,12 @@ function linspace
input Real x2 "end";
input Integer n "number";
output Real v[n];
annotation(Documentation(info="<html>

annotation(__OpenModelica_EarlyInline=true,Documentation(info="<html>
See <a href=\"modelica://ModelicaReference.Operators.'linspace()'\">linspace()</a>
</html>"));
algorithm
assert(n >= 2, "linspace requires n>=2 but got " + String(n));
// assert(n >= 2, "linspace requires n>=2 but got " + String(n));
v := {x1 + (x2-x1)*(i-1)/(n-1) for i in 1:n};
end linspace;

Expand Down

0 comments on commit d248ee7

Please sign in to comment.