Rosetta Code:Solve a Task: Difference between revisions
LOGO |
|||
Line 1: | Line 1: | ||
=={{header| |
=={{header|Matrix Multiplication}}== |
||
<lang LOGO> |
<lang LOGO> |
Revision as of 12:16, 10 April 2012
Matrix Multiplication
<lang LOGO>
TO LISTVMD :A :F :C :NV
- PROCEDURE LISTVMD
- A = LIST
- F = ROWS
- C = COLS
- NV = NAME OF MATRIX / VECTOR NEW
- this procedure transform a list in matrix / vector square or rect
(LOCAL "CF "CC "NV "T "W) MAKE "CF 1 MAKE "CC 1 MAKE "NV (MDARRAY (LIST :F :C) 1) MAKE "T :F * :C FOR [Z 1 :T][MAKE "W ITEM :Z :A MDSETITEM (LIST :CF :CC) :NV :W MAKE "CC :CC + 1 IF :CC = :C + 1 [MAKE "CF :CF + 1 MAKE "CC 1]] OUTPUT :NV END
TO XX
- MAIN PROGRAM
- LRCVS 10.04.12
- THIS PROGRAM multiplies two "square" matrices / vector ONLY!!!
- THE RECTANGULAR NOT WORK!!!
CT CS HT
- FIRST DATA MATRIX / VECTOR
MAKE "A [1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49] MAKE "FA 5 ;"ROWS MAKE "CA 5 ;"COLS
- SECOND DATA MATRIX / VECTOR
MAKE "B [2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50] MAKE "FB 5 ;"ROWS MAKE "CB 5 ;"COLS
IF (OR :FA <> :CA :FB <>:CB) [PRINT "Las_matrices/vector_no_son_cuadradas THROW
"TOPLEVEL ]
IFELSE (OR :CA <> :FB :FA <> :CB) [PRINT
"Las_matrices/vector_no_son_compatibles THROW "TOPLEVEL ][MAKE "MA LISTVMD :A
- FA :CA "MA MAKE "MB LISTVMD :B :FB :CB "MB] ;APPLICATION <<< "LISTVMD"
PRINT (LIST "THIS_IS: "ROWS "X "COLS) PRINT [] PRINT (LIST :MA "=_M1 :FA "ROWS "X :CA "COLS) PRINT [] PRINT (LIST :MB "=_M2 :FA "ROWS "X :CA "COLS) PRINT []
MAKE "T :FA * :CB
MAKE "RE (ARRAY :T 1)
MAKE "CO 0
FOR [AF 1 :CA][
FOR [AC 1 :CA][
MAKE "TEMP 0
FOR [I 1 :CA ][
MAKE "TEMP :TEMP + (MDITEM (LIST :I :AF) :MA) * (MDITEM (LIST :AC :I) :MB)]
MAKE "CO :CO + 1
SETITEM :CO :RE :TEMP]]
PRINT []
PRINT (LIST "THIS_IS: :FA "ROWS "X :CB "COLS)
SHOW LISTVMD :RE :FA :CB "TO ;APPLICATION <<< "LISTVMD"
END
- \
M1 * M2 RESULT / SOLUTION
1 3 5 7 9 2 4 6 8 10 830 1880 2930 3980 5030
11 13 15 17 19 12 14 16 18 20 890 2040 3190 4340 5490 21 23 25 27 29 X 22 24 26 28 30 = 950 2200 3450 4700 5950 31 33 35 37 39 32 34 36 38 40 1010 2360 3710 5060 6410 41 43 45 47 49 42 44 46 48 50 1070 2520 3970 5420 6870
- \
NOW IN LOGO!!!!
THIS_IS: ROWS X COLS
{{1 3 5 7 9} {11 13 15 17 19} {21 23 25 27 29} {31 33 35 37 39} {41 43 45 47 49}} =_M1 5 ROWS X 5 COLS
{{2 4 6 8 10} {12 14 16 18 20} {22 24 26 28 30} {32 34 36 38 40} {42 44 46 48 50}} =_M2 5 ROWS X 5 COLS
THIS_IS: 5 ROWS X 5 COLS
{{830 1880 2930 3980 5030} {890 2040 3190 4340 5490} {950 2200 3450 4700 5950}
{1010 2360 3710 5060 6410} {1070 2520 3970 5420 6870}}
</lang>