aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Odersky <jodersky@gmail.com>2012-10-28 11:13:50 +0100
committerJakob Odersky <jodersky@gmail.com>2012-10-28 11:13:50 +0100
commit9349244b45532ea0184e3009aa2335ced8e3f7f4 (patch)
tree0b251c06d28b477fbd6cc2db81bb9062a2d88d09
parent9892e907e8db3204a890fc530ddc9e34cdaf096b (diff)
downloadscalam-9349244b45532ea0184e3009aa2335ced8e3f7f4.tar.gz
scalam-9349244b45532ea0184e3009aa2335ced8e3f7f4.tar.bz2
scalam-9349244b45532ea0184e3009aa2335ced8e3f7f4.zip
implement generic 'smooth' function for all sequence types
-rw-r--r--src/main/scala/scalam/math/package.scala24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/main/scala/scalam/math/package.scala b/src/main/scala/scalam/math/package.scala
new file mode 100644
index 0000000..58063fa
--- /dev/null
+++ b/src/main/scala/scalam/math/package.scala
@@ -0,0 +1,24 @@
+package scalam
+
+import scala.collection.SeqLike
+import scala.collection.generic.CanBuildFrom
+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] = {
+ def average(xs: Seq[Elem]): Elem = {
+ import fractional._
+ xs.sum / fromInt(xs.length)
+ }
+
+ def smoothOnce(width: Int, collection: Coll[Elem]) = {
+ val b = cbf(collection)
+ collection.sliding(width).foreach(neighbours => b += average(neighbours))
+ b.result()
+
+ }
+
+ if (passes <= 0) collection
+ else smooth(width, passes - 1, smoothOnce(width, collection))
+ }
+
+} \ No newline at end of file