From 6417e0aeccb84995279fd0adb0a11feee575c18b Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Fri, 16 Feb 2024 13:52:28 +0100 Subject: [PATCH] add initial go mixture implementation --- go/squiggle.go | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/go/squiggle.go b/go/squiggle.go index 4f312bec..9d554d1f 100644 --- a/go/squiggle.go +++ b/go/squiggle.go @@ -54,7 +54,31 @@ func sample_mixture(fs []func64, weights []float64) float64 { for _, weight := range weights { sum_weights += weight } - return sum_weights + + var total float64 = 0 + for i, weight := range weights { + total += weight / sum_weights + weights[i] = total + } + + var result float64 + var flag int = 0 + var p float64 = r.Float64() + + for i, weight := range weights { + if p < weight { + result = fs[i]() + flag = 1 + break + } + } + + if flag == 0 { + result = fs[len(fs)-1]() + } + return result + // return weights[0] + } func main() {