0
我發現這個neat trick計算的平均值和標準差在一遍數據。我想要這個工作float32
和float
。 Again,我很努力地用通用數字來解決這個問題。如何統一這兩個浮點函數?
module Seq =
let inline private avgVarianceReducer toFloat (count, oldM, oldS) x =
if count = 1 then
2, x, LanguagePrimitives.GenericZero
else
let meanFree = x - oldM
let newM = oldM + meanFree/(toFloat count)
count + 1, newM, oldS + meanFree * (x - newM)
let inline private avgVarianceWith toFloat source =
match source |> Seq.fold (avgVarianceReducer toFloat) (1, LanguagePrimitives.GenericZero, LanguagePrimitives.GenericZero) with
| 0, _, _ -> LanguagePrimitives.GenericZero, LanguagePrimitives.GenericZero
| 1, mean, _ -> mean, LanguagePrimitives.GenericZero
| n, mean, var -> mean, var/(n - 2 |> toFloat)
let avgVariance source = source |> avgVarianceWith float
let avgVariancef source = source |> avgVarianceWith float32
這適用於這兩種類型,但我有額外的加avgVariancef
我不得不選擇打電話的時候是正確的。
對我來說,核心問題是轉換到avgVarianceReducer
一種權利浮動的,我通過傳遞正確的轉換funtion解決。我嘗試了op_Explicit
,但失敗了。
人有一個更優雅的解決方案的想法?