Search code examples
f#mathematical-optimizationmath.net

Vector<double> vs. Double.Vector error in F# when using BfgsSolver


I'm trying to use the BfgsSolve method from MathNet.Numerics for F# on a simple test case I wrote, but I'm running into a type mismatching issue.

Here's my code:

#r "nuget: MathNet.Numerics, 5.0.0-alpha08"
#r "nuget: MathNet.Numerics.FSharp, 5.0.0-alpha08"
open MathNet.Numerics.Optimization 
open MathNet.Numerics.LinearAlgebra


let vecGuess = vector [double 1.0]

let sineFuncTest (x : Vector<float>) = 
    let xval = x.[0]
    System.Math.Sin (xval ** 2.0  - 1.0 )

let sinegrad (x : Vector<float>) = 
    let xval = x.[0]
    vector [2.0 * xval * (System.Math.Cos (xval ** 2.0 - 1.0))]

let result = BfgsSolver.Solve (vecGuess, sineFuncTest ,sinegrad)

However the BfgsSolve function won't accept my vecGuess variable. The error I get is: error FS0001: The type 'Vector<double>' is not compatible with the type 'Double.Vector'.

What's the difference between the two? I've tried and failed to create a Double.Vector type.

Thanks.


Solution

  • It will compile and run if you create your guess like this:

    open MathNet.Numerics.LinearAlgebra.Double
    
    let vecGuess = DenseVector [|1.0|]
    

    I don't know if the answer is correct, though. See this SO question for a similar example.