math.extras: more maths functions.

db4
John Benediktsson 2012-07-27 12:44:51 -07:00
parent a7fff4f192
commit 01abb351b5
3 changed files with 81 additions and 2 deletions

View File

@ -21,3 +21,35 @@ HELP: chi2P
{ $values { "chi" real } { "df" real } { "p" real } }
{ $description "Returns the inverse chi-squared value according to " { $snippet "P(chi|df) = P(df/2,chi/2)" } "." } ;
HELP: bartlett
{ $values { "n" integer } { "seq" sequence } }
{ $description "Return the Bartlett window." } ;
HELP: hanning
{ $values { "n" integer } { "seq" sequence } }
{ $description "Return the Hanning window." } ;
HELP: hamming
{ $values { "n" integer } { "seq" sequence } }
{ $description "Return the Hamming window." } ;
HELP: blackman
{ $values { "n" integer } { "seq" sequence } }
{ $description "Return the Blackman window." } ;
HELP: nan-sum
{ $values { "seq" sequence } { "n" number } }
{ $description "Return the " { $link sum } " of " { $snippet "seq" } " treating any NaNs as zero." } ;
HELP: nan-min
{ $values { "seq" sequence } { "n" number } }
{ $description "Return the " { $link infimum } " of " { $snippet "seq" } " ignoring any NaNs." } ;
HELP: nan-max
{ $values { "seq" sequence } { "n" number } }
{ $description "Return the " { $link supremum } " of " { $snippet "seq" } " ignoring any NaNs." } ;
HELP: sinc
{ $values { "x" number } { "y" number } }
{ $description "Returns the " { $link sinc } " function, calculated according to " { $snippet "sin(pi * x) / (pi * x)" } ". The name " { $link sinc } " is short for \"sine cardinal\" or \"sinus cardinalis\"." }
{ $notes { $snippet "0 sinc" } " is the limit value of 1." } ;

View File

@ -23,3 +23,11 @@ IN: math.extras.test
{ { } } [ { 0 0 } nonzero ] unit-test
{ { 1 2 3 } } [ { 0 1 0 2 0 3 0 } nonzero ] unit-test
{ { } } [ 0 bartlett ] unit-test
{ { 1 } } [ 1 bartlett ] unit-test
{ { 0 0 } } [ 2 bartlett ] unit-test
{ { 0 1 0 } } [ 3 bartlett ] unit-test
{ { 0 2/3 2/3 0 } } [ 4 bartlett ] unit-test
{ { 0 1/2 1 1/2 0 } } [ 5 bartlett ] unit-test
{ { 0 2/5 4/5 4/5 2/5 0 } } [ 6 bartlett ] unit-test

View File

@ -2,8 +2,9 @@
! See http://factorcode.org/license.txt for BSD license
USING: combinators.short-circuit grouping kernel math
math.combinatorics math.functions math.order math.primes
math.ranges math.statistics math.vectors memoize sequences ;
math.combinatorics math.constants math.functions math.order
math.primes math.ranges math.statistics math.vectors memoize
sequences ;
IN: math.extras
@ -97,3 +98,41 @@ PRIVATE>
: nonzero ( seq -- seq' )
[ zero? not ] filter ;
: bartlett ( n -- seq )
dup 1 <= [ 1 = { 1 } { } ? ] [
[ iota ] [ 1 - 2 / ] bi [
[ recip * ] [ >= ] 2bi [ 2 swap - ] when
] curry map
] if ;
: hanning ( n -- seq )
dup 1 <= [ 1 = { 1 } { } ? ] [
[ iota ] [ 1 - 2pi swap / ] bi v*n
[ cos -0.5 * 0.5 + ] map!
] if ;
: hamming ( n -- seq )
dup 1 <= [ 1 = { 1 } { } ? ] [
[ iota ] [ 1 - 2pi swap / ] bi v*n
[ cos -0.46 * 0.54 + ] map!
] if ;
: blackman ( n -- seq )
dup 1 <= [ 1 = { 1 } { } ? ] [
[ iota ] [ 1 - 2pi swap / ] bi v*n
[ [ cos -0.5 * ] map ] [ [ 2 * cos 0.08 * ] map ] bi
v+ 0.42 v+n
] if ;
: nan-sum ( seq -- n )
0 [ dup fp-nan? [ drop ] [ + ] if ] binary-reduce ;
: nan-min ( seq -- n )
[ fp-nan? not ] filter infimum ;
: nan-max ( seq -- n )
[ fp-nan? not ] filter supremum ;
: sinc ( x -- y )
[ 1 ] [ pi * [ sin ] [ / ] bi ] if-zero ;