今天继续我的 F# 学习,尝试使用递归重新创建一个简单的二分法,这里我使用 MathNet 库从 Beta 发行版继承。
我收到有关“搜索”功能(二进制搜索方法)的错误消息the value is not a function and cannot be applied
。
//Beta class inheriting from MathNet Beta distribution
//Extends the class by implementing an InverseCDF function
type newBeta(alpha:double, beta:double) =
inherit MathNet.Numerics.Distributions.Beta(alpha, beta)
member this.InverseCDF(p: float) =
let rec search (min: float, max: float, acc: uint32) =
let x = (min + max) / 2.0
let error = 0.001
let maxiters : uint32 = 1000u
let cdf = this.CumulativeDistribution(x)
match cdf, (Math.Abs(cdf - p) < error || acc > maxiters) with //while statement
| _ , true -> cdf //auto match cdf and if while statement evaluates true then break and return cdf result
| p , _ -> cdf //if exactly matches p then break and return cdf result
| p , false when p > cdf -> search (min) (x) (acc + 1) //if p > cdf then set max = x and increment then call func with new params
| p , false when p < cdf -> search (x) (max) (acc + 1) //if p < cdf then set min = x and increment then call func with new params
search (0.0) (1.0) (0) //Call the bisection method with initial parameters
任何人都可以帮忙吗?同样显然,任何关于如何使其更具“功能性”的输入都会很酷。由于错误,尚未能够运行此测试。我的前 2 个匹配模式看起来很可疑,因为我试图返回cdf
.