# .Modelica_LinearSystems2.UsersGuide.GettingStarted.ComplexNumbers.FunctionsAndOperators

## Information

The definition of a Complex number represented by the record Modelica_LinearSystems2.Math.Complex was originally defined by calling constructor function "Complex.constructor()". Constructor function calls can be abbreviated by "ThisRecord()", i.e., "constructor()" need not to be given. Hence, in the case of Complex a complex number, e.g. c = 1 + 2j would be generated by

```c = Complex(1, 2)
```

whereas the constructor-function is simply defined by

```function constructor "Default constructor for a complex number"
import Modelica_LinearSystems2.Math.Complex;
input Real re "Real part of complex number";
input Real im=0 "Imaginary part of complex number";
output Complex result "Complex number";
algorithm
result.re :=re;
result.im :=im;
end constructor;
```

In a similar manner the mathematical operations on complex numbers were defined by operator functions. Addition of two complex numbers for example was defined by a function

```function '+' "Add two complex numbers"
import Modelica_LinearSystems2.Math.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);
end '+';
```

and applicated by a function call

```c3 := Complex.'+'(c1,c2)
```

Although, the underlying principle is intelligibly it results in a very inconvenient way for the usage of complex numbers.

Since support of operation overloading has been enabled in the Modelica language, the procedures described above are still the base of the mathematical operations but can be used in a much more convenient way. The principle is that if there is an operation "c1 + c2" for which the operation '+' is not defined, it will be determined whether "c1" is a record type and, if it is, if it contains a function '+'. If applicable, this function call then replaces the above operation, i.e,. "c1 + c2" is interpreted as Complex.'+'(c1,c2). In other words, an operation like c3 = c1 + c2 can now be realized by

```import Modelica_LinearSystems2.Math.Complex;

j  = Complex.j();    // same as Complex(0,1);
c1 = 1 + 3*j;        // = Complex.'+'(Complex(1),Complex.'*'(Complex3,j));
c2 = 1 - 5*j;
c3 = c1 + c2;
```

and

```Modelica.Utilities.Streams.print("c3 = "+String(c3));
```

results in

```c1 = 2 - 2j
```

A necessary extension to the Modelica language to realize overloading operators for constructors, conversions, and operations has been the introduction of the "operator" keyword, i.e.

```operator 'constructor'
function fromReal
input Real re;
output Complex result = Complex(re=re, im=0.0);
annotation(Inline=true);
end fromReal;
end 'constructor';
```

and

```operator function '+'
input Complex c1;
input Complex c2;
output Complex result "= c1 + c2";
annotation(Inline=true);
algorithm
result := Complex(c1.re + c2.re, c1.im + c2.im);
end '+';
```

For details see Modelica Language Specification version 3.1 (chapter 14) from June 2009.

Generated at 2020-06-02T01:38:30Z by OpenModelicaOpenModelica 1.16.0~dev-420-gc007a39 using GenerateDoc.mos