You are on page 1of 3

# PUSAT PENGAJIAN SAINS MATEMATIK

## UNIVERSITI SAINS MALAYSIA

SEM 2 SA 2017/2018

SAS EXERCISES 1

## /* TO CREATE MATRIX A & B */

PROC IML;
reset print;
A={10 15,11 9, 1 -6};
B={5 2, 1 0, 10 7};
print A B; /* notice the difference between these two print commands*/
print A;
print B;

/*transpose of matrix*/
At=A`;
Bt=B`;
print At;
print Bt;

## /*addition & substraction of matrices*/

C=A+B;
D=A-b;
print A, B, C, D;
quit;
/* be careful, once you have given 'quit' command,next line should again start with proc IML!*/

/*multiplication of matrices*/
proc iml;
A={10 15,11 9, 1 -6};
B={5 2, 1 0, 10 7};
C2=A*B`; /* matrix A multiplies with matrix B transposed*/
D2=4*A;
D3=(1/3)*A;
print A, B, C2, D2, D3;

## Q1. Now try these matrix operations:

1 4 1 3 
3 8 1 
A   2 6  , B  1 4  , C   
    5 4 0 
 2 8   2 5

## Obtain: (1) A  B , (2) A  B , (3) AC , (4) AB , (5) BA .

Write down your complete SAS code.
/*determinant and inverse*/
proc iml;
A={1 3 2 5,1 2 5 3,1 -1 2 2, 5 2 -1 4};
Bdet=det(A);
Cinv=inv(A);
print A, Bdet, Cinv;
quit;

/*trace of a matrix*/
proc iml;
W={1 2 3, 4 5 6, 7 8 10};
trw1=trace(W);
trw2=sum(diag(W));
print W, trw1, trw2;

/* elementwise multiplication*/
proc iml;
a={3 6, 2 1};
b={7 -4, -3 2};
ab=a#b;
print a, b, ab;

/*Kronecker Product*/
a={2 4,0 -2,3 -1};
b={5 3,2 1};
ab=a@b;
print a, b, ab;

proc iml;
A=i(3);
print A;

## /*to create a matrix where every element is the same.

The first argument is the number of rows,
the second argument is the number of columns,
the last argument is the value of each element*/

newmatrix=j(2,3,4.2);
print newmatrix;

proc IML;
A={1 1 1,2 5 -1,0 1 -1};
rankA=round(trace(ginv(A)*A));
print A, rankA;

## /*note the rank function in IML is related to sorting.

It computes the ranks of numbers in a matrix*/
C={1.2 3.5, 5.1 9.8};
rc=rank(C);
print C, rc;

4 3 2
Q2. Let B  6
 5 10  .
 
10 1 6 
(a) Are the column vectors of B linearly dependent?
(b) What is the rank of B ?
(c) Calculate the determinant of B
(d) Find the inverse of B and check that the resulting matrix is indeed the inverse.
/*compute the sum of all elements in a matrix*/
proc iml;
A={3 6,2 1};
B={7 -4,-3 2};
sumA=sum(A);
print A, sumA;

## /*compute row & column sums*/

rowsumA=A[ ,+];
colsumA=A[+, ];
print A, rowsumA, colsumA;

## /*select the second column of A*/

col2=A[ ,2];
print col2;

/*select the element in the first row and the second column of A*/
a12=A[1,2];
print a12;

## /* to find eigen values & eigen vectors*/

proc iml;
A={13 -4 2,-4 11 -2,2 -2 8};
L=eigval(A); /*compute eigenvalues of a square matrix A*/
V=eigvec(a); /*compute an n x n matrix whose columns are eigenvectors of A*/
print A, L;
print V [format=Fract.];

Q3: Let
 23 0 2 3
 
A 0 1 0 
 2 3 0 1 3 

## (i) Find the rank of A .

(ii) Show that A is idempotent.
(iii) Show that I  A is idempotent.
(iv) Show that A  I  A   O .
(v) Find trace of matrix A .
(vi) Find the eigenvalues of A .