forked from modelica/ModelicaStandardLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
package.mo
11695 lines (10072 loc) · 412 KB
/
package.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
within Modelica;
package Math "Library of mathematical functions (e.g., sin, cos) and of functions operating on vectors and matrices"
extends Modelica.Icons.Package;
package Vectors "Library of functions operating on vectors"
extends Modelica.Icons.Package;
function toString "Convert a real vector in to a string representation"
extends Modelica.Icons.Function;
import Modelica.Utilities.Strings;
input Real v[:] "Real vector";
input String name="" "Independent variable name used for printing";
input Integer significantDigits=6
"Number of significant digits that are shown";
output String s="";
protected
String blanks=Strings.repeat(significantDigits);
String space=Strings.repeat(8);
Integer r=size(v, 1);
algorithm
if r == 0 then
s := if name == "" then "[]" else name + " = []";
else
s := if name == "" then "\n" else "\n" + name + " = \n";
for i in 1:r loop
s := s + space;
if v[i] >= 0 then
s := s + " ";
end if;
s := s + String(v[i], significantDigits=significantDigits) +
Strings.repeat(significantDigits + 8 - Strings.length(String(abs(v[i]))));
s := s + "\n";
end for;
end if;
annotation (Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
Vectors.<strong>toString</strong>(v);
Vectors.<strong>toString</strong>(v,name=\"\",significantDigits=6);
</pre></blockquote>
<h4>Description</h4>
<p>
The function call \"<code>Vectors.<strong>toString</strong>(v)</code>\" returns the string representation of vector <strong>v</strong>.
With the optional arguments \"name\" and \"significantDigits\" a name and the number of the digits are defined.
The default values of \"name\" and \"significantDigits\" are \"\" and 6 respectively. If name==\"\" (empty string) then the prefix \"<name> =\" is left out at the output-string.
</p>
<h4>Example</h4>
<blockquote><pre>
v = {2.12, -4.34, -2.56, -1.67};
<strong>toString</strong>(v);
// = \"
// 2.12
// -4.34
// -2.56
// -1.67\"
<strong>toString</strong>(v,\"vv\",1);
// = \"vv =
// 2
// -4
// -3
// -2\"
</pre></blockquote>
<h4>See also</h4>
<p>
<a href=\"modelica://Modelica.Math.Matrices.toString\">Matrices.toString</a>,
</p>
</html>", revisions="<html>
</html>"));
end toString;
function isEqual "Determine if two Real vectors are numerically identical"
extends Modelica.Icons.Function;
input Real v1[:] "First vector";
input Real v2[:] "Second vector (may have different length as v1)";
input Real eps(min=0) = 0
"Two elements e1 and e2 of the two vectors are identical if abs(e1-e2) <= eps";
output Boolean result
"= true, if vectors have the same length and the same elements";
protected
Integer n=size(v1, 1) "Dimension of vector v1";
Integer i=1;
algorithm
result := false;
if size(v2, 1) == n then
result := true;
while i <= n loop
if abs(v1[i] - v2[i]) > eps then
result := false;
i := n;
end if;
i := i + 1;
end while;
end if;
annotation (Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
Vectors.<strong>isEqual</strong>(v1, v2);
Vectors.<strong>isEqual</strong>(v1, v2, eps=0);
</pre></blockquote>
<h4>Description</h4>
<p>
The function call \"<code>Vectors.isEqual(v1, v2)</code>\" returns <strong>true</strong>,
if the two Real vectors v1 and v2 have the same dimensions and
the same elements. Otherwise the function
returns <strong>false</strong>. Two elements e1 and e2 of the two vectors
are checked on equality by the test \"abs(e1-e2) ≤ eps\", where \"eps\"
can be provided as third argument of the function. Default is \"eps = 0\".
</p>
<h4>Example</h4>
<blockquote><pre>
Real v1[3] = {1, 2, 3};
Real v2[4] = {1, 2, 3, 4};
Real v3[3] = {1, 2, 3.0001};
Boolean result;
<strong>algorithm</strong>
result := Vectors.isEqual(v1,v2); // = <strong>false</strong>
result := Vectors.isEqual(v1,v3); // = <strong>false</strong>
result := Vectors.isEqual(v1,v1); // = <strong>true</strong>
result := Vectors.isEqual(v1,v3,0.1); // = <strong>true</strong>
</pre></blockquote>
<h4>See also</h4>
<p>
<a href=\"modelica://Modelica.Math.Vectors.find\">Vectors.find</a>,
<a href=\"modelica://Modelica.Math.Matrices.isEqual\">Matrices.isEqual</a>,
<a href=\"modelica://Modelica.Utilities.Strings.isEqual\">Strings.isEqual</a>
</p>
</html>"));
end isEqual;
function norm "Return the p-norm of a vector"
extends Modelica.Icons.Function;
input Real v[:] "Real vector";
input Real p(min=1) = 2
"Type of p-norm (often used: 1, 2, or Modelica.Constants.inf)";
output Real result=0.0 "p-norm of vector v";
protected
Real eps = 10*Modelica.Constants.eps;
algorithm
if size(v,1) > 0 then
if p >= 2-eps and p <= 2+eps then
result := sqrt(v*v);
elseif p >= Modelica.Constants.inf then
result := max(abs(v));
elseif p >= 1-eps and p <= 1+eps then
result := sum(abs(v));
elseif p >= 1 then
result := (sum(abs(v[i])^p for i in 1:size(v, 1)))^(1/p);
else
assert(false, "Optional argument \"p\" (= " + String(p) + ") of function \"norm\" >= 1 required");
end if;
end if;
annotation (Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
Vectors.<strong>norm</strong>(v);
Vectors.<strong>norm</strong>(v,p=2); // 1 ≤ p ≤ ∞
</pre></blockquote>
<h4>Description</h4>
<p>
The function call \"<code>Vectors.<strong>norm</strong>(v)</code>\" returns the
<strong>Euclidean norm</strong> \"<code>sqrt(v*v)</code>\" of vector v.
With the optional
second argument \"p\", any other p-norm can be computed:
</p>
<center>
<img src=\"modelica://Modelica/Resources/Images/Math/Vectors/vectorNorm.png\" alt=\"function Vectors.norm\">
</center>
<p>
Besides the Euclidean norm (p=2), also the 1-norm and the
infinity-norm are sometimes used:
</p>
<table border=\"1\" cellspacing=\"0\" cellpadding=\"2\">
<tr><td><strong>1-norm</strong></td>
<td>= sum(abs(v))</td>
<td><strong>norm</strong>(v,1)</td>
</tr>
<tr><td><strong>2-norm</strong></td>
<td>= sqrt(v*v)</td>
<td><strong>norm</strong>(v) or <strong>norm</strong>(v,2)</td>
</tr>
<tr><td><strong>infinity-norm</strong></td>
<td>= max(abs(v))</td>
<td><strong>norm</strong>(v,Modelica.Constants.<strong>inf</strong>)</td>
</tr>
</table>
<p>
Note, for any vector norm the following inequality holds:
</p>
<blockquote><pre>
<strong>norm</strong>(v1+v2,p) ≤ <strong>norm</strong>(v1,p) + <strong>norm</strong>(v2,p)
</pre></blockquote>
<h4>Example</h4>
<blockquote><pre>
v = {2, -4, -2, -1};
<strong>norm</strong>(v,1); // = 9
<strong>norm</strong>(v,2); // = 5
<strong>norm</strong>(v); // = 5
<strong>norm</strong>(v,10.5); // = 4.00052597412635
<strong>norm</strong>(v,Modelica.Constants.inf); // = 4
</pre></blockquote>
<h4>See also</h4>
<p>
<a href=\"modelica://Modelica.Math.Matrices.norm\">Matrices.norm</a>
</p>
</html>"));
end norm;
function length
"Return length of a vector (better as norm(), if further symbolic processing is performed)"
extends Modelica.Icons.Function;
input Real v[:] "Real vector";
output Real result "Length of vector v";
algorithm
result := sqrt(v*v);
annotation (Inline=true, Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
Vectors.<strong>length</strong>(v);
</pre></blockquote>
<h4>Description</h4>
<p>
The function call \"<code>Vectors.<strong>length</strong>(v)</code>\" returns the
<strong>Euclidean length</strong> \"<code>sqrt(v*v)</code>\" of vector v.
The function call is equivalent to Vectors.norm(v). The advantage of
length(v) over norm(v) is that function length(..) is implemented
in one statement and therefore the function is usually automatically
inlined. Further symbolic processing is therefore possible, which is
not the case with function norm(..).
</p>
<h4>Example</h4>
<blockquote><pre>
v = {2, -4, -2, -1};
<strong>length</strong>(v); // = 5
</pre></blockquote>
<h4>See also</h4>
<p>
<a href=\"modelica://Modelica.Math.Vectors.norm\">Vectors.norm</a>
</p>
</html>"));
end length;
function normalize
"Return normalized vector such that length = 1 and prevent zero-division for zero vector"
extends Modelica.Icons.Function;
input Real v[:] "Real vector";
input Real eps(min=0.0)=100*Modelica.Constants.eps
"if |v| < eps then result = v/eps";
output Real result[size(v, 1)](each final unit="1") "Input vector v normalized to length=1";
algorithm
/* This function has the inline annotation. If the function is inlined:
- "smooth(..)" defines how often the expression can be differentiated
(if symbolic processing is performed).
*/
result := smooth(0, if length(v) >= eps then v/length(v) else v/eps);
annotation (Inline=true, Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
Vectors.<strong>normalize</strong>(v);
Vectors.<strong>normalize</strong>(v,eps=100*Modelica.Constants.eps);
</pre></blockquote>
<h4>Description</h4>
<p>
The function call \"<code>Vectors.<strong>normalize</strong>(v)</code>\" returns the
<strong>unit vector</strong> \"<code>v/length(v)</code>\" of vector v.
If length(v) is close to zero (more precisely, if length(v) < eps),
v/eps is returned in order to avoid
a division by zero. For many applications this is useful, because
often the unit vector <strong>e</strong> = <strong>v</strong>/length(<strong>v</strong>) is used to compute
a vector x*<strong>e</strong>, where the scalar x is in the order of length(<strong>v</strong>),
i.e., x*<strong>e</strong> is small, when length(<strong>v</strong>) is small and then
it is fine to replace <strong>e</strong> by <strong>v</strong> to avoid a division by zero.
</p>
<p>
Since the function has the \"Inline\" annotation, it
is usually inlined and symbolic processing is applied.
</p>
<h4>Example</h4>
<blockquote><pre>
<strong>normalize</strong>({1,2,3}); // = {0.267, 0.534, 0.802}
<strong>normalize</strong>({0,0,0}); // = {0,0,0}
</pre></blockquote>
<h4>See also</h4>
<p>
<a href=\"modelica://Modelica.Math.Vectors.length\">Vectors.length</a>,
<a href=\"modelica://Modelica.Math.Vectors.normalize\">Vectors.normalizeWithAssert</a>
</p>
</html>"));
end normalize;
function normalizeWithAssert
"Return normalized vector such that length = 1 (trigger an assert for zero vector)"
import Modelica.Math.Vectors.length;
extends Modelica.Icons.Function;
input Real v[:] "Real vector";
output Real result[size(v, 1)] "Input vector v normalized to length=1";
algorithm
assert(length(v) > 0.0, "Vector v={0,0,0} shall be normalized (= v/sqrt(v*v)), but this results in a division by zero.\nProvide a non-zero vector!");
result := v/length(v);
annotation (
Inline=true,
Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
Vectors.<strong>normalizeWithAssert</strong>(v);
</pre></blockquote>
<h4>Description</h4>
<p>
The function call \"<code>Vectors.<strong>normalizeWithAssert</strong>(v)</code>\" returns the
<strong>unit vector</strong> \"<code>v/sqrt(v*v)</code>\" of vector v.
If vector v is a zero vector, an assert is triggered.
</p>
<p>
Since the function has the \"Inline\" annotation, it
is usually inlined and symbolic processing is applied.
</p>
<h4>Example</h4>
<blockquote><pre>
<strong>normalizeWithAssert</strong>({1,2,3}); // = {0.267, 0.534, 0.802}
<strong>normalizeWithAssert</strong>({0,0,0}); // error (an assert is triggered)
</pre></blockquote>
<h4>See also</h4>
<p>
<a href=\"modelica://Modelica.Math.Vectors.length\">Vectors.length</a>,
<a href=\"modelica://Modelica.Math.Vectors.normalize\">Vectors.normalize</a>
</p>
</html>"));
end normalizeWithAssert;
function reverse "Reverse vector elements (e.g., v[1] becomes last element)"
extends Modelica.Icons.Function;
input Real v[:] "Real vector";
output Real result[size(v, 1)] "Elements of vector v in reversed order";
algorithm
result := {v[end - i + 1] for i in 1:size(v, 1)};
annotation (Inline=true, Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
Vectors.<strong>reverse</strong>(v);
</pre></blockquote>
<h4>Description</h4>
<p>
The function call \"<code>Vectors.<strong>reverse</strong>(v)</code>\" returns the
vector elements in reverse order.
</p>
<h4>Example</h4>
<blockquote><pre>
<strong>reverse</strong>({1,2,3,4}); // = {4,3,2,1}
</pre></blockquote>
</html>"));
end reverse;
function sort "Sort elements of vector in ascending or descending order"
extends Modelica.Icons.Function;
input Real v[:] "Real vector to be sorted";
input Boolean ascending=true
"= true, if ascending order, otherwise descending order";
output Real sorted_v[size(v, 1)]=v "Sorted vector";
output Integer indices[size(v, 1)]=1:size(v, 1) "sorted_v = v[indices]";
/* shellsort algorithm; should be improved later */
protected
Integer gap;
Integer i;
Integer j;
Real wv;
Integer wi;
Integer nv=size(v, 1);
Boolean swap;
algorithm
gap := div(nv, 2);
while gap > 0 loop
i := gap;
while i < nv loop
j := i - gap;
if j >= 0 then
if ascending then
swap := sorted_v[j + 1] > sorted_v[j + gap + 1];
else
swap := sorted_v[j + 1] < sorted_v[j + gap + 1];
end if;
else
swap := false;
end if;
while swap loop
wv := sorted_v[j + 1];
wi := indices[j + 1];
sorted_v[j + 1] := sorted_v[j + gap + 1];
sorted_v[j + gap + 1] := wv;
indices[j + 1] := indices[j + gap + 1];
indices[j + gap + 1] := wi;
j := j - gap;
if j >= 0 then
if ascending then
swap := sorted_v[j + 1] > sorted_v[j + gap + 1];
else
swap := sorted_v[j + 1] < sorted_v[j + gap + 1];
end if;
else
swap := false;
end if;
end while;
i := i + 1;
end while;
gap := div(gap, 2);
end while;
annotation (Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
sorted_v = Vectors.<strong>sort</strong>(v);
(sorted_v, indices) = Vectors.<strong>sort</strong>(v, ascending=true);
</pre></blockquote>
<h4>Description</h4>
<p>
Function <strong>sort</strong>(..) sorts a Real vector v
in ascending order and returns the result in sorted_v.
If the optional argument \"ascending\" is <strong>false</strong>, the vector
is sorted in descending order. In the optional second
output argument the indices of the sorted vector with respect
to the original vector are given, such that sorted_v = v[indices].
</p>
<h4>Example</h4>
<blockquote><pre>
(v2, i2) := Vectors.sort({-1, 8, 3, 6, 2});
-> v2 = {-1, 2, 3, 6, 8}
i2 = {1, 5, 3, 4, 2}
</pre></blockquote>
</html>"));
end sort;
function find "Find element in a vector"
extends Modelica.Icons.Function;
input Real e "Search for e";
input Real v[:] "Real vector";
input Real eps(min=0) = 0
"Element e is equal to a element v[i] of vector v if abs(e-v[i]) <= eps";
output Integer result
"v[result] = e (first occurrence of e); result=0, if not found";
protected
Integer i;
algorithm
result := 0;
i := 1;
while i <= size(v, 1) loop
if abs(v[i] - e) <= eps then
result := i;
i := size(v, 1) + 1;
else
i := i + 1;
end if;
end while;
annotation (Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
Vectors.<strong>find</strong>(e, v);
Vectors.<strong>find</strong>(e, v, eps=0);
</pre></blockquote>
<h4>Description</h4>
<p>
The function call \"<code>Vectors.find(e, v)</code>\" returns the index of the first occurrence of input e in vector <strong>v</strong>.
The test of equality is performed by \"abs(e-v[i]) ≤ eps\", where \"eps\"
can be provided as third argument of the function. Default is \"eps = 0\".
</p>
<h4>Example</h4>
<blockquote><pre>
Real v[3] = {1, 2, 3};
Real e1 = 2;
Real e2 = 3.01;
Boolean result;
<strong>algorithm</strong>
result := Vectors.find(e1,v); // = <strong>2</strong>
result := Vectors.find(e2,v); // = <strong>0</strong>
result := Vectors.find(e2,v,eps=0.1); // = <strong>3</strong>
</pre></blockquote>
<h4>See also</h4>
<p>
<a href=\"modelica://Modelica.Math.Vectors.isEqual\">Vectors.isEqual</a>
</p>
</html>"));
end find;
function interpolate "Interpolate linearly in a vector"
extends Modelica.Icons.Function;
input Real x[:]
"Abscissa table vector (strict monotonically increasing values required)";
input Real y[size(x, 1)] "Ordinate table vector";
input Real xi "Desired abscissa value";
input Integer iLast=1 "Index used in last search";
output Real yi "Ordinate value corresponding to xi";
output Integer iNew=1 "xi is in the interval x[iNew] <= xi < x[iNew+1]";
protected
Integer i;
Integer nx=size(x, 1);
Real x1;
Real x2;
Real y1;
Real y2;
algorithm
assert(nx > 0, "The table vectors must have at least 1 entry.");
if nx == 1 then
yi := y[1];
else
// Search interval
i := min(max(iLast, 1), nx - 1);
if xi >= x[i] then
// search forward
while i < nx and xi >= x[i] loop
i := i + 1;
end while;
i := i - 1;
else
// search backward
while i > 1 and xi < x[i] loop
i := i - 1;
end while;
end if;
// Get interpolation data
x1 := x[i];
x2 := x[i + 1];
y1 := y[i];
y2 := y[i + 1];
assert(x2 > x1, "Abscissa table vector values must be increasing");
// Interpolate
yi := y1 + (y2 - y1)*(xi - x1)/(x2 - x1);
iNew := i;
end if;
annotation (smoothOrder( normallyConstant=x, normallyConstant=y)=100,
Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
// Real x[:], y[:], xi, yi;
// Integer iLast, iNew;
yi = Vectors.<strong>interpolate</strong>(x,y,xi);
(yi, iNew) = Vectors.<strong>interpolate</strong>(x,y,xi,iLast=1);
</pre></blockquote>
<h4>Description</h4>
<p>
The function call \"<code>Vectors.interpolate(x,y,xi)</code>\" interpolates
<strong>linearly</strong> in vectors
(x,y) and returns the value yi that corresponds to xi. Vector x[:] must consist
of monotonically increasing values. If xi < x[1] or > x[end], then
extrapolation takes places through the first or last two x[:] values, respectively.
If the x and y vectors have length 1, then always y[1] is returned.
The search for the interval x[iNew] ≤ xi < x[iNew+1] starts at the optional
input argument \"iLast\". The index \"iNew\" is returned as output argument.
The usage of \"iLast\" and \"iNew\" is useful to increase the efficiency of the call,
if many interpolations take place.
If x has two or more identical values then interpolation utilizes the x-value
with the largest index.
</p>
<h4>Example</h4>
<blockquote><pre>
Real x1[:] = { 0, 2, 4, 6, 8, 10};
Real x2[:] = { 1, 2, 3, 3, 4, 5};
Real y[:] = {10, 20, 30, 40, 50, 60};
<strong>algorithm</strong>
(yi, iNew) := Vectors.interpolate(x1,y,5); // yi = 35, iNew=3
(yi, iNew) := Vectors.interpolate(x2,y,4); // yi = 50, iNew=5
(yi, iNew) := Vectors.interpolate(x2,y,3); // yi = 40, iNew=4
</pre></blockquote>
</html>"));
end interpolate;
function relNodePositions "Return vector of relative node positions (0..1)"
extends Modelica.Icons.Function;
input Integer nNodes
"Number of nodes (including node at left and right position)";
output Real xsi[nNodes] "Relative node positions";
protected
Real delta;
algorithm
if nNodes >= 1 then
xsi[1] := 0;
end if;
if nNodes >= 2 then
xsi[nNodes] := 1;
end if;
if nNodes == 3 then
xsi[2] := 0.5;
elseif nNodes > 3 then
delta := 1/(nNodes - 2);
for i in 2:nNodes - 1 loop
xsi[i] := (i - 1.5)*delta;
end for;
end if;
annotation (Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
Vectors.<strong>relNodePositions</strong>(nNodes);
</pre></blockquote>
<h4>Description</h4>
<p>
The function call \"<code>relNodePositions(nNodes)</code>\" returns a vector
with the relative positions of the nodes of a discretized pipe with nNodes nodes (including the node
at the left and at the right side of the pipe), see next figure:
</p>
<div>
<img src=\"modelica://Modelica/Resources/Images/Math/Vectors/relNodePositions.png\">
</div>
<h4>Example</h4>
<blockquote><pre>
Real xsi[7];
<strong>algorithm</strong>
xsi = relNodePositions(7); // xsi = {0, 0.1, 0.3, 0.5, 0.7, 0.9, 1}
</pre></blockquote>
<h4>See also</h4>
<p>
<a href=\"modelica://Modelica.Mechanics.MultiBody.Visualizers.PipeWithScalarField\">MultiBody.Visualizers.PipeWithScalarField</a>
</p>
</html>"));
end relNodePositions;
annotation (preferredView="info", Documentation(info="<html>
<h4>Library content</h4>
<p>
This library provides functions operating on vectors:
</p>
<ul>
<li> <a href=\"modelica://Modelica.Math.Vectors.toString\">toString</a>(v)
- returns the string representation of vector v.</li>
<li> <a href=\"modelica://Modelica.Math.Vectors.isEqual\">isEqual</a>(v1, v2)
- returns true if vectors v1 and v2 have the same size and the same elements.</li>
<li> <a href=\"modelica://Modelica.Math.Vectors.norm\">norm</a>(v,p)
- returns the p-norm of vector v.</li>
<li> <a href=\"modelica://Modelica.Math.Vectors.length\">length</a>(v)
- returns the length of vector v (= norm(v,2), but inlined and therefore usable in
symbolic manipulations)</li>
<li> <a href=\"modelica://Modelica.Math.Vectors.normalize\">normalize</a>(v)
- returns vector in direction of v with length = 1 and prevents
zero-division for zero vector.</li>
<li> <a href=\"modelica://Modelica.Math.Vectors.reverse\">reverse</a>(v)
- reverses the vector elements of v.</li>
<li> <a href=\"modelica://Modelica.Math.Vectors.sort\">sort</a>(v)
- sorts the elements of vector v in ascending or descending order.</li>
<li> <a href=\"modelica://Modelica.Math.Vectors.find\">find</a>(e, v)
- returns the index of the first occurrence of scalar e in vector v.</li>
<li> <a href=\"modelica://Modelica.Math.Vectors.interpolate\">interpolate</a>(x, y, xi)
- returns the interpolated value in (x,y) that corresponds to xi.</li>
<li> <a href=\"modelica://Modelica.Math.Vectors.relNodePositions\">relNodePositions</a>(nNodes)
- returns a vector of relative node positions (0..1).</li>
</ul>
<h4>See also</h4>
<a href=\"modelica://Modelica.Math.Matrices\">Matrices</a>
</html>"),
Icon(graphics={Rectangle(
extent={{-16,66},{14,18}},
lineColor={95,95,95},
fillColor={175,175,175},
fillPattern=FillPattern.Solid), Rectangle(
extent={{-16,-14},{14,-62}},
lineColor={95,95,95},
fillColor={175,175,175},
fillPattern=FillPattern.Solid)}));
end Vectors;
package Matrices "Library of functions operating on matrices"
package Examples
"Examples demonstrating the usage of the Math.Matrices functions"
extends Modelica.Icons.ExamplesPackage;
function solveLinearEquations
"Demonstrate the solution of linear equation systems"
extends Modelica.Icons.Function;
import Modelica.Utilities.Streams.print;
// solve and solve2
protected
Real A0[0, 0];
Real A1[2, 2]=[1, 2; 3, 4];
Real x1_ref[2]={-2,3};
Real b1[2]=A1*x1_ref;
Real x1[2];
Real B2[2, 3]=[b1, 2*b1, -3*b1];
Real X2[2, 3];
// leastSquares and leastSquares2
Integer rank;
Real a[3]={2,3,-1};
Real A3[3, 3]=transpose([{2,3,-4}, a, 3*a]);
Real x3_ref[3]={-2,3,5};
Real b3[3]=A3*x3_ref;
Real x3[3];
Real B3[3, 2]=[b3, -3*b3];
Real X3[3, 2];
algorithm
print("\nDemonstrate how to solve linear equation systems:\n");
// Solve regular linear equation with a right hand side vector
x1 := Math.Matrices.solve(A1, b1);
print("diff1 = " + String(Vectors.norm(x1 - x1_ref)));
// Solve regular linear equation with a right hand side matrix
X2 := Math.Matrices.solve2(A1, B2);
print("diff2 = " + String(Matrices.norm(X2 - [x1_ref, 2*x1_ref, -3*x1_ref])));
// Solve singular linear equation with a right hand side vector
(x3,rank) := Math.Matrices.leastSquares(A3, b3);
print("diff3 = " + String(Vectors.norm(A3*x3 - b3)) + ", n = " + String(
size(A3, 1)) + ", rank = " + String(rank));
// Solve singular linear equation with a right hand side matrix
(X3,rank) := Math.Matrices.leastSquares2(A3, B3);
print("diff4 = " + String(Matrices.norm(A3*X3 - B3)) + ", n = " + String(
size(A3, 1)) + ", rank = " + String(rank));
annotation (Documentation(info="<html>
<p>
With simple examples this function demonstrates how to solve
regular linear equation systems with Matrices.solve and Matrices.solve2,
and how to solve singular linear equation systems with
Matrices.leastSquares and Matrices.leastSquares2.
</p>
</html>"));
end solveLinearEquations;
end Examples;
function toString "Convert a matrix into its string representation"
extends Modelica.Icons.Function;
import Modelica.Utilities.Strings;
input Real M[:, :] "Real matrix";
input String name="" "Independent variable name used for printing";
input Integer significantDigits=6
"Number of significant digits that are shown";
output String s="" "String expression of matrix M";
protected
String blanks=Strings.repeat(significantDigits);
String space=Strings.repeat(8);
String space2=Strings.repeat(3);
Integer r=size(M, 1);
Integer c=size(M, 2);
algorithm
if r == 0 or c == 0 then
s := name + " = []";
else
s := if name == "" then "\n" else "\n" + name + " = \n";
for i in 1:r loop
s := s + space;
for j in 1:c loop
if M[i, j] >= 0 then
s := s + " ";
end if;
s := s + String(M[i, j], significantDigits=significantDigits) +
Strings.repeat(significantDigits + 8 - Strings.length(String(abs(M[
i, j]))));
end for;
s := s + "\n";
end for;
end if;
annotation (Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
Matrices.<strong>toString</strong>(A);
Matrices.<strong>toString</strong>(A, name=\"\", significantDigits=6);
</pre></blockquote>
<h4>Description</h4>
<p>
The function call \"<code>Matrices.<strong>toString</strong>(A)</code>\" returns the
string representation of matrix <strong>A</strong>.
With the optional arguments \"name\" and \"significantDigits\", a name and the number of the digits are defined.
The default values of name and significantDigits are \"\" and 6 respectively. If name==\"\" then the
prefix \"<name> =\" is left out.
</p>
<h4>Example</h4>
<blockquote><pre>
A = [2.12, -4.34; -2.56, -1.67];
toString(A);
// = \"
// 2.12 -4.34
// -2.56 -1.67\";
toString(A,\"A\",1);
// = \"A =
// 2 -4
// -3 -2\"
</pre></blockquote>
<h4>See also</h4>
<p>
<a href=\"modelica://Modelica.Math.Vectors.toString\">Vectors.toString</a>
</p>
</html>", revisions="<html>
</html>"));
end toString;
extends Modelica.Icons.Package;
function isEqual "Compare whether two Real matrices are identical"
extends Modelica.Icons.Function;
input Real M1[:, :] "First matrix";
input Real M2[:, :] "Second matrix (may have different size as M1)";
input Real eps(min=0) = 0
"Two elements e1 and e2 of the two matrices are identical if abs(e1-e2) <= eps";
output Boolean result
"= true, if matrices have the same size and the same elements";
protected
Integer nrow=size(M1, 1) "Number of rows of matrix M1";
Integer ncol=size(M1, 2) "Number of columns of matrix M1";
Integer i=1;
Integer j;
algorithm
result := false;
if size(M2, 1) == nrow and size(M2, 2) == ncol then
result := true;
while i <= nrow loop
j := 1;
while j <= ncol loop
if abs(M1[i, j] - M2[i, j]) > eps then
result := false;
i := nrow;
j := ncol;
end if;
j := j + 1;
end while;
i := i + 1;
end while;
end if;
annotation (Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
Matrices.<strong>isEqual</strong>(M1, M2);
Matrices.<strong>isEqual</strong>(M1, M2, eps=0);
</pre></blockquote>
<h4>Description</h4>
<p>
The function call \"<code>Matrices.isEqual(M1, M2)</code>\" returns <strong>true</strong>,
if the two Real matrices M1 and M2 have the same dimensions and
the same elements. Otherwise the function
returns <strong>false</strong>. Two elements e1 and e2 of the two matrices
are checked on equality by the test \"abs(e1-e2) ≤ eps\", where \"eps\"
can be provided as third argument of the function. Default is \"eps = 0\".
</p>
<h4>Example</h4>
<blockquote><pre>
Real A1[2,2] = [1,2; 3,4];
Real A2[3,2] = [1,2; 3,4; 5,6];
Real A3[2,2] = [1,2, 3,4.0001];
Boolean result;
<strong>algorithm</strong>
result := Matrices.isEqual(M1,M2); // = <strong>false</strong>
result := Matrices.isEqual(M1,M3); // = <strong>false</strong>
result := Matrices.isEqual(M1,M1); // = <strong>true</strong>
result := Matrices.isEqual(M1,M3,0.1); // = <strong>true</strong>
</pre></blockquote>
<h4>See also</h4>
<a href=\"modelica://Modelica.Math.Vectors.isEqual\">Vectors.isEqual</a>,
<a href=\"modelica://Modelica.Utilities.Strings.isEqual\">Strings.isEqual</a>
</html>"));
end isEqual;
function solve
"Solve real system of linear equations A*x=b with a b vector (Gaussian elimination with partial pivoting)"
extends Modelica.Icons.Function;
input Real A[:, size(A, 1)] "Matrix A of A*x = b";
input Real b[size(A, 1)] "Vector b of A*x = b";
output Real x[size(b, 1)] "Vector x such that A*x = b";
protected
Integer info;
algorithm
(x,info) := LAPACK.dgesv_vec(A, b);
assert(info == 0, "Solving a linear system of equations with function
\"Matrices.solve\" is not possible, because the system has either
no or infinitely many solutions (A is singular).");
annotation (Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
Matrices.<strong>solve</strong>(A,b);
</pre></blockquote>
<h4>Description</h4>
<p>
This function call returns the
solution <strong>x</strong> of the linear system of equations
</p>
<blockquote>
<p>
<strong>A</strong>*<strong>x</strong> = <strong>b</strong>
</p>
</blockquote>
<p>
If a unique solution <strong>x</strong> does not exist (since <strong>A</strong> is singular),
an assertion is triggered. If this is not desired, use instead
<a href=\"modelica://Modelica.Math.Matrices.leastSquares\">Matrices.leastSquares</a>
and inquire the singularity of the solution with the return argument rank
(a unique solution is computed if rank = size(A,1)).
</p>
<p>
Note, the solution is computed with the LAPACK function \"dgesv\",
i.e., by Gaussian elimination with partial pivoting.
</p>
<h4>Example</h4>
<blockquote><pre>
Real A[3,3] = [1,2,3;
3,4,5;
2,1,4];
Real b[3] = {10,22,12};
Real x[3];
<strong>algorithm</strong>
x := Matrices.solve(A,b); // x = {3,2,1}
</pre></blockquote>
<h4>See also</h4>
<a href=\"modelica://Modelica.Math.Matrices.LU\">Matrices.LU</a>,
<a href=\"modelica://Modelica.Math.Matrices.LU_solve\">Matrices.LU_solve</a>,
<a href=\"modelica://Modelica.Math.Matrices.leastSquares\">Matrices.leastSquares</a>.
</html>"));
end solve;
function solve2
"Solve real system of linear equations A*X=B with a B matrix (Gaussian elimination with partial pivoting)"
extends Modelica.Icons.Function;
input Real A[:, size(A, 1)] "Matrix A of A*X = B";
input Real B[size(A, 1), :] "Matrix B of A*X = B";
output Real X[size(B, 1), size(B, 2)] "Matrix X such that A*X = B";
protected
Integer info;
algorithm
(X,info) := LAPACK.dgesv(A, B);
assert(info == 0, "Solving a linear system of equations with function
\"Matrices.solve2\" is not possible, because the system has either
no or infinitely many solutions (A is singular).");
annotation (Documentation(info="<html>
<h4>Syntax</h4>
<blockquote><pre>
Matrices.<strong>solve2</strong>(A,b);
</pre></blockquote>
<h4>Description</h4>
<p>
This function call returns the
solution <strong>X</strong> of the linear system of equations
</p>
<blockquote>
<p>
<strong>A</strong>*<strong>X</strong> = <strong>B</strong>
</p>
</blockquote>
<p>
If a unique solution <strong>X</strong> does not exist (since <strong>A</strong> is singular),
an assertion is triggered. If this is not desired, use instead
<a href=\"modelica://Modelica.Math.Matrices.leastSquares2\">Matrices.leastSquares2</a>
and inquire the singularity of the solution with the return argument rank
(a unique solution is computed if rank = size(A,1)).
</p>
<p>
Note, the solution is computed with the LAPACK function \"dgesv\",
i.e., by Gaussian elimination with partial pivoting.
</p>
<h4>Example</h4>
<blockquote><pre>
Real A[3,3] = [1,2,3;
3,4,5;
2,1,4];
Real B[3,2] = [10, 20;