Matrix multiplication: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: added/changed comments and whitespace, changed a subroutine name.)
(Revise to be more idiomatic and not use local)
Line 4,329: Line 4,329:


=={{header|Standard ML}}==
=={{header|Standard ML}}==
<lang sml>structure Matrix = struct
<lang sml>structure IMatrix = 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)))
tabulate ColMajor (nRows x, nCols y, fn (i,j) => dot(row(x,i),column(y,j)))
end
end;
(* for display *)
(* for display *)
fun toList a =
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 *)
(* example *)
let
let open Matrix
open IMatrix
val m1 = fromList [[1,2],[3,4]]
val m2 = fromList [[~3,~8,3],[~2,1,4]]
val m1 = Array2.fromList [[1,2],[3,4]]
val m2 = Array2.fromList [[~3,~8,3],[~2,1,4]]
in
in
toList (m1*m2)
toList (m1*m2)
end;</lang>
end;</lang>
'''Output:'''
'''Output:'''