Code: Select all
function Main()
// [ 2, 2 ] x [ 2, 2 ] = [ 2, 2 ]
local aMatriz1 := { { 1, 2 }, { 3, 4 } }
local aMatriz2 := { { 11, 12 }, { 13, 14 } }
// [ 1, 3 ] x [ 3, 1 ] = [ 2, 1 ]
//local aMatriz1 := { { 1, -2, 3 } }
//local aMatriz2 := { { 4 }, { 5 }, { 6 } }
// [ 2, 3 ] x [ 3, 1 ] = [ 2, 1 ]
//local aMatriz1 := { { 1, -2, 3 }, { 1, 0, -1 } }
//local aMatriz2 := { { 4 }, { 5 }, { 6 } }
// [ 1, 3 ] x [ 3, 2 ] = [ 1, 2 ]
//local aMatriz1 := { { 1, -2, 3 } }
//local aMatriz2 := { { 4, 2 }, { 5, 0 }, { 6, 0 } }
// [ 1, 2 ] x [ 2, 2 ] = [ 1, 2 ]
//local aMatriz1 := { { 1, 5 } }
//local aMatriz2 := { { 1, 2 }, { -2, 0 } }
// [ 2, 2 ] x [ 2, 3 ] = [ 2, 3 ]
//local aMatriz1 := { { 1, 2 }, { -2, 0 } }
//local aMatriz2 := { { 1, 0, 2 }, { 0, 2, 0 } }
// [ 1, 2 ] x [ 2, 1 ] = [ 1, 1 ]
//local aMatriz1 := { { 1, -1 } }
//local aMatriz2 := { { 5 }, { 5 } }
// [ 3, 2 ] x [ 2, 3 ] = [ 3, 3 ]
//local aMatriz1 := { { 1, 2 }, { 0, -1 }, { 0, 1 } }
//local aMatriz2 := { { 0, 3, 0 }, { 3, 0, 3 } }
// [ 3, 3 ] x [ 3, 1 ] = [ 3, 1 ]
//local aMatriz1 := { { 1, 0, 2 }, { 0, 2, 0 }, { 0, 1, 3 } }
//local aMatriz2 := { { 5 }, { -1 }, { 0 } }
// [ 3, 3 ] x [ 3, 2 ] = [ 3, 2 ]
//local aMatriz1 := { { 1, 2, 3 }, { 0, 1, 0 }, { 3, 2, 1 } }
//local aMatriz2 := { { 1, -1 }, { 0, 2 }, { -2, 0 } }
// [ 3, 3 ] x [ 3, 3 ] = [ 3, 3 ]
//local aMatriz1 := { { 1, -1, 1 }, { 2, 2, 3 }, { -2, -3, -1 } }
//local aMatriz2 := { { 1, 0, 4 }, { 0, 2, 5 }, { 1, 3, 0 } }
// [ 2, 3 ] x [ 3, 2 ] = [ 2, 2 ]
//local aMatriz1 := { { 1, 2, 6 }, { 3, 4, 9 } }
//local aMatriz2 := { { 11, 12 }, { 13, 14 }, { 6, 9 } }
if Len( aMatriz1[ 1 ] ) = Len( aMatriz2 )
MatrixMult( aMatriz1, aMatriz2 )
else
? "Only multiply matrix if number columns first matrix equal to number rows second matrix"
endif
return nil
function MatrixMult( aMatrix1, aMatrix2 )
local x
local y
local z
local nRowsMatrix1 := Len( aMatrix1 )
local nColsMatrix1 := Len( aMatrix1[ 1 ] )
local nRowsMatrix2 := Len( aMatrix2 )
local nColsMatrix2 := Len( aMatrix2[ 1 ] )
local aResult := Array( nRowsMatrix1, nColsMatrix2 )
local nSum := 0
local nVal := 0
local nIter := 0
local aRowMatrix1
local aRowMatrix2
local nValRowMatrix1
// Show Matrix1, Matrix2
?
For x = 1 to Len( aMatrix1 )
?? "[ "
For y = 1 to Len( aMatrix1[ x ] )
?? aMatrix1[ x ][ y ]
?? " "
Next y
?? " ]"
?
Next x
?
For x = 1 to Len( aMatrix2 )
?? "[ "
For y = 1 to Len( aMatrix2[ x ] )
?? aMatrix2[ x ][ y ]
?? " "
Next y
?? " ]"
?
Next x
aMatrix2 = ArrTranspose( aMatrix2, .F. )
? "Transpose Matrix2"
?
For x = 1 to Len( aMatrix2 )
?? "[ "
For y = 1 to Len( aMatrix2[ x ] )
?? aMatrix2[ x ][ y ]
?? " "
Next y
?? " ]"
?
Next x
// Multiply matrix
For x = 1 to Len( aMatrix1 )
nSum := 0
aRowMatrix1 := aMatrix1[ x ]
For y = 1 to nColsMatrix2
aRowMatrix2 := aMatrix2[ y ]
nSum := 0
For z = 1 to Len( aRowMatrix2 )
nVal := aRowMatrix1[ z ] * aRowMatrix2[ z ]
nSum += nVal
Next z
aResult[ x ][ y ] := nSum
Next y
Next x
// Show Result
? "Result"
?
For x = 1 to Len( aResult )
?? "[ "
For y = 1 to Len( aResult[ x ] )
?? aResult[ x ][ y ]
?? " "
Next y
?? " ]"
?
Next x
return aResult
function ArrTranspose( aArray, lSquare )
local nRows, nCols, nRow, nCol, nWidth
local aNew
// DEFAULT lSquare := .f.
nRows := Len( aArray )
if lSquare
nCols := Len( aArray[ 1 ] )
else
nCols := 1
for nRow := 1 to nRows
if ValType( aArray[ nRow ] ) == 'A'
nCols := Max( nCols, Len( aArray[ nRow ] ) )
endif
next
endif
aNew := Array( nCols, nRows )
for nRow := 1 to nRows
if ValType( aArray[ nRow ] ) == 'A'
nWidth := Len( aArray[ nRow ] )
for nCol := 1 to nWidth
aNew[ nCol, nRow ] := aArray[ nRow, nCol ]
next
else
aNew[ 1, nRow ] := aArray[ nRow ]
endif
next
return aNew