]> gitweb.factorcode.org Git - factor.git/blobdiff - extra/math/analysis/analysis.factor
Delete empty unit tests files, remove 1- and 1+, reorder IN: lines in a lot of places...
[factor.git] / extra / math / analysis / analysis.factor
index a41281d7795d431f9a6a069e7cc1eb1b6616c85f..39d6450ba0cffc20d317b4e4608f473964bf746c 100755 (executable)
@@ -1,5 +1,7 @@
-USING: kernel math math.constants math.functions math.intervals
-math.vectors namespaces sequences ;
+! Copyright (C) 2008 Doug Coleman, Slava Pestov, Aaron Schaefer.
+! See http://factorcode.org/license.txt for BSD license.
+USING: combinators.short-circuit kernel math math.constants
+math.functions math.vectors sequences ;
 IN: math.analysis
 
 <PRIVATE
@@ -7,29 +9,29 @@ IN: math.analysis
 ! http://www.rskey.org/gamma.htm  "Lanczos Approximation"
 ! n=6: error ~ 3 x 10^-11
 
-: gamma-g6 5.15 ; inline
+CONSTANT: gamma-g6 5.15
 
-: gamma-p6
+CONSTANT: gamma-p6
     {
         2.50662827563479526904 225.525584619175212544 -268.295973841304927459
-        80.9030806934622512966 -5.00757863970517583837 0.0114684895434781459556 
-    } ; inline
+        80.9030806934622512966 -5.00757863970517583837 0.0114684895434781459556
+    }
 
 : gamma-z ( x n -- seq )
     [ + recip ] with map 1.0 0 pick set-nth ;
 
 : (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 + [ log * ] keep - ]
+    [ 6 gamma-z gamma-p6 v. log ] bi + ;
 
 : gamma-lanczos6 ( x -- gamma[x] )
     #! gamma(x) = gamma(x+1) / x
-    dup (gamma-lanczos6) exp swap / ;
+    [ (gamma-lanczos6) exp ] keep / ;
 
 : gammaln-lanczos6 ( x -- gammaln[x] )
     #! log(gamma(x)) = log(gamma(x+1)) - log(x)
-    dup (gamma-lanczos6) swap log - ;
+    [ (gamma-lanczos6) ] keep log - ;
 
 : gamma-neg ( gamma[abs[x]] x -- gamma[x] )
     dup pi * sin * * pi neg swap / ; inline
@@ -39,23 +41,23 @@ 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 [
-            drop 1./0.
-        ] [
-            dup abs gamma-lanczos6 swap dup 0 > [ drop ] [ gamma-neg ] if
+    dup { [ 0.0 <= ] [ 1.0 mod zero? ] } 1&& [
+        drop 1/0.
+    ] [
+        [ abs gamma-lanczos6 ] keep dup 0 > [ drop ] [ gamma-neg ] if
     ] if ;
 
 : gammaln ( x -- gamma[x] )
     #! gammaln(x) is an alternative when gamma(x)'s range
     #! varies too widely
     dup 0 < [
-            drop 1./0.
-        ] [
-            dup abs gammaln-lanczos6 swap dup 0 > [ drop ] [ gamma-neg ] if
+        drop 1/0.
+    ] [
+        [ abs gammaln-lanczos6 ] keep dup 0 > [ drop ] [ gamma-neg ] if
     ] if ;
 
 : nth-root ( n x -- y )
-    over 0 = [ "0th root is undefined" throw ] when >r recip r> swap ^ ;
+    swap recip ^ ;
 
 ! Forth Scientific Library Algorithm #1
 !
@@ -114,6 +116,6 @@ PRIVATE>
 
 : stirling-fact ( n -- fact )
     [ pi 2 * * sqrt ]
-    [ dup e / swap ^ ]
-    [ 12 * recip 1 + ]
-    tri * * ;
+    [ [ e / ] keep ^ ]
+    [ 12 * recip 1 + ] tri * * ;
+