Skip to content

Commit 72f9453

Browse files
committed
- Implemented linspace in ModelicaBuiltin.mo
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@10025 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent c7c8a9a commit 72f9453

File tree

3 files changed

+12
-136
lines changed

3 files changed

+12
-136
lines changed

Compiler/FrontEnd/Ceval.mo

Lines changed: 2 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,6 @@ algorithm
10811081
case "identity" then cevalBuiltinIdentity;
10821082
case "promote" then cevalBuiltinPromote;
10831083
case "String" then cevalBuiltinString;
1084-
case "linspace" then cevalBuiltinLinspace;
10851084
case "Integer" then cevalBuiltinIntegerEnumeration;
10861085
case "rooted" then cevalBuiltinRooted; //
10871086
case "cross" then cevalBuiltinCross;
@@ -2370,74 +2369,8 @@ algorithm
23702369
end match;
23712370
end cevalBuiltinStringFormat;
23722371

2373-
protected function cevalBuiltinLinspace "
2374-
author: PA
2375-
Evaluates the linpace function"
2376-
input Env.Cache inCache;
2377-
input Env.Env inEnv;
2378-
input list<DAE.Exp> inExpExpLst;
2379-
input Boolean inBoolean;
2380-
input Option<Interactive.SymbolTable> st;
2381-
input Msg inMsg;
2382-
output Env.Cache outCache;
2383-
output Values.Value outValue;
2384-
output Option<Interactive.SymbolTable> outST;
2385-
algorithm
2386-
(outCache,outValue,outST):=
2387-
match (inCache,inEnv,inExpExpLst,inBoolean,st,inMsg)
2388-
local
2389-
DAE.Exp x,y,n; Integer size;
2390-
Real rx,ry; list<Values.Value> valLst; Env.Cache cache; Boolean impl; Env.Env env; Msg msg;
2391-
case (cache,env,{x,y,n},impl,st,msg) equation
2392-
(cache,Values.INTEGER(size),_) = ceval(cache,env, n, impl, st,msg);
2393-
verifyLinspaceN(size,{x,y,n});
2394-
(cache,Values.REAL(rx),_) = ceval(cache,env, x, impl, st,msg);
2395-
(cache,Values.REAL(ry),_) = ceval(cache,env, y, impl, st,msg);
2396-
valLst = cevalBuiltinLinspace2(rx,ry,size,1);
2397-
then (cache,ValuesUtil.makeArray(valLst),st);
2398-
2399-
end match;
2400-
end cevalBuiltinLinspace;
2401-
2402-
protected function verifyLinspaceN "checks that n>=2 for linspace(x,y,n) "
2403-
input Integer n;
2404-
input list<DAE.Exp> expl;
2405-
algorithm
2406-
_ := matchcontinue(n,expl)
2407-
local String s; DAE.Exp x,y,nx;
2408-
case(n,_) equation
2409-
true = n >= 2;
2410-
then ();
2411-
case(_,{x,y,nx})
2412-
equation
2413-
s = "linspace("+&ExpressionDump.printExpStr(x)+&", "+&ExpressionDump.printExpStr(y)+&", "+&ExpressionDump.printExpStr(nx)+&")";
2414-
Error.addMessage(Error.LINSPACE_ILLEGAL_SIZE_ARG,{s});
2415-
then fail();
2416-
end matchcontinue;
2417-
end verifyLinspaceN;
2418-
2419-
protected function cevalBuiltinLinspace2 "Helper function to cevalBuiltinLinspace"
2420-
input Real rx;
2421-
input Real ry;
2422-
input Integer size;
2423-
input Integer i "iterator 1 <= i <= size";
2424-
output list<Values.Value> valLst;
2425-
algorithm
2426-
valLst := matchcontinue(rx,ry,size,i)
2427-
local Real r;
2428-
case(rx,ry,size,i) equation
2429-
true = i > size;
2430-
then {};
2431-
case(rx,ry,size,i) equation
2432-
r = rx +. (ry -. rx)*. intReal(i-1) /. intReal(size - 1);
2433-
valLst = cevalBuiltinLinspace2(rx,ry,size,i+1);
2434-
then Values.REAL(r)::valLst;
2435-
end matchcontinue;
2436-
end cevalBuiltinLinspace2;
2437-
2438-
protected function cevalBuiltinPrint "
2439-
author: sjoelund
2440-
Prints a String"
2372+
protected function cevalBuiltinPrint
2373+
"Prints a String to stdout"
24412374
input Env.Cache inCache;
24422375
input Env.Env inEnv;
24432376
input list<DAE.Exp> inExpExpLst;

Compiler/FrontEnd/ModelicaBuiltin.mo

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,16 @@ function homotopy
215215
external "builtin";
216216
end homotopy;
217217

218+
function linspace
219+
input Real x1 "start";
220+
input Real x2 "end";
221+
input Integer n "number";
222+
output Real v[n];
223+
algorithm
224+
assert(n >= 2, "linspace requires n>=2 but got " + String(n));
225+
v := {x1 + (x2-x1)*(i-1)/(n-1) for i in 1:n};
226+
end linspace;
227+
218228
function div = overload(OpenModelica.Internal.intDiv,OpenModelica.Internal.realDiv);
219229

220230
function mod = overload(OpenModelica.Internal.intMod,OpenModelica.Internal.realMod);
@@ -293,10 +303,6 @@ function fill
293303
external "builtin";
294304
end fill;
295305

296-
function linspace
297-
external "builtin";
298-
end linspace;
299-
300306
function noEvent
301307
external "builtin";
302308
end noEvent;

Compiler/FrontEnd/Static.mo

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -5568,68 +5568,6 @@ algorithm
55685568
end matchcontinue;
55695569
end elabBuiltinString;
55705570

5571-
protected function elabBuiltinLinspace "
5572-
author: PA
5573-
5574-
This function handles the built-in linspace function.
5575-
e.g. linspace(1,3,3) => {1,2,3}
5576-
linspace(0,1,5) => {0,0.25,0.5,0.75,1.0}
5577-
"
5578-
input Env.Cache inCache;
5579-
input Env.Env inEnv;
5580-
input list<Absyn.Exp> expl;
5581-
input list<Absyn.NamedArg> inNamedArg;
5582-
input Boolean impl;
5583-
input Prefix.Prefix inPrefix;
5584-
input Absyn.Info info;
5585-
output Env.Cache outCache;
5586-
output DAE.Exp outExp;
5587-
output DAE.Properties outProperties;
5588-
algorithm
5589-
(outCache,outExp,outProperties):=
5590-
matchcontinue (inCache,inEnv,expl,inNamedArg,impl,inPrefix,info)
5591-
local
5592-
Absyn.Exp x,y,n;
5593-
DAE.Exp x1,y1,n1,x2,y2, call;
5594-
DAE.Type tp1,tp2,tp3,tp11,tp22;
5595-
DAE.Const c1,c2,c3,c;
5596-
Integer size;
5597-
Env.Cache cache; Env.Env env;
5598-
Prefix.Prefix pre;
5599-
DAE.ExpType res_type;
5600-
5601-
/* linspace(x,y,n) where n is constant or parameter */
5602-
case (cache,env,{x,y,n},_,impl,pre,info) equation
5603-
(cache,x1,DAE.PROP(tp1,c1),_) = elabExp(cache,env, x, impl,NONE(),true,pre,info);
5604-
(cache,y1,DAE.PROP(tp2,c2),_) = elabExp(cache,env, y, impl,NONE(),true,pre,info);
5605-
(x2,tp11) = Types.matchType(x1,tp1,DAE.T_REAL_DEFAULT,true);
5606-
(y2,tp22) = Types.matchType(y1,tp2,DAE.T_REAL_DEFAULT,true);
5607-
(cache,n1,DAE.PROP(tp3 as (DAE.T_INTEGER(_),_),c3),_) =
5608-
elabExp(cache,env, n, impl,NONE(),true,pre,info);
5609-
true = Types.isParameterOrConstant(c3);
5610-
(cache,Values.INTEGER(size),_) =
5611-
Ceval.ceval(cache,env, n1, false,NONE(), Ceval.MSG(info));
5612-
c = Types.constAnd(c1,c2);
5613-
res_type = DAE.ET_ARRAY(DAE.ET_REAL(), {DAE.DIM_INTEGER(size)});
5614-
call = Expression.makeBuiltinCall("linspace", {x2, y2, n1}, res_type);
5615-
then (cache, call, DAE.PROP((DAE.T_ARRAY(DAE.DIM_INTEGER(size),tp11),NONE()),c));
5616-
5617-
/* linspace(x,y,n) where n is variable time expression */
5618-
case (cache,env,{x,y,n},_,impl,pre,info) equation
5619-
(cache,x1,DAE.PROP(tp1,c1),_) = elabExp(cache,env, x, impl,NONE(),true,pre,info);
5620-
(cache,y1,DAE.PROP(tp2,c2),_) = elabExp(cache,env, y, impl,NONE(),true,pre,info);
5621-
(x2,tp11) = Types.matchType(x1,tp1,DAE.T_REAL_DEFAULT,true);
5622-
(y2,tp22) = Types.matchType(y1,tp2,DAE.T_REAL_DEFAULT,true);
5623-
(cache,n1,DAE.PROP(tp3 as (DAE.T_INTEGER(_),_),c3),_) =
5624-
elabExp(cache,env, n, impl,NONE(),true,pre,info);
5625-
false = Types.isParameterOrConstant(c3);
5626-
c = Types.constAnd(c1,Types.constAnd(c2,c3));
5627-
res_type = DAE.ET_ARRAY(DAE.ET_REAL(), {DAE.DIM_UNKNOWN()});
5628-
call = Expression.makeBuiltinCall("linspace", {x2, y2, n1}, res_type);
5629-
then (cache, call, DAE.PROP((DAE.T_ARRAY(DAE.DIM_UNKNOWN(),tp11),NONE()),c));
5630-
end matchcontinue;
5631-
end elabBuiltinLinspace;
5632-
56335571
protected function elabBuiltinVector "function: elabBuiltinVector
56345572
author: PA
56355573

@@ -6111,7 +6049,6 @@ algorithm
61116049
case "skew" then elabBuiltinSkew;
61126050
case "String" then elabBuiltinString;
61136051
case "rooted" then elabBuiltinRooted;
6114-
case "linspace" then elabBuiltinLinspace;
61156052
case "Integer" then elabBuiltinIntegerEnum;
61166053
case "inStream" then elabBuiltinInStream;
61176054
case "actualStream" then elabBuiltinActualStream;

0 commit comments

Comments
 (0)