Basicelement01

Last-modified: 2025-03-20 (木) 22:44:22

Basic elements of the language
Table of contents

  1. Creating real variables
  2. Variable names
  3. Comments and continuation
  4. Elementary mathematical functions
  5. Pre-defined mathematical variables
  6. BooleansComplex numbersIntegers
  7. Overview of integers
  8. Conversion between integersCircular integers and portability issues
  9. StringsDynamic type of variables

Scilab はインタープリタ型言語です。つまり、非常に動的な方法で変数を操作できます。このセクションでは、言語の基本的な機能、つまり実変数の作成方法と、実変数に適用できる基本的な数学関数について説明します。Scilab がこれらの機能だけを提供していたら、それはただのスーパーデスクトップ計算機に過ぎません。幸いなことに、それはもっとたくさんあります。これが残りのセクションの主題です。そこでは、ブール値、複素数、整数、文字列など、他の種類の変数を管理する方法を示します。最初は奇妙に思えるかもしれませんが、最初に述べておく価値はあります。Scilabでは、すべてが行列です。より正確には、すべての実数、複素数、ブール値、整数、文字列、多項式変数は行列であると記述する必要があります。リストやその他の複雑なデータ構造 (tlist や mlist など) は行列ではありません (ただし、行列を含むことはできます)。これらの複雑なデータ構造については、このドキュメントでは説明しません。これが、行列の紹介から始めることができる理由です。それでも、Scilab 行列は実際にはこれらの基本的な構成要素の特別な構成であるため、最初に基本的なデータ型を紹介することにしました。Scilab では、実数と複素数を扱うことができます。コンテキストが十分に明確でないと、常に混乱が生じます。以下では、実変数と記述する場合、内容が複素数ではない変数を参照します。ほとんどの場合、実変数と複素変数は非常によく似た動作をしますが、複素数データを処理する必要がある場合は、特別な注意が必要です。プレゼンテーションが煩雑になるため、実変数のみを検討し、必要な場合にのみ複素変数に特別な注意を払うことで、ほとんどの説明を簡略化します。

Creating real variables

Scilab is an interpreted language, which implies that there is no need to declare a variable before using it. Variables are created at the moment where they are first set. In [1]: x = 1

x = x * 2  Out [1]:   x  =
  1.
x  =
  2.

The value of the variable is displayed each time a statement is executed. That behavior can be suppressed if the line ends with the semicolon ; character. In [2]: y = 1;
y = y * 2; All the common algebraic operators presented in table are available in Scilab. Notice that the power operator is represented by the hat $\hat{\;}$ character so that computing x2 in Scilab is performed by the x ^ 2 expression.Scilab elementary mathematical operators:

  1. , addition -, subtraction $*$, multiplication /, right division, i.e. x/y=xy-1 $\backslash$, left division, i.e. x\y=x-1y $\hat{\;}$, power, i.e. xy $'$, transpose conjugate

Variable names
Variable names may be as long as the user wants. All ASCII letters from a to z, from A to Z and digits from 0 to 9 are allowed, with the additional characters %, _, !, $, ?. Notice though that variable names, whose first letter is %, often have
a special meaning in Scilab (pre-defined mathematical variables...).Scilab is case sensitive, which means that upper and lower case letters are considered to be different by Scilab. In [3]: A = 2;

a = 1;
A
a  Out [3]:   A  =
  2.
a  =
  1.

Comments and continuation lines
Any line which begins with two slashes // is considered by Scilab as a comment and is ignored. To comment out a block of lines, use the /* ... */ comments as the C language.When an executable statement is too long to be written on a single line, the second and subsequent lines are called continuation lines. In Scilab, any line which ends with two dots is considered to be the start of a new continuation line.// This is my comment.

--> x = 1 ..
 > + 2 ..
 > + 3 ..
 > + 4
x      10.
  10.

Elementary mathematical functions
This section present a list of elementary mathematical functions.
Most of these functions take one input argument and return one output argument. These functions are vectorized in the sense that their input and output arguments are matrices. As a consequence, we can compute data with higher performance, without any loop.In the following example, we use the cos and sin functions and check the equality cos(x)2+sin(x)2=1. In [4]: x = cos(2)

y = sin(2)
x^2 + y^2  Out [4]:   x  =
 -0.4161468
y  =
  0.9092974
ans  =
  1.

For more details on Scilab elementary mathematical functions:
trigonometry functionslog - exp - power functionssum - prod - min - max functions

Pre-defined mathematical variables
In Scilab, several mathematical variables are pre-defined variables, whose names begin with a percent % character.
The variables which have a mathematical meaning are summarized below:
%i, the imaginary number%e, Euler's number%pi, the mathematical constant π
In the following example, we use the variable %pi to check the mathematical equality cos(x)2+sin(x)2=1. In [5]: c = cos(%pi)

s = sin(%pi)
c^2 + s^2  Out [5]:   c  =
 -1.
s  =
  1.225D-16
ans  =
  1.

The fact that the computed value of sin(π) is not exactly equal to 0 is a consequence of the fact that Scilab stores the real numbers with floating point numbers, that is, with limited precision. Booleans
Boolean variables can store true or false values. In Scilab, true is written with %t or %T and false is written with %f or %F.
This list presents the several comparison operators which are available in Scilab. These operators return boolean values and take as input arguments all basic data types (i.e. real and complex numbers, integers and strings). Comparison operators:
a&b, logical anda|b, logical or~a, logical nota == b true if the two expressions are equala ~= b or a <>b true if the two expressions are differenta < b true if a is lower than ba > b true if a is greater than ba <= b true if a is lower or equal to ba >= b true if a is greater or equal to b

In [6]:  a = %t
b = (0 == 1)
a & b  Out [6]:   a  =
 T
b  =
 F
ans  =
 F

Complex numbers
Scilab provides complex numbers, which are stored as pairs of floating point numbers.
The pre-defined variable %i represents the mathematical imaginary number i which satisfies i2=-1.
All elementary functions previously presented before, such as sin for example, are overloaded for complex numbers. This means that if their input argument is a complex number, the output is a complex number.
Here is Scilab complex numbers elementary functions:
real: real partimag: imaginary partimult: multiplication by i, the imaginary unitaryisreal: returns true if the variable has no complex entry
In the following example, we set the variable x to 1+i, and perform several basic operations on it, such as retrieving its real and imaginary parts. Notice how the single quote operator, denoted by ', is used to compute the conjugate of a complex number. In [7]: x = 1 + %i

isreal(x)
x'
y = 1 - %i
real(y)
imag(y)  Out [7]:   x  =
  1. + i
ans  =
 F
ans  =
  1. - i
y  =
  1. - i
ans  =
  1.
ans  =
 -1.

We finally check that the equality (1+i)(1-i)=1-i2=2
is verified by Scilab. In [8]: x * y Out [8]: ans =

  2. + 0.i

Integers
We can create various types of integer variables with Scilab.The functions that create such integers are: int8, int16, int32, int64, uint8, uint16, int32, int64.
In this section, we first review the basic features of integers, which are associated with a particular range of values. Then we analyze the conversion between integers.
In the final section, we consider the behaviour of integers at the boundaries and focus on portability issues. Overview of integers
There is a direct link between the number of bits used to store an integer and the range of values that the integer can manage. The range of an integer variable depends on the number of its bits:
An n-bit signed integer takes its values from the range [-2n-1,2n-1-1].An n-bit unsigned integer takes its values from the range [0,2n-1].
For example, an 8-bit signed integer, as created by the int8 function, can store values in the range [-27,27-1], which simplifies to [-128,127].
Here is the map from the type of integer to the corresponding range of values:
y = int8(x), a 8-bit signed integer in [-27,27-1]=[-128,127]y = uint8(x), a 8-bit unsigned integer in [0,28-1]=[0,255]y = in16(x), a 16-bit signed integer in [-215,215-1]=[-32768,32767]y = uint16(x), a 16-bit unsigned integer in [0,216-1]=[0,65535]y = int32(x), a 32-bit signed integer in [-231,231-1]y = uint32(x), a 32-bit unsigned integer in [0,232-1]=[0,4294967295]y = int64(x), a 64-bit signed integer in [-263,263-1]y = uint64(x), a 64-bit unsigned integer in [0,264-1]=[0,18446744073709551615]
In the following session, we check that an unsigned 32-bit integer has values inside the range [0,232-1], which simplifies to [0,4294967295]. In [9]: format(25)

n = 32
2^n-1
i = uint32(0)
j = i - 1
k = j + 1
format(10)  Out [9]:   n  =
  32.
ans  =
  4294967295.
i  =
 0
j  =
 4294967295
k  =
 0

Conversion between integers
There are functions which convert to and from integer data types:
iconvert, conversion to integer representationinttype, type of integers
The inttype function inquires about the type of an integer variable. Depending on the type, the function returns a corresponding value, as
1 & 8-bit signed integer,2 & 16-bit signed integer,4 & 32-bit signed integer,11 & 8-bit unsigned integer,12 & 16-bit unsigned integer,14 & 32-bit unsigned integer.
When two integers are added, the types of the operands are analyzed: the resulting integer type is the larger, so that the result can be stored. In the following script, we create an 8-bit integer i (which is associated with inttype=1) and a 16-bit integer j (which is associated with intype==2).
The result is stored in k, a 16-bit signed integer. In [10]: i = int8(1)

inttype(i)
j = int16(2)
inttype(j)
k = i + j
inttype(k)  Out [10]:   i  =
 1
ans  =
  1.
j  =
 2
ans  =
  2.
k  =
 3
ans  =
  2.

Circular integers and portability issues
The behaviour of integers at the range boundaries deserves a particular analysis, since it is different from software to software.
In Scilab, the behaviour is circular, that is, if an integer at the upper limit is incremented, the next value is at the lower limit.An example of circular behaviour is given in the following session, where In [11]: uint8(0 + (-4:4))

uint8(2^8 + (-4:4))
int8(2^7 + (-4:4))  Out [11]:   ans  =
 252  253  254  255  0  1  2  3  4
ans  =
 252  253  254  255  0  1  2  3  4
ans  =
 124  125  126  127 -128 -127 -126 -125 -124

This is in contrast with other mathematical packages, such as Octave or Matlab.
In these packages, if an integer is at the upper limit, the next integer stays at the upper limit. In the following Octave session, we execute the same computations
as previously.octave-3.2.4.exe:1> uint8(0+(-4:4))

ans =
 0  0  0  0  0  1  2  3  4
octave-3.2.4.exe:5> uint8(2^8+(-4:4))
ans =
 252  253  254  255  255  255  255  255  255
octave-3.2.4.exe:2> int8(2^7+(-4:4))
ans =
 124  125  126  127  127  127  127  127  127

The Scilab circular way gives a greater flexibility in the processing of integers, since we can write algorithms with fewer if statements.
But these algorithms must be checked, particularly if they involve the boundaries of the integer range. Moreover, translating a script from another computation system
into Scilab may lead to different results. Strings

Strings can be stored in variables, provided that they are delimited by simple ' or double quotes ".

The concatenation operation is available from the + operator. In [12]: x = "foo"
y = "bar"

x + y  Out [12]:   x  =
 "foo"
y  =
 "bar"
ans  =
 "foobar"

For more details on strings, see help page: strings. Dynamic type of variables
When we create and manage variables, Scilab changes the variable type dynamically. This means that we can create a real value, and then put a string variable in it. In [13]: x = 1

x + 1
x = "foo"
x + "bar"  Out [13]:   x  =
  1.
ans  =
  2.
x  =
 "foo"
ans  =
 "foobar"

We emphasize here that Scilab is not a typed language, that is, we do not have to declare the type of a variable before setting its content.
Moreover, the type of a variable can change during the life of the variable.