## Output from imlrank.sas

Source
0 Graphs

```NOTE: Capture of log output started.
```
```NOTE: %INCLUDE (level 1) file n:\psy6140\examples\iml\imlrank.sas is file
n:\psy6140\examples\iml\imlrank.sas.```
```898  +title 'IMLRANK: Matrix rank and linear independence';
899  +proc iml;```
`IML Ready`
```900  +   *-- Define a function module to find the rank of a matrix;
901  +start r(A);
902  +    reset noprint;
903  +   *-- rank of a matrix (cant call it rank, since that name
904  +       is used for sorting);
905  +   *-- rank = number of nonzero rows/cols in echelon form;
906  +   e = echelon(A);
907  +   do i=1 to nrow(e);
907  +                        *-- Find rows which are not all zero;
908  +      if any( e[i,] <> 0 ) then rows = rows || i;
909  +      end;
910  +   e = e[rows,];
910  +                        *-- Keep only non-zero rows;
911  +   do i=1 to ncol(e);
911  +                        *-- Find cols which are not all zero;
912  +      if any( e[,i] <> 0 ) then cols = cols || i;
913  +      end;
914  +   e = e[,cols];
914  +                        *-- Keep only non-zero cols;
915  +   reset print;
916  +   return( min( nrow(e), ncol(e)) );
917  +   finish;```
`NOTE: Module R defined.`
```918  +
919  +   reset print log fuzz;
920  +* 1. MATRIX RANK AND LINEAR INDEPENDENCE;
921  +*    A set of vectors is linearly DEPENDENT if there are scalars;
922  +*    (not all =0) which give a linear combination = zero vector.;
923  +X1 = {1 3 5};```
```                X1            1 row       3 cols    (numeric)

1         3         5

```
`924  +X2 = {0 1 2};`
```                X2            1 row       3 cols    (numeric)

0         1         2

```
`925  +X3 = {-1 4 9};`
```                X3            1 row       3 cols    (numeric)

-1         4         9

```
`926  +X4 = {2 2 2};`
```                X4            1 row       3 cols    (numeric)

2         2         2

```
`927  +comb = (2#X1) + (-4#X2) + (0#X3) + -1#X4;`
```                COMB          1 row       3 cols    (numeric)

0         0         0

```
```928  +* X3, X4 are linear combinations of X1, X2;
929  +print (t(X3)) '=' (t(-X1)) '+' (t(7#X2));```
```                       #TEM1001    #TEM1003    #TEM1005
-1 =        -1 +         0
4          -3           7
9          -5          14
```
`930  +print (t(X4)) '=' (t(2#X1)) '+' (t(-4#X2));`
```                       #TEM1001    #TEM1003    #TEM1006
2 =         2 +         0
2           6          -4
2          10          -8
```
```931  +* only two of x1, x2, x3, x4 are linearly independent;
932  +X = t(X1) || t(X2) || t(X3)  || t(X4);```
```                X             3 rows      4 cols    (numeric)

1         0        -1         2
3         1         4         2
5         2         9         2

```
`933  +r = r(X);`
```                R             1 row       1 col     (numeric)

2

```
```934  +* rank = number of nonzero rows in echelon form;
935  +r = echelon(X);```
```                R             3 rows      4 cols    (numeric)

1         0        -1         2
0         1         7        -4
0         0         0         0

```
```936  +* change last element so X4 not dependent;
937  +X[3,4] = 4;```
```                X             3 rows      4 cols    (numeric)

1         0        -1         2
3         1         4         2
5         2         9         4

```
`938  +r = r(X);`
```                R             1 row       1 col     (numeric)

3

```
`939  +r = echelon(X);`
```                R             3 rows      4 cols    (numeric)

1         0        -1         0
0         1         7         0
0         0         0         1

```
```941  +* -----------------------------------------------------;
942  +* 2. MATRIX RANK BY ELEMENTARY ROW OPERATIONS;
943  +*    Use elementary row ops to reduce matrix to echelon
944  +*    form. Zero rows/cols do not contribute to rank.;
945  +* -----------------------------------------------------;
946  +A = {4 1 8, 5 2 7,  5 1 11,  8 3 12};```
```                A             4 rows      3 cols    (numeric)

4         1         8
5         2         7
5         1        11
8         3        12

```
`947  +SAVE = A;`
```                SAVE          4 rows      3 cols    (numeric)

4         1         8
5         2         7
5         1        11
8         3        12

```
```948  +* ROW 4 - 2#ROW 1;
949  +A[4,] = A[4,] - 2#A[1,];```
```                A             4 rows      3 cols    (numeric)

4         1         8
5         2         7
5         1        11
0         1        -4

```
```950  +*  Each elementary row operation is equivalent to premultiplication
951  +   by an identity matrix with the same operation applied to its rows;
952  +T = I(4);```
```                T             4 rows      4 cols    (numeric)

1         0         0         0
0         1         0         0
0         0         1         0
0         0         0         1

```
`953  +T[4,] = T[4,] - 2#T[1,];`
```                T             4 rows      4 cols    (numeric)

1         0         0         0
0         1         0         0
0         0         1         0
-2         0         0         1

```
`954  +A = T * SAVE;`
```                A             4 rows      3 cols    (numeric)

4         1         8
5         2         7
5         1        11
0         1        -4

```
```955  +   * ROW 3 - ROW 2;
956  +A[3,] = A[3,] - A[2,];```
```                A             4 rows      3 cols    (numeric)

4         1         8
5         2         7
0        -1         4
0         1        -4

```
```957  +   * .25 # ROW 1;
958  +A[1,] = .25 # A[1,];```
```                A             4 rows      3 cols    (numeric)

1      0.25         2
5         2         7
0        -1         4
0         1        -4

```
```959  +   * ROW 2 - 5#ROW 1;
960  +A[2,] = A[2,] - 5#A[1,];```
```                A             4 rows      3 cols    (numeric)

1      0.25         2
0      0.75        -3
0        -1         4
0         1        -4

```
```961  +   * ROW 4 + 1#ROW 3;
962  +A[4,] = A[4,] + A[3,];```
```                A             4 rows      3 cols    (numeric)

1      0.25         2
0      0.75        -3
0        -1         4
0         0         0

```
```963  +   * 1.33#ROW 2;
964  +A[2,] = (4/3) # A[2,];```
```                A             4 rows      3 cols    (numeric)

1      0.25         2
0         1        -4
0        -1         4
0         0         0

```
```965  +   * ROW 2 + ROW 3;
966  +A[2,] = A[2,] + A[3,];```
```                A             4 rows      3 cols    (numeric)

1      0.25         2
0         0         0
0        -1         4
0         0         0

```
```967  +   *-- RANK = number of nonzero rows/cols;
968  +r = r(A);```
```                R             1 row       1 col     (numeric)

2

```
`969  +quit;`
`Exiting IML.`
```NOTE: The PROCEDURE IML used 0.22 seconds.

```
`970  +`
`NOTE: %INCLUDE (level 1) ending.`