Clean up and shorten partial-sums; tweak it to not use float-mod
parent
e1578b5848
commit
24b8bc5a4f
|
@ -1,63 +1,42 @@
|
||||||
USING: math math.functions kernel sequences io io.styles
|
USING: math math.functions kernel io io.styles
|
||||||
prettyprint words hints ;
|
prettyprint combinators hints fry namespaces ;
|
||||||
IN: benchmark.partial-sums
|
IN: benchmark.partial-sums
|
||||||
|
|
||||||
: summing ( n quot -- y )
|
! Helper words
|
||||||
[ >float ] swap [ + ] 3compose
|
: summing-integers ( n quot -- y ) [ 0.0 1 ] 2dip '[ @ + ] (each-integer) ; inline
|
||||||
0.0 -rot 1 -rot (each-integer) ; inline
|
: summing-floats ( n quot -- y ) '[ >float @ ] summing-integers ; inline
|
||||||
|
|
||||||
: 2/3^k ( n -- y ) [ 2.0 3.0 / swap 1- ^ ] summing ;
|
|
||||||
|
|
||||||
HINTS: 2/3^k fixnum ;
|
|
||||||
|
|
||||||
: k^-0.5 ( n -- y ) [ -0.5 ^ ] summing ;
|
|
||||||
|
|
||||||
HINTS: k^-0.5 fixnum ;
|
|
||||||
|
|
||||||
: 1/k(k+1) ( n -- y ) [ dup 1+ * recip ] summing ;
|
|
||||||
|
|
||||||
HINTS: 1/k(k+1) fixnum ;
|
|
||||||
|
|
||||||
: cube ( x -- y ) dup dup * * ; inline
|
: cube ( x -- y ) dup dup * * ; inline
|
||||||
|
: -1^ 2 mod 2 * 1- ; inline
|
||||||
|
|
||||||
: flint-hills ( n -- y )
|
! The functions
|
||||||
[ dup cube swap sin sq * recip ] summing ;
|
: 2/3^k ( n -- y ) [ 2.0 3.0 / swap 1- ^ ] summing-floats ; inline
|
||||||
|
: k^-0.5 ( n -- y ) [ -0.5 ^ ] summing-floats ; inline
|
||||||
|
: 1/k(k+1) ( n -- y ) [ dup 1+ * recip ] summing-floats ; inline
|
||||||
|
: flint-hills ( n -- y ) [ [ cube ] [ sin sq ] bi * recip ] summing-floats ; inline
|
||||||
|
: cookson-hills ( n -- y ) [ [ cube ] [ cos sq ] bi * recip ] summing-floats ; inline
|
||||||
|
: harmonic ( n -- y ) [ recip ] summing-floats ; inline
|
||||||
|
: riemann-zeta ( n -- y ) [ sq recip ] summing-floats ; inline
|
||||||
|
: alternating-harmonic ( n -- y ) [ [ -1^ ] keep /f ] summing-integers ; inline
|
||||||
|
: gregory ( n -- y ) [ [ -1^ ] [ 2.0 * 1- ] bi / ] summing-integers ; inline
|
||||||
|
|
||||||
HINTS: flint-hills fixnum ;
|
: partial-sums ( n -- results )
|
||||||
|
[
|
||||||
|
{
|
||||||
|
[ 2/3^k \ 2/3^k set ]
|
||||||
|
[ k^-0.5 \ k^-0.5 set ]
|
||||||
|
[ 1/k(k+1) \ 1/k(k+1) set ]
|
||||||
|
[ flint-hills \ flint-hills set ]
|
||||||
|
[ cookson-hills \ cookson-hills set ]
|
||||||
|
[ harmonic \ harmonic set ]
|
||||||
|
[ riemann-zeta \ riemann-zeta set ]
|
||||||
|
[ alternating-harmonic \ alternating-harmonic set ]
|
||||||
|
[ gregory \ gregory set ]
|
||||||
|
} cleave
|
||||||
|
] { } make-assoc ;
|
||||||
|
|
||||||
: cookson-hills ( n -- y )
|
HINTS: partial-sums fixnum ;
|
||||||
[ dup cube swap cos sq * recip ] summing ;
|
|
||||||
|
|
||||||
HINTS: cookson-hills fixnum ;
|
: partial-sums-main ( -- )
|
||||||
|
2500001 partial-sums simple-table. ;
|
||||||
: harmonic ( n -- y ) [ recip ] summing ;
|
|
||||||
|
|
||||||
HINTS: harmonic fixnum ;
|
|
||||||
|
|
||||||
: riemann-zeta ( n -- y ) [ sq recip ] summing ;
|
|
||||||
|
|
||||||
HINTS: riemann-zeta fixnum ;
|
|
||||||
|
|
||||||
: -1^ 2 mod zero? 1 -1 ? ; inline
|
|
||||||
|
|
||||||
: alternating-harmonic ( n -- y ) [ dup -1^ swap / ] summing ;
|
|
||||||
|
|
||||||
HINTS: alternating-harmonic fixnum ;
|
|
||||||
|
|
||||||
: gregory ( n -- y ) [ dup -1^ swap 2 * 1- / ] summing ;
|
|
||||||
|
|
||||||
HINTS: gregory fixnum ;
|
|
||||||
|
|
||||||
: functions
|
|
||||||
{ 2/3^k k^-0.5 1/k(k+1) flint-hills cookson-hills harmonic riemann-zeta alternating-harmonic gregory } ;
|
|
||||||
|
|
||||||
: partial-sums ( n -- )
|
|
||||||
standard-table-style [
|
|
||||||
functions [
|
|
||||||
[ tuck execute pprint-cell pprint-cell ] with-row
|
|
||||||
] with each
|
|
||||||
] tabular-output ;
|
|
||||||
|
|
||||||
: partial-sums-main ( -- ) 2500000 partial-sums ;
|
|
||||||
|
|
||||||
MAIN: partial-sums-main
|
MAIN: partial-sums-main
|
||||||
|
|
Loading…
Reference in New Issue