Basic operators in Scilab

Scilab is capable of simple mathematical calculation as well as complex calculations. From the operators point of view, Scilab is able to fulfil arithmetic calculations, comparison and logical operations.

How to install Scilab

Arithmetic operators in Scilab

Within Scilab we can perform: additions, subtractions, multiplications, left and right divisions and exponentiation. Except exponentiation, all mathematical operations can be applied to scalars, vectors and matrices. Also, Scilab sets priorities regarding the calculation order.

Operator Description Example with scalar Example with matrix Priority
+ Addition -->3+4
ans =
7.
-->
-->[1 2;3 4]+[5 6;7 8]
ans =
6. 8.
10. 12.
-->
3
Subtraction -->8-2
ans =
6.
-->
-->[1 2;3 4]-[5 6;7 8]
ans =
- 4. - 4.
- 4. - 4.
-->
3
* Multiplication -->4*8
ans =
32.
-->
-->[1 2;3 4]*[5 6;7 8]
ans =
19. 22.
43. 50.
-->
2
\ Left division -->8\4
ans =
0.5
-->
-->[1 2;3 4]\[5 6;7 8]
ans =
- 3. - 4.
4. 5.
-->
2
/ Right division ->8/4
ans =
2.
-->
-->[1 2;3 4]/[5 6;7 8]
ans =
3. - 2.
2. - 1.
-->
2
^ Exponentiation -->2^3
ans =
8.
-->
not applicable 1

Special matrix arithmetic operations in Scilab

A normal matrix multiplication, using the star operator “*”, is done in the following manner:

\[ \begin{equation*} \begin{split}
\begin{bmatrix}
a_{11} & a_{12} \\
a_{21} & a_{22}
\end{bmatrix} *
\begin{bmatrix}
b_{11} & b_{12} \\
b_{21} & b_{22}
\end{bmatrix} =
\begin{bmatrix}
a_{11} \cdot b_{11} +  a_{12} \cdot b_{21} & a_{11} \cdot b_{12} +  a_{12} \cdot b_{22} \\
a_{21} \cdot b_{11} +  a_{22} \cdot b_{21} & a_{21} \cdot b_{12} +  a_{22} \cdot b_{22}
\end{bmatrix}
\end{split} \end{equation*} \]
There are some cases in which the user would like to multiply not as the usual matrix multiplication but term by term. For matrix operation there is also a special operator, the dot operator “.” which placed before a normal operator changes the calculation result. The dot operator applies the normal operator for each member of the matrix.
\[ \begin{equation*} \begin{split}
\begin{bmatrix}
a_{11} & a_{12} \\
a_{21} & a_{22}
\end{bmatrix} .*
\begin{bmatrix}
b_{11} & b_{12} \\
b_{21} & b_{22}
\end{bmatrix} =
\begin{bmatrix}
a_{11} \cdot b_{11} & a_{12} \cdot b_{12}\\
a_{21} \cdot b_{21} & a_{22} \cdot b_{22}
\end{bmatrix}
\end{split} \end{equation*} \]

For example, for matrix multiplication if you use the dot operator you’ll get the following result:

-->[1 2;3 4].*[5 6;7 8]

ans = 
 5. 12. 
 21. 32. 
 
-->

In the above example each member of the left matrix was multiplied with the corresponding member of the right matrix.

The table below contains examples for all dot operation applied to matrices:

Operator Description Example
.* Dot multiplication -->[1 2;3 4].*[5 6;7 8]
ans =
5. 12.
21. 32.
-->
.\ Dot left division -->[1 2;3 4].\[5 6;7 8]
ans =
5. 3.
2.3333333 2.
-->
./ Dot right division -->[1 2;3 4]./[5 6;7 8]
ans =
0.2 0.3333333
0.4285714 0.5
-->
.^ Dot exponentiation -->[1 2;3 4].^[5 6;7 8]
ans =
1. 64.
2187. 65536.
-->

Relational (comparison) operators in Scilab

In order to compare two or more variables between them, Scilab uses the relational operators. With these operators you can make the following comparisons: smaller, greater, smaller or equal, greater or equal, equal, not equal.

Operator Description
< Smaller than
> Greater than
<= Smaller or equal than
>= Greater or equal than
== Equal with
~= Not equal with

The output of a comparison will be a variable of type Boolean:

-->32 > 17
 ans =
 
 T 
 
-->type(ans)
 ans =
 
 4. 
 
-->

The comparison operators can be applied also to matrices, strings or complex numbers:

-->[1 2;3 4]==[1 2;5 4]
 ans =
 
 T T 
 F T 
 
-->"abx" ~= "abq"
 ans =
 
 T 
 
-->1+2*%i == 1+2*%i
 ans =
 
 T 
 
-->

Logical operators in Scilab

In order to perform logical operation in Scilab we can use AND, OR and NOT operators. The logical operators are mostly used within conditional loop like: if, while, etc.

Logical operator Scilab symbol Example
OR | -->%F | %T

ans =
T

AND & -->%F & %T

ans =
F

NOT ~ -->~%F

ans =
T

The logical operators can be used in order to test several conditions in the same time:

-->temperature = rand()*100

temperature = 
 21.132487 
 
-->pressure = rand()*10

pressure = 
 7.5604385 
 
-->operation = "NOMINAL"

operation = 
 NOMINAL 
 
-->((temperature > 100) | (pressure > 20)) & (operation == "NOMINAL")

ans = 
 F 
 
-->

I think these are the Scilab operators that you’ll use in your applications. If you can manage these simple operators you can build complex application, they are basics in Scilab programming.

For any questions, observations and queries regarding Scilab variables use the comment form below.

Don’t forget to Like, Share and Subscribe!

2 Comments

  1. Sahana
  2. Andrew Brown

Reply Cancel Reply

Ad Blocker Detected

Dear user, Our website provides free and high quality content by displaying ads to our visitors. Please support us by disabling your Ad blocker for our site. Thank you!

Refresh