2.3 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.

--> 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. !
Note that in the above Scilab session the function trianfml performs the symbolic triangularization of the matrix A. The value of the resulting symbolic matrix can be obtained by using 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 3.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 s and 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 2.7). The polynomial to be studied is $ (t^2+2t^3)-(t+t^2)s+ts^2+s^3$.

-->pst=makefunction(p) //pst is a function t->p (number->polynomial)
 pst       =
 ans       =
              2   3  
    3 - 2s + s + s
Here the polynomial is represented by the command which puts the coefficients of the variable s in the list p. The list p is then processed by the function makefunction which makes a new function pst. The contents of the new function can be displayed and this function can be evaluated at values of t. The creation of the new function pst is accomplished as follows

function [newfunction]=makefunction(p)
// Copyright INRIA
for k=2:size(p);

function [str]=makestr(p)
for k=2:n, 
   if c(k)<>0 then,

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