Merge branch 'master' of git://factorcode.org/git/factor
commit
f75fdc5744
|
@ -125,9 +125,13 @@ M: node node>quot drop ;
|
||||||
: nodes>quot ( node -- quot )
|
: nodes>quot ( node -- quot )
|
||||||
[ [ node>quot ] each ] [ ] make ;
|
[ [ node>quot ] each ] [ ] make ;
|
||||||
|
|
||||||
: optimized. ( quot/word -- )
|
GENERIC: optimized. ( quot/word -- )
|
||||||
dup word? [ specialized-def ] when
|
|
||||||
build-tree optimize-tree nodes>quot . ;
|
M: method-spec optimized. first2 method optimized. ;
|
||||||
|
|
||||||
|
M: word optimized. specialized-def optimized. ;
|
||||||
|
|
||||||
|
M: callable optimized. build-tree optimize-tree nodes>quot . ;
|
||||||
|
|
||||||
SYMBOL: words-called
|
SYMBOL: words-called
|
||||||
SYMBOL: generics-called
|
SYMBOL: generics-called
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
USE: specialized-arrays.functor
|
USING: io.mmap.functor specialized-arrays.direct.ushort ;
|
||||||
IN: specialized-arrays.ushort
|
IN: io.mmap.ushort
|
||||||
|
|
||||||
<< "ushort" define-array >>
|
<< "ushort" define-mapped-array >>
|
|
@ -2,48 +2,54 @@
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
! mersenne twister based on
|
! mersenne twister based on
|
||||||
! http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/CODES/mt19937ar.c
|
! http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/CODES/mt19937ar.c
|
||||||
USING: kernel math namespaces sequences system init
|
USING: kernel math namespaces sequences sequences.private system
|
||||||
accessors math.ranges random circular math.bitwise
|
init accessors math.ranges random math.bitwise combinators
|
||||||
combinators specialized-arrays.uint ;
|
specialized-arrays.uint fry ;
|
||||||
IN: random.mersenne-twister
|
IN: random.mersenne-twister
|
||||||
|
|
||||||
<PRIVATE
|
<PRIVATE
|
||||||
|
|
||||||
TUPLE: mersenne-twister seq i ;
|
TUPLE: mersenne-twister { seq uint-array } { i fixnum } ;
|
||||||
|
|
||||||
: mt-n 624 ; inline
|
: mt-n 624 ; inline
|
||||||
: mt-m 397 ; inline
|
: mt-m 397 ; inline
|
||||||
: mt-a HEX: 9908b0df ; inline
|
: mt-a HEX: 9908b0df ; inline
|
||||||
|
|
||||||
|
: wrap-nth ( n seq -- obj )
|
||||||
|
[ length mod ] keep nth-unsafe ; inline
|
||||||
|
|
||||||
|
: set-wrap-nth ( obj n seq -- )
|
||||||
|
[ length mod ] keep set-nth-unsafe ; inline
|
||||||
|
|
||||||
: calculate-y ( n seq -- y )
|
: calculate-y ( n seq -- y )
|
||||||
[ nth 31 mask-bit ]
|
[ wrap-nth 31 mask-bit ]
|
||||||
[ [ 1+ ] [ nth ] bi* 31 bits ] 2bi bitor ; inline
|
[ [ 1+ ] [ wrap-nth ] bi* 31 bits ] 2bi bitor ; inline
|
||||||
|
|
||||||
: (mt-generate) ( n seq -- next-mt )
|
: (mt-generate) ( n seq -- next-mt )
|
||||||
[
|
[
|
||||||
calculate-y
|
calculate-y
|
||||||
[ 2/ ] [ odd? mt-a 0 ? ] bi bitxor
|
[ 2/ ] [ odd? mt-a 0 ? ] bi bitxor
|
||||||
] [
|
] [
|
||||||
[ mt-m + ] [ nth ] bi*
|
[ mt-m + ] [ wrap-nth ] bi*
|
||||||
] 2bi bitxor ;
|
] 2bi bitxor ; inline
|
||||||
|
|
||||||
: mt-generate ( mt -- )
|
: mt-generate ( mt -- )
|
||||||
[
|
[
|
||||||
mt-n swap seq>> [
|
mt-n swap seq>> '[
|
||||||
[ (mt-generate) ] [ set-nth ] 2bi
|
_ [ (mt-generate) ] [ set-wrap-nth ] 2bi
|
||||||
] curry each
|
] each
|
||||||
] [ 0 >>i drop ] bi ;
|
] [ 0 >>i drop ] bi ; inline
|
||||||
|
|
||||||
: init-mt-formula ( i seq -- f(seq[i]) )
|
: init-mt-formula ( i seq -- f(seq[i]) )
|
||||||
dupd nth dup -30 shift bitxor 1812433253 * + 1+ 32 bits ;
|
dupd wrap-nth dup -30 shift bitxor 1812433253 * + 1+ 32 bits ; inline
|
||||||
|
|
||||||
: init-mt-rest ( seq -- )
|
: init-mt-rest ( seq -- )
|
||||||
mt-n 1- swap [
|
mt-n 1- swap '[
|
||||||
[ init-mt-formula ] [ [ 1+ ] dip set-nth ] 2bi
|
_ [ init-mt-formula ] [ [ 1+ ] dip set-wrap-nth ] 2bi
|
||||||
] curry each ;
|
] each ; inline
|
||||||
|
|
||||||
: init-mt-seq ( seed -- seq )
|
: init-mt-seq ( seed -- seq )
|
||||||
32 bits mt-n <uint-array> <circular>
|
32 bits mt-n <uint-array>
|
||||||
[ set-first ] [ init-mt-rest ] [ ] tri ;
|
[ set-first ] [ init-mt-rest ] [ ] tri ;
|
||||||
|
|
||||||
: mt-temper ( y -- yt )
|
: mt-temper ( y -- yt )
|
||||||
|
@ -53,7 +59,7 @@ TUPLE: mersenne-twister seq i ;
|
||||||
dup -18 shift bitxor ; inline
|
dup -18 shift bitxor ; inline
|
||||||
|
|
||||||
: next-index ( mt -- i )
|
: next-index ( mt -- i )
|
||||||
dup i>> dup mt-n < [ nip ] [ drop mt-generate 0 ] if ;
|
dup i>> dup mt-n < [ nip ] [ drop mt-generate 0 ] if ; inline
|
||||||
|
|
||||||
PRIVATE>
|
PRIVATE>
|
||||||
|
|
||||||
|
@ -66,7 +72,7 @@ M: mersenne-twister seed-random ( mt seed -- )
|
||||||
|
|
||||||
M: mersenne-twister random-32* ( mt -- r )
|
M: mersenne-twister random-32* ( mt -- r )
|
||||||
[ next-index ]
|
[ next-index ]
|
||||||
[ seq>> nth mt-temper ]
|
[ seq>> wrap-nth mt-temper ]
|
||||||
[ [ 1+ ] change-i drop ] tri ;
|
[ [ 1+ ] change-i drop ] tri ;
|
||||||
|
|
||||||
USE: init
|
USE: init
|
||||||
|
|
|
@ -2,3 +2,69 @@ USE: specialized-arrays.functor
|
||||||
IN: specialized-arrays.double
|
IN: specialized-arrays.double
|
||||||
|
|
||||||
<< "double" define-array >>
|
<< "double" define-array >>
|
||||||
|
|
||||||
|
! Specializer hints. These should really be generalized, and placed
|
||||||
|
! somewhere else
|
||||||
|
USING: hints math.vectors arrays kernel math accessors sequences ;
|
||||||
|
|
||||||
|
HINTS: <double-array> { 2 } { 3 } ;
|
||||||
|
|
||||||
|
HINTS: vneg { array } { double-array } ;
|
||||||
|
HINTS: v*n { array object } { double-array float } ;
|
||||||
|
HINTS: n*v { array object } { float double-array } ;
|
||||||
|
HINTS: v/n { array object } { double-array float } ;
|
||||||
|
HINTS: n/v { object array } { float double-array } ;
|
||||||
|
HINTS: v+ { array array } { double-array double-array } ;
|
||||||
|
HINTS: v- { array array } { double-array double-array } ;
|
||||||
|
HINTS: v* { array array } { double-array double-array } ;
|
||||||
|
HINTS: v/ { array array } { double-array double-array } ;
|
||||||
|
HINTS: vmax { array array } { double-array double-array } ;
|
||||||
|
HINTS: vmin { array array } { double-array double-array } ;
|
||||||
|
HINTS: v. { array array } { double-array double-array } ;
|
||||||
|
HINTS: norm-sq { array } { double-array } ;
|
||||||
|
HINTS: norm { array } { double-array } ;
|
||||||
|
HINTS: normalize { array } { double-array } ;
|
||||||
|
HINTS: distance { array array } { double-array double-array } ;
|
||||||
|
|
||||||
|
! Type functions
|
||||||
|
USING: words classes.algebra compiler.tree.propagation.info
|
||||||
|
math.intervals ;
|
||||||
|
|
||||||
|
{ v+ v- v* v/ vmax vmin } [
|
||||||
|
[
|
||||||
|
[ class>> double-array class<= ] both?
|
||||||
|
double-array object ? <class-info>
|
||||||
|
] "outputs" set-word-prop
|
||||||
|
] each
|
||||||
|
|
||||||
|
{ n*v n/v } [
|
||||||
|
[
|
||||||
|
nip class>> double-array class<= double-array object ? <class-info>
|
||||||
|
] "outputs" set-word-prop
|
||||||
|
] each
|
||||||
|
|
||||||
|
{ v*n v/n } [
|
||||||
|
[
|
||||||
|
drop class>> double-array class<= double-array object ? <class-info>
|
||||||
|
] "outputs" set-word-prop
|
||||||
|
] each
|
||||||
|
|
||||||
|
{ vneg normalize } [
|
||||||
|
[
|
||||||
|
class>> double-array class<= double-array object ? <class-info>
|
||||||
|
] "outputs" set-word-prop
|
||||||
|
] each
|
||||||
|
|
||||||
|
\ norm-sq [
|
||||||
|
class>> double-array class<= [ float 0. 1/0. [a,b] <class/interval-info> ] [ object-info ] if
|
||||||
|
] "outputs" set-word-prop
|
||||||
|
|
||||||
|
\ v. [
|
||||||
|
[ class>> double-array class<= ] both?
|
||||||
|
float object ? <class-info>
|
||||||
|
] "outputs" set-word-prop
|
||||||
|
|
||||||
|
\ distance [
|
||||||
|
[ class>> double-array class<= ] both?
|
||||||
|
[ float 0. 1/0. [a,b] <class/interval-info> ] [ object-info ] if
|
||||||
|
] "outputs" set-word-prop
|
||||||
|
|
|
@ -7,11 +7,11 @@ $nl
|
||||||
"The " { $slot "underlying" } " slot holds a " { $link c-ptr } " with the raw data. This pointer can be passed to C functions." } ;
|
"The " { $slot "underlying" } " slot holds a " { $link c-ptr } " with the raw data. This pointer can be passed to C functions." } ;
|
||||||
|
|
||||||
HELP: <struct-array>
|
HELP: <struct-array>
|
||||||
{ $values { "length" integer } { "c-type" string } }
|
{ $values { "length" integer } { "c-type" string } { "struct-array" struct-array } }
|
||||||
{ $description "Creates a new array for holding values of the specified C type." } ;
|
{ $description "Creates a new array for holding values of the specified C type." } ;
|
||||||
|
|
||||||
HELP: <direct-struct-array>
|
HELP: <direct-struct-array>
|
||||||
{ $values { "alien" c-ptr } { "length" integer } { "c-type" string } }
|
{ $values { "alien" c-ptr } { "length" integer } { "c-type" string } { "struct-array" struct-array } }
|
||||||
{ $description "Creates a new array for holding values of the specified C type, backed by the memory at " { $snippet "alien" } "." } ;
|
{ $description "Creates a new array for holding values of the specified C type, backed by the memory at " { $snippet "alien" } "." } ;
|
||||||
|
|
||||||
ARTICLE: "struct-arrays" "C struct and union arrays"
|
ARTICLE: "struct-arrays" "C struct and union arrays"
|
||||||
|
|
Loading…
Reference in New Issue