Skip to content

Commit

Permalink
Clean up listings with bad indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
henrikt-ma committed Jan 18, 2022
1 parent 0d67136 commit 491d1c1
Showing 1 changed file with 33 additions and 31 deletions.
64 changes: 33 additions & 31 deletions chapters/overloaded.tex
Expand Up @@ -273,9 +273,9 @@ \section{Example of Overloading for Complex Numbers}\label{example-of-overloadin
function fromReal
input Real re;
input Real im := 0;
output Complex result(re=re, im=im);
output Complex result(re = re, im = im);
algorithm
annotation(Inline=true);
annotation(Inline = true);
end fromReal;
end 'constructor';

Expand All @@ -286,7 +286,7 @@ \section{Example of Overloading for Complex Numbers}\label{example-of-overloadin
output Complex result "= c1 + c2";
algorithm
result := Complex(c1.re + c2.re, c1.im + c2.im);
annotation(Inline=true);
annotation(Inline = true);
end '+';

encapsulated operator '-'
Expand All @@ -296,7 +296,7 @@ \section{Example of Overloading for Complex Numbers}\label{example-of-overloadin
output Complex result "= - c";
algorithm
result := Complex(-c.re, -c.im);
annotation(Inline=true);
annotation(Inline = true);
end negate;

function subtract
Expand All @@ -305,7 +305,7 @@ \section{Example of Overloading for Complex Numbers}\label{example-of-overloadin
output Complex result "= c1 - c2";
algorithm
result := Complex(c1.re - c2.re, c1.im - c2.im);
annotation(Inline=true);
annotation(Inline = true);
end subtract;
end '-';

Expand All @@ -315,19 +315,20 @@ \section{Example of Overloading for Complex Numbers}\label{example-of-overloadin
input Complex c2;
output Complex result "= c1 * c2";
algorithm
result := Complex(c1.re*c2.re - c1.im*c2.im, c1.re*c2.im + c1.im*c2.re);
annotation(Inline=true);
result :=
Complex(c1.re * c2.re - c1.im * c2.im, c1.re * c2.im + c1.im * c2.re);
annotation(Inline = true);
end '*';

encapsulated operator function '/'
import Complex; input Complex c1;
input Complex c2;
output Complex result "= c1 / c2";
algorithm
result := Complex(( c1.re*c2.re + c1.im*c2.im)/(c2.re^2 +
c2.im^2),
(-c1.re*c2.im + c1.im*c2.re)/(c2.re^2 + c2.im^2));
annotation(Inline=true);
result :=
Complex((c1.re*c2.re + c1.im*c2.im) / (c2.re^2 + c2.im^2),
(-c1.re*c2.im + c1.im*c2.re) / (c2.re^2 + c2.im^2));
annotation(Inline = true);
end '/';

encapsulated operator function '=='
Expand All @@ -337,61 +338,62 @@ \section{Example of Overloading for Complex Numbers}\label{example-of-overloadin
output Boolean result "= c1 == c2";
algorithm
result := c1.re == c2.re and c1.im == c2.im;
annotation(Inline=true);
annotation(Inline = true);
end '==';

encapsulated operator function 'String'
import Complex;
input Complex c;
input String name := "j" "Name of variable representing sqrt(-1) in the string";
input Integer significantDigits=6 "Number of significant digits to be shown";
input String name := "j"
"Name of variable representing sqrt(-1) in the string";
input Integer significantDigits = 6
"Number of significant digits to be shown";
output String s;
algorithm
s := String(c.re, significantDigits=significantDigits);
s := String(c.re, significantDigits = significantDigits);
if c.im <> 0 then
s := if c.im > 0 then s + " + "
else s + " - ";
s := s + String(abs(c.im), significantDigits=significantDigits) + name;
end if;
s := if c.im > 0 then s + " + " else s + " - ";
s := s + String(abs(c.im), significantDigits = significantDigits) + name;
end if;
end 'String';

encapsulated function j
import Complex;
output Complex c;
algorithm
c := Complex(0,1);
annotation(Inline=true);
c := Complex(0, 1);
annotation(Inline = true);
end j;

encapsulated operator function '0'
import Complex;
output Complex c;
algorithm
c := Complex(0,0);
annotation(Inline=true);
c := Complex(0, 0);
annotation(Inline = true);
end '0';
end Complex;

function eigenValues
input Real A [:,:];
output Complex ev[size(A, 1)];
protected
Integer nx=size(A, 1);
Real eval[nx,2];
Integer nx = size(A, 1);
Real eval[nx, 2];
Integer i;
algorithm
eval := Modelica.Math.Matrices.eigenValues(A);
for i in 1:nx loop
for i in 1 : nx loop
ev[i] := Complex(eval[i, 1], eval[i, 2]);
end for;
end eigenValues;

// Usage of Complex number above:
Complex j = Complex.j();
Complex c1 = 2 + 3*j;
Complex c2 = 3 + 4*j;
Complex c1 = 2 + 3 * j;
Complex c2 = 3 + 4 * j;
Complex c3 = c1 + c2;
Complex c4[:] = eigenValues([1,2; -3,4]);
Complex c4[:] = eigenValues([1, 2; -3, 4]);
algorithm
Modelica.Utilities.Streams.print("c4 = " + String(c4));
// results in output:
Expand Down Expand Up @@ -420,8 +422,8 @@ \section{Example of Overloading for Complex Numbers}\label{example-of-overloadin

Complex can be used in a connector:
\begin{lstlisting}[language=modelica]
operator record ComplexVoltage = Complex(re(unit="V"),im(unit="V"));
operator record ComplexCurrent = Complex(re(unit="A"),im(unit="A"));
operator record ComplexVoltage = Complex(re(unit = "V"), im(unit = "V"));
operator record ComplexCurrent = Complex(re(unit = "A"), im(unit = "A"));

connector ComplexPin
ComplexVoltage v;
Expand Down

0 comments on commit 491d1c1

Please sign in to comment.