-
Notifications
You must be signed in to change notification settings - Fork 169
/
Complex.mo
283 lines (263 loc) · 10.7 KB
/
Complex.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
within ;
operator record Complex "Complex number with overloaded operators"
replaceable Real re "Real part of complex number" annotation(Dialog);
replaceable Real im "Imaginary part of complex number" annotation(Dialog);
encapsulated operator 'constructor' "Constructor"
function fromReal "Construct Complex from Real"
import Complex;
input Real re "Real part of complex number";
input Real im=0 "Imaginary part of complex number";
output Complex result(re=re, im=im) "Complex number";
algorithm
annotation(Inline=true, Documentation(info="<html>
<p>This function returns a Complex number defined by real part <em>re</em> and optional imaginary part <em>im</em> (default=0).</p>
</html>"));
end fromReal;
annotation (Documentation(info="<html>
<p>Here the constructor operator(s) is/are defined.</p>
</html>"), Icon(graphics={Rectangle(
lineColor={200,200,200},
fillColor={248,248,248},
fillPattern=FillPattern.HorizontalCylinder,
extent={{-100,-100},{100,100}},
radius=25.0), Rectangle(
lineColor={128,128,128},
extent={{-100,-100},{100,100}},
radius=25.0)}));
end 'constructor';
encapsulated operator function '0' "Zero-element of addition (= Complex(0))"
import Complex;
output Complex result "Complex(0)";
algorithm
result := Complex(0);
annotation(Inline=true, Documentation(info="<html>
<p>This function returns the zero-element of Complex, that is, Complex(0) = 0 + j*0.</p>
</html>"));
end '0';
encapsulated operator '-' "Unary and binary minus"
function negate "Unary minus (multiply complex number by -1)"
import Complex;
input Complex c1 "Complex number";
output Complex c2 "= -c1";
algorithm
c2 := Complex(-c1.re, -c1.im);
annotation(Inline=true, smoothOrder=100, Documentation(info="<html>
<p>This function returns the binary minus of the given Complex number.</p>
</html>"));
end negate;
function subtract "Subtract two complex numbers"
import Complex;
input Complex c1 "Complex number 1";
input Complex c2 "Complex number 2";
output Complex c3 "= c1 - c2";
algorithm
c3 := Complex(c1.re - c2.re, c1.im - c2.im);
annotation(Inline=true, smoothOrder=100, Documentation(info="<html>
<p>This function returns the difference of two given Complex numbers.</p>
</html>"));
end subtract;
annotation (Documentation(info="<html>
<p>Here the unary and binary minus operator(s) is/are defined.</p>
</html>"), Icon(coordinateSystem(preserveAspectRatio=false, extent={{-100,-100},
{100,100}}), graphics={
Rectangle(
lineColor={200,200,200},
fillColor={248,248,248},
fillPattern=FillPattern.HorizontalCylinder,
extent={{-100,-100},{100,100}},
radius=25.0),
Rectangle(
lineColor={128,128,128},
extent={{-100,-100},{100,100}},
radius=25.0),
Text(
extent={{-200,-200},{200,250}},
textColor={128,128,128},
textString="-",
fontName="serif")}));
end '-';
encapsulated operator '*' "Multiplication"
function multiply "Multiply two complex numbers"
import Complex;
input Complex c1 "Complex number 1";
input Complex c2 "Complex number 2";
output Complex c3 "= c1*c2";
algorithm
c3 := Complex(c1.re*c2.re - c1.im*c2.im, c1.re*c2.im + c1.im*c2.re);
annotation(Inline=true, smoothOrder=100, Documentation(info="<html>
<p>This function returns the product of two given Complex numbers.</p>
</html>"));
end multiply;
function scalarProduct "Scalar product of two complex vectors c1 and c2"
import Complex;
input Complex c1[:] "Vector of Complex numbers 1";
input Complex c2[size(c1,1)] "Vector of Complex numbers 2";
output Complex c3 "Scalar product of c1 and c2";
algorithm
c3 := Complex(0);
for i in 1:size(c1,1) loop
c3 := Complex(c3.re + c1[i].re * c2[i].re + c1[i].im * c2[i].im,
c3.im + c1[i].re * c2[i].im - c1[i].im * c2[i].re);
end for;
annotation(Inline=true, smoothOrder=100, Documentation(info = "<html><p>This function returns the scalar product of two given vectors of Complex numbers of length <code>n</code>.</p>
<blockquote><pre>c3 = sum(conj(c1[k]) * c2[k] for k in 1:n)
</pre></blockquote>
</html>",
revisions = "<html><em>Important bug fix note:</em> The scalar product function was originally implemented without conjugating the argument <code>c1</code>. This issue is fixed based on <a href=\"https://github.com/modelica/ModelicaStandardLibrary/issues/1260\">#1260</a>.</html>"));
end scalarProduct;
annotation (
Documentation(info="<html>
<p>Here the multiplication operator(s) is/are defined.</p>
</html>"),
Icon(coordinateSystem(
preserveAspectRatio=false,
extent={{-100,-100},{100,100}}),
graphics={
Rectangle(
lineColor={200,200,200},
fillColor={248,248,248},
fillPattern=FillPattern.HorizontalCylinder,
extent={{-100,-100},{100,100}},
radius=25.0),
Rectangle(
lineColor={128,128,128},
extent={{-100,-100},{100,100}},
radius=25.0),
Text(
extent={{-200,-200},{200,100}},
textColor={128,128,128},
fontName="serif",
textString="*")}));
end '*';
encapsulated operator function '+' "Add two complex numbers"
import Complex;
input Complex c1 "Complex number 1";
input Complex c2 "Complex number 2";
output Complex c3 "= c1 + c2";
algorithm
c3 := Complex(c1.re + c2.re, c1.im + c2.im);
annotation(Inline=true, smoothOrder=100, Documentation(info="<html>
<p>This function returns the sum of two given Complex numbers.</p>
</html>"));
end '+';
encapsulated operator function '/' "Divide two complex numbers"
import Complex;
input Complex c1 "Complex number 1";
input Complex c2 "Complex number 2";
output Complex c3 "= c1/c2";
algorithm
c3 := Complex((+c1.re*c2.re + c1.im*c2.im)/(c2.re*c2.re + c2.im*c2.im),
(-c1.re*c2.im + c1.im*c2.re)/(c2.re*c2.re + c2.im*c2.im));
annotation(Inline=true, smoothOrder=100, Documentation(info="<html>
<p>This function returns the quotient of two given Complex numbers.</p>
</html>"));
end '/';
encapsulated operator '^' "Power"
function complexPower "Complex power of complex number"
import Complex;
input Complex c1 "Complex number";
input Complex c2 "Complex exponent";
output Complex c3 "= c1^c2";
protected
Real lnz=0.5*log(c1.re*c1.re + c1.im*c1.im);
Real phi=atan2(c1.im, c1.re);
Real re=lnz*c2.re - phi*c2.im;
Real im=lnz*c2.im + phi*c2.re;
algorithm
c3 := Complex(exp(re)*cos(im), exp(re)*sin(im));
annotation(Inline=true, smoothOrder=100, Documentation(info="<html>
<p>This function returns the given Complex number c1 to the power of the Complex number c2.</p>
</html>"));
end complexPower;
function integerPower "Integer power of complex number"
import Complex;
input Complex c1 "Complex number";
input Integer c2 "Integer exponent";
output Complex c3 "= c1^c2";
algorithm
c3 := if c2==0 then Complex(1) else Complex.'^'.complexPower(c1,Complex(c2));
annotation(Inline=true, smoothOrder=100, Documentation(info="<html>
<p>This function returns the given Complex number c1 to the power of the Integer number c2.</p>
<p>This also works for zero exponent.</p>
</html>"));
end integerPower;
end '^';
encapsulated operator function '=='
"Test whether two complex numbers are identical"
import Complex;
input Complex c1 "Complex number 1";
input Complex c2 "Complex number 2";
output Boolean result "c1 == c2";
algorithm
result := c1.re == c2.re and c1.im == c2.im;
annotation(Inline=true, Documentation(info="<html>
<p>This function tests whether two given Complex numbers are equal.</p>
</html>"));
end '==';
encapsulated operator function '<>'
"Test whether two complex numbers are not identical"
import Complex;
input Complex c1 "Complex number 1";
input Complex c2 "Complex number 2";
output Boolean result "c1 <> c2";
algorithm
result := c1.re <> c2.re or c1.im <> c2.im;
annotation(Inline=true, Documentation(info="<html>
<p>This function tests whether two given Complex numbers are not equal.</p>
</html>"));
end '<>';
encapsulated operator function 'String'
"Transform Complex number into a String representation"
import Complex;
input Complex c
"Complex number to be transformed in a String representation";
input String name="j"
"Name of variable representing sqrt(-1) in the string";
input Integer significantDigits=6
"Number of significant digits that are shown";
output String s="";
algorithm
s := String(c.re, significantDigits=significantDigits);
if c.im <> 0 then
if c.im > 0 then
s := s + " + ";
else
s := s + " - ";
end if;
s := s + String(abs(c.im), significantDigits=significantDigits) + "*" + name;
end if;
annotation(Inline=true, Documentation(info="<html>
<p>This function converts a given Complex number to String representation.</p>
</html>"));
end 'String';
annotation (
version="4.1.0",
versionDate="2024-01-12",
dateModified = "2024-01-12 19:40:00Z",
revisionId="$Format:%h %ci$",
conversion(
noneFromVersion="4.0.0",
noneFromVersion="3.2.3",
noneFromVersion="3.2.2",
noneFromVersion="3.2.1",
noneFromVersion="1.0",
noneFromVersion="1.1"),
Documentation(info="<html>
<p>Complex number defined as a record containing real and imaginary part, utilizing operator overloading.</p>
<p>
<strong>Licensed by the Modelica Association under the 3-Clause BSD License</strong><br>
Copyright © 2010-2024, Modelica Association and <a href=\"modelica://Modelica.UsersGuide.Contact\">contributors</a>
</p>
<p>
<em>This Modelica package is <u>free</u> software and the use is completely at <u>your own risk</u>; it can be redistributed and/or modified under the terms of the 3-Clause BSD license. For license conditions (including the disclaimer of warranty) visit <a href=\"https://modelica.org/licenses/modelica-3-clause-bsd\">https://modelica.org/licenses/modelica-3-clause-bsd</a>.</em>
</p></html>"),
Icon(graphics={Rectangle(
lineColor={160,160,164},
fillColor={160,160,164},
fillPattern=FillPattern.Solid,
extent={{-100,-100},{100,100}},
radius=25.0), Text(
textColor={255,255,255},
extent={{-90,-50},{90,50}},
textString="C")}));
end Complex;