Skip to content

Instantly share code, notes, and snippets.

@ihavenonickname
Last active October 26, 2016 20:45
Show Gist options
  • Save ihavenonickname/772c28e024e3f5bfd68582309b91fb33 to your computer and use it in GitHub Desktop.
Save ihavenonickname/772c28e024e3f5bfd68582309b91fb33 to your computer and use it in GitHub Desktop.
fsharp matrix multiplication
let transposeColumn matrix index =
seq { for row in matrix -> Seq.item index row }
let transposeMatrix matrix =
matrix
|> Seq.mapi (fun index _ -> transposeColumn matrix index)
let multiplyRows row1 row2 =
Seq.zip row1 row2
|> Seq.map (fun (a, b) -> a * b)
|> Seq.reduce (fun acc elem -> acc + elem)
let multiplyMatrices matrix1 matrix2 =
transposeMatrix matrix2
|> Seq.map (fun rowm2 ->
Seq.map (fun rowm1 -> multiplyRows rowm1 rowm2) matrix1)
[<EntryPoint>]
let main argv =
let m1 =
Seq.ofList [
Seq.ofList [ 1; 2; 3 ]
]
let m2 =
Seq.ofList [
Seq.ofList [ 2; 1; 3 ]
Seq.ofList [ 3; 3; 2 ]
Seq.ofList [ 4; 1; 2 ]
]
printfn "alterada: %A" (multiplyMatrices m1 m2)
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment