【发布时间】:2018-01-15 18:00:57
【问题描述】:
在这个问题中:In F# how can I produce an expression with a type of Func<obj>? 表明单值 lambda 表达式会自动转换/转换为 Func 类型,然后在函数中接受。
我正在使用 MathNet.Numerics 库,可以通过在 0 到 10 之间积分 x^2 来确认这一点:
#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
但是,我不能让它与多值函数一起使用。当我尝试这个时,我得到一个类型错误。有谁知道解决这个问题的方法?
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) |])
)
我收到以下错误...
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>'
【问题讨论】:
-
尝试
fun (x, y) ->而不是fun x y ->不在电脑上,稍后将转换为答案。 -
@CaringDev 我有点沮丧地发现这不起作用,至少在我使用
IEnumerable.Aggregate的测试中 -
@TheQuickBrownFox 很有可能...没有做太多 F# -> C# 交互,不记得我最后遇到的确切情况。
标签: c# f# mathematical-optimization func mathnet-numerics