Matrix multiplication: Difference between revisions

Content added Content deleted
(→‎{{header|Frink}}: Fixed language tag.)
No edit summary
Line 312: Line 312:
}
}
}</lang>
}</lang>
===Using Objects===
<lang AutoHotkey>Multiply_Matrix(A,B){
if (A[1].MaxIndex() <> B.MaxIndex())
return
RCols := A[1].MaxIndex()>B[1].MaxIndex()?A[1].MaxIndex():B[1].MaxIndex()
RRows := A.MaxIndex()>B.MaxIndex()?A.MaxIndex():B.MaxIndex(), R := []
Loop, % RRows {
RRow:=A_Index
loop, % RCols {
RCol:=A_Index, v := 0
loop % A[1].MaxIndex()
col := A_Index, v += A[RRow, col] * B[col,RCol]
R[RRow,RCol] := v
}
}
return R
}</lang>
Examples:<lang AutoHotkey>A := [[1,2]
, [3,4]
, [5,6]
, [7,8]]

B := [[1,2,3]
, [4,5,6]]

if Res := Multiply_Matrix(A,B)
MsgBox % Print(Res)
else
MsgBox Error
return
Print(M){
for i, row in M
for j, col in row
Res .= (A_Index=1?"":"`t") col (Mod(A_Index,M[1].MaxIndex())?"":"`n")
return Trim(Res,"`n")
}</lang>
Outputs:<pre>9 12 15
19 26 33
29 40 51
39 54 69</pre>



=={{header|BASIC}}==
=={{header|BASIC}}==