]> gitweb.factorcode.org Git - factor.git/blob - extra/math/analysis/analysis.factor
Fix comments to be ! not #!.
[factor.git] / extra / math / analysis / analysis.factor
1 ! Copyright (C) 2008 Doug Coleman, Slava Pestov, Aaron Schaefer.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: combinators.short-circuit kernel math math.constants
4 math.functions math.vectors sequences ;
5 IN: math.analysis
6
7 <PRIVATE
8
9 ! http://www.rskey.org/gamma.htm  "Lanczos Approximation"
10 ! n=6: error ~ 3 x 10^-11
11
12 CONSTANT: gamma-g6 5.15
13
14 CONSTANT: gamma-p6
15     {
16         2.50662827563479526904 225.525584619175212544 -268.295973841304927459
17         80.9030806934622512966 -5.00757863970517583837 0.0114684895434781459556
18     }
19
20 : gamma-z ( x n -- seq )
21     [ + recip ] with { } map-integers 1.0 0 pick set-nth ;
22
23 : (gamma-lanczos6) ( x -- log[gamma[x+1]] )
24     ! log(gamma(x+1)
25     [ 0.5 + dup gamma-g6 + [ log * ] keep - ]
26     [ 6 gamma-z gamma-p6 v. log ] bi + ;
27
28 : gamma-lanczos6 ( x -- gamma[x] )
29     ! gamma(x) = gamma(x+1) / x
30     [ (gamma-lanczos6) e^ ] keep / ;
31
32 : gammaln-lanczos6 ( x -- gammaln[x] )
33     ! log(gamma(x)) = log(gamma(x+1)) - log(x)
34     [ (gamma-lanczos6) ] keep log - ;
35
36 : gamma-neg ( gamma[abs[x]] x -- gamma[x] )
37     dup pi * sin * * pi neg swap / ; inline
38
39 PRIVATE>
40
41 : gamma ( x -- y )
42     ! gamma(x) = integral 0..inf [ t^(x-1) exp(-t) ] dt
43     ! gamma(n+1) = n! for n > 0
44     dup { [ 0.0 <= ] [ 1.0 mod zero? ] } 1&& [
45         drop 1/0.
46     ] [
47         [ abs gamma-lanczos6 ] keep dup 0 > [ drop ] [ gamma-neg ] if
48     ] if ;
49
50 : gammaln ( x -- gamma[x] )
51     ! gammaln(x) is an alternative when gamma(x)'s range
52     ! varies too widely
53     dup 0 < [
54         drop 1/0.
55     ] [
56         [ abs gammaln-lanczos6 ] keep dup 0 > [ drop ] [ gamma-neg ] if
57     ] if ;
58
59 ! Forth Scientific Library Algorithm #1
60 !
61 ! Evaluates the Real Exponential Integral,
62 !     E1(x) = - Ei(-x) =   int_x^\infty exp^{-u}/u du      for x > 0
63 ! using a rational approximation
64 !
65 ! Collected Algorithms from ACM, Volume 1 Algorithms 1-220,
66 ! 1980; Association for Computing Machinery Inc., New York,
67 ! ISBN 0-89791-017-6
68 !
69 ! (c) Copyright 1994 Everett F. Carter.  Permission is granted by the
70 ! author to use this software for any application provided the
71 ! copyright notice is preserved.
72
73 : exp-int ( x -- y )
74     ! For real values of x only. Accurate to 7 decimals.
75     dup 1.0 < [
76         dup 0.00107857 * 0.00976004 -
77         over *
78         0.05519968 +
79         over *
80         0.24991055 -
81         over *
82         0.99999193 +
83         over *
84         0.57721566 -
85         swap log -
86     ] [
87         dup 8.5733287401 +
88         over *
89         18.059016973 +
90         over *
91         8.6347608925 +
92         over *
93         0.2677737343 +
94
95         over
96         dup 9.5733223454 +
97         over *
98         25.6329561486 +
99         over *
100         21.0996530827 +
101         over *
102         3.9584969228 +
103
104         nip
105         /
106         over /
107         swap -1.0 * e^
108         *
109     ] if ;
110
111 ! James Stirling's approximation for N!:
112 ! http://www.csse.monash.edu.au/~lloyd/tildeAlgDS/Numerical/Stirling/
113
114 : stirling-fact ( n -- fact )
115     [ pi 2 * * sqrt ]
116     [ [ e / ] keep ^ ]
117     [ 12 * recip 1 + ] tri * * ;