General
This program checks out the multiplication and addition between
matrices. More specifically, the program checks if the dimensions of
matrices follow the requirements of the operation and if so, it returns
the dimensions of the resulting matrix.
Instructions
First define the dimensions of matrices in lines 20-100. The
dimensions should be a string of three characters with the form "nxm"
("n" and/or "m" could be numbers or letters). Then define the
expression string in line 110. Only "*" and "+" can be used in this
program (parentheses cannot be processed). Run the program with RUN 10.
The matrices in lines 25-65 and the expression in 110 in this code
concern the Kalman filter (see
http://en.wikipedia.org/wiki/Kalman_filter)
1 LIST 10-19
5 ' Evangelos Rozos Jan. 2012
11 ' This program investigates the
12 ' dimensions of matrices in
13 ' algebraic formulas.
14 '
15 ' Define matrices in lines
16 ' 20-99, define the expression in
17 ' line 110 (valid operands are +
18 ' and *). Run with RUN 10.
19 '
20 ' Define matrix dimensions
21 ' ************************
25 X$="nx1" ' n=no. of variables
35 Z$="ox1" ' o=no. of observations
40 E$=Z$
45 P$="nxn": Q$=P$: F$="nxn": FT$=F$
55 H$="oxn": HT$="nxo"
60 R$="oxo": S$=R$
65 K$="nxo"
100 '
101 ' Expression
102 ' **********
110 XSN$= H$ + "*" + P$ + "*" + HT$ + "+" + R$
200 '
201 ' Parse multiplications
202 ' *********************
205 LR$=MID$(XSN$,1,1)
207 LC$=MID$(XSN$,3,1)
210 FOR II=3 TO LEN(XSN$)-3 STEP 4
220 : OPER$=MID$(XSN$,II+1,1)
230 : RR$=MID$(XSN$,II+2,1)
240 : RC$=MID$(XSN$,II+4,1)
250 : IF OPER$="*" THEN GOTO 300
255 : IF OPER$<>"+" THEN GOTO 600
260 : XPN$=XPN$+LR$+"x"+LC$+"+"
270 : LR$=RR$: LC$=RC$
280 NEXT
290 XPN$=XPN$+LR$+"x"+LC$
295 GOTO 400
300 '
301 ' Check multiplication
302 ' ********************
305 IF LC$=RR$ THEN GOTO 335
307 IF LR$="1" AND LC$="1" THEN GOTO 333
310 PRINT "Multiplication error!"
315 PRINT "LCOL,RROW="LC$","RR$
330 END
333 LR$=RR$
335 LC$=RC$
340 GOTO 280 ' RETURN
400 '
401 ' Parse additions
402 ' ***************
403 IF LEN(XPN$)=3 THEN GOTO 498
405 LR$=MID$(XPN$,1,1)
407 LC$=MID$(XPN$,3,1)
410 FOR II=3 TO LEN(XPN$)-3 STEP 4
420 : OPER$=MID$(XPN$,II+1,1)
430 : RR$=MID$(XPN$,II+2,1)
440 : RC$=MID$(XPN$,II+4,1)
460 : GOSUB 500
470 NEXT
497 '
498 PRINT "Result is "LR$"x"RC$
499 END
500 '
501 ' Check addition
502 ' **************
505 IF LC$=RC$ AND LR$=RR$ THEN GOTO 535
510 PRINT "Addition error!"
515 PRINT "Left is "LR$"x"LC$
517 PRINT "Rght is "RR$"x"RC$
530 END
535 RETURN
600 '
601 PRINT "Invalid operand!"
602 END
V.