# Basic Matrix Operations

For more details and examples, please consult Technical Report T.R. 95-74.

### DIMENSIONS OF A MATRIX

The number of rows and columns in a matrix may be extracted, and used in the construction of general purpose problem solving procedures. For example, a common use for matrix dimensions is in setting the beginning and end points for looping constructs in numerical algorithms.

```    FUNCTION           PURPOSE
=====================================================================

Dimension( A )     Extract the number of rows and columns in matrix
"A" and return the result in a (1x2) matrix.

Element  holds the number of rows in "A"
Element  holds the number of columns in "A"
```

Example 1 : The script of code:

```    Z = One([ 13, 20]);    /* Allocate 3 by 20 matrix called Z   */
size = Dimension(Z);   /* Extract and print dimensions of Z  */

print "Rows in [Z]     = ",size ,"\n";
print "Columns in [Z]  = ",size ,"\n";
```

generates the output:

```    Rows in [Z]    = 1.3000e+01
Columns in [Z] = 2.0000e+01
```

### MATRIX COPY

Let X be a (mxn) matrix. A copy of X can be made by simply writing

```    Y = X;
```

There are situations, however, where a matrix copy is needed, but without an assignment. The matrix function

```    FUNCTION                  DESCRIPTION
================================================================
Copy(A)                   Takes one matrix argument, and returns
a matrix copy.
```

Of course, Copy() can be used with an assignment.

Example 2 : The script of code:

```    /* [a] : Define and print (1x3) test matrix */

response = [ 1 sec, 2 cm/sec, 3 cm/sec^2 ];
PrintMatrix( response );

/* [b] : Copy and print "response" matrices */

copy1 = response;
copy2 = Copy ( response );
PrintMatrix( copy1, copy2 );
```

generates the output:

```    MATRIX : "response"

row/col                  1            2            3
units          sec        m/sec      m/sec^2
1            1.00000e+00  2.00000e-02  3.00000e-02

MATRIX : "copy1"

row/col                  1            2            3
units          sec        m/sec      m/sec^2
1            1.00000e+00  2.00000e-02  3.00000e-02

MATRIX : "copy2"

row/col                  1            2            3
units          sec        m/sec      m/sec^2
1            1.00000e+00  2.00000e-02  3.00000e-02
```

Example 3 : The script of code:

```    /* [a] : Define and print (1x3) test matrix */

response = [ 1 sec, 2 cm/sec, 3 cm/sec^2 ];
PrintMatrix( response );

/* [b] : Copy and print "response" matrices */

copy1 = response;
PrintMatrix( copy1, Copy (response) );
```

generates the same block of output as in Example 2.

### MATRIX TRANSPOSE

Let "A" be a (mxn) matrix. The matrix transpose of "A" is a (nxm) matrix with the rows and columns of A interchanged.

```    FUNCTION          DESCRIPTION
================================================================
Trans(A)          Generates a new matrix corresponding to the
matrix transpose of "A"
```

Example 4 : The script of code:

```    /* [a] : Define and print (1x3) test matrix */

response = [ 0 sec, 0 cm/sec, 0 cm/sec^2 ;
1 sec, 2 cm/sec, 3 cm/sec^2 ];
PrintMatrix( response );

/* [b] : Compute and print transpose of "response" */

transpose1 = Trans(response);
PrintMatrix( transpose1 );
```

generates the output:

```    MATRIX : "response"

row/col                  1            2            3
units          sec        m/sec      m/sec^2
1            0.00000e+00  0.00000e+00  0.00000e+00
2            1.00000e+00  2.00000e-02  3.00000e-02

MATRIX : "transpose1"

row/col                  1            2
units
1      sec   0.00000e+00  1.00000e+00
2    m/sec   0.00000e+00  2.00000e-02
3  m/sec^2   0.00000e+00  3.00000e-02
```

### MAXIMUM AND MINIMUM MATRIX ELEMENTS

```    FUNCTION           PURPOSE
===============================================================

Min ( A )          Return a (1x1) matrix containing the minimum
matrix element in matrix "A".

Max ( A )          Return a (1x1) matrix containing the maximum
matrix element in matrix "A".
```

Example 5 : The script

```    A = [ 3.78,  9.7, -4.7,  10.50  ;
0.00, -5.8,  0.2,  -9.34] ;

MaxValue = Max( A );
MinValue = Min( A );

PrintMatrix(A);

print "\n";
print "Max(A) =", MaxValue ,"\n";
print "Min(A) =", MinValue ,"\n";
```
generates the output
```    MATRIX : "A"

row/col                  1            2            3            4
units
1            3.78000e+00  9.70000e+00 -4.70000e+00  1.05000e+01
2            0.00000e+00 -5.80000e+00  2.00000e-01 -9.34000e+00

Max(A) =      10.5
Min(A) =     -9.34
```

Note. In Aladdin 2, Max() and Min() truncate the units from the matrix element. In other words, the max/min test applies to the max/min numerical value of the matrix element. We need to change the program so that when all of the elements have the same units type, Min()/Max() also returns the units.

### EUCLIDEAN NORM

The L2 norm of a row matrix or column matrix is simply the square root of the sum of the matrix elements squared.

```    FUNCTION             PURPOSE
===================================================================

L2Norm( A )          Compute L2 norm of either a (1xn) matrix or a
(nx1) matrix.
```

Example 6 : The script

```    /* [a] : Define (1x4) test vector and compute L2 norm */

testVector = [ 1, 2, 3, 4 ];
norm  = L2Norm( testVector );

/* [b] : Print "testVector" and L2 norm */

PrintMatrix( testVector );
print "\n";
print "L2 norm of testVector is :", norm, "\n";
```
generates the output:
```    MATRIX : "testVector"

row/col                  1            2            3            4
units
1            1.00000e+00  2.00000e+00  3.00000e+00  4.00000e+00

L2 norm of testVector is :     5.477
```

Developed in 1996-1997 by Mark Austin,