Chapter seven: Matrix handling

Scan of page 45 Scan of page 46 Scan of page 47 Scan of page 48 Scan of page 49 Scan of page 51 Scan of page 51

Declaring

The matrix functions are used to handle arrays. A one dimensional array is called a vector and a two dimensional array is called a matrix. They are declared using the DIMENSION (or DIM) statement.

e.g.:

DIM A(3)

will reserve space for a three element vector

\[\begin{bmatrix} a_1 & a_2 & a_3 \end{bmatrix}\]
DIM B(3,4)

will reserve space for a matrix with three rows and four columns.

\[\begin{split}\begin{bmatrix} b_{11} & b_{12} & b_{13} & b_{14} \\ b_{21} & b_{22} & b_{23} & b_{24} \\ b_{31} & b_{32} & b_{33} & b_{34} \end{bmatrix}\end{split}\]

If no bounds are given a 10 x 10 array is assumed.

Note: Generally the lowest bound element of an array is zero. However, when handling a matrix only elements with non zero subscripts are used.

One and two dimensional arrays containing string values may also be used and are declared as:

e.g.:

   DIM A$(3)
or DIM B$(2,4)

(See Chapter three)

Only certain MAT statements can be used with string arrays; these are denoted appropriately.

The statement OPTION BASE 1 may be used in a program. The effect of this is to set the lower bound of all arrays in that program at one instead of zero. This saves storage space

e.g.:

10 ΟΡΤΙΟΝ ΒΑSΕ 1
20 DIM A(3), B(4,4)

Dimensions and redimensioning

Once declared in a DIM statement the number of dimensions assigned to an array (1 or 2) cannot be changed.

Although the amount of space (i.e. the total number of elements) allowed for an array cannot be increased the current bounds of the array can be changed. This is called redimensioning.

If OPTION BASE 1 is not used in a program the zero elements in the array must be considered when redimensioning. For example an array declared as A (3,4) has 4 x 5 = 20 elements and may be redimensioned as, say, A(2,5) or A(3,3) as these have 3 x 6 = 18 and 4 x 4 = 16 elements, but not A(2,6) as this has 3 x 7 = 21 elements or A(20) as this has the wrong number of dimensions.

No assumptions should be made about the elements which are no longer used after redimensioning.

Redimensioning is actually done using MAT assignment statements. These are in the form

Line number MAT destination = array expression.

e.g.:

120 МАТ А = В + С
130 MAT E = IDN
140 ΜΑΤ F = ΖΕR (2,2)

The destination (Left hand side) is redimensioned to the bounds of the array expression (Right hand side) unless this is one of ZER, CON or IDN with no bounds stated in which case the array expression is redimensioned to the bounds of the destination.

Examples from above:

If B and C are (4,3) arrays and A is (5,5) then A will be redimensioned to (4,3) and the result of B + C will be assigned to it.

In Line 130 if E isa (4,4) array the RHS will become the 4 x 4 Identity matrix, which will then be assigned to E. In Line 140 if F is a (4,4) array the RHS will become a 2 x 2 zero matrix, F will be redimensioned to (2,2) and the zero matrix will be assigned to it.

Input and output

Input from and output to a terminal

Vectors, matrices and string arrays can be input from a terminal using the MAT INPUT statement.

e.g.:

10 DIM A$(4)
20 MAT INPUT A$
.
.
999 END
RUN
? 1, 2, 3, 4, 5
? 6, 7, 8, 9

Items are input to 2 dimensional arrays with the second subscript varying faster than the first.

e.g.

\[\begin{split}\begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix}\end{split}\]

in that order.

The MAT LINPUT statement may also be used to input complete string arrays. Each complete line of text will be assigned to one element of the array.

e.g.:

10 DIM A$(4)
20 MAT LINPUT A$
.
.
999 END
RUN
? JACK AND JILL WENT UP THE HILL
? TO FETCH A PAIL OF WATER
? JACK FELL DOWN AND BROKE HIS CROWN
? AND JILL CAME TUMBLING AFTER

Output to a terminal of both numeric and string arrays can be done using the MAT PRINT or MAT PRINT USING statements. (See Chapter five for PRINT USING formats)

e.g.:

10 DIM A(3,3), B(4)
90 PRINT "MATRIX A AND VECTOR B"
100 MAT PRINT A, B,
9999 END
RUN
ΜΑΤRΙΧ Α ΑΝD VECTOR Β
Α(1,1)         A(1,2)          Α(1,3)
A(2,1)         A(2,2)          A(2,3)
A(3,1)         A(3,2)          A(3,3)
B(1)           B(2)            B(3)           B(4)

When printing, if the last item in the print list is a vector and it is followed by a separator it will be printed as a column vector.

e.g.:

10 DIM A(4)
20 ΜΑΤ ΙΝΡUΤ Α
30 PRINT "THIS IS A ROW VECTOR"
40 MAT PRINT A,
50 PRINT "THIS IS A COLUMN VECTOR"
60 ΜΑΤ ΡRΙΝΤ Α
70 END
RUN
? 1, 2, 3, 4,
THIS IS A ROW VECTOR
1               2              3               4
THIS IS A COLUMN VECTOR
1
2
3
4
LINE 70 DONE

If “;” is used instead of “,” the elements of the row vector will be printed close together.

e.g.:

1  2  3  4

Input from and output to a terminal format file

Matrices, vectors and string arrays can be input from a terminal format file using the MAT INPUT # Channel Number statement, or for strings only the MAT LINPUT # Channel Number statement.

e.g. If MATS is a terminal format file containing nine numbers

10 FΙLΕ #1: "ΜΑΤS"
20 DIM A(3,3)
30 ΜΑΤ ΙΝΡUΤ #1: Α

Note that the file must be in the correct format (see Chapter six on files).

Output to a terminal format file of both numeric and string arrays can be done using the MAT PRINT or MAT PRINT USING statements with a channel number.

e.g. If MATS is a terminal format file.

10 DIM A(3,3)
20 FILE #1: "MATS"
.
.
140 MAT PRINT #1: A

or If P$ is a suitable print format (see Chapter five)

140 MAT PRINT #1: USING PŞ : A

This will write the matrix to the file in the same format that would be used to print on a terminal, with a new record for each new line.

Input from DATA statements within the program

This can be done for both numeric and string arrays using the MAT READ and DATA statements

e.g.:

10 DIM A(3,3)
20 ΜΑΤ RΕΑD Α
 .
 .
200 DATA 1, 2, 3,
210 DATA 4,5
220 DATA 6, 7, 8, 9

Input from the output to an internal format file

Matrices, vectors and string arrays can be input from an internal format file using the MAT READ # Channel Number statement.

e.g. If IMATS is an internal format file containing 9 numeric values

10 FΙLΕ #1: "ΙΜΑΤS"
20 DIM A(3,3)
30 MAT READ #1: A

Output to an internal format file of both numeric and string arrays can be done using the MAT WRITE statement.

e.g.:

10 DIM A(3,3)
20 FILE #1: "IMATS"
 .
 .
100 MAT WRITE #1: A

The internal format file must be set up at OPEN time in the correct format (See Chapter six on files).

The most convenient format for matrices is OPEN filename (N,), No. of records as this is more flexible than the more obvious format of, say, OPEN filename(10 N), 10 for a 10 x 10 matrix.

Similarly for string arrays OPEN filename (Sn,), No. of records could be used.

Matrix functions

ZER

This function is used to set all the elements of the specified matrix or vector to zero.

e.g.:

40 ΜΑΤ Α = ΖΕR

will set all of the elements of A to zero.

CON

This function is used to set all the elements of the specified matrix or vector to one.

e.g.:

40 ΜΑΤ Α = CΟΝ

will set all the elements of A to 1 60

MAT B = CON(4,7)

will redimension B to a (4,7) array and set all the elements to 1

ΙDΝ

This function is used to set the specified matrix to the identity matrix. The identity matrix is always square (i.e. the same number of rows and columns) with 1’s on the leading diagonal and 0’s elsewhere.

e.g.

\[\begin{split}\begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix}\end{split}\]

is the 3 x 3 identity matrix.

e.g.:

40 ΜΑΤ Α = ΙDΝ

will set the square matrix A to the identity matrix.

60 MAT B = IDN(3,3)

will redimension B to a (3,3) array and set it to the identity matrix.

TRN(Z)

This function is used to transpose the specified matrix. That is to replace each column by the corresponding row.

e.g. ΜΑΤRΙΧ Α =

\[\begin{split}\begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix}\end{split}\]

Transpose of A =

\[\begin{split}\begin{bmatrix} 1 & 4 & 7 \\ 2 & 5 & 8 \\ 3 & 6 & 9 \end{bmatrix}\end{split}\]

e.g.:

40 ΜΑΤ Β = ΤRΝ(Α)

will assign to B the transpose of A.

INV(R)

This function is used to invert the specified matrix.

e.g.:

40 ΜΑΤ Β = ΙΝV(Α)

will assign to B the inverse of A.

This can only be performed on square matrices. Certain matrices do not have an inverse, these are called singular matrices.

DΕΤ

This function will find the determinant of the last square matrix which was inverted (see INV above).

e.g.:

40 ΜΑΤ Β = ΙΝV(Α)
50 LET X = DET

will find the determinant of B.

COL(A)

This function will give the current second bound for a matrix A.

e.g.:

40 DIM A(4,7)
50 LET B = COL(A)

B will be set equal to 7.

Note: This will give 0 (zero) for vectors.

ROW(A)

This function will give the current first bound for a matrix A.

e.g.:

40 DIM A(4,7)
50 LET B = ROW(A)

B will be set equal to 4.

These two functions could be especially useful after redimensioning has taken place.

Matrix manipulation and arithmetic

Assignment

To assign the value of one matrix to another, with new bounds if necessary, the MAT assignment statement is used. This is of the form

Line number MAT destination = array expression

e.g.:

40 ΜΑΤ Α = Β

will redimension A to the bounds of B (if necessary) and assign to A the values of B.

Arithmetic operations

Addition and Subtraction can be performed on complete matrices (or vectors) provided that they are the same size.

e.g.:

10 DIM A(4,4), B(3,3), C(3,3)
20 MAT INPUT B, C
30 ΜΑΤ Α = Β + C
40 ΜΑΤ ΡRΙΝΤ Α
50 ΜΑΤ Α = Β - C
60 ΜΑΤ ΡRΙΝΤ Α
70 ΕΝD

Note: There are no matrix functions that can access a single row or column of a matrix.

This must be done using FOR loops.

e.g. To access the first column of a 3 x 3 matrix.

10 DIM A(3,3), B(3)
20 ΜΑΤ ΙΝΡUΤ Α
30 FΟR Χ = 1 ΤΟ 3
40 LET B(X) = A(1,X)
50 ΝΕΧΤ Χ

Multiplication

can be performed on

  1. 2 matrices with the number of columns of the first equal to the number of rows of the second.

    e.g.

\[\begin{split}\begin{bmatrix} a_{11} & a_{12} & a_{13} \\ a_{21} & a_{22} & a_{23} \end{bmatrix} \times \begin{bmatrix} b_{11} & b_{12} \\ b_{21} & b_{22} \\ b_{31} & b_{32} \end{bmatrix} = \begin{bmatrix} c_{11} & c_{12} \\ c_{21} & c_{22} \end{bmatrix}\end{split}\]
  1. a vector and a matrix with the number of columns of the first being equal to the number of rows of the second.

    e.g.

\[ \begin{align}\begin{aligned}\begin{split}\begin{bmatrix} d_1 & d_2 & d_3 \end{bmatrix} \times \begin{bmatrix} e_{11} & e_{12} & e_{13} & e_{14} \\ e_{21} & e_{22} & e_{23} & e_{24} \\ e_{31} & e_{32} & e_{33} & e_{34} \end{bmatrix} = \begin{bmatrix} f_1 & f_2 & f_3 & f_4 \end{bmatrix}\end{split}\\or\end{aligned}\end{align} \]
\[\begin{split}\begin{bmatrix} e_{11} & e_{12} & e_{13} & e_{14} \\ e_{21} & e_{22} & e_{23} & e_{24} \\ e_{31} & e_{32} & e_{33} & e_{34} \end{bmatrix} \times \begin{bmatrix} g_1 \\ g_2 \\ g_3 \end{bmatrix} = \begin{bmatrix} h_1 \\ h_2 \\ h_3 \end{bmatrix}\end{split}\]
  1. A scalar (i.e. a number) and a vector or matrix.

    e.g.

\[ \begin{align}\begin{aligned}\begin{split}j \times \begin{bmatrix} k_{11} & k_{12} \\ k_{21} & k_{22} \end{bmatrix} = \begin{bmatrix} l_{11} & l_{12} \\ l_{21} & l_{22} \end{bmatrix}\end{split}\\or\end{aligned}\end{align} \]
\[\begin{split}j \times \begin{bmatrix} m_1 \\ m_2 \end{bmatrix} = \begin{bmatrix} n_1 \\ n_2 \end{bmatrix}\end{split}\]

Examples in BASIC.

10 DIM A(2,3), B(3,2), C(2,2), D(3), E(3,4), F(3)
20 DIM G(3), H(3), K(2,2), L(2,2), M(2), N(2)
30 MAT INPUT A, B, C, D, E, G, K, M
40 INPUT J
50 ΜΑΤ C = Α * Β
60 ΜΑΤ Ρ = D * Ε
70 ΜΑΤ Η = Ε * G
80 ΜΑΤ L = (J) * Κ
90 ΜΑΤ Ν = (J) * Μ

Note that scalars should always appear in brackets to distinguish them from matrices or vectors.

Division

It is also possible to divide a matrix or vector by a scalar.

e.g.:

40 ΜΑΤΒ = (1/J) * Α

will divide every element of A by J and assign the result to B