Complex Gamma Function... revisited  Printable Version + HP Forums (https://archived.hpcalc.org/museumforum) + Forum: HP Museum Forums (https://archived.hpcalc.org/museumforum/forum1.html) + Forum: Old HP Forum Archives (https://archived.hpcalc.org/museumforum/forum2.html) + Thread: Complex Gamma Function... revisited (/thread156312.html) 
Complex Gamma Function... revisited  Ángel Martin  09142009 Valid for the complex plane plane where Gamma is defined (i.e. includes Re(z)<0, and excludes negative integers and zero). Here's the program listing, a nice example of the HP41Z function set (can you tell it's FOCAL?? :) Uses the Lanczos approximation, split in three parts as per the function names clearly show. More soon...
LBL "ZG" Re: Complex Gamma Function... revisited  hugh steers  09142009 I have had a lot of trouble with the Lanczos approximation. The coefficients of the series vary wildly and cause large swings in value. This causes a lot of cancellation which destroys accuracy. Currently, Im using increased precision to cope with this problem. I'd prefer a more stable formula than Lanczos. There's another method by Spouge, but you need a lot more series terms for convergence.
it will be interesting to see how many digits you get on the 41.
Re: Complex Gamma Function... revisited  Marcus von Cube, Germany  09152009 Go and ask Viktor T. Toth at rskey.org! He's dedicated part of his life to Gamma.
Re: Complex Gamma Function... revisited  Namir  09152009 I think Toth calculates the Gamma function for real arguments only. This way he can apply the Gamma (or Ln Gamma) to a whole collection of programmable calculators.
Re: Complex Gamma Function... revisited  Egan Ford  09152009 Nice. How about a complex LogGamma and a complex Lambert W?
Re: Complex Gamma Function... revisited  hugh steers  09152009 You kindof get LnGamma anyway. Out of Lanczos you wind up with three numbers; S, A and B (say). Gamma(z) = S*exp(A*ln(B)B) so using the same subroutine, you can have, LnGamma(z) = A*ln(B)  B + Ln(S) However, the latter might not be the best for larger numbers.
Re: Complex Gamma Function... revisited  Marcus von Cube, Germany  09152009 Namir, that depends on the calculator. On machines with decent complex support (like the TI92) he calculates the complex Gamma function.
His general article about Gamma is worth reading.
Re: Complex Gamma Function... revisited  Ángel Martin  09152009 Here's where I'm going to show my utter lack of math finesse but what the heck:
LBL "LNZG" Of course this is just a teaser, will look into a proper way during the weekend :)
'AM
Re: Complex Gamma Function... revisited  Ángel Martin  09152009 That exactly is the source I have used to programm it. http://www.rskey.org/gamma.htm To the careful reader it's clear that:
ZGSUM computes the sum of the seven terms (qk * Z^k) Cheers,
Re: Complex Gamma Function... revisited  Paul Dale  09162009 Quote:
Wikipedia has an algorithm for Lambert's W function that seems okay and converges in the complex plane: For complex (and real) LogGamma, I'm using Lanczos. Look for An Analysis of the Lanczos Gamma Approximation by Glendon Ralph Pugh pages 134 and 135 for the formula and coefficients of the approximation I'm using.
Re: Complex Gamma Function... revisited  Les Wright  09162009 Hugh, I have done a lot of fiddling with Spouge, and the cancellation and digit loss issues persist thereonly worse. You need a LOT of guard digits to preserve accuracy. I understand you double precision BCD20 type has gone a long way to help here.
Re: Complex Gamma Function... revisited  Namir  09162009 Marcus, You are correct! I did revisit Toth's Gamma function math page (have not seen it for a while) and saw the Lancsoz approximation that is being talked about here. While series approximation are in general easy to work with, a few here and there are tough to tame! Using the old programmable calculators, series approximation were really time consuming. With today's machine's we have faster CPU and the approximation (again in general) are not too bad.
Namir
Re: Complex Gamma Function... revisited  Ángel Martin  09222009 Well, I'll leave Lambert for the real mathematicians out there but here's a pragmatic approach for the LnGamma case. The first program is the "brute force" approach, adding terms to the summ until their contribution isn't relevant to the (rounded) result. VERY slow, and not an appropriate method just good for comparison purposes. LnG(z) = Gz  Lnz + SUM [(z/k)Ln(1+z/k) k=1,2.... ] No doubt you'll recognized Stirling's approach embedded in the second method. The routine calculates LnG(z+7) for enhanced accuracy (about E9), and adjusts it back appropriately. MCODE functions from the 41Z module help bring down the execution time to manageable on the real 41, and of course irrelevant on emulators. 2LnG(z)=Ln(2pi)Ln(z)+z{2Lnz + Ln[z*sinh(1/z)+(1/810*z^6)]2} LnG(z) = LnG(z+n)  Ln [ PROD(z+k)k=1,2..(n1)]
Method2 is much faster, more accurate and takes less program
Enjoy,
Edited: 22 Sept 2009, 9:16 a.m.
Re: Complex Gamma Function... revisited  Steven Thomas Smith  09252009 See GSL's complex Gamma implementation  they use Lanczos for the right halfplane, Stirling for the left away from the negative real axis, and reflection to avoid the unstable parts  actually, they use reflection to avoid Stirling altogether. It's a good implementation.
Edited: 25 Sept 2009, 12:19 p.m.
