]> gitweb.factorcode.org Git - factor.git/commitdiff
initial math cleanup
authorDoug Coleman <doug.coleman@gmail.com>
Fri, 3 Oct 2008 07:19:03 +0000 (02:19 -0500)
committerDoug Coleman <doug.coleman@gmail.com>
Fri, 3 Oct 2008 07:19:03 +0000 (02:19 -0500)
17 files changed:
extra/math/algebra/algebra.factor
extra/math/analysis/analysis.factor
extra/math/combinatorics/combinatorics.factor
extra/math/compare/compare.factor
extra/math/derivatives/derivatives.factor
extra/math/erato/erato.factor
extra/math/fft/fft.factor
extra/math/miller-rabin/miller-rabin.factor
extra/math/newtons-method/newtons-method.factor
extra/math/numerical-integration/numerical-integration.factor
extra/math/polynomials/polynomials.factor
extra/math/primes/primes.factor
extra/math/quaternions/quaternions.factor
extra/math/secant-method/secant-method.factor
extra/math/statistics/statistics.factor
extra/math/text/english/english.factor
extra/math/trig/trig.factor

index 8bb8420d1a993d72b782f4a63ac77e3d0feaf79d..8cccded26a8c046540197c2f77ea95b2d70be267 100644 (file)
@@ -1,8 +1,10 @@
 ! Copyright (c) 2007 Samuel Tardieu
 ! See http://factorcode.org/license.txt for BSD license.
-USING: kernel math math.functions sequences ;
+USING: kernel math math.functions sequences fry ;
 IN: math.algebra
 
 : chinese-remainder ( aseq nseq -- x )
   dup product
-  [ [ over / [ swap gcd drop ] keep * * ] curry 2map sum ] keep rem ; foldable
+    [
+        '[ _ over / [ swap gcd drop ] keep * * ] 2map sum
+    ] keep rem ; foldable
index a41281d7795d431f9a6a069e7cc1eb1b6616c85f..7da1c96b611f339d1ead03010482573555285f0d 100755 (executable)
@@ -1,5 +1,7 @@
+! Copyright (C) 2008 Doug Coleman, Slava Pestov.
+! See http://factorcode.org/license.txt for BSD license.
 USING: kernel math math.constants math.functions math.intervals
-math.vectors namespaces sequences ;
+math.vectors namespaces sequences combinators.short-circuit ;
 IN: math.analysis
 
 <PRIVATE
@@ -20,8 +22,8 @@ IN: math.analysis
 
 : (gamma-lanczos6) ( x -- log[gamma[x+1]] )
     #! log(gamma(x+1)
-    dup 0.5 + dup gamma-g6 + dup >r log * r> -
-    swap 6 gamma-z gamma-p6 v. log + ;
+    [ 0.5 + dup gamma-g6 + dup [ log * ] dip - ]
+    [ 6 gamma-z gamma-p6 v. log ] bi + ;
 
 : gamma-lanczos6 ( x -- gamma[x] )
     #! gamma(x) = gamma(x+1) / x
@@ -39,7 +41,7 @@ PRIVATE>
 : gamma ( x -- y )
     #! gamma(x) = integral 0..inf [ t^(x-1) exp(-t) ] dt
     #! gamma(n+1) = n! for n > 0
-    dup 0.0 <= over 1.0 mod zero? and [
+    dup { [ 0.0 <= ] [ 1.0 mod zero? ] } 1&& [
             drop 1./0.
         ] [
             dup abs gamma-lanczos6 swap dup 0 > [ drop ] [ gamma-neg ] if
@@ -55,7 +57,7 @@ PRIVATE>
     ] if ;
 
 : nth-root ( n x -- y )
-    over 0 = [ "0th root is undefined" throw ] when >r recip r> swap ^ ;
+    [ recip ] dip swap ^ ;
 
 ! Forth Scientific Library Algorithm #1
 !
index a0c6df083bc18856543d153ab10e6c1af80df895..b1c49b8ab5dff26c6d2f764235e11b1a954d0feb 100644 (file)
@@ -1,7 +1,7 @@
 ! Copyright (c) 2007, 2008 Slava Pestov, Doug Coleman, Aaron Schaefer.
 ! See http://factorcode.org/license.txt for BSD license.
 USING: assocs kernel math math.order math.ranges mirrors
-namespaces make sequences sequences.lib sorting ;
+namespaces sequences sorting fry ;
 IN: math.combinatorics
 
 <PRIVATE
@@ -13,7 +13,7 @@ IN: math.combinatorics
     2dup - dupd > [ dupd - ] when ; inline
 
 ! See this article for explanation of the factoradic-based permutation methodology:
-!     http://msdn2.microsoft.com/en-us/library/aa302371.aspx
+! http://msdn2.microsoft.com/en-us/library/aa302371.aspx
 
 : factoradic ( n -- factoradic )
     0 [ over 0 > ] [ 1+ [ /mod ] keep swap ] [ ] produce reverse 2nip ;
@@ -39,13 +39,10 @@ PRIVATE>
     twiddle [ nPk ] keep factorial / ;
 
 : permutation ( n seq -- seq )
-    tuck permutation-indices swap nths ;
+    [ permutation-indices ] keep nths ;
 
 : all-permutations ( seq -- seq )
-    [
-        [ length factorial ] keep [ permutation , ] curry each
-    ] { } make ;
+    [ length factorial ] keep '[ _ permutation ] map ;
 
 : inverse-permutation ( seq -- permutation )
     <enum> >alist sort-values keys ;
-
index 28a8eadc816a1eed8a25471f1f9fd80957c48216..d19dac3d2b5d01d8f1fbfe35202524a04b4bc7a6 100644 (file)
@@ -19,4 +19,3 @@ IN: math.compare
 
 : clamp ( a value b -- x )
    min max ; 
-
index ad8d944bfe4f34b38fc7a6e158efdb24b76c04b6..b7612e112b5ea0831e5fcb92871e4d7afeada46e 100644 (file)
@@ -1,4 +1,3 @@
-
 USING: kernel continuations combinators sequences math
       math.order math.ranges accessors float-arrays ;
 
@@ -7,11 +6,11 @@ IN: math.derivatives
 TUPLE: state x func h err i j errt fac hh ans a done ;
 
 : largest-float ( -- x ) HEX: 7fefffffffffffff bits>double ; foldable
-: ntab ( -- val ) 8 ;
-: con ( -- val ) 1.6 ;
-: con2 ( -- val ) con con * ;
-: big ( -- val ) largest-float ;
-: safe ( -- val ) 2.0 ;
+: ntab ( -- val ) 8 ; inline
+: con ( -- val ) 1.6 ; inline
+: con2 ( -- val ) con con * ; inline
+: big ( -- val ) largest-float ; inline
+: safe ( -- val ) 2.0 ; inline
 
 ! Yes, this was ported from C code.
 : a[i][i]     ( state -- elt ) [ i>>     ] [ i>>     ] [ a>> ] tri nth nth ;
@@ -120,4 +119,4 @@ TUPLE: state x func h err i j errt fac hh ans a done ;
  bi ;
 
 : derivative ( x func -- m ) 0.01 2.0 (derivative) drop ; 
-: derivative-func ( func -- der ) [ derivative ] curry ;
\ No newline at end of file
+: derivative-func ( func -- der ) [ derivative ] curry ;
index f836d71a99d10d51b90a8bb6b1bab4d43bd3e837..4c6675e8f170c91698dce1df3582ae4c762923e2 100644 (file)
@@ -11,8 +11,8 @@ TUPLE: erato limit bits latest ;
 : ind ( n -- i )
   2/ 1- ; inline
 
-: is-prime ( n erato -- bool )
-  >r ind r> bits>> nth ; inline
+: is-prime ( n limit -- bool )
+  [ ind ] [ bits>> ] bi* nth ; inline
 
 : indices ( n erato -- range )
   limit>> ind over 3 * ind swap rot <range> ;
index 682d2a49dbbb35d3ba0daad2e48b3994fe1cc0a3..b82ecb6b2c4c2ea3fb39c4f3ce91b8b15b11d26b 100644 (file)
@@ -9,7 +9,7 @@ IN: math.fft
 : odd ( seq -- seq ) 2 group 1 <column> ;
 DEFER: fft
 : two ( seq -- seq ) fft 2 v/n dup append ;
-: omega ( n -- n ) recip -2 pi i* * * exp ;
+: omega ( n -- n' ) recip -2 pi i* * * exp ;
 : twiddle ( seq -- seq ) dup length dup omega swap n^v v* ;
 : (fft) ( seq -- seq ) dup odd two twiddle swap even two v+ ;
 : fft ( seq -- seq ) dup length 1 = [ (fft) ] unless ;
index f1953340dbc98d2bf24f838206643c4a5b3b5ff3..45665c701dff56944dff6117bb76ddf543e80925 100755 (executable)
@@ -1,3 +1,5 @@
+! Copyright (C) 2008 Doug Coleman.
+! See http://factorcode.org/license.txt for BSD license.
 USING: combinators combinators.lib io locals kernel math
 math.functions math.ranges namespaces random sequences
 hashtables sets ;
index 5bf71deac892d1229e03b8a9bfb716bcc118b6f4..269eae2538feaf0d090723cfb7ee637d51ce067b 100644 (file)
@@ -1,11 +1,17 @@
 ! Copyright © 2008 Reginald Keith Ford II
+! See http://factorcode.org/license.txt for BSD license.
 ! Newton's Method of approximating roots
-
 USING: kernel math math.derivatives ;
 IN: math.newtons-method
 
 <PRIVATE
-: newton-step ( x function -- x2 ) dupd [ call ] [ derivative ] 2bi / - ;
-: newton-precision ( -- n ) 13 ;
+
+: newton-step ( x function -- x2 )
+    dupd [ call ] [ derivative ] 2bi / - ; inline
+
+: newton-precision ( -- n ) 13 ; inline
+
 PRIVATE>
-: newtons-method ( guess function -- x ) newton-precision [ [ newton-step ] keep ] times drop ;
+
+: newtons-method ( guess function -- x )
+    newton-precision [ [ newton-step ] keep ] times drop ;
index 798d3a5e7154ee0fe81eaa4e35e11a4ba117b630..dfaa618b536f27b2ea0b4cb8e4e1e2d823cab5c6 100644 (file)
@@ -1,18 +1,20 @@
+! Copyright (C) 2008 Doug Coleman.
+! See http://factorcode.org/license.txt for BSD license.
 USING: arrays kernel sequences namespaces make math math.ranges
 math.vectors vectors ;
 IN: math.numerical-integration
 
 SYMBOL: num-steps 180 num-steps set-global
+
 : setup-simpson-range ( from to -- frange )
     2dup swap - num-steps get / <range> ;
 
 : generate-simpson-weights ( seq -- seq )
-    [
-        { 1 4 } % length 2 / 2 - { 2 4 } <repetition> concat % 1 ,
-    ] { } make ;
+    { 1 4 }
+    swap length 2 / 2 - { 2 4 } <repetition> concat
+    { 1 } 3append ;
 
 : integrate-simpson ( from to f -- x )
-    >r setup-simpson-range r>
-    dupd map dup generate-simpson-weights
+    [ setup-simpson-range dup ] dip 
+    map dup generate-simpson-weights
     v. swap [ third ] keep first - 6 / * ;
-
index 8662bbb0895725d69e7de8784daee3739103835c..51512ca2e337af35197e35c1e80054b76771b40c 100644 (file)
@@ -1,3 +1,5 @@
+! Copyright (C) 2008 Doug Coleman.
+! See http://factorcode.org/license.txt for BSD license.
 USING: arrays kernel sequences vectors math math.vectors
 namespaces make shuffle splitting sequences.lib math.order ;
 IN: math.polynomials
@@ -82,5 +84,5 @@ PRIVATE>
 
 : polyval ( p x -- p[x] )
     #! Evaluate a polynomial.
-    >r dup length r> powers v. ;
+    [ dup length ] dip powers v. ;
 
index f3a515e72b221a955ec6dcc193a22d33dfb8afc5..feb60c555dc09199aced7017ff6fa7029e5fae41 100644 (file)
@@ -8,44 +8,45 @@ IN: math.primes
 <PRIVATE
 
 : find-prime-miller-rabin ( n -- p )
-  dup miller-rabin [ 2 + find-prime-miller-rabin ] unless ; foldable
+    dup miller-rabin [ 2 + find-prime-miller-rabin ] unless ; foldable
 
 PRIVATE>
 
 : next-prime ( n -- p )
-  dup 999983 < [
-    primes-under-million [ natural-search drop 1+ ] keep nth
-  ] [
-    next-odd find-prime-miller-rabin
-  ] if ; foldable
+    dup 999983 < [
+        primes-under-million [ natural-search drop 1+ ] keep nth
+    ] [
+        next-odd find-prime-miller-rabin
+    ] if ; foldable
 
 : prime? ( n -- ? )
-  dup 1000000 < [
-    dup primes-under-million natural-search nip =
-  ] [
-    miller-rabin
-  ] if ; foldable
+    dup 1000000 < [
+        dup primes-under-million natural-search nip =
+    ] [
+        miller-rabin
+    ] if ; foldable
 
 : lprimes ( -- list )
-  0 primes-under-million seq>list
-  1000003 [ 2 + find-prime-miller-rabin ] lfrom-by
-  lappend ;
+    0 primes-under-million seq>list
+    1000003 [ 2 + find-prime-miller-rabin ] lfrom-by
+    lappend ;
 
 : lprimes-from ( n -- list )
-  dup 3 < [ drop lprimes ] [  1- next-prime [ next-prime ] lfrom-by ] if ;
+    dup 3 < [ drop lprimes ] [ 1- next-prime [ next-prime ] lfrom-by ] if ;
 
 : primes-upto ( n -- seq )
-  {
-    { [ dup 2 < ] [ drop { } ] }
-    { [ dup 1000003 < ]
-      [ primes-under-million [ natural-search drop 1+ 0 swap ] keep <slice> ] }
-    [ primes-under-million 1000003 lprimes-from
-        rot [ <= ] curry lwhile list>array append ]
-  } cond ; foldable
+    {
+        { [ dup 2 < ] [ drop { } ] }
+        { [ dup 1000003 < ] [
+            primes-under-million [ natural-search drop 1+ 0 swap ] keep <slice>
+        ] }
+        [ primes-under-million 1000003 lprimes-from
+            rot [ <= ] curry lwhile list>array append ]
+    } cond ; foldable
 
 : primes-between ( low high -- seq )
-  primes-upto
-  [ 1- next-prime ] dip
-  [ natural-search drop ] keep [ length ] keep <slice> ; foldable
+    primes-upto
+    [ 1- next-prime ] dip
+    [ natural-search drop ] keep [ length ] keep <slice> ; foldable
 
 : coprime? ( a b -- ? ) gcd nip 1 = ; foldable
index 3c450f1c054b018a0ad3cbc2bba811a95ea26fd7..65f18d35689e1fe6cc2811561aa3046be4c34c5c 100755 (executable)
@@ -28,7 +28,7 @@ PRIVATE>
 
 : qconjugate ( u -- u' )
     #! Quaternion conjugate.
-    first2 neg >r conjugate r> 2array ;
+    first2 [ conjugate ] [ neg  ] bi* 2array ;
 
 : qrecip ( u -- 1/u )
     #! Quaternion inverse.
index e039b42bbdffe9126c54d6783c374fe6efd486c8..ad52c0cd4ab447d5d784937f5f560141df37c3f1 100644 (file)
@@ -1,14 +1,26 @@
 ! Copyright © 2008 Reginald Keith Ford II
+! See http://factorcode.org/license.txt for BSD license.
 ! Secant Method of approximating roots
-
 USING: kernel math math.function-tools math.points math.vectors ;
 IN: math.secant-method
 
 <PRIVATE
-: secant-solution ( x1 x2 function -- solution ) [ eval ] curry bi@ linear-solution ;
-: secant-step ( x1 x2 func -- x2 x3 func ) 2dup [ secant-solution ] 2dip swapd ;
-: secant-precision ( -- n ) 15 ;
+
+: secant-solution ( x1 x2 function -- solution )
+    [ eval ] curry bi@ linear-solution ;
+
+: secant-step ( x1 x2 func -- x2 x3 func )
+    [ secant-solution ] 2keep swapd ;
+
+: secant-precision ( -- n ) 15 ; inline
+
 PRIVATE>
-: secant-method ( left right function -- x ) secant-precision [ secant-step ] times drop + 2 / ;
+
+: secant-method ( left right function -- x )
+    secant-precision [ secant-step ] times drop + 2 / ;
+
 ! : close-enough? ( a b -- t/f ) - abs tiny-amount < ;
-! : secant-method2 ( left right function -- x ) 2over close-enough? [ drop average ] [ secant-step secant-method ] if  ;
\ No newline at end of file
+
+! : secant-method2 ( left right function -- x )
+    ! 2over close-enough?
+    ! [ drop average ] [ secant-step secant-method ] if  ;
index 28cc05151bb5e76c7760d00589fc39953b433a72..8cd6d26c1c1e0492d0fa5e3eac696c3cda3920ed 100644 (file)
@@ -1,5 +1,7 @@
+! Copyright (C) 2008 Doug Coleman, Michael Judge.
+! See http://factorcode.org/license.txt for BSD license.
 USING: kernel math math.analysis math.functions math.vectors sequences
-    sequences.lib sorting ;
+sequences.lib sorting ;
 IN: math.statistics
 
 : mean ( seq -- n )
@@ -18,7 +20,7 @@ IN: math.statistics
 : median ( seq -- n )
     #! middle number if odd, avg of two middle numbers if even
     natural-sort dup length dup even? [
-        1- 2 / swap [ nth ] [ >r 1+ r> nth ] 2bi + 2 /
+        1- 2 / swap [ nth ] [ [ 1+ ] dip nth ] 2bi + 2 /
     ] [
         2 / swap nth
     ] if ;
index 387be4d7912f240cada6484e965b884efc76fd87..439d0a75fe9c01686a3706c07a9394ecd7ed1c53 100755 (executable)
@@ -1,8 +1,7 @@
 ! Copyright (c) 2007 Aaron Schaefer.
 ! See http://factorcode.org/license.txt for BSD license.
 USING: combinators.lib kernel math math.functions math.parser namespaces
-    sequences splitting grouping sequences.lib
-    combinators.short-circuit ;
+sequences splitting grouping combinators.short-circuit ;
 IN: math.text.english
 
 <PRIVATE
@@ -86,14 +85,10 @@ SYMBOL: and-needed?
     ] if ;
 
 : (number>text) ( n -- str )
-    dup negative-text swap abs 3digit-groups recombine append ;
+    [ negative-text ] [ abs 3digit-groups recombine ] bi append ;
 
 PRIVATE>
 
 : number>text ( n -- str )
-    dup zero? [
-        small-numbers
-    ] [
-        [ (number>text) ] with-scope
-    ] if ;
+    dup zero? [ small-numbers ] [ [ (number>text) ] with-scope ] if ;
 
index be9ec6a56c56aa1f93506283a90fa2cbedbd0a58..3d9428adda4a5a2918a36cb96f3299eb34c3f994 100644 (file)
@@ -1,6 +1,6 @@
-
+! Copyright (C) 2008 Eduardo Cavazos.
+! See http://factorcode.org/license.txt for BSD license.
 USING: math math.constants ;
-
 IN: math.trig
 
 : deg>rad pi * 180 / ; inline