Proper F# type annotation for MathNet.Numerics.LinearAlgebra.vector

140 views Asked by At

I have the following program:

open System
open MathNet.Numerics
open MathNet.Numerics.LinearAlgebra

//entropy
let entropy v  =
    let pct = v / v.Sum()
    let l1 = pct.Map (fun x -> System.Math.Log(x, 2.0))
    let p = Vector.map2 (fun x y -> x * y) pct l1
    let e = - p.Sum()
    e

[<EntryPoint>]
let main argv = 

    let v1 = vector [ 1.0 ; 3.0 ; 5.0 ]
    let e1 = entropy v1

    0 // return an integer exit code

I need to provide a type annotation for the varable v in the entropy function. As you can see, the parameter I am passing to the function (v1) is defined as MathNet.Numerics.LinearAlgebra.vector. I have tried lots of options for the type annotation without success.

What should it be? Bonus points if you can help me understand how you came up with your answer.

1

There are 1 answers

0
Vandroiy On BEST ANSWER

This vector type is generic; the generic argument indicates the type of each component of the vector. A type annotation must at least indicate the number of generic arguments, e.g. Vector<_> for any such vector, or Vector<float> for the exact type used in the question.

In other words, Vector<_> and Vector are unrelated types to the compiler. The annotation is supposed to denote the type Vector with one, not zero generic arguments.

I would expect the "rough" annotation (v : Vector<_>) to suffice; the compiler would then infer the generic argument from the use of a float -- the value 2.0 -- later in the function. I don't use the library though, so I didn't test this.