Interactive Matrix Calculator Version of 2 January 1987 Copyright (c) 1987 by David L. Turner Mountain Home Software Co. 13360 N. Mt. Home Road Cove (Lewiston),Utah 84320 Phone (801)258-5164 Provided as User Supported Software, to be freely copied for noncomercial use. The author reserves the right to distribute this package or any part thereof, for profit. If you find this program useful, your voluntary contribution of $25, or whatever you feel the program is worth, will provide updates, further documentation and encouragement for production of similar software. The program is written and compiled in Microsoft Basic. It uses double precision arithmetic for all storage and operations. Due to a limitation of Microsoft Basic, the program operates on only 20 matrix areas, numbered from 1 through 20. Each matrix may store 150 elements, the product of the number of rows with the number of columns. Since the matrices are stored in contiguous blocks, if every other matrix, say numbers 1,3,5, etc. only, are used, then up to 300 elements per matrix may be stored. Storage of 450 elements per matrix is possible if only areas 1,4,7 etc. are used, 600 elements if only 1,5, etc. are used, etc. Since the program makes no boundary checks, the 151st element of a matrix will be placed into the first position of the next matrix. Only one Operation at a time is performed, i.e. only one multiplication, one addition, etc. Results may be displayed at any time using the Print command. The storage limit of 20 matrices may be extended by using the disk for additional storage and retrieval. Known limitations of the program are those inherent to Microsoft Basic, i.e. limited array storage size, no recognition of path names for files, etc. The program has been in use in classroom and consulting work for several years, so most of the major bugs have been taken care of. Registered users will be notified of major updates and new releases. Problem reports are encouraged as are suggestions for enhancements. The program is invoked from the MS-DOS prompt by giving the command MATRIX. This displays the opening screen, which lists the version, the author and the following menu: Interactive Matrix Calculator Documentation Page 2 Valid Commands are: + or A to Add matrices C to do Cholesky (Square root) decomposition D to Duplicate (copy) from one area to another E for Elementary operations loop F to Fix a single element G to get Part of a matrix (copy part to another) I to calculate the Inverse using sweep operator (Gaussian eliminition) J to Join two matrices together K to Keep or retrive a worksheet to or from disk ?, L or H for Help or to List the menu * or M to Multiply matrices N to print or read a 'nice' matrix P to Print a matrix to screen or disk Q or X to Quit or Exit R to Read in a matrix from keyboard or disk - to Subtract matrices S to Sweep a matrix T to calculate Trace and/or sum of all elements of a matrix ' to Transpose a matrix Z to change Epsilon or change values < epsilon to 0 READY Execution of each command is begun immediately after the appropriate letter is touched. The program prompts the user for additional required input. A brief alphabetical listing and discussion of each command follows. ADD OR + COMMAND The program asks for indices for the left, right and answer or destination matrices. The example below adds Matrix 1 on the left and Matrix 2 on the right, putting the answer into Matrix 3. READY A To Add (left or first) matrix + (second or right) matrix into (destination) matrix, Enter numbers for left, right and destination matrices: 1 2 3 Matrix 3 = Matrix 1 + Matrix 2 CHOLESKY COMMAND For a nonnegative (positive definite or positive semidefinite) matrix A, the Cholesky or square root decomposition finds an upper triangular matrix T such that T'T = A. If a nonsquare matrix (A,B), is input to this routine, then A is still assumed to be a nonnegative matrix. For such nonsquare matrices, the Cholesky decomposition leaves the T matrix in the first k rows and columns and the product (T'inverse)B in place of the B matrix. This is a very useful procedure in statistical modeling. If the following matrix is in area 3, Row # Row # 1 : 2 : 1 , 2 , 2 , 20 , 3 , 26 , Interactive Matrix Calculator Documentation Page 3 then the Cholesky command execution is begun by touching the C key at the READY prompt. The program then asks for the number of the original matrix, the matrix to be "square rooted," and a number for the result. Again it is a good idea to print the resulting "Choleskied" matrix. To be certain of the output, calculation and examination of T'T is also recommended. READY C Cholesky or square root decomposition routine Enter number of matrix to square root and number of matrix for result: 3 4 Matrix 4 contains the 'square root' of Matrix 3 This produces the following matrix in area 4: Row # Row # 1 : 2 : 1 , 0 , 2 , 4 , 3 , 5 , DUPLICATE OR COPY COMMAND Sometimes it is useful to operate on a copy of a matrix rather than the original matrix. To Duplicate or copy a matrix, touch the D key at the READY prompt. Then enter the number of the original matrix and the number for the copy or duplicate. READY D Matrix Duplication or Copy routine Enter numbers for original and copy matrices: Matrix 2 is a copy or duplicate of Matrix 1 1 2 ELEMENTARY OPERATIONS COMMAND This command lets the user perform elementary row operations such as interchanging 2 rows, multiplying or dividing a row by a constant or adding a multiple of a row to another row. If a matrix A is augmented by an appropriate sized identity, and then elementary row operations are performed on the augmented matrix (A,I) until the matrix looks like (I,B), then B will be the inverse of A. If the original A matrix cannot be reduced to an identity, coming as close as possible will make B a generalized inverse of A. For the example below, the matrix (1 2/2 5) is augmented with a 2 x 2 identity matrix and this 2 x 4 matrix is then loaded into matrix area 1. To start the Elemenrary row operations, press the E key at the READY prompt and then follow the instructions. For this simple example, the first row is multiplied by -2 and added to the second row. After printing this intermediate result, it is easy to see that the second row must be multiplied by -2 and added to the first row. This makes an identity matrix where the A matrix started and consequently contains the inverse of A where the identity started. This inverse may then be extracted from this augmented matrix using the Get command. READY E Interactive Matrix Calculator Documentation Page 4 Routine to do elementary row operations Enter number of matrix to work on: ? 1 C followed by numbers of rows to interchange Enter M or * (D or /) followed by first row#, scalar, second row# to Multiply(Divide) first row by scalar and add to second row If second row# is omitted, row will be multiplied by scalar P will print the matrix L or H to print menu (list) of elementary commands <CR> will return to main menu (terminate elementary operations) Enter operator: M 1 -2 2 Row # 1 has been multiplied by -2 Enter operator: Row # Row # 1 : 2 : P 1 , 0 , 2 , 1 , 1 , -2 , 0 , 1 , Enter operator: M 2 -2 1 Row # 2 has been multiplied by -2 Enter operator: and added to row # 2 and added to row # 1 P Row # 1 : 1 , 0 , Row # 2 : 0 , 1 , Enter operator: <cr> 5 , -2 , -2 , 1 , FIX COMMAND Since few typists or data entry personnel are perfect, a command is provided to fix a single element in any of the matrices. To Fix a matrix, touch the F key at the READY prompt and follow the instructions. The example below makes the element in row 1 and column 3 of matrix 1 equal to 33. It is a good idea to print the matrix after changing an element to confirm that the change has taken place. READY F Matrix Fixing routine Enter matrix number, row no. and column no. of element to fix and the correct value: 1 1 3 33 The element in row 1 and column 1 of Matrix 3 has been changed to 33 GET COMMAND Sometimes a matrix must be decomposed into some smaller pieces. This may be done with the Get command. Columns and rows from one matrix are copied into a second matrix. If matrix 3 contains the product (X,Y)'(X,Y), then Rows 1 and 2 and columns 1 and 2 contain the X'X matrix. Rows 1 and 2 of column 3 contain X'Y. Interactive Matrix Calculator Documentation Page 5 To start the Get command, just touch the G key at the READY prompt and follow the instructions. READY G Get part of a matrix Enter number of original and new matrices: 3 5 Enter the number of rows for the new matrix: ? 2 Now enter 2 indices for the rows to be selected from old matrix to be put into the new matrix 1 2 Now enter the number of columns for the new matrix ? 1 Now enter 1 indices for columns to be selected from the old matrix to be put into the new matrix 3 Matrix 5 contains parts of Matrix 3 READY P Enter number of matrix to print: or enter file name to write matrix to a disk file: 5 Row # Row # 1 : 2 : 6 , 46 , 46 , 462 , INVERSE COMMAND Gaussian elimination or pivoting is the only explicit inverse routine provided in this package. Some very elementary checks are built in to prevent division by zero or by a number "close" to zero. The validity of any inverse computed by this procedure should be checked by seeing if the product of the orignial matrix and the supposed inverse is "close" to an identity matrix. The X'X matrix Gotten in the Get example is inverted. The procedure is invoked by touching the I key at the READY prompt and then giving the number of the matrix to invert and the number of the matrix for storing the result. The actual value of the pivoting element is printed. The matrix must be swept or pivoted on each diagonal element. In the example below, the answer is checked by multiplication. Note that this product matrix is "close" to an identity matrix. READY I Inversion using Sweep operator (Gaussian elimination) Enter index of matrix to invert and index for result: 4 6 Sweeping element # 2 With diagonal value= 462 Sweeping element # 1 With diagonal value= 1.4199134199134 Matrix 6 = (Matrix 4)inverse READY M To Multiply (left or first) matrix * (second or right) matrix into (destination) matrix, enter numbers for left, right and destination matrices Or to multiply a matrix by a scalar enter M (or D to divide) followed by matrix number and scalar: 4 6 7 Matrix 7 = Matrix 4 * Matrix 6 Interactive Matrix Calculator Documentation Page 6 READY P Matrix output routine Enter number of matrix to display or enter a file name to write matrix to a disk file: 7 Row # Row # 1 : 2 : .9999999999999999 , -6.938893903907228D-18 , -8.881784197001252D-16 , 1 , JOIN COMMAND The Join command is used to augment or put together two matrices. To Join 2 matrices together, touch the J key at the READY prompt and follow the instructions. Again it is a good idea to print the resulting joined or augmented matrix. For vertical augmentation, work with transposes and then transpose the final augmented matrix. READY J Matrix Joining or augmentation routine Enter numbers for left, right and augmented matrices: 2 2 3 Matrix 3 = (Matrix 2, Matrix 2) KEEP AND RETRIEVE COMMANDS To save or retrieve all the matrices for a particular worksheet, the Keep command is used. It dumps or retrieves all the matrices to or from a file, allowing the user to save all of the current matrices or to retrieve all of the matrices used in a previously saved worksheet. Touching the K key at the READY prompt causes the program to prompt the user for more information. To save the current worksheet type S followed by a file name. To retrieve a previously saved worksheet, type R followed by the name of the file to be retrieved. The Save operation creates a file whose first 2 records describe what the file is. The next record tells the overall size of the worksheet, the number of matrices available and the number of elements for each one. Then comes information about each matrix: the matrix number, the number of rows and the number of columns followed by the elements of the matrix in rowwise order. When retrieving a saved worksheet, the program lists the matrix number and size for each matrix as it is retrieved. An example of both saving and the later retrieval of a worksheet is given below. READY K Worksheet Save/Retrieve routine Enter S to Save a worksheet R to Retrieve or Reload a worksheet: S Enter name of file to be Saved or Retrieved: try.sav Saving the matrix worksheet into a file named try.sav 2 x 2 Matrix 1 in the worksheet stored in file try.sav 2 x 2 Matrix 2 in the worksheet stored in file try.sav 2 x 4 Matrix 3 in the worksheet stored in file try.sav Interactive Matrix Calculator Documentation Page 7 READY K Worksheet Save/Retrieve routine Enter S to Save a worksheet R to Retrieve or Reload a worksheet: R Enter name of file to be Saved or Retrieved: try.sav Retrieving a worksheet from the file named try.sav Beginning to load a matrix into area # 1 Matrix area number 1 has been successfully loaded with a matrix 2 x 2 Beginning to load a matrix into area # 2 Matrix area number 2 has been successfully loaded with a matrix 2 x 2 Beginning to load a matrix into area # 3 Matrix area number 3 has been successfully loaded with a matrix 2 x 4 ?, HELP or LIST COMMAND Very limited help is provided in this program, i.e. the list of valid commands is printed whenever a ?,H or L key is touched at the READY prompt. MULTIPLY COMMAND Two matrices may be multiplied together only if they are conformable for multiplication. This means that the number of columns in the left or first matrix must be the same as the number of rows in the right or second matrix. The program does not make any checks for conformability, so the user must be careful, and printing of each of the component matrices and the product matrix is recommended. As an example, the product M2*M1 is computed below and placed into matrix area number 3. READY M To Multiply (left or first) matrix * (second or right) matrix into (destination) matrix, enter numbers for left, right and destination matrices Or to multiply a matrix by a scalar enter M (or D to divide) followed by matrix number and scalar: 2 1 3 Matrix 3 = Matrix 2 * Matrix 1 This command may also be used for performing scalar multiplication or division where each element of a matrix is to be multplied or divided by a scalar. READY M To Multiply (left or first) matrix * (second or right) matrix into (destination) matrix, enter numbers for left, right and destination matrices Or to multiply a matrix by a scalar enter M (or D to divide) followed by matrix number and scalar: M 1 3 Every element in matrix number 1 has been multiplied by 3 READY M To Multiply (left or first) matrix * (second or right) matrix into (destination) matrix, enter numbers for Interactive Matrix Calculator Documentation Page 8 left, right and destination matrices Or to multiply a matrix by a scalar enter M (or D to divide) followed by matrix number and scalar: D 1 2 Every element in matrix number 1 has been multiplied by .5 NICE PRINT/READ COMMAND Microsoft Basic has very primitive output formatting capacities, but some nicer formatting is possible. This command functions just about like the Print command. To print a matrix in a "nice" format, touch the N key, then the P key and then specify the matrix to print. The matrix is then printed in scientific notation. For example, -.7012195122D-01 translates as -.7012195122 times 10 to the -1 power, or -0.07012195122 for the final answer. READY N For a 'nice' printout enter 'P' To input a 'nice' (i.e. rectangular) matrix, enter 'R': P Matrix output routine Enter number of matrix to display or enter a file name to write matrix to a disk file: 4 Row # Row # Row # 1 : 2 : 3 : 0.7042682927D+00, -.7012195122D-01, -.4975609756D+01, -.7012195122D-01, 0.9146341463D-02, -.8292682927D+00, 0.4975609756D+01, 0.8292682927D+00, 0.1214634146D+02, The Nice command may also be used to read in rectangular matrices or data from a previously prepared disk file. For example, if the matrix (1 2 3/4 5 6) is stored in a file named TRY.IT which contains 2 records, one for each row, then this file may be read into a matrix area using the Nice Read command. Note that the 2 values for number of rows and number of columns must be separated by a comma for this routine. READY N For a 'nice' printout enter 'P' To input a 'nice' (i.e. square) matrix, enter 'R': R Enter name of previously saved file to read from disk: try.it Reading a matrix from a previously saved file try.it Enter the number of the area to read matrix into ? 1 Enter number of rows and columns for the matrix: ? 2 3 ?Redo from start ? 2,3 2 x 3 matrix from file try.it read into Matrix 1 READY P Matrix output routine Enter number of matrix to display or enter a file name to write matrix to a disk file: 2 Row # Row # 1 : 2 : 1 , 4 , 2 , 5 , 3 , 6 , Interactive Matrix Calculator Documentation Page 9 PRINT COMMAND To see the results of any command, the Print command must be used. The program prompts for and prints only one matrix at a time. Any matrix may also be printed to disk, expanding the storage capacity of the program. An example of disk storage for an individual matrix is provided below. The example below is started by touching the P key at the READY prompt and then entering 2 as the number of the matrix to be printed. READY P Matrix output routine Enter number of matrix to display or enter a file name to write matrix to a disk file: 1 PRINT TO DISK COMMAND While only 20 matrices are available for internal storage, matrices may be written to disk files, one file per matrix. To create a disk file for any matrix, simply touch the P key at the READY prompt and then enter a file name rather than a matrix number. The program will then prompt for the number of the matrix to be written to the disk file. For example, to create a file which contains the matrix stored in area number 1, give the file name EXAMPLE.MAT when prompted for the matrix number. READY P Matrix output routine Enter number of matrix to display or enter a file name to write matrix to a disk file: example.mat Number of Matrix to write to disk: ? 1 Matrix 1 written to the file example.mat QUIT, EXIT OR STOP COMMAND Although a control-C or control-Break command will kill the program, the Q or X command will cause a "graceful" exit from the program. Touching the Q or X key at the READY prompt causes the program to terminate. READ COMMAND Before any operations may be performed, something must be read into the matrix worksheet to operate on. To read a matrix in, the Read command is used. The program then asks for the number of the matrix to read data into, and then the number of rows and columns to be read in. The program then prompts for the entries for each row. The numbers for each row must then be entered separated by commas or blanks. For example, to read a 2 row by 3 column matrix into area 1, start by touching the R key at the READY prompt, and then following the instructions. The first line indicates that the matrix is to be read into area 1 and that it will contain 2 rows and 3 columns. The elements for the first row are then entered, followed by the elements for the second row. Interactive Matrix Calculator Documentation Page 10 READY R Matrix Read or Input Routine Enter matrix number, no. rows and no. columns or enter a file name to read a previously saved file from disk: 1 2 3 Input row # 1 : 1 2,3 1 2,3 Input row # 2 : 4,5,1 4,5,1 READ FROM DISK COMMAND To read a matrix back into a matrix area, give the file name when prompted for a matrix number. The example below reloads the matrix saved previously in the file EXAMPLE.MAT. READY R Matrix Read or Input Routine Enter matrix number, no. rows and no. columns or enter a Enter name of previously saved file to read from disk: example.mat Reading a matrix from a previously saved file example.mat Enter the number of the area to read matrix into ? 2 2 x 4 matrix read from the file example.mat into Matrix 2 SUBTRACT OR - COMMAND The Subtract command works just like the addition command except that the right or second matrix is subtracted from the left or first matrix. For example, READY To Subtract (left or first) matrix - (second or right) matrix into (destination) matrix, Enter numbers for left, right and destination matrices: 1 2 3 Matrix 3 = Matrix 1 - Matrix 2 SWEEP COMMAND The sweep command may be used in finding inverses of "nice" matrices. It performs Gaussian elimination one step at a time. It is a very convenient method of performing multiple regression analysis computations especially when any variable selection is to be done. The matrix listed below contains the product (X,Y)'(X,Y) = X'X X'Y Y'X Y'Y The X matrix contains the X or independent variables to be used in predicting the Y variable. After sweeping one or more variables into the equation, the column which originally contained X'Y will contain the regression coefficients for those variables in the equation. The single element corresponding to the Y'Y element in the initial or original matrix will contain the error sum of squares for the model containing the swept variables. One nice feature of the sweep operator is that it is reversible. This means that if a variable is swept twice, the second sweep removes the Interactive Matrix Calculator Documentation variable from the equation. regression programs. Page 11 This procedure is widely used in stepwise Generally the first column and row corresponds to an X variable which is equal to 1 for each observation to put the intercept into the equation. The second, third, etc. columns correspond to the first, second, etc. variables available for the equation. The last row and column corresponds to the Y variables. In the example below, the intercept is first "swept" into a 1 variable equation which also has an intercept. This gives corrected sums of squares and crossproducts for the second and third columns and rows of this matrix. The second sweep operation gives the (X'X) inverse, the vector of regression coefficients and the error sum of squares. The initial, unswept matrix is Row # Row # Row # 1 : 2 : 3 : 6 , 46 , 68 , 46 , 68 , 462 , 612 , 612 , 858 , READY S Sweep Operator Enter matrix and element number to sweep: 4 1 Matrix 4 has been swept on element 1 READY S Sweep Operator Enter matrix and element number to sweep: 4 2 Matrix 4 has been swept on element 2 READY P Matrix output routine Enter number of matrix to display or enter a file name to write matrix to a disk file: 4 Row # 1 : .1666666666666667 , 11.33333333333333 , Row # 2 : -7.666666666666667 , 90.66666666666667 , Row # 3 : -11.33333333333333 , 87.33333333333333 , 7.666666666666667 , 109.3333333333333 , 90.66666666666667 , TRACE COMMAND Sometimes the sum of the diagonal elements, the trace of a matrix, is needed. This is done with the Trace Command which is started by touching the T key at the READY prompt. This causes the program to prompt the user for a matrix number. The sum of all the elements and the sum of the diagonal elements is then computed and printed. READY T Matrix Trace and sum routine Enter number of matrix whose elements are to be summed or whose trace is to be found if matrix is square: ? 2 Sum of all elements in matrix area # 2 is 2 Trace of matrix area # 2 is 2 Interactive Matrix Calculator Documentation Page 12 TRANSPOSE COMMAND The Transpose command interchanges rows and columns, for example changing the ith row into the ith column. If a(ij) represents the element in the ith row and jth column of the original matrix A, then it will become the element in the jth row and ith column of A', the transpose. If the original matrix has m rows and n columns, then the transpose will have n rows and m columns. Again, it is a good idea to print the transposed matrix. Execution begins by touching the ' key and then entering the numbers for the origninal and transpose matrices. READY ' Transpose routine Enter number of matrix to transpose and matrix number for result: Matrix 2 = Matrix 1' 1 2 FUZZ COMMAND Sometimes values that should be zero turn out "close" to zero. For example, -8.88 times 10 to the minus 16 is "close" to zero. The Fuzz routine goes through and compares each element of a matrix with an "Epsilon" value, changing those which are smaller in absolute value than Epsilon to zero. The first part of the Fuzz command allows the user to change the value of Epsilon. The second part allows a particular matrix to be "defuzzed." The product of a matrix with its diagonal should be an identity matrix, but often the off-diagonal elements are close but not quite equal to zero. Touching the "Z" key at the READY prompt starts the Fuzz procedure. For the example below, a matrix is defuzzed and then printed. Note that the first element should be 1, but is instead "close" to one. The fuzz routine changes only values that are "close" to zero. READY Z Matrix defuZzing routine Epsilon is currently 1.000000013351432D-10 Enter a new value to change, or hit <CR> to leave: To defuZz or convert absolute values < 1.000000013351432D-10 zeroes, Enter matrix area number. <CR> Otherwise: 2 Values in matrix area # 2 which were less than 1.000000013351432D-10 have been changed to zeroes p Matrix output routine Enter number of matrix to display or enter a file name to write matrix to a disk file: 2 Row # Row # 1 : 2 : .9999999999999999 , 0 , 1 , 0 , to