-
Notifications
You must be signed in to change notification settings - Fork 298
/
SBPWLinearMap.mo
403 lines (347 loc) · 10.5 KB
/
SBPWLinearMap.mo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Linköping University,
* Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3
* AND THIS OSMC PUBLIC LICENSE (OSMC-PL).
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S
* ACCEPTANCE OF THE OSMC PUBLIC LICENSE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from Linköping University, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS
* OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
encapsulated uniontype SBPWLinearMap
import SBAtomicSet;
import SBSet;
import SBLinearMap;
import SBPWAtomicLinearMap;
protected
import Array;
import MetaModelica.Dangerous.*;
import System;
import Vector;
public
record PW_LINEAR_MAP
array<SBSet> dom;
array<SBLinearMap> lmap;
Integer ndim;
end PW_LINEAR_MAP;
function new
input array<SBSet> dom;
input array<SBLinearMap> lmap;
output SBPWLinearMap map;
protected
Integer dim = 0;
Boolean same_dims;
algorithm
if arrayLength(dom) <> arrayLength(lmap) then
// Warning: Domain size should be equal to map size.
map := newEmpty();
return;
end if;
if not arrayEmpty(dom) then
dim := SBSet.ndim(dom[1]);
same_dims := Array.all(dom, function SBSet.isDim(dim = dim)) and
Array.all(lmap, function SBLinearMap.isDim(dim = dim));
end if;
if not same_dims then
// Warning: Sets and maps should have the same dimension.
map := newEmpty();
else
map := PW_LINEAR_MAP(arrayCopy(dom), arrayCopy(lmap), dim);
end if;
end new;
function newScalar
input SBSet dom;
input SBLinearMap lmap;
output SBPWLinearMap map;
algorithm
if SBSet.ndim(dom) == SBLinearMap.ndim(lmap) then
map := PW_LINEAR_MAP(arrayCreate(1, dom), arrayCreate(1, lmap), 1);
else
// Warning: Sets and maps should have the same dimension.
map := newEmpty();
end if;
end newScalar;
function newEmpty
output SBPWLinearMap map;
algorithm
map := PW_LINEAR_MAP(listArray({}), listArray({}), 0);
end newEmpty;
function newIdentity
input SBSet set;
output SBPWLinearMap map;
protected
SBLinearMap lmap = SBLinearMap.newIdentity(SBSet.ndim(set));
algorithm
map := PW_LINEAR_MAP(arrayCreate(1, set), arrayCreate(1, lmap), 1);
end newIdentity;
function copy
input output SBPWLinearMap map;
algorithm
map.dom := Array.map(map.dom, SBSet.copy);
map.lmap := Array.map(map.lmap, SBLinearMap.copy);
end copy;
function dom
input SBPWLinearMap map;
output array<SBSet> dom = map.dom;
end dom;
function lmap
input SBPWLinearMap map;
output array<SBLinearMap> lmap = map.lmap;
end lmap;
function ndim
input SBPWLinearMap map;
output Integer ndim = map.ndim;
end ndim;
function isEmpty
input SBPWLinearMap map;
output Boolean empty = arrayEmpty(map.dom);
end isEmpty;
function image
input SBPWLinearMap map;
input SBSet set;
output SBSet outSet = SBSet.newEmpty();
protected
array<SBSet> dom = map.dom;
array<SBLinearMap> lmap = map.lmap;
SBSet ss, partial_res;
function add_set
input SBAtomicSet aset;
input SBLinearMap map;
input output SBSet set;
protected
SBPWAtomicLinearMap aux_map;
algorithm
aux_map := SBPWAtomicLinearMap.new(aset, map);
set := SBSet.addAtomicSet(SBPWAtomicLinearMap.image(aux_map, aset), set);
end add_set;
algorithm
for i in 1:arrayLength(dom) loop
ss := dom[i];
ss := SBSet.intersection(ss, set);
partial_res := UnorderedSet.fold(SBSet.asets(ss),
function add_set(map = lmap[i]), SBSet.newEmpty());
outSet := SBSet.union(outSet, partial_res);
end for;
end image;
function preImage
input SBPWLinearMap map;
input SBSet set;
output SBSet outSet = SBSet.newEmpty();
protected
array<SBSet> dom = map.dom;
array<SBLinearMap> lmap = map.lmap;
SBSet ss, partial_res;
array<SBAtomicSet> sets;
function add_set
input SBAtomicSet aset;
input SBLinearMap map;
input array<SBAtomicSet> sets;
input output SBSet set;
protected
SBPWAtomicLinearMap aux_map;
algorithm
aux_map := SBPWAtomicLinearMap.new(aset, map);
for as2 in sets loop
set := SBSet.addAtomicSet(SBPWAtomicLinearMap.preImage(aux_map, as2), set);
end for;
end add_set;
algorithm
sets := UnorderedSet.toArray(SBSet.asets(set));
for i in 1:arrayLength(dom) loop
ss := dom[i];
partial_res := SBSet.newEmpty();
partial_res := UnorderedSet.fold(SBSet.asets(ss),
function add_set(map = lmap[i], sets = sets), SBSet.newEmpty());
outSet := SBSet.union(outSet, partial_res);
end for;
end preImage;
function compPW
input SBPWLinearMap map1;
input SBPWLinearMap map2;
output SBPWLinearMap outMap;
protected
array<SBSet> dom1 = map1.dom, dom2 = map2.dom;
Vector<SBSet> ress;
array<SBLinearMap> lmap1 = map1.lmap, lmap2 = map2.lmap;
Vector<SBLinearMap> reslm;
SBSet aux_dom, new_dom, d1, d2;
SBLinearMap l1, l2, new_lm;
algorithm
if SBPWLinearMap.isEmpty(map1) or SBPWLinearMap.isEmpty(map2) then
outMap := SBPWLinearMap.newEmpty();
return;
end if;
ress := Vector.new(dom1[1]);
reslm := Vector.new(lmap1[1]);
for i in 1:arrayLength(dom1) loop
d1 := arrayGetNoBoundsChecking(dom1, i);
for j in 1:arrayLength(dom2) loop
d2 := arrayGetNoBoundsChecking(dom2, j);
aux_dom := image(map2, d2);
aux_dom := SBSet.intersection(aux_dom, d1);
aux_dom := preImage(map2, aux_dom);
new_dom := SBSet.intersection(aux_dom, d2);
if not SBSet.isEmpty(new_dom) then
l1 := lmap1[i];
l2 := lmap2[j];
new_lm := SBLinearMap.compose(l1, l2);
Vector.push(ress, new_dom);
Vector.push(reslm, new_lm);
end if;
end for;
end for;
outMap := new(Vector.toArray(ress), Vector.toArray(reslm));
end compPW;
function minInvCompact
input SBPWLinearMap map;
output SBPWLinearMap outMap;
protected
SBSet aux_dom, dom_inv;
SBLinearMap aux_map, map_inv;
array<Integer> min;
array<Real> resg, reso, g, o;
algorithm
if arrayLength(map.dom) <> 1 then
// Warning: There should be only one component.
outMap := newEmpty();
return;
end if;
aux_dom := arrayGet(map.dom, 1);
dom_inv := image(map, aux_dom);
aux_map := arrayGet(map.lmap, 1);
map_inv := SBLinearMap.inverse(aux_map);
min := SBSet.minElem(aux_dom);
g := SBLinearMap.gain(map_inv);
o := SBLinearMap.offset(map_inv);
resg := arrayCreateNoInit(arrayLength(g), 0.0);
reso := arrayCreateNoInit(arrayLength(o), 0.0);
for i in 1:arrayLength(g) loop
if g[i] == intReal(System.intMaxLit()) then
resg[i] := 0;
reso[i] := intReal(min[i]);
else
resg[i] := g[i];
reso[i] := o[i];
end if;
end for;
outMap := new(arrayCreate(1, dom_inv), arrayCreate(1, SBLinearMap.new(resg, reso)));
end minInvCompact;
function wholeDom
input SBPWLinearMap map;
output SBSet set;
algorithm
set := SBSet.newEmpty();
for s in map.dom loop
set := SBSet.union(set, s);
end for;
end wholeDom;
function combine
input SBPWLinearMap map1;
input SBPWLinearMap map2;
output SBPWLinearMap outMap;
protected
Vector<SBSet> sres;
Vector<SBLinearMap> lres;
array<SBSet> dom2;
array<SBLinearMap> lm2;
SBSet aux1, s2, new_dom;
algorithm
if SBPWLinearMap.isEmpty(map1) then
outMap := copy(map2);
return;
end if;
if SBPWLinearMap.isEmpty(map2) then
outMap := copy(map1);
return;
end if;
sres := Vector.fromArray(map1.dom);
lres := Vector.fromArray(map1.lmap);
dom2 := map2.dom;
lm2 := map2.lmap;
aux1 := wholeDom(map1);
for i in 1:arrayLength(dom2) loop
s2 := dom2[i];
new_dom := SBSet.complement(s2, aux1);
if not SBSet.isEmpty(new_dom) then
Vector.push(sres, new_dom);
Vector.push(lres, lm2[i]);
end if;
end for;
outMap := new(Vector.toArray(sres), Vector.toArray(lres));
end combine;
function atomize
input SBPWLinearMap map;
output SBPWLinearMap outMap;
protected
list<SBSet> dres = {};
array<SBSet> dom = map.dom;
list<SBLinearMap> lres = {};
array<SBLinearMap> lm = map.lmap;
SBSet d, aux;
SBLinearMap l;
array<SBAtomicSet> asets;
algorithm
for i in 1:arrayLength(dom) loop
d := dom[i];
l := lm[i];
asets := UnorderedSet.toArray(SBSet.asets(d));
for s in asets loop
aux := SBSet.newEmpty();
aux := SBSet.addAtomicSet(s, aux);
dres := aux :: dres;
lres := l :: lres;
end for;
end for;
outMap := new(listArray(listReverseInPlace(dres)),
listArray(listReverseInPlace(lres)));
end atomize;
function isEqual
input SBPWLinearMap map1;
input SBPWLinearMap map2;
output Boolean equal;
algorithm
equal := Array.isEqualOnTrue(map1.dom, map2.dom, SBSet.isEqual) and
Array.isEqualOnTrue(map1.lmap, map2.lmap, SBLinearMap.isEqual);
end isEqual;
function toString
input SBPWLinearMap map;
output String str;
protected
array<SBSet> dom = map.dom;
array<SBLinearMap> lmap = map.lmap;
list<String> strl = {};
function helper
input SBAtomicSet set;
input SBLinearMap lm;
output String str;
algorithm
str := "{" + SBPWAtomicLinearMap.toString(SBPWAtomicLinearMap.PW_ATOMIC_LINEAR_MAP(set, lm)) + "}";
end helper;
algorithm
for i in arrayLength(dom):-1:1 loop
strl := UnorderedSet.toString(SBSet.asets(dom[i]), function helper(lm = lmap[i]), "U") :: strl;
end for;
str := "[" + stringDelimitList(strl, ",") + "]";
end toString;
annotation(__OpenModelica_Interface="util");
end SBPWLinearMap;