Paste: psums

Author: pruned
Mode: factor
Date: Sun, 28 Sep 2008 17:13:07
Plain Text |
! Copyright (C) 2008 Marc Fauconneau.
! See http://factorcode.org/license.txt for BSD license.
USING: kernel sequences math hints math.ranges locals sequences.private 
       math.functions math.private kernel.private assocs math.parser 
       arrays ;
USING: lexer namespaces parser strings.parser splitting compiler.units 
       io prettyprint quotations accessors strings stack-checker combinators
       effects polytest ;
IN: psums

: psums-list ( -- list )
  { 
    { "(2/3)^k"              [ 2 3 / swap ^ ] }
    { "k^-0.5"               [ dup -1/2 ^ ] }
    { "1/k(k+1)"             [ dup 1 + * swap / ] }
    { "Flint Hills"          [ dup 3 ^ sin sq * 1 swap / ] }
    { "Cookson Hills"        [ dup 3 ^ cos sq * 1 swap / ] }
    { "Harmonic"             [ 1 swap / ] }
    { "Riemann Zeta"         [ sq 1 swap / ] }
    { "Alternating Harmonic" [ -1 over ^ swap / ] }
    { "Gregory"              [ -1 over ^ swap 2 * 1 - / ] } 
  } ;
     
: compute-sum ( n term -- sum )
  [ 1 swap [a,b] ] dip [ >float ] compose map sum ; inline ! inline to make inference happy
  
:: (psums) ( N -- )
  psums-list [| name term! | N term compute-sum term! I: "[ 9 term ]\t[ name ]" . ] assoc-each ;

:: (psums') ( N -- )
  psums-list [| name term! | I: "[ 9 N term compute-sum ]\t[ name ]" . ] assoc-each ;

: psums ( -- )
  100 (psums') ;

New Annotation

Summary:
Author:
Mode:
Body: