From d0ff06b8c14e3b505d0c14ca5c9c37e8b667c875 Mon Sep 17 00:00:00 2001 From: Josep Sanjuas Date: Sun, 12 Apr 2015 19:40:30 +0200 Subject: [PATCH 1/4] Generalize mean to other types --- lib/pure/math.nim | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/pure/math.nim b/lib/pure/math.nim index aa64933fb..0051c9d88 100644 --- a/lib/pure/math.nim +++ b/lib/pure/math.nim @@ -114,10 +114,13 @@ proc sum*[T](x: openArray[T]): T {.noSideEffect.} = ## If `x` is empty, 0 is returned. for i in items(x): result = result + i -proc mean*(x: openArray[float]): float {.noSideEffect.} = +template toFloat(f: float): float = f + +proc mean*[T](x: openArray[T]): float {.noSideEffect.} = ## computes the mean of the elements in `x`. ## If `x` is empty, NaN is returned. - result = sum(x) / toFloat(len(x)) + ## ``toFloat(x: T): float`` must be defined. + result = toFloat(sum(x)) / toFloat(len(x)) proc variance*(x: openArray[float]): float {.noSideEffect.} = ## computes the variance of the elements in `x`. From e76f54e059fc89bb2cdab0a61700988c56b609d3 Mon Sep 17 00:00:00 2001 From: Josep Sanjuas Date: Sat, 18 Apr 2015 13:28:48 +0200 Subject: [PATCH 2/4] Generalize variance to other types --- lib/pure/math.nim | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/pure/math.nim b/lib/pure/math.nim index 0051c9d88..ef9c69e3c 100644 --- a/lib/pure/math.nim +++ b/lib/pure/math.nim @@ -122,13 +122,13 @@ proc mean*[T](x: openArray[T]): float {.noSideEffect.} = ## ``toFloat(x: T): float`` must be defined. result = toFloat(sum(x)) / toFloat(len(x)) -proc variance*(x: openArray[float]): float {.noSideEffect.} = +proc variance*[T](x: openArray[T]): float {.noSideEffect.} = ## computes the variance of the elements in `x`. ## If `x` is empty, NaN is returned. result = 0.0 var m = mean(x) - for i in 0 .. high(x): - var diff = x[i] - m + for i in items(x): + var diff = toFloat(i) - m result = result + diff*diff result = result / toFloat(len(x)) From 44246b670921aca1d31cb74dcc00139b5ed9a2c2 Mon Sep 17 00:00:00 2001 From: Josep Sanjuas Date: Sat, 18 Apr 2015 13:38:15 +0200 Subject: [PATCH 3/4] Document toFloat is necessary --- lib/pure/math.nim | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/pure/math.nim b/lib/pure/math.nim index ef9c69e3c..05c2cf57c 100644 --- a/lib/pure/math.nim +++ b/lib/pure/math.nim @@ -125,6 +125,7 @@ proc mean*[T](x: openArray[T]): float {.noSideEffect.} = proc variance*[T](x: openArray[T]): float {.noSideEffect.} = ## computes the variance of the elements in `x`. ## If `x` is empty, NaN is returned. + ## ``toFloat(x: T): float`` must be defined. result = 0.0 var m = mean(x) for i in items(x): From f72bb57fff29609336beae56ae148e20d0dd702e Mon Sep 17 00:00:00 2001 From: Josep Sanjuas Date: Sat, 18 Apr 2015 13:40:20 +0200 Subject: [PATCH 4/4] Convert to float before sum --- lib/pure/math.nim | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/pure/math.nim b/lib/pure/math.nim index 05c2cf57c..9bd594549 100644 --- a/lib/pure/math.nim +++ b/lib/pure/math.nim @@ -117,10 +117,11 @@ proc sum*[T](x: openArray[T]): T {.noSideEffect.} = template toFloat(f: float): float = f proc mean*[T](x: openArray[T]): float {.noSideEffect.} = - ## computes the mean of the elements in `x`. + ## computes the mean of the elements in `x`, which are first converted to floats. ## If `x` is empty, NaN is returned. ## ``toFloat(x: T): float`` must be defined. - result = toFloat(sum(x)) / toFloat(len(x)) + for i in items(x): result = result + toFloat(i) + result = result / toFloat(len(x)) proc variance*[T](x: openArray[T]): float {.noSideEffect.} = ## computes the variance of the elements in `x`.