This is a relatively long post. F # has a matrix and vector type (not in the kernel in PowerPack). Fine! Even the numerical computational power of Python belongs to the third part.
But the functions provided there are limited by matrix and vector arithmetic, therefore, for performing inversions, decompositions, etc. we still need to use another library. Now I use the latest dnAnalytics , which is integrated into the Math.Net project. But the Math.Net project has not had any updates to the public for more than a year, I donβt know if they have a continuation plan.
I made the following shell, this shell uses Matlab-like functions for simple linear algebra. Since I am new to F # and FP, could you give some tips on improving the shell and code? Thanks!
#r @"D:\WORK\tools\dnAnalytics_windows_x86\bin\dnAnalytics.dll" #r @"FSharp.PowerPack.dll" open dnAnalytics.LinearAlgebra open Microsoft.FSharp.Math open dnAnalytics.LinearAlgebra.Decomposition
and test code
(* todo: read matrix market format ref: http://math.nist.gov/MatrixMarket/formats.html *) let readmat (filename:string) = System.IO.File.ReadAllLines(filename) |> Array.map (fun x-> (x |> String.split [' '] |> List.toArray |> Array.map float)) |> matrix let timeit f str= let watch = new System.Diagnostics.Stopwatch() watch.Start() let res = f() watch.Stop() printfn "%s Needed %f ms" str watch.Elapsed.TotalMilliseconds res let test() = let testlu() = for i=1 to 10 do let a,b = lu (randmat 1000 1000) () () let testsvd() = for i=1 to 10 do let u,w,v = svd (randmat 300 300) () () let testdet() = for i=1 to 10 do let d = det (randmat 650 650) () () timeit testlu "lu" timeit testsvd "svd" timeit testdet "det"
I also compared with matlab
t = cputime; for i=1:10, [l,u] = lu(rand(1000,1000)); end; e = cputime-t t = cputime; for i=1:10, [u,w,vt] = svd(rand(300,300)); end; e = cputime-t t = cputime; for i=1:10, d = det(rand(650,650)); end; e = cputime-t
Timings (Duo Core 2.0GH, 2 GB memory, Matlab 2009a)
f#: lu Needed 8875.941700 ms svd Needed 14469.102900 ms det Needed 2820.304600 ms matlab: lu 3.7752 svd 5.7408 det 1.2636
matlab is about twice as fast. This is reasonable since the native R also has similar results .