title 'IMLMAT1: Basic matrix definitions';
options nodate;
proc iml;
reset print log;
*-- Matrix: a rectanglar array of numbers;
X = {1 2 3, 4 0 6, 9 1 4, 6 2 4};
print (nrow(X)) (ncol(X));
rowvec = { 1 2 3};
colvec = { 1, 4, 6};
*-- transpose: interchanges rows and cols;
xt = t(x);
xt = t(x);
*-- square matrix: same number of rows and cols;
A = {5 -1 3, -1 4 7, 3 3 -4};
*-- main diagonal: entries A[i,i];
d = vecdiag(A);
*-- diagnonal matrix: all zero except for main diagonal;
d = diag({1 4 9});
*-- trace: for square matrix, the sum of main diagonal;
t = trace(A);
*-- symmetric matrix: square, and equal to its transpose
i.e., A[i,j] = A[j,i];
sym = ( A = t(A) );
sym = all( A = t(A) );
*-- any square matrix may be made symmetric by averaging
with its transpose;
B = ( A + t(A) ) / 2;
sym = all( B = t(B));
*-- upper triangular matrix: all below main diag = 0;
upper = {-5 2 7, 0 -2 4, 0 0 3};
*-- lower triangular matrix: all below main diag = 0;
lower = t(upper);
*-- identity matrix: diagonal matrix with 1s on main diag;
I = I(3);
*-- scalar matrix: diagonal matrix with equal diagonal entries;
S = diag({10 10 10});
*-- unit matrix: all elements = 1;
J = J(2,5);
J = J(3,1);
*-- zero matrix: all elements = 0;
Z = J(2,5,0);
quit;