0

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.

johnymm
  • 131
  • 6

1 Answers1

1

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.

Brian Berns
  • 15,499
  • 2
  • 30
  • 40
  • 1
    Thanks. That example compiles, though for some reason the function `BfgsSolver.Solve` produces the wrong answer. I was able to get a correct answer by following [these csharp examples](https://www.csharpcodi.com/csharp-examples/MathNet.Numerics.Optimization.BfgsMinimizer.FindMinimum(MathNet.Numerics.Optimization.IObjectiveFunction,%20MathNet.Numerics.LinearAlgebra.Vector)/) and converting to FSharp. I'm not sure what the `Solve` function is supposed to do or if I'm using it wrong. The documentation for this package is awful, lol. – johnymm Feb 07 '22 at 06:38