Getting Started with dnAnalytics and F#

F# is a functional oriented language for the .NET platform. Although the project started at a Microsoft Research lab, it is currently under development to be included in the next version of Visual Studio.

Since F# is just one of the languages for the .NET platform, dnAnalytics can be used just as one would use it from C#: in other words, all the information on this page is relevant in the context of F# too.

Nevertheless, there is a custom functional F# front-end for dnAnalytics which we describe how to use below. The library is in dnAnalytics.FSharp.dll which one will find in the release. (Currently, you will have to build this dll by downloading the sources.)

Creating vectors and matrices

After you reference the dll in your project, you can use the following code:
open dnAnalytics.LinearAlgebra

This will not only open the dnAnalytics.LinearAlgebra namespace but also open an F# module with the same name. At this point it is fairly straightforward to create vectors and matrices: first you choose wether you want to construct a dense or a sparse vector, you call the appropriate function. Since most useful function (other than initialization) work on the Vector type, it is advised to downcast your reference to the appropriate type. E.g. I want to create a 5 dimensional vector with the squares as the entries
let v = DenseVector.init 5 (fun i -> (double i)**2.0) :> Vector

Modifying vectors and matrices

Pure functional programming suggests not modifying your datastructures while imperative programming is oblivious about this. Since F# is functionally oriented (but also supports imperative style code) you often have two options for manipulating vectors and matrices: a) a function that changes state, b) a function which doesn't change state. E.g. I want to map a vector by squaring all the entries; the state changing version
let v = DenseVector.init 5 (fun i -> (double i)**2.0) :> Vector
let w = Vector.inplace_map (fun x -> x*x) v

the functional version would look like
let v = DenseVector.init 5 (fun i -> (double i)**2.0) :> Vector
let w = (fun x -> x*x) v

Last edited Jul 3, 2008 at 8:01 AM by jvangael, version 2


No comments yet.