File: chap2.tex

package info (click to toggle)
scilab 2.4-1
  • links: PTS
  • area: non-free
  • in suites: potato, slink
  • size: 55,196 kB
  • ctags: 38,019
  • sloc: ansic: 231,970; fortran: 148,976; tcl: 7,099; makefile: 4,585; sh: 2,978; csh: 154; cpp: 101; asm: 39; sed: 5
file content (1031 lines) | stat: -rw-r--r-- 28,294 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
% 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}