diff --git a/extra/math/compare/authors.txt b/extra/math/compare/authors.txt new file mode 100644 index 0000000000..e091bb8164 --- /dev/null +++ b/extra/math/compare/authors.txt @@ -0,0 +1 @@ +John Benediktsson diff --git a/extra/math/compare/compare-docs.factor b/extra/math/compare/compare-docs.factor new file mode 100644 index 0000000000..eb199cd5fe --- /dev/null +++ b/extra/math/compare/compare-docs.factor @@ -0,0 +1,37 @@ +! Copyright (C) 2008 John Benediktsson +! See http://factorcode.org/license.txt for BSD license + +USING: help.markup help.syntax ; + +IN: math.compare + +HELP: absmin +{ $values { "a" "a number" } { "b" "a number" } { "x" "a number" } } +{ $description + "Returns the smaller absolute number with the original sign." +} ; + +HELP: absmax +{ $values { "a" "a number" } { "b" "a number" } { "x" "a number" } } +{ $description + "Returns the larger absolute number with the original sign." +} ; + +HELP: posmax +{ $values { "a" "a number" } { "b" "a number" } { "x" "a number" } } +{ $description + "Returns the most-positive value, or zero if both are negative." +} ; + +HELP: negmin +{ $values { "a" "a number" } { "b" "a number" } { "x" "a number" } } +{ $description + "Returns the most-negative value, or zero if both are positive." +} ; + +HELP: clamp +{ $values { "a" "a number" } { "value" "a number" } { "b" "a number" } { "x" "a number" } } +{ $description + "Returns the value when between 'a' and 'b', 'a' if <= 'a', or 'b' if >= 'b'." +} ; + diff --git a/extra/math/compare/compare-tests.factor b/extra/math/compare/compare-tests.factor new file mode 100644 index 0000000000..765f34e695 --- /dev/null +++ b/extra/math/compare/compare-tests.factor @@ -0,0 +1,28 @@ +! Copyright (C) 2008 John Benediktsson +! See http://factorcode.org/license.txt for BSD license + +USING: kernel math math.functions math.compare tools.test ; + +IN: math.compare.tests + +[ -1 ] [ -1 5 absmin ] unit-test +[ -1 ] [ -1 -5 absmin ] unit-test + +[ -5 ] [ 1 -5 absmax ] unit-test +[ 5 ] [ 1 5 absmax ] unit-test + +[ 0 ] [ -1 -3 posmax ] unit-test +[ 1 ] [ 1 -3 posmax ] unit-test +[ 3 ] [ -1 3 posmax ] unit-test + +[ 0 ] [ 1 3 negmin ] unit-test +[ -3 ] [ 1 -3 negmin ] unit-test +[ -1 ] [ -1 3 negmin ] unit-test + +[ 0 ] [ 0 -1 2 clamp ] unit-test +[ 1 ] [ 0 1 2 clamp ] unit-test +[ 2 ] [ 0 3 2 clamp ] unit-test + + + + diff --git a/extra/math/compare/compare.factor b/extra/math/compare/compare.factor new file mode 100644 index 0000000000..28a8eadc81 --- /dev/null +++ b/extra/math/compare/compare.factor @@ -0,0 +1,22 @@ +! Copyright (C) 2008 John Benediktsson +! See http://factorcode.org/license.txt for BSD license + +USING: math math.order kernel ; + +IN: math.compare + +: absmin ( a b -- x ) + [ [ abs ] bi@ < ] 2keep ? ; + +: absmax ( a b -- x ) + [ [ abs ] bi@ > ] 2keep ? ; + +: posmax ( a b -- x ) + 0 max max ; + +: negmin ( a b -- x ) + 0 min min ; + +: clamp ( a value b -- x ) + min max ; + diff --git a/extra/math/compare/summary.txt b/extra/math/compare/summary.txt new file mode 100644 index 0000000000..95edea509b --- /dev/null +++ b/extra/math/compare/summary.txt @@ -0,0 +1 @@ +Comparison functions. diff --git a/extra/math/finance/authors.txt b/extra/math/finance/authors.txt new file mode 100644 index 0000000000..e091bb8164 --- /dev/null +++ b/extra/math/finance/authors.txt @@ -0,0 +1 @@ +John Benediktsson diff --git a/extra/math/finance/finance-docs.factor b/extra/math/finance/finance-docs.factor new file mode 100644 index 0000000000..9094a5bff6 --- /dev/null +++ b/extra/math/finance/finance-docs.factor @@ -0,0 +1,38 @@ +! Copyright (C) 2008 John Benediktsson +! See http://factorcode.org/license.txt for BSD license + +USING: help.markup help.syntax ; + +IN: math.finance + +HELP: enumerate +{ $values { "seq" "a sequence" } { "newseq" "a sequence" } } +{ $description "Returns a new sequence where each element is an array of { value, index }" } ; + +HELP: sma +{ $values { "seq" "a sequence" } { "n" "number of periods" } { "newseq" "a sequence" } } +{ $description "Returns the Simple Moving Average with the specified periodicity." } ; + +HELP: ema +{ $values { "seq" "a sequence" } { "n" "number of periods" } { "newseq" "a sequence" } } +{ $description + "Returns the Exponential Moving Average with the specified periodicity, calculated by:\n" + { $list + "A = 2.0 / (N + 1)" + "EMA[t] = (A * SEQ[t]) + ((1-A) * EMA[t-1])" } +} ; + +HELP: macd +{ $values { "seq" "a sequence" } { "n1" "short number of periods" } { "n2" "long number of periods" } { "newseq" "a sequence" } } +{ $description + "Returns the Moving Average Converge of the sequence, calculated by:\n" + { $list "MACD[t] = EMA2[t] - EMA1[t]" } +} ; + +HELP: momentum +{ $values { "seq" "a sequence" } { "n" "number of periods" } { "newseq" "a sequence" } } +{ $description + "Returns the Momentum of the sequence, calculated by:\n" + { $list "MOM[t] = SEQ[t] - SEQ[t-n]" } +} ; + diff --git a/extra/math/finance/finance-tests.factor b/extra/math/finance/finance-tests.factor new file mode 100644 index 0000000000..dce701bb2f --- /dev/null +++ b/extra/math/finance/finance-tests.factor @@ -0,0 +1,8 @@ +USING: kernel math math.functions math.finance tools.test ; + +IN: math.finance.tests + +[ { 2 4 } ] [ { 1 3 5 } 2 sma ] unit-test + +[ { 1 3 1 } ] [ { 1 3 2 6 3 } 2 momentum ] unit-test + diff --git a/extra/math/finance/finance.factor b/extra/math/finance/finance.factor new file mode 100644 index 0000000000..8d33c1eb7d --- /dev/null +++ b/extra/math/finance/finance.factor @@ -0,0 +1,37 @@ +! Copyright (C) 2008 John Benediktsson +! See http://factorcode.org/license.txt for BSD license + +USING: arrays assocs kernel grouping sequences shuffle +math math.functions math.statistics math.vectors ; + +IN: math.finance + +: enumerate ( seq -- newseq ) + >alist ; + + + +: ema ( seq n -- newseq ) + a swap first-rest swap [ [ dup ] 2dip swap rot weighted ] accumulate 2nip ; + +: sma ( seq n -- newseq ) + clump [ mean ] map ; + +: macd ( seq n1 n2 -- newseq ) + rot dup ema [ swap ema ] dip v- ; + +: momentum ( seq n -- newseq ) + 2dup tail-slice -rot swap [ length ] keep + [ - neg ] dip swap head-slice v- ; + diff --git a/extra/math/finance/summary.txt b/extra/math/finance/summary.txt new file mode 100644 index 0000000000..9fb3bc8aa7 --- /dev/null +++ b/extra/math/finance/summary.txt @@ -0,0 +1 @@ +Moving averages and other calculations useful for finance. diff --git a/extra/printf/printf-docs.factor b/extra/printf/printf-docs.factor old mode 100755 new mode 100644 index d59320e350..b28b5e1c86 --- a/extra/printf/printf-docs.factor +++ b/extra/printf/printf-docs.factor @@ -1,74 +1,80 @@ - -USING: help.syntax help.markup kernel prettyprint sequences strings ; - -IN: printf - -HELP: printf -{ $values { "format-string" string } } -{ $description "Writes the arguments (specified on the stack) formatted according to the format string." } -{ $examples - { $example - "USING: printf ;" - "123 \"%05d\" printf" - "00123" } - { $example - "USING: printf ;" - "HEX: ff \"%04X\" printf" - "00FF" } - { $example - "USING: printf ;" - "1.23456789 \"%.3f\" printf" - "1.234" } - { $example - "USING: printf ;" - "1234567890 \"%.5e\" printf" - "1.23456e+09" } - { $example - "USING: printf ;" - "12 \"%'#4d\" printf" - "##12" } - { $example - "USING: printf ;" - "1234 \"%+d\" printf" - "+1234" } -} ; - -HELP: sprintf -{ $values { "format-string" string } { "result" string } } -{ $description "Returns the arguments (specified on the stack) formatted according to the format string as a result string." } -{ $see-also printf } ; - -ARTICLE: "printf" "Formatted printing" -"The " { $link printf } " and " { $link sprintf } " words are used for formatted printing.\n" -"\n" -"Several format specifications exist for handling arguments of different types, and specifying attributes for the result string, including such things as maximum width, padding, and decimals.\n" -{ $table - { "%%" "Single %" "" } - { "%P.Ds" "String format" "string" } - { "%P.DS" "String format uppercase" "string" } - { "%c" "Character format" "char" } - { "%C" "Character format uppercase" "char" } - { "%+Pd" "Integer format" "fixnum" } - { "%+P.De" "Scientific notation" "fixnum, float" } - { "%+P.DE" "Scientific notation" "fixnum, float" } - { "%+P.Df" "Fixed format" "fixnum, float" } - { "%+Px" "Hexadecimal" "hex" } - { "%+PX" "Hexadecimal uppercase" "hex" } -} -"\n" -"A plus sign ('+') is used to optionally specify that the number should be formatted with a '+' preceeding it if positive.\n" -"\n" -"Padding ('P') is used to optionally specify the minimum width of the result string, the padding character, and the alignment. By default, the padding character defaults to a space and the alignment defaults to right-aligned. For example:\n" -{ $list - "\"%5s\" formats a string padding with spaces up to 5 characters wide." - "\"%08d\" formats an integer padding with zeros up to 3 characters wide." - "\"%'#5f\" formats a float padding with '#' up to 3 characters wide." - "\"%-10d\" formats an integer to 10 characters wide and left-aligns." -} -"\n" -"Digits ('D') is used to optionally specify the maximum digits in the result string. For example:\n" -{ $list - "\"%.3s\" formats a string to truncate at 3 characters (from the left)." - "\"%.10f\" formats a float to pad-right with zeros up to 10 digits beyond the decimal point." - "\"%.5E\" formats a float into scientific notation with zeros up to 5 digits beyond the decimal point, but before the exponent." -} ; + +USING: help.syntax help.markup kernel prettyprint sequences strings ; + +IN: printf + +HELP: printf +{ $values { "format-string" string } } +{ $description "Writes the arguments (specified on the stack) formatted according to the format string." } +{ $examples + { $example + "USING: printf ;" + "123 \"%05d\" printf" + "00123" } + { $example + "USING: printf ;" + "HEX: ff \"%04X\" printf" + "00FF" } + { $example + "USING: printf ;" + "1.23456789 \"%.3f\" printf" + "1.234" } + { $example + "USING: printf ;" + "1234567890 \"%.5e\" printf" + "1.23456e+09" } + { $example + "USING: printf ;" + "12 \"%'#4d\" printf" + "##12" } + { $example + "USING: printf ;" + "1234 \"%+d\" printf" + "+1234" } +} ; + +HELP: sprintf +{ $values { "format-string" string } { "result" string } } +{ $description "Returns the arguments (specified on the stack) formatted according to the format string as a result string." } +{ $see-also printf } ; + +ARTICLE: "printf" "Formatted printing" +"The " { $vocab-link "printf" } " vocabulary is used for formatted printing.\n" +{ $subsection printf } +{ $subsection sprintf } +"\n" +"Several format specifications exist for handling arguments of different types, and specifying attributes for the result string, including such things as maximum width, padding, and decimals.\n" +{ $table + { "%%" "Single %" "" } + { "%P.Ds" "String format" "string" } + { "%P.DS" "String format uppercase" "string" } + { "%c" "Character format" "char" } + { "%C" "Character format uppercase" "char" } + { "%+Pd" "Integer format" "fixnum" } + { "%+P.De" "Scientific notation" "fixnum, float" } + { "%+P.DE" "Scientific notation" "fixnum, float" } + { "%+P.Df" "Fixed format" "fixnum, float" } + { "%+Px" "Hexadecimal" "hex" } + { "%+PX" "Hexadecimal uppercase" "hex" } +} +"\n" +"A plus sign ('+') is used to optionally specify that the number should be formatted with a '+' preceeding it if positive.\n" +"\n" +"Padding ('P') is used to optionally specify the minimum width of the result string, the padding character, and the alignment. By default, the padding character defaults to a space and the alignment defaults to right-aligned. For example:\n" +{ $list + "\"%5s\" formats a string padding with spaces up to 5 characters wide." + "\"%08d\" formats an integer padding with zeros up to 3 characters wide." + "\"%'#5f\" formats a float padding with '#' up to 3 characters wide." + "\"%-10d\" formats an integer to 10 characters wide and left-aligns." +} +"\n" +"Digits ('D') is used to optionally specify the maximum digits in the result string. For example:\n" +{ $list + "\"%.3s\" formats a string to truncate at 3 characters (from the left)." + "\"%.10f\" formats a float to pad-right with zeros up to 10 digits beyond the decimal point." + "\"%.5E\" formats a float into scientific notation with zeros up to 5 digits beyond the decimal point, but before the exponent." +} ; + +ABOUT: "printf" + + diff --git a/extra/printf/printf-tests.factor b/extra/printf/printf-tests.factor index b365343bf0..5302048dfe 100644 --- a/extra/printf/printf-tests.factor +++ b/extra/printf/printf-tests.factor @@ -7,6 +7,10 @@ USING: kernel printf tools.test ; [ "%s" sprintf ] must-infer +[ t ] [ "" "" sprintf = ] unit-test + +[ t ] [ "asdf" "asdf" sprintf = ] unit-test + [ t ] [ "10" 10 "%d" sprintf = ] unit-test [ t ] [ "+10" 10 "%+d" sprintf = ] unit-test diff --git a/extra/sequences/lib/lib-docs.factor b/extra/sequences/lib/lib-docs.factor index 9975da00db..197d092aa2 100755 --- a/extra/sequences/lib/lib-docs.factor +++ b/extra/sequences/lib/lib-docs.factor @@ -18,3 +18,8 @@ HELP: each-withn "passed to the quotation given to each-withn for each element in the sequence." } { $see-also map-withn } ; + +HELP: randomize +{ $values { "seq" sequence } { "seq'" sequence } } +{ $description "Shuffle the elements in the sequence randomly, returning the new sequence." } ; + diff --git a/extra/sequences/lib/lib.factor b/extra/sequences/lib/lib.factor index 690d7f4b76..a7202c9cae 100755 --- a/extra/sequences/lib/lib.factor +++ b/extra/sequences/lib/lib.factor @@ -160,3 +160,11 @@ PRIVATE> : ?nth* ( n seq -- elt/f ? ) 2dup bounds-check? [ nth-unsafe t ] [ 2drop f f ] if ; flushable + +! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +USE: math.ranges +USE: random +: randomize ( seq -- seq' ) + dup length 1 swap [a,b) [ dup random pick exchange ] each ; +