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

-->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
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
num=mulf(makestr(p(1)),'1');
for k=2:size(p);
   new=mulf(makestr(p(k)),'s^'+string(k-1));
   num=addf(num,new);
end,
text='p='+num;
deff('[p]=newfunction(t)',text),


function [str]=makestr(p)
n=degree(p)+1;c=coeff(p);str=string(c(1));x=part(varn(p),1);
xstar=x+'^',
for k=2:n, 
   if c(k)<>0 then,
   str=addf(str,mulf(string(c(k)),(xstar+string(k-1))));
   end;
end

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.