It is possible to transparently define fundamental operations for new data types in Scilab. That is, the user can give a sense to multiplication, division, addition, etc. on any two data types which exist in Scilab. As an example, two linear systems (represented by lists) can be added together to represent their parallel inter-connection or can be multiplied together to represent their series inter-connection. Scilab performs these user defined operations by searching for functions (written by the user) which follow a special naming convention described below.
The naming convention Scilab uses to recognize operators defined by the user is determined by the following conventions. The name of the user defined function is composed of four (or possibly three) fields. The first field is always the symbol %. The third field is one of the characters in the following table which represents the type of operation to be performed between the two data types.
Third field | |
SYMBOL | OPERATION |
a |
+ |
|
; (row separator) |
|
[ ] (matrix definition) |
|
./ |
|
() extraction: m=a(k) |
|
() insertion: a(k)=m |
|
.*. |
|
\ left division |
|
* |
|
^ exponent |
|
.\ |
|
/ right division |
|
- |
|
' (transpose) |
|
*. |
|
/. |
|
\. |
|
.* |
|
./. |
|
.\. |
The second and fourth fields represent the type of the first and second data objects, respectively, to be treated by the function and are represented by the symbols given in the following table.
Second and Fourth fields | |
SYMBOL | VARIABLE TYPE |
|
scalar |
|
polynomial |
|
list (untyped) |
|
character string |
|
function |
|
list (typed) |
A typed list is one in which the first entry of the list is a character string where the first characters of the string are represented by the xxx in the above table. For example a typed list representing a linear system has the form tlist(['lss','A','B','C','D','X0','dt'],a,b,c,d,x0,'c') and, thus, the xxx above is lss.
An example of the function name which multiplies two linear systems together (to represent their series inter-connection) is %lss_m_lss. Here the first field is %, the second field is lss (linear state-space), the third field is m ``multiply'' and the fourth one is lss. A possible user function which performs this multiplication is as follows
function [s]=%lss_m_lss(s1,s2) [A1,B1,C1,D1,x1,dom1]=s1(2:7), [A2,B2,C2,D2,x2]=s2(2:6), B1C2=B1*C2, s=lsslist([A1,B1C2;0*B1C2' ,A2],... [B1*D2;B2],[C1,D1*C2],D1*D2,[x1;x2],dom1),An example of the use of this function after having loaded it into Scilab (using for example getf or inserting it in a library) is illustrated in the following Scilab session
-->A1=[1 2;3 4];B1=[1;1];C1=[0 1;1 0]; -->A2=[1 -1;0 1];B2=[1 0;2 1];C2=[1 1];D2=[1,1]; -->s1=syslin('c',A1,B1,C1); -->s2=syslin('c',A2,B2,C2,D2); -->ssprint(s1) . | 1 2 | | 1 | x = | 3 4 |x + | 1 |u | 0 1 | y = | 1 0 |x -->ssprint(s2) . | 1 -1 | | 1 0 | x = | 0 1 |x + | 2 1 |u y = | 1 1 |x + | 1 1 |u -->s12=s1*s2; //This is equivalent to s12=%lss_m_lss(s1,s2) -->ssprint(s12) | 1 2 1 1 | | 1 1 | . | 3 4 1 1 | | 1 1 | x = | 0 0 1 -1 |x + | 1 0 |u | 0 0 0 1 | | 2 1 | | 0 1 0 0 | y = | 1 0 0 0 |xNotice that the use of %lss_m_lss is totally transparent in that the multiplication of the two lists s1 and s2 is performed using the usual multiplication operator *.
The directory SCIDIR/macros/percent contains all the functions (a very large number...) which perform operations on linear systems and transfer matrices. Conversions are automatically performed. For example the code for the function %lss_m_lss is there (note that it is much more complicated that the code given here!).