You are on page 1of 8

Some symbolic operations in SCILAB

By Gilberto Urroz, September 2002


SCILAB provides functions addf, subf, mulf, ldivf, rdivf to perform symbolic operations with
variable names and constants resulting in a string. Examples:
-->addf('x','5')
ans =
x+5
-->addf('x+1','y-1')
ans =
x+y
-->mulf('x+1','y-1')
ans =
(x+1)*(y-1)
-->rdivf('x^2+2*x+1','x+1')
ans =
(x^2+2*x+1)/(x+1)

The command cmb_lin, with four arguments, say, a, x, b, y, can be used to create a linear
combination of the arguments, a*x + b*y, e.g.,
-->cmb_lin('c(1)','x+1','c(2)','y+2')
ans =
c(1)*(x+1)-c(2)*(y+2)
-->cmb_lin('A','x^2+x+2','B','y^2-y-2')
ans =
A*(x^2+x+2)-B*(y^2-y-2)

Functions eval and evstr can be used to evaluate symbolic expressions created with any of the
functions shown above, after the variable names used have been assigned values, for example:
-->y = cmb_lin(mulf('3','x'),'x^2+1',addf('5','x'),'x^3')
y =
3*x*(x^2+1)-(x+5)*x^3
-->x = 5
x =
5.
-->y
y =
3*x*(x^2+1)-(x+5)*x^3
-->eval(y)
ans =
- 860.

-->x = poly(0,'x')
x =
x
-->y
y =
3*x*(x^2+1)-(x+5)*x^3
-->eval(y)
ans =
3
4
3x - 2x - x

Functions addf, subf, mulf, rdivf, ldivf, cmb_lin, eval, and evstr can be combined to form
expressions and evaluate them in scripts and functions. However, SCILAB is not a full symbolic
environment and one should not expect to have the same capabilities of, say, Maple, MuPad, or
Mathematica for processing symbolic expressions.
Since the symbolic expressions produced by functions addf, subf, mulf, rdivf, ldivf, and
cmb_lin are strings, you can use other string manipulation functions such as length, part,
strindex, strsubst, string, strcat, etc., to manipulate those results. Examples of string
manipulation are available in Chapter 2 of the textbook: Urroz, G., 2001, Numerical and
Statistical Methods with SCILAB for Science and Engineering - volume 1,
www.greatunpublished.com, Charleston, S.C.
SCILAB also provides functions trianfml and solve for manipulating matrices with symbolic
elements. Function trianfml produces an upper triangular matrix out of a matrix with
symbolic elements, for example:
-->A = ["a11","a12";"a21","a22"]
A =
!a11
!
!a21

a12

!
!
!

a22

-->trianfml(A)
ans =
!a21
!
!0

a22
a21*a12-a11*a22

!
!
!

Function solve is used to obtain the solution of a matricial equation Ax = b in which the
components of A and b are symbolic elements, and A is an upper triangular matrix. For
example,
-->A = ["a1","a2";"0","a3"]
A =
!a1
!
!0

a2
a3

!
!
!

-->b = ["b1";"b2"]
b =
!b1
!
!b2

!
!
!

-->solve(A,b)
ans =
!a1\(b1-a2*(a3\b2))
!
!a3\b2

!
!
!

To solve a symbolic matrix equation Ax = b in which matrix A is not an upper triangular matrix,
follow these steps:
1 - Create an augmented matrix Aaug = [A|b]
2 - Use function trainfml on the augmented matrix to produce an upper triangular matrix, say
Aaug_up.
3 - Extract an upper triangular matrix of the same dimensions as A from Aaug_up, call it A1
4 - Make the last column in Aaug_up equal to a vector b1.
5 - The solution to the matrix equation Ax = b is obtained by using function solve with
arguments A1 and b1.
Here is an example:
-->A = ["a11","a12";"a21","a22"], b = ["b1";"b2"]
A =
!a11
!
!a21
b
!b1
!
!b2

a12
a22

!
!
!

=
!
!
!

-->A_aug = [A b]
A_aug =
!a11
!
!a21

a12

b1

a22

b2

!
!
!

-->A_aug_up = trianfml(A_aug)
ans =
!a21
!
!0

a22

b2

a21*a12-a11*a22

a21*b1-a11*b2

!
!
!

-->A1 = A_aug_up(1:2,1:2), b1 = A_aug_up(:,3)


A1 =
!a21
!
!0

a22
a21*a12-a11*a22

!
!
!

b1

!b2
!
!a21*b1-a11*b2

!
!
!

-->x = solve(A1,b1)
x =
!a21\(b2-a22*((a21*a12-a11*a22)\(a21*b1-a11*b2)))
!
!(a21*a12-a11*a22)\(a21*b1-a11*b2)

!
!
!

These steps can be put together in the following function symbol_solve:


function [x] = symbol_solve(A,B)
//Solution of the matrix equation A*X = B, for matrices with symbolic elements
[nrA,ncA] = size(A)
[nrB,ncB] = size(B)
if nrA <> nrB then
error('symbol_solve: symbolic matrices A and B must have the same number of
rows')
abort
end
A_aug = [A B]
A_aug_up = trianfml(A_aug)
A1 = A_aug_up(1:nrA,1:ncA)
B1 = A_aug_up(1:nrB,ncA+1:ncA+ncB)
x = solve(A1,B1)

With this function, the solution to the symbolic matrix equation Ax = b is straightforward:
-->A = ["a11","a12";"a21","a22"], b = ["b1";"b2"]
A =
!a11
!
!a21
b
!b1
!
!b2

a12
a22

!
!
!

=
!
!
!

-->getf('symbol_solve.sci')
-->x = symbol_solve(A,b)
x =
!a21\(b2-a22*((a21*a12-a11*a22)\(a21*b1-a11*b2)))
!
!(a21*a12-a11*a22)\(a21*b1-a11*b2)

!
!
!

A symbolic inverse can be calculated by using user-defined function symbol_solve(A,B) with


matrix B being a symbolic identity matrix. Here is an example:
-->A = ['a11','a12';'a21','a22'], B = ['1','0';'0','1']
A =

!a11 a12
!
!a21 a22
B =
!1
!
!0

0
1

!
!
!

!
!
!

-->Ainv = symbol_solve(A,B)
Ainv =

column 1
!-a21\a22*((a21*a12-a11*a22)\a21)
!
!(a21*a12-a11*a22)\a21

!
!
!

column 2
!a21\(-a22*(-(a21*a12-a11*a22)\a11)+1)
!
!-(a21*a12-a11*a22)\a11

!
!
!

To verify the results, we now replace values of the elements of A:


-->a11 = 2; a12 = 3; a21 = 5; a22 = 8;
-->An = eval(A)
An =
!
!

2.
5.

3. !
8. !

-->An_inv = eval(Ainv)
An_inv =
!
8.
! - 5.

1.
0.

//Numerical values for A inverse

- 3. !
2. !

-->clean(An*An_inv)
ans =
!
!

//Numerical values for A

//Verifying properties of the inverse

0. !
1. !

A modification of function symbol_solve can be used to create a function that calculates an


inverse for a symbolic matrix:

function [x] = symbol_inverse(A)


//Calculate the inverse of a square matrix
[n,m] = size(A)
//Check that A is a square matrix
if n <> m then
error('symbol_inverse: symbolic matrix A is not a square matrix')
abort
end

//Create symbolic identity matrix


for i = 1:n
for j = 1:n
if i==j then
B(i,j) = '1'
else
B(i,j) = '0'
end
end
end
//Calculate inverse
A_aug = [A B]
A_aug_up = trianfml(A_aug)
A1 = A_aug_up(1:n,1:n)
B1 = A_aug_up(1:n,n+1:2*n)
x = solve(A1,B1)

Here is the calculation of the inverse to matrix A using function symbol_inverse:


-->getf('symbol_inverse.sci')
-->symbol_inverse(A)
ans =

column 1
!-a21\a22*((a21*a12-a11*a22)\a21)
!
!(a21*a12-a11*a22)\a21

!
!
!

column 2
!a21\(-a22*(-(a21*a12-a11*a22)\a11)+1)
!
!-(a21*a12-a11*a22)\a11

!
!
!

The determinant of a symbolic square matrix of dimensions nxn can be obtained by using
element (n,n) of the upper triangular matrix, multiplied by (-1)n, for example:
-->A = ['a11','a12';'a21','a22']
A =
!a11
!
!a21

a12
a22

!
!
!

-->AT = trianfml(A)
AT =
!a21
!
!0

a22
a21*a12-a11*a22

!
!
!

-->[m n] = size(A)
n =

2.
=
2.

-->detA = string((-1)^n)
detA =
1
-->detA = mulf(detA,AT(n,n))
detA =
a21*a12-a11*a22

A function to calculate the determinant of a symbolic matrix is presented next:


function [d] = symbol_det(A)
//Calculation of the determinant of a square symbolic matrix
[m n] = size(A)
if m<>n then
error('symbol_det: matrix A must be square')
abort
end
AT = trianfml(A)
d = string((-1)^n)
d = mulf(d,AT(n,n))

Here is the calculation of the determinant of the 2x2 matrix A defined above:
-->getf('c:\symbol_det.sci')
-->symbol_det(A)
ans =
a21*a12-a11*a22

The following example shows the calculation of the determinant of a 3x3 matrix, verifying the
results by using some numerical values:
-->A=['a11','a12','a13';
-->
'a21','a22','a23';
-->
'a31','a32','a33']
A =
!a11
!
!a21
!
!a31

a12

a13

a22

a23

a32

a33

!
!
!
!
!

-->detA = symbol_det(A)
detA =
-((a31*a12-a11*a32)*(a31*a23-a21*a33)-(a31*a22-a21*a32)*(a31*a13-a11*a33))
-->a11=1;a12=2;a13=5;a21=5;a22=6;a23=7;a31=-1;a32=2;a33=5;
-->det(eval(A))
ans =32.

//Using det for matrix A evaluated with eval

-->eval(detA)
ans =32.

//Evaluating detA with eval

The examples shown above illustrate the use of some SCILAB functions and some user-defined
functions for the manipulation of symbolic expressions in SCILAB. While SCILAB is not a
symbolic environment, these functions allow some limited symbolic manipulation of
expressions. There is, however, a type of SCILAB data that can be manipulated symbolically
more extensively that with the functions shown above. These are polynomial expressions.
Details on the use of polynomials are presented in Chapters 6 and 7 in of the textbook: Urroz,
G., 2001, Numerical and Statistical Methods with SCILAB for Science and Engineering - volume
1, www.greatunpublished.com, Charleston, S.C.

You might also like