math.extras: adding ) and ] versions of linspace.

db4
John Benediktsson 2013-03-20 15:09:36 -07:00
parent 5291eec130
commit 5e41a50870
3 changed files with 35 additions and 9 deletions

View File

@ -58,10 +58,18 @@ HELP: sinc
{ $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\"." } { $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." } ; { $notes { $snippet "0 sinc" } " is the limit value of 1." } ;
HELP: linspace HELP: linspace)
{ $values { "from" number } { "to" number } { "points" number } { "seq" sequence } }
{ $description "Return evenly spaced numbers over a specified interval " { $snippet "[from,to)" } "." } ;
HELP: linspace]
{ $values { "from" number } { "to" number } { "points" number } { "seq" sequence } } { $values { "from" number } { "to" number } { "points" number } { "seq" sequence } }
{ $description "Return evenly spaced numbers over a specified interval " { $snippet "[from,to]" } "." } ; { $description "Return evenly spaced numbers over a specified interval " { $snippet "[from,to]" } "." } ;
HELP: logspace HELP: logspace)
{ $values { "from" number } { "to" number } { "points" number } { "base" number } { "seq" sequence } }
{ $description "Return evenly spaced numbers on a log scaled interval " { $snippet "[base^from,base^to)" } "." } ;
HELP: logspace]
{ $values { "from" number } { "to" number } { "points" number } { "base" number } { "seq" sequence } } { $values { "from" number } { "to" number } { "points" number } { "base" number } { "seq" sequence } }
{ $description "Return evenly spaced numbers on a log scaled interval " { $snippet "[base^from,base^to]" } "." } ; { $description "Return evenly spaced numbers on a log scaled interval " { $snippet "[base^from,base^to]" } "." } ;

View File

@ -78,4 +78,5 @@ IN: math.extras.test
{ 0 1 0 0 2 3 } { 0 1 0 0 2 3 }
} [ { 1 2 1 1 3 4 } unique-indices ] unit-test } [ { 1 2 1 1 3 4 } unique-indices ] unit-test
{ { 1 10+3/4 20+1/2 30+1/4 40 } } [ 1 40 5 linspace >array ] unit-test { { 1 8+4/5 16+3/5 24+2/5 32+1/5 } } [ 1 40 5 linspace) >array ] unit-test
{ { 1 10+3/4 20+1/2 30+1/4 40 } } [ 1 40 5 linspace] >array ] unit-test

View File

@ -4,8 +4,8 @@
USING: arrays assocs assocs.extras combinators.short-circuit fry USING: arrays assocs assocs.extras combinators.short-circuit fry
grouping kernel locals math math.combinatorics math.constants grouping kernel locals math math.combinatorics math.constants
math.functions math.order math.primes math.ranges math.functions math.order math.primes math.ranges
math.statistics math.vectors memoize random sequences math.ranges.private math.statistics math.vectors memoize
sequences.extras sets sorting ; random sequences sequences.extras sets sorting ;
IN: math.extras IN: math.extras
@ -199,8 +199,25 @@ PRIVATE>
: unique-indices ( seq -- unique indices ) : unique-indices ( seq -- unique indices )
[ members ] keep over dup length iota H{ } zip-as '[ _ at ] map ; [ members ] keep over dup length iota H{ } zip-as '[ _ at ] map ;
: linspace ( from to points -- seq ) <PRIVATE
1 - [ 2dup swap - ] dip / <range> ;
: logspace ( from to points base -- seq ) : steps ( from to point -- from to step )
[ linspace ] dip swap n^v ; [ 2dup swap - ] dip / ; inline
PRIVATE>
: linspace) ( from to points -- seq )
steps ,b) <range> ;
: linspace] ( from to points -- seq )
{
{ [ dup 1 < ] [ 3drop { } ] }
{ [ dup 1 = ] [ 2drop 1array ] }
[ steps <range> ]
} cond ;
: logspace) ( from to points base -- seq )
[ linspace) ] dip swap n^v ;
: logspace] ( from to points base -- seq )
[ linspace] ] dip swap n^v ;