Converting an F# Function with multiple parameters to a Func Type - MathNet.Numerics

Viewed 644

In this question: In F# how can I produce an expression with a type of Func<obj>? it is shown that a single valued lambda expression is automatically cast/converted to a Func type and then accepted in the function.

I'm working with the MathNet.Numerics library and can confirm this by integrating x^2 between 0 and 10:

#r "../packages/MathNet.Numerics.3.20.0/lib/net40/MathNet.Numerics.dll"
#r "../packages/MathNet.Numerics.FSharp.3.20.0/lib/net40/MathNet.Numerics.FSharp.dll"

#load "Library1.fs"
open Library3

// Define your library scripting code here

open MathNet.Numerics.Integration

let integral = DoubleExponentialTransformation.Integrate((fun x -> x**2.0), 0.0, 10.0, 1.0)

val answer : float = 333.3333333

However, I can't get this to work with multiple valued functions. When I try this I get a type error. Does anyone know a work around for this?

open MathNet.Numerics.Optimization
open MathNet.Numerics.LinearAlgebra.Double

let newAnswer = BfgsSolver.Solve(DenseVector[|1.0, 1.0|], 
                                 (fun x y -> (x + y - 5.0) ** 2.0 + (y - x*x - 4.0) ** 2.0), 
                                 (fun x y -> DenseVector[| 2.0 * (x + y - 5.0) - 4.0 * x * (y - x*x - 4); 
                                                           2.0 * (x + y - 5.0) + 2.0 * (y - x*x - 4.0)   |])
                                                           )

and I get the following error...

Script.fsx(20,34): error FS0193: Type constraint mismatch. The type 
    ''a -> 'b -> 'c'    
is not compatible with type
    'System.Func<MathNet.Numerics.LinearAlgebra.Vector<float>,float>'   
1 Answers
Related