Jump to content

Matrix multiplication: Difference between revisions

Revise to be more idiomatic and not use local
m (→‎{{header|REXX}}: added/changed comments and whitespace, changed a subroutine name.)
(Revise to be more idiomatic and not use local)
Line 4,329:
 
=={{header|Standard ML}}==
<lang sml>structure MatrixIMatrix = struct
fun dot(x,y) = Vector.foldli (fn (i,xi,agg) => agg+xi*Vector.sub(y,i)) 0 x
local
fun x*y =
open Array2
let
fun dot(x,y) = Vector.foldli (fn (i,xi,agg) => agg+xi*Vector.sub(y,i)) 0 x
open Array2
in
in
val fromList = fromList
fun x*y = tabulate ColMajor (nRows x, nCols y, fn (i,j) => dot(row(x,i),column(y,j)))
end
end;
(* for display *)
fun toList a =
let
List.tabulate(nRows a, fn i => List.tabulate(nCols a, fn j => sub(a,i,j)))
open Array2
end
in
end;
List.tabulate(nRows a, fn i => List.tabulate(nCols a, fn j => sub(a,i,j)))
end;
(* example *)
let
let open Matrix
open IMatrix
val m1 = fromList [[1,2],[3,4]]
val m2m1 = Array2.fromList [[~31,~8,32],[~2,13,4]]
val m1m2 = Array2.fromList [[1~3,2~8,3],[3~2,1,4]]
in
toList (m1*m2)
end;</lang>
'''Output:'''
136

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.