[英]What is the equivalend MathNet.Numerics of Excel TINV function?
[英]Converting an F# Function with multiple parameters to a Func Type - MathNet.Numerics
在這個問題中: 在F#中,如何生成類型為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>'
您可以使用System.Func<_,_,_>()
來轉換這樣的函數:
let newAnswer = BfgsSolver.Solve(DenseVector[|1.0, 1.0|],
(System.Func<_,_,_>(fun x y -> (x + y - 5.0) ** 2.0 + (y - x*x - 4.0) ** 2.0)),
(System.Func<_,_,_>(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) |])))
如果你發現自己經常需要這個,你可以用幫助器使代碼變得不那么難看:
let f2 f = System.Func<_,_,_> f
UPDATE
看看這個方法的Math.NET文檔,我現在看到它實際上只需要一個輸入。 也許您對Func<A, B>
類型簽名感到困惑,但在這種情況下, A
是輸入類型, B
是輸出類型。
具有一個輸入的F#函數將自動轉換為Func<_,_>
。 我下載了Math.NET,這個非常小的例子沒有給出編譯錯誤:
open MathNet.Numerics.Optimization
open MathNet.Numerics.LinearAlgebra.Double
BfgsSolver.Solve(DenseVector [||], (fun x -> x.[0]), (fun x -> x))
這表明問題不在於在函數類型之間進行轉換,而在於使用具有錯誤arity的函數。 我應該從你原來的錯誤信息中看到這個!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.