# Basic Matrix Arithmetic

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

### ADDITION AND SUBTRACTION

Let X be a (mxn) matrix and Y be a (pxq) matrix. The matrix sum "X + Y" is defined when:

• Matrices X and Y have the same number of rows, and the same number of columns.
• The row and column units of X and Y are compatible.

Example 1 : In the following script we define two (2x2) matrices, compute their sum, and print all of the participating matrices. The script:

```    /* [a] : Define (2x2) matrices "A" and "B" */

A = [ 1, 2; 3, 4];
B = [ 3, 4; 5, 6];

/* [b] : Compute and print the matrix sum "A+B" */

C = A+B;
PrintMatrix( A, B, C);
```

generates the output:

```    MATRIX : "A"

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

MATRIX : "B"

row/col                  1            2
units
1            3.00000e+00  4.00000e+00
2            5.00000e+00  6.00000e+00

MATRIX : "C"

row/col                  1            2
units
1            4.00000e+00  6.00000e+00
2            8.00000e+00  1.00000e+01
```

Example 2 : The script of code:

```    /* [a] : Initialize (4x1) matrix "X" */

X = Diag([4, 1]);
X = ColumnUnits(X, [ksi, ft,  N,  m]);
X =    RowUnits(X, [psi, in, kN, mm]);

/* [b] : Initialize (4x1) matrix "Y" */

Y = One([4]);
Y = ColumnUnits(Y, [psi, in, kN, km]);
Y =    RowUnits(Y, [ksi, ft,  N, mm]);

/* [c] : Compute and print matrix sum "Z = X + Y" */

Z = X + Y;
PrintMatrix(X, Y, Z);
```

generates the output:

```    MATRIX : "X"

row/col                  1            2            3            4
units          ksi           ft            N            m
1      psi   1.00000e+00  0.00000e+00  0.00000e+00  0.00000e+00
2       in   0.00000e+00  1.00000e+00  0.00000e+00  0.00000e+00
3       kN   0.00000e+00  0.00000e+00  1.00000e+00  0.00000e+00
4       mm   0.00000e+00  0.00000e+00  0.00000e+00  1.00000e+00

MATRIX : "Y"

row/col                  1            2            3            4
units          psi           in           kN           km
1      ksi   1.00000e+00  1.00000e+00  1.00000e+00  1.00000e+00
2       ft   1.00000e+00  1.00000e+00  1.00000e+00  1.00000e+00
3        N   1.00000e+00  1.00000e+00  1.00000e+00  1.00000e+00
4       mm   1.00000e+00  1.00000e+00  1.00000e+00  1.00000e+00

MATRIX : "Z"

row/col                  1            2            3            4
units          ksi           ft            N            m
1      psi   2.00000e+00  8.33333e+01  1.00000e+06  1.00000e+06
2       in   1.20000e-02  2.00000e+00  1.20000e+04  1.20000e+04
3       kN   1.00000e-06  8.33333e-05  2.00000e+00  1.00000e+00
4       mm   1.00000e-03  8.33333e-02  1.00000e+03  1.00100e+03
```

Matrix addition is commutative (i.e. X + Y = Y + X).

### MULTIPLICATION

Let X be a (mxn) matrix and Y be a (pxq) matrix. The matrix product X.Y is defined when:

• Matrix dimensions n = p.
• The row and column units of X and Y are compatible.

Example 3 : Here is an example of multiplication of matrices containing dimensionless physical quantities -- the script of input:

```    /* [a] : Define (3x3) X matrix, and (3x4) Z matrix */

X = [ 2, 3, 5;
4, 6, 7;
10, 2, 3];
Z = One([3, 4]);

/* [b] : Compute U = X*Z and print all matrices */

U = X*Z;
PrintMatrix(X, Z, U);
```

The generated output is:

```    MATRIX : "X"

row/col                  1            2            3
units
1            2.00000e+00  3.00000e+00  5.00000e+00
2            4.00000e+00  6.00000e+00  7.00000e+00
3            1.00000e+01  2.00000e+00  3.00000e+00

MATRIX : "Z"

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

MATRIX : "U"

row/col                  1            2            3            4
units
1            1.00000e+01  1.00000e+01  1.00000e+01  1.00000e+01
2            1.70000e+01  1.70000e+01  1.70000e+01  1.70000e+01
3            1.50000e+01  1.50000e+01  1.50000e+01  1.50000e+01
```

Example 4 : The following script demonstrates multiplication of matrices containing physical quantities that have dimensions:

```    /* [a] : Define (3x3) stiffness matrix */

stiff = [ 2, 3, 5;
4, 6, 7;
10, 2, 3];
stiff = ColumnUnits( stiff, [N/m, N/m, N/rad ]);
stiff =    RowUnits( stiff, [m] , [3]);

/* [b] : Define (3x1) displacement matrix */

displ = One([3, 1]);
displ = RowUnits( displ, [m, m, rad]);

/* [c] : Compute (3x1) external load vector/matrix */

PrintMatrix( stiff, displ, load );
```

The generated output is:

```    MATRIX : "stiff"

row/col                  1            2            3
units          N/m          N/m        N/rad
1            2.00000e+00  3.00000e+00  5.00000e+00
2            4.00000e+00  6.00000e+00  7.00000e+00
3        m   1.00000e+01  2.00000e+00  3.00000e+00

MATRIX : "displ"

row/col                  1
units
1        m   1.00000e+00
2        m   1.00000e+00

row/col                  1
units
1        N   1.00000e+01
2        N   1.70000e+01
3      N.m   1.50000e+01
```

### SCALING A MATRIX BY A QUANTITY

Let A be a matrix and q be a physical quantity. ALADDIN's supports pre- and post-multiplication of A by quantity q (i.e. q.A and A.q), and division of A by a quantity (i.e. A/q).

Example 5 : The script

```    /* [a] : Define (3x2) matrix and velocity quantity */

coord = [ 1, 2;
3, 4;
5, 6 ];
velocity = 2 in/sec;

/* [b] : Compute and print the matrix sum */

Gv = coord*velocity;
PrintMatrix( coord, Gv );
```

generates the output

```    MATRIX : "coord"

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

MATRIX : "Gv"

row/col                  1            2
units       in/sec       in/sec
1            2.00000e+00  4.00000e+00
2            6.00000e+00  8.00000e+00
3            1.00000e+01  1.20000e+01
```

and demonstrates post-multiplication of a matrix by a quantity. Notice how the units of "velocity" are transformed into the matrix-quantity product.

Similar results are obtained for matrix-quantity expressions

```       velocity*coord;  coord/velocity;
```
and so forth.

Developed in April 1996 by Mark Austin