Program multiplies two matrices AxB. (Sample Programs)

by lrcvs, Sunday, May 04, 2014, 17:08 (1318 days ago)

This program multiplies two matrices AxB.
Any rows or colunmas.
Eg:
A)
1 2 3
4 5 6
7 8 9

B)
9 8 7
6 5 4
3 2 1

Note:
Jim, to assign the tag value of anonymous array,
working without declaring arrays. Is that right?
See you the init program !!!

**************************************************************************
#matrices multiplication

#dim a(8) #<<< >>> !!!!!!!!! ?????????? and run
#dim b(8) #<<< >>> !!!!!!!!! ?????????? and run

a = {1,2,3,4,5,6,7,8,9}
b = {9,8,7,6,5,4,3,2,1}

redim a(3,3)
redim b(3,3)

cls

gosub show

for row = 0 to a[?,]-1
for col = 0 to b[,?]-1
t = 0
for h1 = 0 to a[,?]-1
t = t + (a[row,h1] * b[h1,col])
print "("+ string(a[row,h1]) + " x " + string (b[h1,col]) +") + ";
next h1
print " = "+t
next col
print
next row

print
print "*****************"
print

for row = 0 to a[?,]-1
for col = 0 to b[,?]-1
t = 0
for h1 = 0 to a[,?]-1
t = t + (a[row,h1] * b[h1,col])
print "("+ string(a[row,h1]* b[h1,col])+") + ";
next h1
print " = "+t
next col
print
next row

end

#*********************
show:

print "A"
for row = 0 to a[?,]-1
for col = 0 to a[,?]-1
print a[row,col] + " ";
next col
print
next row

print
print "x"
print
print "B"

for row = 0 to a[?,]-1
for col = 0 to a[,?]-1
print b[col,row] + " ";
next col
print
next row

print
print "="
print
return

******************************************************
Out Screen

A
1 2 3
4 5 6
7 8 9

x

B
9 6 3
8 5 2
7 4 1

=

(1 x 9) + (2 x 6) + (3 x 3) + = 30
(1 x 8) + (2 x 5) + (3 x 2) + = 24
(1 x 7) + (2 x 4) + (3 x 1) + = 18

(4 x 9) + (5 x 6) + (6 x 3) + = 84
(4 x 8) + (5 x 5) + (6 x 2) + = 69
(4 x 7) + (5 x 4) + (6 x 1) + = 54

(7 x 9) + (8 x 6) + (9 x 3) + = 138
(7 x 8) + (8 x 5) + (9 x 2) + = 114
(7 x 7) + (8 x 4) + (9 x 1) + = 90


*****************

(9) + (12) + (9) + = 30
(8) + (10) + (6) + = 24
(7) + (8) + (3) + = 18

(36) + (30) + (18) + = 84
(32) + (25) + (12) + = 69
(28) + (20) + (6) + = 54

(63) + (48) + (27) + = 138
(56) + (40) + (18) + = 114
(49) + (32) + (9) + = 90

*****************************************************************

Regards!

Program multiplies two matrices AxB.

by Jim ⌂ @, Russell, KY, Tuesday, May 06, 2014, 16:26 (1316 days ago) @ lrcvs

Yes, that works well.

Since sometime around 9.9.50 or so (if my memory serves me) the assigning of an array {} to a variable automagically creates the array and assigning a single value to an array variable destroys the array. This behavior greatly simplified the variable object and eliminated several memory leaks caused by arrays.

Jim

Program multiplies two matrices AxB.

by lrcvs, Tuesday, May 06, 2014, 17:37 (1316 days ago) @ Jim

Ok!, That is a great advantage because we save / remove the arrays.

I put it by mistake and it was a surprise to see this special operation when reviewing the program.

Thanks!

RSS Feed of thread
powered by my little forum