From e9ffd2da37d2dedff67a19afd96c8537a511bcbe Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Thu, 12 Apr 2012 10:30:16 -0700 Subject: [PATCH] math.statistics: adding cumulative versions of sum, product, min, and max. --- basis/math/statistics/statistics-docs.factor | 40 +++++++++++++++++++ basis/math/statistics/statistics-tests.factor | 5 +++ basis/math/statistics/statistics.factor | 12 ++++++ 3 files changed, 57 insertions(+) diff --git a/basis/math/statistics/statistics-docs.factor b/basis/math/statistics/statistics-docs.factor index a86709c7be..129125d7c3 100644 --- a/basis/math/statistics/statistics-docs.factor +++ b/basis/math/statistics/statistics-docs.factor @@ -174,6 +174,46 @@ HELP: sequence>hashtable } } ; +HELP: cum-sum +{ $values { "seq" sequence } { "seq'" sequence } } +{ $description "Returns the cumulative sum of " { $snippet "seq" } "." } +{ $examples + { $example "USING: math.statistics prettyprint ;" + "{ 1 -1 2 -1 4 } cum-sum ." + "{ 1 0 2 1 5 }" + } +} ; + +HELP: cum-product +{ $values { "seq" sequence } { "seq'" sequence } } +{ $description "Returns the cumulative product of " { $snippet "seq" } "." } +{ $examples + { $example "USING: math.statistics prettyprint ;" + "{ 1 2 3 4 } cum-product ." + "{ 1 2 6 24 }" + } +} ; + +HELP: cum-min +{ $values { "seq" sequence } { "seq'" sequence } } +{ $description "Returns the cumulative min of " { $snippet "seq" } "." } +{ $examples + { $example "USING: math.statistics prettyprint ;" + "{ 5 3 4 1 } cum-min ." + "{ 5 3 3 1 }" + } +} ; + +HELP: cum-max +{ $values { "seq" sequence } { "seq'" sequence } } +{ $description "Returns the cumulative max of " { $snippet "seq" } "." } +{ $examples + { $example "USING: math.statistics prettyprint ;" + "{ 1 -1 3 5 } cum-max ." + "{ 1 1 3 5 }" + } +} ; + ARTICLE: "histogram" "Computing histograms" "Counting elements in a sequence:" { $subsections diff --git a/basis/math/statistics/statistics-tests.factor b/basis/math/statistics/statistics-tests.factor index 5b7606eab6..67323f0a41 100644 --- a/basis/math/statistics/statistics-tests.factor +++ b/basis/math/statistics/statistics-tests.factor @@ -71,3 +71,8 @@ IN: math.statistics.tests [ 1.0 ] [ { 1 2 3 } { 1 2 3 } corr ] unit-test [ -1.0 ] [ { 1 2 3 } { -4 -5 -6 } corr ] unit-test + +[ { 1 2 4 7 } ] [ { 1 1 2 3 } cum-sum ] unit-test +[ { 1 1 2 6 } ] [ { 1 1 2 3 } cum-product ] unit-test +[ { 5 3 3 1 } ] [ { 5 3 4 1 } cum-min ] unit-test +[ { 1 3 3 5 } ] [ { 1 3 1 5 } cum-max ] unit-test diff --git a/basis/math/statistics/statistics.factor b/basis/math/statistics/statistics.factor index f14a2c3680..d0f203f5c7 100644 --- a/basis/math/statistics/statistics.factor +++ b/basis/math/statistics/statistics.factor @@ -156,3 +156,15 @@ ERROR: empty-sequence ; : corr ( {x} {y} -- corr ) [ cov ] [ [ var ] bi@ * sqrt ] 2bi / ; + +: cum-sum ( seq -- seq' ) + 0 swap [ + dup ] map nip ; + +: cum-product ( seq -- seq' ) + 1 swap [ * dup ] map nip ; + +: cum-min ( seq -- seq' ) + [ ?first ] keep [ min dup ] map nip ; + +: cum-max ( seq -- seq' ) + [ ?first ] keep [ max dup ] map nip ;