From 2228d91e7b7c9e258e669bad03a8c9919c850309 Mon Sep 17 00:00:00 2001 From: Jakob Odersky Date: Thu, 1 Nov 2012 13:15:03 +0100 Subject: documentation --- src/main/scala/scalam/math/package.scala | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/main/scala/scalam/math/package.scala b/src/main/scala/scalam/math/package.scala index 4121b8c..44e6876 100644 --- a/src/main/scala/scalam/math/package.scala +++ b/src/main/scala/scalam/math/package.scala @@ -1,10 +1,22 @@ package scalam -import scala.collection.SeqLike import scala.collection.generic.CanBuildFrom + +/** Contains useful math functions. */ package object math { - def smooth[Elem, Coll[Elem]](width: Int, passes: Int, collection: Coll[Elem])(implicit fractional: Fractional[Elem], cbf: CanBuildFrom[Coll[Elem], Elem, Coll[Elem]], view: Coll[Elem] => Seq[Elem]): Coll[Elem] = { + /** + * Smooths a collection of numeric values using the moving-average algorithm. + * The algorithm takes a window of width `width` that "slides" along the whole collection. + * A new collection is created by taking the average of all points located in each window. + * + * @param collection the collection to smooth + * @param width the window's witdh + * @param passes how many times the collection is smoothed (i.e. how often smooth calls itself recursively) + * @tparam Elem the type of the elements contained in the collection + * @tparam Coll[Elem] the type of the collection to be smoothed + */ + def smooth[Elem, Coll[Elem]](collection: Coll[Elem], width: Int, passes: Int = 1)(implicit fractional: Fractional[Elem], cbf: CanBuildFrom[Coll[Elem], Elem, Coll[Elem]], view: Coll[Elem] => Seq[Elem]): Coll[Elem] = { def average(xs: Seq[Elem]): Elem = { import fractional._ xs.sum / fromInt(xs.length) @@ -14,8 +26,8 @@ package object math { else { val b = cbf(collection) collection.sliding(width).foreach(neighbours => b += average(neighbours)) - smooth(width, passes - 1, b.result()) + smooth(b.result(), width, passes - 1) } } - + } \ No newline at end of file -- cgit v1.2.3