If you calculate the gamma function and have to use an approximation (instead of built in functions like with Excel and Matlab), what approximation would you use?
What does the WP34S use for the Gamma function?
Namir
What is the Gamma approximation you use?
|
|
« Next Oldest | Next Newest »
|
▼
Post: #10
08-02-2013, 04:46 PM
If you calculate the gamma function and have to use an approximation (instead of built in functions like with Excel and Matlab), what approximation would you use? What does the WP34S use for the Gamma function? Namir ▼
Post: #11
08-02-2013, 05:19 PM
Quote:Just look at it - it's open source. d;-) ▼
Post: #12
08-02-2013, 07:02 PM
If you haven't looked at the source before you can grab your svn checkout svn://svn.code.sf.net/p/wp34s/code wp34s I took a look at the code. It is in wp34s/trunk/decn.c file. It seems to be using a function that computes the Natural Log of Gamma and then raises e to that power.
The function seems similar to the Spouge Gamma Approximation using
Edited: 2 Aug 2013, 7:05 p.m. ▼
Post: #13
08-02-2013, 07:39 PM
I made a small survey for the most popular approximations for the gamma function. The Spouge method came first. You can calculate the constants somewhat easily and dynamically, unlike many other approximations. The Lanczos approximation appearing in Numerical Recipes. This method required storing an array of constants.
Namir Edited: 2 Aug 2013, 10:01 p.m. after one or more responses were posted ▼
Post: #14
08-02-2013, 07:57 PM
Quote:
Like Lanczos, does it work in the complex domain also? I Gerson. ------------------
Gamma:
Edited: 2 Aug 2013, 9:15 p.m.
Post: #15
08-03-2013, 02:22 AM
Quote: Well that sure helps the sharing and debating nature of this forum... I used a Lanczos approximation with 6 terms, for both the SandMath and 41Z implementations. It works fine (accurate to the 9th decimal digit for real arguments and the 8th for complex at worst) - with the reduced precision range in the platform but you guys are in the stratospheric accuracy range so I'm sure have used more terms or yet a better approach.
Post: #16
08-02-2013, 08:37 PM
I'm not certain which approximation we use now, I think it is Lanczos. Marcus did some work to improve the accuracy of gamma a year or two back and I don't remember if the algorithm was changed or just the constants. The history is in subversion if anyone really wants to check. The reference I used for the first implementation was Pugh's thesis on the gamma function. I originally used the table of constants on page 126 which we later found out weren't entirely correct. the algorithm used works for real and complex arguments using the same series.
▼
Post: #17
08-04-2013, 06:47 AM
Pauli is right, it's some time ago when I had a closer look at Gamma to make it fit for double precision. I used Pugh's thesis and information from Victor T. Toth's site. The list of constants can be found in the file compile_consts.c, but here you are:
// Gamma estimate constants ▼
Post: #18
08-04-2013, 06:59 PM
The Nemes approximations that Viktor Toth mentions are good ones, but not as good as the Spouge and Lanczos approximations. The Nemes approximation DO come third in my little study!
Post: #19
08-03-2013, 09:41 AM
Namir The algorithim employed varies w/ the artifact employed (different routines for different machines). I find the assorted routines on Viktor T. Toth's web site http://www.rskey.org/CMS/index.php/exhibit-hall/95 very interesting: thoughts? ps: view the page for a model to see the attendant gamma routine for that model. SlideRule
Post: #20
08-03-2013, 10:41 AM
Here are the source codes for the gamma function using Spouge's approximation for the HP-71B, HP41C, and HP-67:
HP-71B Implementation In the case of the 67 and 41C, enter the value for x and press the [A] key to get the gamma function value.
Namir Edited: 3 Aug 2013, 10:42 a.m. ▼
Post: #21
08-03-2013, 11:17 AM
THANKS! ps: I am indebted to you for ALL your marvelous postings (here & your web page)! Many thanks, again! SlideRule ▼
Post: #22
08-03-2013, 02:39 PM
You are most welcome. Sharing code here is fun. You can find more code for calculators and some programming languages on my web site. Please click here. ▼
Post: #23
08-04-2013, 12:58 AM
I took a look at your page also. Bookmarked it in fact. I even took a couple things. Fantastic site, thanks so much.
Post: #24
08-03-2013, 12:05 PM
Here is a non-optimal HP-48G/GX version:
%%HP: T(3)A(D)F(.);As a comparison, the HP-50g GAMMA function returns (.15190400267, 1980488015619E-2) Very nice! P.S.: The local variable s is not necessary. The following should be slightly faster:
%%HP: T(3)A(D)F(.);
Edited: 3 Aug 2013, 1:43 p.m.
Post: #25
08-03-2013, 05:20 PM
Only 8 terms appear to give more accurate results on the HP-71B and HP-48: 10 DESTROY ALLWhen the lines 40 and 60 are changed to 40 A=12.5 @ P2=SQR(2*PI)the output is GAMMA(N) N ▼
Post: #26
08-03-2013, 06:39 PM
Very interesting. Spouge's methods calculates the upper limit of the summation (that needs the FOR loop) as the integer(ceiling(A))-1 which gives 12 for A=12.5. Using an upper limit of 8 must be causing the accuracy of the 71B to give better results. I will keep that in mind! Thanks! Namir ▼
Post: #27
08-04-2013, 10:50 AM
To get better results:
- do loop the other way (8 downto 1) starting with small factors to keep accuracy
10 DESTROY ALL It gives better results most of the time (rounding can be tricky)
1.00000000002 1.00000000001 1 Olivier
Edited: 4 Aug 2013, 11:28 a.m. ▼
Post: #28
08-04-2013, 01:40 PM
It appears the left column results are more accurate, but perhaps the sample is too small. You might want to expand it. K=8 in line 35 gives more exact answer when compared to other even K. Regards, Gerson.
10 DESTROY ALL
Post: #29
08-04-2013, 06:41 PM
Thanks Olivier and Gerson for your input. As Voltaire once said, "The better is the enemy of the good!" I tried to do a curve fit between 1/gamma(x) and a tenth order polynomial. I also tired a Pade approximation using fifth order polynomials in the numerator and denominator. Neither attempts yielded good results. Namir
Post: #30
08-05-2013, 03:03 AM
Hello Namir, 20 years ago I programmed the gamma-function in "turbo pascal 6" with assembler routines coded for the 387 coprocessor. The Stirling-formula was used (for arguments > 10), for smaller arguments the recursion (gamma(x) = gamma(x+1)/x)). For negative Arguments the equation: gamma(x) = pi/(sin(pi*x)*gamma(1-x)). The function was only usefull for real numbers, and it was a luck, that I didn't had to earn my money with programming.... Greetings peacecalc ▼
Post: #31
08-05-2013, 07:14 AM
Interesting that you mentioned Turbo Pascal. I remember implementing the gamma function in Turbo Pascal in the late eighties. I used the series expansion that employs 26 constants too implement a polynomial approximation for 1/Gamma(x) for 1<=x<=2. I used recursion for arguments that were greater than 2. I made a living then by writing books about programming in Turbo Pascal, and then switched to Visual Basic and Visual C++. Namir |