5

The following calculation runs much slower than it does in R:

let result = (X.Transpose() * b * X).Inverse() * (X.Transpose() * b) * e // about 4 seconds on my machine

or this:

let result = (X.Transpose() * b * X).QR().Solve((X.Transpose() * b) * e)

where

open MathNet.Numerics.LinearAlgebra

let X = DenseMatrix.init 2000 7 (fun i j -> 1.)
let b = SparseMatrix.ofDiag (vector [for i in 0 .. 1999 do yield 1.])
let e = DenseMatrix.init 2000 1 (fun i j -> 1.)

In R however, the same calculation can be achieved via the following which only takes a couple hundred milliseconds:

result <- solve(crossprod(X,b*X), crossprod(X,b*e)) // less than 200ms

Is there a way I can speed up the F# calculation?

[EDIT] Btw, SparseMatrix seems to be the bottleneck. When a DenseMatrix is used instead, there seems to be a massive speedup.

0 Answers0