Andrew K. wrote:
> : matrix_product { A[] B[] A*B \ -- } \ leave product array at adr
> 3 0 do
> 3 0 do
> \ using standard Forth inner and outer loop indeces
> \ I and J, do the required summation of 3 products
> \ A(i,k)*B(k,j) and deposit the results at final
> \ array destination [i,j]
> loop
> loop
> ;
Since it is a 3x3 matrix you need 3 levels of do..loops
After some more time I came to the following:
aNew -3x3Matrix.f \ Final source
3 constant rows
: 3x3Allot [ 3 3 * ] literal cells allot ;
Create 3x3Matrix1 \ Create a 3x3 matrix
1 , 2 , 3 , \ And fill them with some values.
-1 , -2 , -3 ,
5 , 6 , 7 ,
Create 3x3Matrix2
10 , 20 , 30 ,
40 , 50 , 60 ,
70 , 80 , 90 ,
Create 3x3Matrix3 3x3Allot \ For the results
: >ColRow ( col row - adr ) >r cells + r> rows * cells + ;
: .3x3M { MatrixAddr -- } \ To see them
Rows 0
do cr
3 0
do MatrixAddr i j >ColRow @ .
loop
loop cr
;
: 3x3m* { MAddr1 MAddr2 MAddr3 -- }
Rows 0
do
Rows 0
do
0 Rows 0
do MAddr1 I K >ColRow @ MAddr2 J I >ColRow @ * +
loop MAddr3 I J >ColRow !
loop
loop
;
cr .( Two 3x3 matrices containing:)
3x3Matrix1 .3x3M
3x3Matrix2 .3x3M
cr .( Then Multiply them and store the result in matrix3 )
3x3Matrix1 3x3Matrix2 3x3Matrix3 3x3m*
3x3Matrix3 .3x3M \ Show the result
\s Jos
\ Output:
Two 3x3 matrices containing:
1 2 3
-1 -2 -3
5 6 7
10 20 30
40 50 60
70 80 90
Then Multiply them and store the result in matrix3
300 360 420
-300 -360 -420
780 960 1140
ok