ElementaryOperatorsElementary operators (+, >, or, ..) |
This information is part of the Modelica Standard Library maintained by the Modelica Association.
Elementary operators are overloaded and operate on variables of type Real, Integer, Boolean, and String, as well as on scalars or arrays.
Arithmetic Operators (operate on Real, Integer scalars or arrays) | ||
Operators | Example | Description |
+, -, .+, .- | a + b a .+ b |
addition and subtraction; element-wise on arrays |
* | a * b | multiplication; scalar*array: element-wise multiplication vector*vector: element-wise multiplication (result: scalar) matrix*matrix: matrix product vector*matrix: row-matrix*matrix (result: vector) matrix*vector: matrix*column-matrix (result: vector) |
/ | a / b | division of two scalars or an array by a scalar; division of an array by a scalar is defined element-wise. The result is always of real type. In order to get integer division with truncation use the function div. |
^ | a^b | scalar power or integer power of a square matrix |
.*, ./, .^ | a .* b | element-wise multiplication, division and exponentiation of scalars and arrays |
= | a * b = c + d | equal operator of an equation; element-wise on arrays |
:= | a := c + d | assignment operator; element-wise on arrays |
Relational Operators (operate on Real, Integer, Boolean, String scalars) | ||
Operators | Example | Description |
== | a == b | equal; for strings: identical characters |
<> | a <> b | not equal; for strings: a is lexicographically less than b |
< | a < b | less than |
<= | a <= b | less than or equal |
> | a > b | greater than |
>= | a >= b | greater than or equal |
Boolean Operators (operate on scalars or element-wise on arrays) | ||
Operators | Example | Description |
and | a and b | logical and |
or | a or b | logical or |
not | not a | logical not |
Other Operators | ||
Operators | Example | Description |
[..] | [1,2;3,4] | Matrix constructor; "," separates columns, ";" separates rows |
{..} | {{1,2}, {3,4}} | Array constructor; every {..} adds one dimension |
"..." | "string value" "string "value"" |
String literal (" is used inside a string for ") |
+ | "abc" + "def" | Concatenation of string scalars or arrays |
Operator precedence determines the order of evaluation of operators in an expression. An operator with higher precedence is evaluated before an operator with lower precedence in the same expression.
The following table presents all the expression operators in order of precedence from highest to lowest. All operators are binary except exponentiation, the postfix operators and those shown as unary together with expr, the conditional operator, the array construction operator {} and concatenation operator [ ], and the array range constructor which is either binary or ternary. Operators with the same precedence occur at the same line of the table:
Operator Group | Operator Syntax | Examples |
postfix array index operator | [] |
arr[index] |
postfix access operator | . |
a.b |
postfix function call | funcName(function-arguments) | sin(4.36) |
array construct/concat | {expressions} [expressions] [expressions; expressions...] |
{2,3} |
exponentiation | ^ |
2^3 |
multiplicative and array elementwise multiplicative |
* / .* ./ |
2*3 2/3 |
additive and array elementwise additive |
+ - +expr -expr |
a+b, a-b, +a, -a |
relational | < <= > >= == <> |
a<b, a<=b, a>b, ... |
... |
||
unary negation | not expr |
not b1 |
logical and | and |
b1 and b2 |
logical or< | or |
b1 or b2 |
array range | expr : expr : expr |
1:5:100 , start:step:stop |
conditional | if expr then expr else expr |
if b then 3 else x |
named argument | ident = expr |
x = 2.26 |
The conditional operator may also include elseif
-clauses. Equality =
and assignment :=
are not expression operators since they are allowed only in equations and in assignment statements respectively. All binary expression
operators are left associative.
Note, the unary minus and plus in Modelica is slightly different than in Mathematica (Mathematica is a registered trademark of Wolfram Research Inc.) and in MATLAB (MATLAB is a registered trademark of MathWorks Inc.), since the following expressions are illegal (whereas in Mathematica and in MATLAB these are valid expressions):
2*-2 // = -4 in Mathematica/MATLAB; is illegal in Modelica --2 // = 2 in Mathematica/MATLAB; is illegal in Modelica ++2 // = 2 in Mathematica/MATLAB; is illegal in Modelica 2--2 // = 4 in Mathematica/MATLAB; is illegal in Modelica