Data Types
SCILAB recognizes several data types. Scalar objects are constants, Booleans, polynomials, strings and rationals (quotients of polynomials). These objects in turn allow to define matrices which admit these scalars as entries. Other basic objects are lists, typed-lists and functions. Only constant and Boolean sparse matrices are defined. The objective of this chapter is to describe the use of each of these data types.
Special Constants
SCILAB provides special constants similar to that of MATLAB. In general, these constants have % before them. These variables are considered as "predefined". They are protected, cannot be deleted and are not saved by the save command. It is possible for a user to have his own "predefined" variables by using the predef command.
The table lists the special constants and their functions,
%i | represents sqrt(-1) |
%pi | P = 3.1415927 ..... |
%e | trigonometric constant e = 2.7182818 |
%eps | constant representing the precision of the machine |
%nan | not a number |
%inf | infinity |
%s | is the polynomial s=poly(0,’s’) with symbol s. |
%t | Boolean constant which stand for true and %t is the same as 1==1 |
%f | Boolean constant which stand for false and %f is the same as ~%t. |
Matrices of Character Strings
Character strings can be created by using single or double quotes. Concatenation of strings is performed by the + operation. Matrices of character strings are constructed as ordinary matrices, e.g. using brackets. A very important feature of matrices of character strings is the capacity to manipulate and create functions.
Furthermore, symbolic manipulation of mathematical objects can be implemented using matrices of character strings. The following illustrates some of these features.
s =['x' 'y';'z' 'w+v']
produces
s =
! x y !
!
!
! z w+v !
and
ss =trianfml(s)
produces
! z
w+v
!
!
!
!0 z*y-x*(w+v) !
Substituting the value for x, y, z, and w
x=1;y=2;z=3;w=4;v=5;
and
evstr(ss)
This produces
ans =
! 3. 9. !
! 0. - 3. !
Polynomials and Polynomial Matrices
Polynomials are easily created and manipulated in SCILAB. Manipulation of polynomial matrices is essentially identical to that of constant matrices. The poly primitive in SCILAB can be used to specify the coefficients of a polynomial or the roots of a polynomial.
p=poly([1 2],'s') //polynomial defined by its roots
produces,
p =
2
2 - 3s + s
and,
q=poly([1 2],'s','c') //polynomial defined by its coefficients
This produces,
q =
1 + 2s
For example,
q/p
produces,
ans =
1 + 2s
----------
2
2 - 3s + s
Boolean Matrices
Boolean constants are %t and %f. They can be used in Boolean matrices. The syntax is the same as for ordinary matrices i.e. they can be concatenated, transposed, etc... Operations symbols used with Boolean matrices or used to create Boolean matrices are == and ˜.
If B is a matrix of Booleans or(B) and and(B) perform the logical or and and.
For example, typing
%t
produces,
%t =
T
Similarly,
[ 3,3] == [3,4]
This produces
ans =
! T F !
and
s = 1:6 ; s(s>3)
will display,
ans =
! 4. 5. 6. !
Similarly,
A = [%t %f %t %f], B = [%f %t %f %t]
produces,
A =
! T F T F !
B =
! F T F T !
and
A|B // logical OR
ans =
! T T T T !
A&B // logical AND
ans =
! F F F F !
Lists
SCILAB has a list data type. The list is a collection of data objects not necessarily of the same type. A list can contain any of the already discussed data types (including functions) as well as other lists. Lists are useful for defining structured data objects. There are two kinds of lists, ordinary lists and typed-lists. A list is defined by the list function.
Here is a simple example:
ls = list(2,%i,'f',ones(3,3)) // a list made of four entires
This produces,
ls =
ls(1)
2.
ls(2)
i
ls(3)
f
ls(4)
! 1. 1. 1. !
! 1. 1. 1. !
! 1. 1. 1. !
To extract the a entry from a list you have to use listname(listindex), for example,
ls(4)
ans =
! 1. 1. 1. !
! 1. 1. 1. !
! 1. 1. 1. !
You can also create a nested list.
ls(2) = list( %t, rand(2,2,'normal')) // ls(2) is now a list
ls(2)(1)
T
ls(2)(2)
! .6380837 - .6834217 !
! .2546697 .8145126 !
Typed lists have a specific first entry. This first entry must be a character string (the type) or a vector of character string (the first component is then the type, and the following elements the names given to the entries of the list). The general format is,
tlist(typ,a1,....an )
where typ argument specifies the list type. and a1...an is the the object.
Typed lists entries can be manipulated by using character strings (the names) as shown below.
lst = tlist(['random numbers';'Name';'Example'], ' Uniform',rand(3,3,'uniform'))
This produces,
lst(1)
!random numbers !
!
!
!Name
!
!
!
!Example
!
lst(2)
Uniform
lst(3)
! .2113249 .3303271 .8497452 !
! .7560439 .6653811 .6857310 !
! .0002211 .6283918 .8782165 !
And,
lst('Name') // same as lst(2)
ans =
Uniform
Functions
Functions are collections of commands which are executed in a new environment thus isolating function variables from the original environments variables. Functions can be created and executed in a number of different ways. Furthermore, functions can pass arguments, have programming features such as conditionals and loops, and can be recursively called. Functions can be arguments to other functions and can be elements in lists. The most useful way of creating functions is by using a text editor, however, functions can be created directly in the SCILAB environment using the deff primitive.
Let us workout a simple function in the command window. the function will convert the input into dB.
deff('[out] = dB(inp)',' out = 10*log10(inp)')
Let us try with some value,
db(10)
produces,
ans =
10.
Usually functions are defined in a file using an editor and loaded into SCILAB with getf('filename'). This can be done also by clicking in the File operation button. This latter syntax loads the function(s) in filename and compiles them.
The first line of filename must be as follows:
function [y1,...,yn]=macname(x1,...,xk)
where the yi’s are output variables and the xi’s the input variables.
Libraries
Libraries are collections of functions which can be either automatically loaded into the SCILAB environment when SCILAB is called, or loaded when desired by the user. Libraries are created by the lib command. Examples of libraries are given in the SCIDIR/macros directory.
Note that in these directory there is an ASCII file "names" which contains the names of each function of the library, a set of .sci files which contains the source code of the functions and a set of .bin files which contains the compiled code of the functions. The Makefile invokes SCILAB for compiling the functions and generating the .bin files. The compiled functions of a library are automatically loaded into SCILAB at their first call.
Objects
SCILAB objects can be viewed by using the function typeof. The general format is,
typeof(object)
For example,
d = 'suren';
typeof(d)
ans =
string
The following table contains the list of SCILAB objects,
usual | for matrices with real or complex entries. |
polynomial | for polynomial matrices: coefficients can be real or complex. |
boolean | for boolean matrices. |
character | for matrices of character strings. |
function | for functions. |
rational | for rational matrices (syslin lists) |
state-space | for linear systems in state-space form (syslin lists). |
sparse | for sparse constant matrices (real or complex) |
boolean sparse | for sparse boolean matrices. |
list | for ordinary lists. |
tlist | for typed lists. |
state-space | (or rational) for syslin lists. |
library | for library definition. |