Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

math.extras: adding moving-average and exponential-moving-average words.

  • Loading branch information...
commit 503d6d3fc24361c59f09cbf7e413f03b4de0e2d8 1 parent a20adde
@mrjbq7 mrjbq7 authored
View
11 extra/math/extras/extras-tests.factor
@@ -1,7 +1,7 @@
! Copyright (C) 2012 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
-USING: math.extras tools.test ;
+USING: math.extras math.ranges tools.test ;
IN: math.extras.test
@@ -9,3 +9,12 @@ IN: math.extras.test
{ 0 } [ 3 3 jacobi ] unit-test
{ -1 } [ 127 703 jacobi ] unit-test
{ 1 } [ -4 197 jacobi ] unit-test
+
+{ { 2 3 4 5 6 7 8 9 } } [ 10 [1,b] 3 moving-average ] unit-test
+{ { 1+1/2 2+1/2 3+1/2 4+1/2 5+1/2 6+1/2 7+1/2 8+1/2 9+1/2 } }
+[ 10 [1,b] 2 moving-average ] unit-test
+
+{ { 1 1+1/2 2+1/4 3+1/8 4+1/16 5+1/32 } }
+[ 6 [1,b] 1/2 exponential-moving-average ] unit-test
+{ { 1 3 3 5 5 7 7 9 9 11 } }
+[ 10 [1,b] 2 exponential-moving-average ] unit-test
View
12 extra/math/extras/extras.factor
@@ -1,9 +1,9 @@
! Copyright (C) 2012 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
-USING: combinators.short-circuit kernel math math.combinatorics
-math.functions math.order math.primes math.ranges
-math.statistics math.vectors memoize sequences ;
+USING: combinators.short-circuit grouping kernel math
+math.combinatorics math.functions math.order math.primes
+math.ranges math.statistics math.vectors memoize sequences ;
IN: math.extras
@@ -85,3 +85,9 @@ PRIVATE>
: legendere ( a m -- n )
check-legendere jacobi ;
+
+: moving-average ( seq n -- newseq )
+ clump [ mean ] map ;
+
+: exponential-moving-average ( seq a -- newseq )
+ [ 1 ] 2dip [ [ dupd swap - ] dip * + dup ] curry map nip ;
Please sign in to comment.
Something went wrong with that request. Please try again.