Matrix multiplication: Difference between revisions
Content added Content deleted
(added Icon example) |
(multiply routine in Icon/Unicon without library) |
||
Line 1,167: | Line 1,167: | ||
write ("Result: ") |
write ("Result: ") |
||
write_matrix ("", m3) |
write_matrix ("", m3) |
||
end |
|||
</lang> |
|||
And a hand-crafted multiply procedure: |
|||
<lang icon> |
|||
procedure multiply_matrix (m1, m2) |
|||
result := [] # to hold the final matrix |
|||
every row1 := !m1 do { # loop through each row in the first matrix |
|||
row := [] |
|||
every colIndex := 1 to *m1 do { # and each column index of the result |
|||
value := 0 |
|||
every rowIndex := 1 to *m2 do { |
|||
value +:= row1[rowIndex] * m2[rowIndex][colIndex] |
|||
} |
|||
put (row, value) |
|||
} |
|||
put (result, row) # add each row as it is complete |
|||
} |
|||
return result |
|||
end |
end |
||
</lang> |
</lang> |