% Copyright INRIA

\chapter{Data Types}
\label{ch2}
\index{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.

\section{Special Constants}
\label{s2.1}
\index{data types!constants}
\index{constants}

	Scilab provides special constants {\tt \%i}, {\tt \%pi},
{\tt \%e}, and {\tt \%eps} as primitives.  The {\tt \%i} constant
represents $\sqrt{-1}$, {\tt \%pi} is $\pi=3.1415927\cdots$ , {\tt \%e}
is the trigonometric constant $e=2.7182818\cdots$, and {\tt \%eps}
is a constant representing the precision of the machine ({\tt \%eps}
is the biggest number for which $1+\mbox{\tt \%eps}=1$). {\tt \%inf}
and {\tt \%nan} stand for ``Infinity'' and ``NotANumber'' respectively.
{\tt \%s} is the polynomial s={\tt poly(0,'s')} with symbol {\tt s}.

(More generally, given a vector {\tt rts}, {\tt p=poly(rts,'x')}
defines the polynomial p(x) with variable {\tt x} and such 
that {\tt roots(p)} = {\tt rts}).

Finally boolean constants are {\tt \%t} and {\tt \%f} which stand for
``true'' and ``false'' respectively. Note that {\tt \%t} is the
same as {\tt 1==1} and {\tt \%f} is the same as {\verb!~%t!}.

These variables are considered as ``predefined''. They are protected, cannot 
be deleted and are not saved by the {\tt save} command. It is possible
for a user to have his own ``predefined'' variables by using the
{\tt predef} command. The best way is probably to set these special variables
in his own startup file {\tt <home dir>/.scilab}.
Of course, the user can use e.g. {\tt i=sqrt(-1)} instead of {\tt \%i}.

\section{Constant Matrices}
\label{s2.2}
\index{data types!matrices}

	Scilab considers a number of data objects as matrices.  
Scalars and  vectors are all considered as matrices.  The details of the use
of these objects are revealed in the following Scilab sessions.

\paragraph{Scalars}\index{scalars}
Scalars are either real or complex numbers.  The values of
scalars can be assigned to variable names chosen by the user.
\begin{verbatim}
--> a=5+2*%i
 a         =
 
    5. + 2.i  
 
--> B=-2+%i;

--> b=4-3*%i
 b  =
 
    4. - 3.i 
 
--> a*b
 ans  =
 
    26. - 7.i  
 
-->a*B
 ans  =
 
  - 12. + i    
\end{verbatim}
Note that Scilab evaluates immediately lines that
end with a carriage return.  Instructions that ends with a semi-colon
are evaluated but are not displayed on screen. 

\paragraph{Vectors}\index{vectors}
The usual way of creating vectors is as follows, using 
commas (or blanks) and semi-columns:
\begin{verbatim}
 
--> v=[2,-3+%i,7]
 v         =
 
!   2.  - 3. + i      7. !
 
--> v'
 ans       =
 
!   2.       !
! - 3. - i   !
!   7.       !
 
--> w=[-3;-3-%i;2]
 w         =
 
! - 3.       !
! - 3. - i   !
!   2.       !
 
--> v'+w
 ans       =
 
! - 1.       !
! - 6. - 2.i !
!   9.       !
 
--> v*w
 ans       =
 
    18.  
 
--> w'.*v
 ans       =
 
! - 6.    8. - 6.i    14. !
\end{verbatim}
Notice that vector elements that are separated by commas (or by blanks)
yield row vectors and those separated by semi-colons give column
vectors. The empty matrix is \verb![]! ; it has zero rows and zero columns.
Note also that a single quote is used for transposing a 
vector\index{vectors!transpose} 
(one obtains the complex conjugate for complex entries).  Vectors of same
dimension can be added and subtracted.  The scalar product of a row and
column vector is demonstrated above.  Element-wise
multiplication ({\tt .*}) and division ({\tt ./}) is also possible 
as was demonstrated.

Note with the following example the role of the position of the blank:
\begin{verbatim}
-->v=[1 +3]
 v  =
 
!   1.    3. !
 
-->w=[1 + 3]
 w  =
 
!   1.    3. !
 
-->w=[1+ 3] 
 w  =
 
    4. 
 
-->u=[1, + 8- 7]
 u  =
 
!   1.    1. !

\end{verbatim}

	Vectors\index{vectors!incremental} of elements which increase
or decrease incrementely are constructed as follows 
\begin{verbatim}
 
--> v=5:-.5:3
 v         =
 
!   5.    4.5    4.    3.5    3. !
\end{verbatim}
The resulting vector begins with the first value and ends
with the third value stepping in increments of the second value.
When not specified the default increment is one.  A constant vector
can be created using the {\tt ones}\index{ones@{\tt ones}}\index{vectors!constant} and {\tt zeros} facility 
\begin{verbatim}
 
--> v=[1 5 6]
 v         =
 
!   1.    5.    6. !
 
--> ones(v)
 ans       =
 
!   1.    1.    1. !
 
--> ones(v')
 ans       =
 
!   1. !
!   1. !
!   1. !
 
--> ones(1:4)
 ans       =
 
!   1.    1.    1.    1. !
 
--> 3*ones(1:4)
 ans       =
 
!   3.    3.    3.    3. !

-->zeros(v)
 ans  =
 
!   0.    0.    0. !
 
-->zeros(1:5)
 ans  =
 
!   0.    0.    0.    0.    0. !
 
\end{verbatim}
Notice that {\tt ones} or {\tt zeros} replace its vector argument by a vector 
of equivalent dimensions filled with ones or zeros.

\paragraph{Matrices}\index{matrices}\index{data types!matrices}
Row elements are separated by commas or spaces
 and column elements by semi-colons.  Multiplication
of matrices by scalars, vectors, or other matrices is in the usual
sense.  Addition and
subtraction of matrices is element-wise and element-wise
multiplication and division can be accomplished with the {\tt .*}
and {\tt ./} operators.
\begin{verbatim}
 
--> A=[2 1 4;5 -8 2]
 A         =
 
!   2.    1.    4. !
!   5.  - 8.    2. !
 
--> b=ones(2,3)
 b         =
 
!   1.    1.    1. !
!   1.    1.    1. !
 
--> A.*b
 ans       =
 
!   2.    1.    4. !
!   5.  - 8.    2. !
 
--> A*b'
 ans       =
 
!   7.    7. !
! - 1.  - 1. !
\end{verbatim}
Notice that the {\tt ones}\index{ones@{\tt ones}}\index{matrices!constant}
operator with two real numbers as arguments separated
by a comma creates a matrix of ones using the arguments as
dimensions (same for {\tt zeros}).
Matrices can be used as elements to larger 
matrices\index{matrices!block construction}.  Furthermore,
the dimensions of a matrix can be changed.
\begin{verbatim}
 
--> A=[1 2;3 4]
 A         =
 
!   1.    2. !
!   3.    4. !
 
--> B=[5 6;7 8];
 
--> C=[9 10;11 12];
 
--> D=[A,B,C]
 D         =
 
!   1.    2.    5.    6.    9.     10. !
!   3.    4.    7.    8.    11.    12. !
 
--> E=matrix(D,3,4)
 E         =
 
!   1.    4.    6.    11. !
!   3.    5.    8.    10. !
!   2.    7.    9.    12. !

-->F=eye(E)
 F  =
 
!   1.    0.    0.    0. !
!   0.    1.    0.    0. !
!   0.    0.    1.    0. !
 
-->G=eye(4,3)
 G  =
 
!   1.    0.    0. !
!   0.    1.    0. !
!   0.    0.    1. !
!   0.    0.    0. !

\end{verbatim}
Notice that matrix {\tt D} is created by using other matrix
elements.  The {\tt matrix}\index{matrix@{\tt matrix}} 
primitive creates a new matrix {\tt E} with the elements of the
matrix {\tt D} using
the dimensions specified by the second two arguments.  The element
ordering in the matrix {\tt D} is top to bottom and then left to right
which explains the ordering of the re-arranged matrix in {\tt E}.

The function {\tt eye} creates an $m\times n$ matrix with 1 along the main
diagonal (if the argument is a matrix {\tt E} , $m$ and $n$ are the 
dimensions of {\tt E} ) .

Sparse constant matrices are defined through their nonzero entries 
(type help {\tt sparse} for more details). Once defined, they are
manipulated as full matrices.


\section{Matrices of Character Strings}
\label{s2.3}
\index{data types!character strings}
\index{character strings}

	Character strings can be created by using single or double quotes.
Concatenation of strings is performed by the {\tt +} 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.
\begin{verbatim}
 
--> A=['x' 'y';'z' 'w+v']
 A         =
 
!x  y    !
!        !
!z  w+v  !
 
--> At=trianfml(A)
 At        =
 
!z  w+v          !
!                !
!0  z*y-x*(w+v)  !
 
--> x=1;y=2;z=3;w=4;v=5;
 
--> evstr(At)
 ans       =
 
!   3.    9. !
!   0.  - 3. !

\end{verbatim}
Note that in the above Scilab session the function 
{\tt trianfml}\index{trianfml@{\tt trianfml}}\index{symbolic triangularization}
performs the symbolic triangularization of the matrix {\tt A}.
The value of the resulting symbolic matrix can be obtained by
using {\tt evstr}.  

	A very important aspect of character strings is that they
can be used to automatically create new functions (for more on functions
see Section~\ref{s4.2}).  An example of automatically creating a 
function is illustrated in the following Scilab session where it is
desired to study a polynomial of two variables {\tt s} and {\tt t}.
Since polynomials in two independent variables are not directly 
supported in Scilab, we can construct a new data structure using
a list (see Section~\ref{s2.5}).
The polynomial to be studied is $(t^2+2t^3)-(t+t^2)s+ts^2+s^3$.
\begin{verbatim}
-->getf("macros/make_macro.sci");
 
-->s=poly(0,'s');t=poly(0,'t');
 
-->p=list(t^2+2*t^3,-t-t^2,t,1+0*t);
 
-->pst=makefunction(p)  //pst is a function t->p (number -> polynomial)
 pst       =
 
[p]=pst(t)
 
-->pst(1)
 ans       =
 
              2   3  
    3 - 2s + s + s   
\end{verbatim}
Here the polynomial is represented by the command which puts
the coefficients of the variable {\tt s} in the list {\tt p}.
The list {\tt p} is then processed by the function {\tt makefunction}
which makes a new function {\tt pst}.  The contents of the new function
can be displayed and this function can be evaluated
at values of {\tt t}.  The creation of the new function {\tt pst}
is accomplished as follows

\input{macros/make_macro.vtex}

Here the function {\tt makefunction} takes the list {\tt p} and creates the
function {\tt pst}.  Inside of {\tt makefunction} there is a call to another 
function {\tt makestr} which makes the string which represents each 
term of the new two variable polynomial.  The functions {\tt addf} and
{\tt mulf} are used for adding and multiplying strings (i.e. 
{\tt addf(x,y)} yields the string {\tt x+y}).  Finally, the 
essential command for creating the new function 
is the primitive {\tt deff}.  The {\tt deff} primitive 
creates a function defined by two matrices
of character strings.  Here the 
function {\tt p} is defined by the two character strings
{\tt '[p]=newfunction(t)'} and {\tt text} where the string {\tt text}
evaluates to the polynomial in two variables.

\section{Polynomials and Polynomial Matrices}
\label{s2.4}
\index{data types!polynomials}
\index{polynomials}

	Polynomials are easily created and manipulated in Scilab.
Manipulation of polynomial matrices is essentially identical
to that of constant matrices.
The {\tt poly}\index{poly@{\tt poly}} 
primitive in Scilab can be used to specify the coefficients
of a polynomial or the roots of a polynomial.
\begin{verbatim}
-->p=poly([1 2],'s')   //polynomial defined by its roots
 p         =
 
              2  
    2 - 3s + s   
 
-->q=poly([1 2],'s','c')  //polynomial defined by its coefficients
 q         =
 
    1 + 2s   
 
-->p+q
 ans       =
 
             2  
    3 - s + s   
 
-->p*q
 ans       =
 
              2    3  
    2 + s - 5s + 2s   
 
--> q/p
 ans       =
 
      1 + 2s     
    -----------  
              2  
    2 - 3s + s   
\end{verbatim}
Note that the polynomial {\tt p} has the {\em roots}
1 and 2 whereas the polynomial {\tt q} has the {\em coefficients}
1 and 2.  It is the third argument in the {\tt poly} primitive which
specifies the coefficient flag option.  
In the case where the first argument of {\tt poly} is a square matrix
and the roots option is in effect the result is the characteristic
polynomial of the matrix.
\begin{verbatim}
 
--> poly([1 2;3 4],'s')
 ans       =
 
              2  
  - 2 - 5s + s   
\end{verbatim}
Polynomials can be added,
subtracted, multiplied, and divided, as usual, but only between polynomials
of same formal variable.

	Polynomials, like real and complex constants, can be used
as elements in matrices.  This is a very useful feature of Scilab
for systems theory.
\begin{verbatim}
 
-->s=poly(0,'s');
 
-->A=[1 s;s 1+s^2]
 A         =
 
!   1     s      !
!                !
!              2 !
!   s     1 + s  !
 
--> B=[1/s 1/(1+s);1/(1+s) 1/s^2]
 B         =
 
!   1           1    !
! ------      ------ !
!   s         1 + s  !
!                    !
!     1       1      !
!    ---     ---     !
!              2     !
!   1 + s     s      !

\end{verbatim}
From the above examples it can be seen that matrices can be constructed
from polynomials and rationals.

\subsection{Rational polynomial simplification}
Scilab automatically performs pole-zero simplifications when the 
the built-in primitive {\tt simp} finds a common factor in the
numerator and denominator of a rational polynomial {\tt num/den}.
Pole-zero simplification is a difficult problem from a
numerical viewpoint and {\tt simp} function is usually conservative.
When making calculations with polynomials, it is sometimes desirable
to avoid pole-zero simplifications: this is possible by switching
Scilab into a ``no-simplify'' mode: \verb!help simp_mode!. The
function {\tt trfmod} can also be used for simplifying specific
pole-zero pairs.


\section{Boolean Matrices}
\index{data types!booleans}
\index{boolean}

Boolean constants are {\tt \%t} and {\tt \%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 {\tt ==} and \verb!~!. 

If {\tt B} is a matrix of booleans {\tt or(B)} and {\tt and(B)} 
perform the logical {\tt or} and {\tt and}.
\begin{verbatim}
-->%t
 %t  =
 
  T  
 
-->[1,2]==[1,3]
 ans  =
 
! T F !
 
-->[1,2]==1
 ans  =
 
! T F !
 
-->a=1:5; a(a>2)
 ans  =
 
!   3.    4.    5. !

-->A=[%t,%f,%t,%f,%f,%f];   
 
-->B=[%t,%f,%t,%f,%t,%t]
 B  =
 
! T F T F T T !
 
-->A|B
 ans  =
 
! T F T F T T !

-->A&B
 ans  =
 
! T F T F F F !
 
\end{verbatim} 

Sparse boolean matrices are generated when, e.g., two constant sparse
matrices are compared. These matrices are handled as ordinary boolean
matrices.

\section{Lists}
\label{s2.5}
\index{data types!lists}
\index{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 {\tt list} function. Here is a simple
example:

\begin{verbatim}
-->L=list(1,'w',ones(2,2))  //L is a list made of 3 entries
 L  =
 
 
       L(1)
 
    1.  
 
       L(2)
 
 w   
 
       L(3)
 
!   1.    1. !
!   1.    1. !

-->L(3)   //extracting entry 3 of list L
 ans  =
 
!   1.    1. !
!   1.    1. !

-->L(3)(2,2)  //entry 2,2 of matrix L(3)
 ans  =
 
    1.  

-->L(2)=list('w',rand(2,2)) //nested list: L(2) is now a list
 L  =
 
 
       L(1)
 
    1.  
 
       L(2)
 
 
        L(2)(1)
 
 w   
 
        L(2)(2)
 
!   0.6653811    0.8497452 !
!   0.6283918    0.6857310 !
 
       L(3)
 
!   1.    1. !
!   1.    1. !

-->L(2)(2)(1,2)  //extracting entry 1,2 of entry 2 of L(2)
 ans  =
 
    0.8497452  

-->L(2)(2)(1,2)=5; //assigning a new value to this entry.

\end{verbatim}

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). Typed lists entries can be manipulated
by using character strings (the names) as shown below.

\begin{verbatim}
-->L=tlist(['Car';'Name';'Dimensions'],'Nevada',[2,3])
 L  =
 
 
       L(1)
 
!Car         !
!            !
!Name        !
!            !
!Dimensions  !
 
       L(2)
 
 Nevada   
 
       L(3)
 
!   2.    3. !

-->L('Name')    //same as L(2)
 ans  =
 
 Nevada   
 
-->L('Dimensions')(1,2)=2.3

 L  =
 
 
       L(1)
 
!Car         !
!            !
!Name        !
!            !
!Dimensions  !
 
       L(2)
 
 Nevada   
 
       L(3)
 
!   2.    2.3 !
 

-->L(3)(1,2)
 ans  =
 
    2.3  

-->L(1)(1)
 ans  =
 
 Car
\end{verbatim}
An important feature of typed-lists is that it is possible to define
operators acting on them (overloading), i.e., it is possible
to define e.g. the multiplication \verb!L1*L2! of the two typed lists 
\verb!L1! and \verb!L2!. An example of use is given below, where
linear systems manipulations (concatenation, addition,
multiplication,...) are done by such operations.

\section{Linear system representation}
Linear systems are treated as specific typed lists {\tt tlist}.
The basic function which is used for defining linear systems is {\tt syslin}.
This function receives as parameters the constant matrices which
define a linear system in state-space form or, in the case of
system in transfer form, its input must be a rational matrix.
To be more specific, the calling sequence of {\tt syslin} is
either {\tt Sl=syslin('dom',A,B,C,D,x0)} or {\tt Sl=syslin('dom',trmat)}.
{\tt dom} is one of the character strings {\tt 'c'} or {\tt 'd'}
for continuous time or discrete time systems respectively.
It is useful to note that {\tt D} can be a polynomial matrix 
(improper systems); {\tt D} and {\tt x0} are optional arguments.
{\tt trmat} is a rational matrix i.e. it is defined as a matrix
of rationals (ratios of polynomials). {\tt syslin} just converts
its arguments (e.g. the four matrices A,B,C,D) into a typed
list {\tt Sl}. For state space representation {\tt Sl} is 
the {\tt tlist(['lss','A','B','C','D'],A,B,C,D,'dom')}. This tlist
representation allows to access the A-matrix i.e. the second entry of
{\tt Sl} by the syntax {\tt Sl('A')} (equivalent to {\tt Sl(2)}).
Conversion from a representation to another is done by {\tt ss2tf}
or {\tt tf2ss}. Improper systems are also treated. {\tt syslin} 
defines linear systems as specific {\tt tlist}. ({\tt help syslin}).

\input{diary/syslin.dia}

	The list representation allows manipulating linear systems as
abstract data objects.  For example, the linear system can be combined
with other linear systems or the transfer function representation of
the linear system can be obtained as was done above using {\tt ss2tf}.
Note that the transfer function representation of the linear system
is itself a tlist. 
	A very useful aspect of the manipulation of systems 
is that a system can be handled as a data object.
Linear systems can be 
inter-connected\index{linear systems!inter-connection of},
their representation
can easily be changed from state-space to transfer function
and vice versa.

	The inter-connection of linear systems can be made
as illustrated in Figure~\ref{f3.2}.
%
\begin{figure}
\center{
\begin{picture}(200,330)(0,-60)

\put(180,280){\makebox(0,0)[lb]{\tt S2*S1}}
\put(9,280){\circle{2}}
\put(10,280){\vector(1,0){20}}
\put(30,270){\framebox(30,20){$S_1$}}
\put(60,280){\vector(1,0){30}}
\put(90,270){\framebox(30,20){$S_2$}}
\put(120,280){\vector(1,0){20}}
\put(141,280){\circle{2}}

\put(180,220){\makebox(0,0)[lb]{\tt S1+S2}}
\put(29,220){\circle{2}}
\put(30,220){\line(1,0){20}}
\put(50,220){\circle*{2}}
\put(50,200){\line(0,1){40}}
\put(50,200){\vector(1,0){20}}
\put(50,240){\vector(1,0){20}}
\put(70,190){\framebox(30,20){$S_2$}}
\put(70,230){\framebox(30,20){$S_1$}}
\put(100,200){\line(1,0){20}}
\put(100,240){\line(1,0){20}}
\put(120,240){\vector(0,-1){15}}
\put(120,200){\vector(0,1){15}}
\put(120,220){\circle{10}}
\put(120,220){\framebox(0,0){$+$}}
\put(125,220){\vector(1,0){15}}
\put(141,220){\circle{2}}

\put(180,140){\makebox(0,0)[lb]{\tt [S1,S2]}}
\put(49,160){\circle{2}}
\put(49,120){\circle{2}}
\put(50,120){\vector(1,0){20}}
\put(50,160){\vector(1,0){20}}
\put(70,110){\framebox(30,20){$S_2$}}
\put(70,150){\framebox(30,20){$S_1$}}
\put(100,120){\line(1,0){20}}
\put(100,160){\line(1,0){20}}
\put(120,160){\vector(0,-1){15}}
\put(120,120){\vector(0,1){15}}
\put(120,140){\circle{10}}
\put(120,140){\framebox(0,0){$+$}}
\put(125,140){\vector(1,0){15}}
\put(141,140){\circle{2}}

\put(180,50){\makebox(0,0)[lb]{\tt [S1 ; S2]}}
\put(49,50){\circle{2}}
\put(50,50){\line(1,0){20}}
\put(70,50){\circle*{2}}
\put(70,30){\line(0,1){40}}
\put(70,30){\vector(1,0){20}}
\put(70,70){\vector(1,0){20}}
\put(90,20){\framebox(30,20){$S_2$}}
\put(90,60){\framebox(30,20){$S_1$}}
\put(120,30){\vector(1,0){20}}
\put(120,70){\vector(1,0){20}}
\put(141,30){\circle{2}}
\put(141,70){\circle{2}}

\put(180,-40){\makebox(0,0)[lb]{\tt S1/.S2}}
\put(70,-20){\circle{10}}
\put(70,-20){\framebox(0,0){$+$}}
\put(34,-20){\vector(1,0){30}}
\put(33,-20){\circle{2}}
\put(70,-60){\line(0,1){40}}
\put(70,-60){\line(1,0){20}}
\put(70,-20){\vector(1,0){20}}
\put(90,-70){\framebox(30,20){$S_2$}}
\put(90,-30){\framebox(30,20){$S_1$}}
\put(140,-60){\vector(-1,0){20}}
\put(120,-20){\line(1,0){20}}
\put(141,-20){\circle{2}}
\put(140,-60){\line(0,1){40}}
\end{picture}
}

\bigskip

\caption{Inter-Connection of Linear Systems}
\label{f3.2}
\end{figure}
%
For each of the possible inter-connections of two systems
{\tt S1} and {\tt S2} the command which makes the inter-connection
is shown on the right side of the corresponding block diagram in
Figure~\ref{f3.2}. Note that feedback interconnection is performed by
\verb!S1/.S2!. 

	The representation of linear systems can be in state-space
form or in transfer function form.  These two representations can
be interchanged by using the functions 
{\tt tf2ss}\index{linear systems!{\tt tf2ss}}\index{tf2ss@{\tt tf2ss}} and 
{\tt ss2tf}\index{linear systems!{\tt ss2tf}}\index{ss2tf@{\tt ss2tf}}
which change the representations of systems from transfer function
to state-space and from state-space to transfer function, respectively.
An example of the creation, the change in representation, and the
inter-connection of linear systems is demonstrated in the following
Scilab session.

\input{diary/connect.dia}

The above session is a bit long but illustrates some very important
aspects of the handling of linear systems.  First, two linear systems
are created in transfer function form using the function called 
{\tt syslin}\index{linear systems!{\tt syslin}}\index{syslin@{\tt syslin}}.
This function was used to label the systems in this example 
as being continuous (as opposed to discrete).  
The primitive {\tt tf2ss} is used to convert one of the
two transfer functions to its equivalent state-space representation
which is in list form (note that the function {\tt ssprint} creates a more
readable format for the state-space linear system).
The following multiplication of the two systems yields their
series inter-connection.  Notice that the inter-connection 
of the two systems is effected even though one of the systems is
in state-space form and the other is in transfer function form.
The resulting inter-connection is given in state-space form.
Finally, the function {\tt ss2tf} is used to convert the resulting
inter-connected systems to the equivalent transfer function representation.

\section{Functions (Macros)}
\label{s2.6}
\index{data types!functions}
\index{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 
{\tt deff}\index{deff@{\tt deff}}\index{functions!{\tt deff}} primitive.
\begin{verbatim}
 
--> deff('[x]=foo(y)','if y>0 then, x=1; else, x=-1; end')
 
--> foo(5)
 ans       =
 
    1.  
 
--> foo(-3)
 ans       =
 
  - 1.  
\end{verbatim}
Usually functions are defined in a file using an editor and loaded
into Scilab with {\tt getf('filename')}.
This can be done also by clicking in the {\tt File operation} button.
This latter syntax loads the function(s) in {\tt filename} and compiles
them.
The first line of {\tt filename} must be as follows:
\begin{verbatim}
function [y1,...,yn]=macname(x1,...,xk)
\end{verbatim}
where the {\tt yi}'s are output variables and the {\tt xi}'s the
input variables.

For more on the use and creation of functions see Section~\ref{s4.2}.

\section{Libraries}
\label{s2.7}
\index{data types!libraries}
\index{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 {\tt lib} command. Examples of librairies
are given in the {\tt 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 {\tt .sci} files which
contains the source code of the functions and a set of {\tt .bin} files
which contains the compiled code of the functions. The Makefile invokes
{\tt scilab} for compiling the functions and generating the {\tt .bin}
files. The compiled functions of a library are automatically loaded 
into Scilab at their first call.

\section{Objects}
We conclude this chapter by noting that the function {\tt typeof}
returns the type of the various Scilab objects. The following objects
are defined:
\begin{itemize}
\item{\tt usual} 	for matrices with real or complex entries.
\item{\tt polynomial} 	for polynomial matrices: coefficients can be 
real or complex.
\item{\tt boolean} 	for boolean matrices.
\item{\tt character} 	for matrices of character strings.
\item{\tt function} 	for functions.
\item{\tt rational} 	for rational matrices ({\tt syslin} lists)
\item{\tt state-space} 	for linear systems in state-space 
form ({\tt syslin} lists).
\item{\tt sparse} 	for sparse constant matrices (real or complex)
\item{\tt boolean sparse} for sparse boolean matrices.
\item{\tt list} 	for ordinary lists.
\item{\tt tlist}        for typed lists.
\item{\tt state-space (or rational)} for syslin lists.
\item{\tt library} 	for library definition.
\end{itemize}


\section{Matrix Operations}
The following table gives the syntax of the basic matrix operations
available in Scilab.

\begin{center}
\begin{tabular}{|c|c|}
\hline
SYMBOL & OPERATION 
\\ \hline \hline
\verb![ ]! & matrix definition, concatenation\\ \hline

\verb!;! & row separator\\ \hline

\verb!( )! & extraction \verb!m=a(k)! \\ \hline

\verb!( )! & insertion:  \verb!a(k)=m!  \\ \hline

\verb!'! & transpose \\ \hline

\verb!+! & addition  \\ \hline

\verb!-! & subtraction \\ \hline

\verb!*! & multiplication \\ \hline

\verb!\! & left division \\ \hline

\verb!/! & right division \\ \hline

\verb!^! &  exponent \\ \hline

\verb!.*! & elementwise multiplication  \\ \hline

\verb!.\! &  elementwise left division  \\ \hline

\verb!./! &  elementwise right division  \\ \hline

\verb!.^! &  elementwise exponent  \\ \hline

\verb!.*.! & kronecker product \\ \hline

\verb!./.! & kronecker right division \\ \hline

\verb!.\.! &  kronecker left division \\ \hline  
\end{tabular}
\end{center}

\section{Indexing}

The following sample sessions shows the flexibility which is offered
for extracting and inserting entries in matrices or lists.
For additional details enter \verb!help extraction! 
or \verb!help insertion!.
\subsection{Indexing in matrices}
Indexing in matrices can be done by giving the indices of selected
rows and columns or by boolean indices or by using the \verb!$! symbol.
\input{diary/extract.dia}

\subsection{Indexing in lists}
The following session illustrates how to create lists and
insert/extract entries in {\tt lists} and {\tt tlists}.
\input{diary/list.dia}

