The Museum of HP Calculators

HP Forum Archive 19

[ Return to Index | Top of Index ]

Complex Gamma Function... revisited
Message #1 Posted by Angel Martin on 14 Sept 2009, 2:54 p.m.

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 HP-41Z 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"
CF 00
X<0?
SF 00
X<0?
ZNEG
ZREPL
ZGSUM
LASTZ
ZGLNC
Z*
Z<>W
ZGPROD
Z/
FC?C 00
GTO 00
ZINV
Z<>W
ZGNZG
Z*
LBL 00
ZAVIEW
END

      
Re: Complex Gamma Function... revisited
Message #2 Posted by hugh steers on 14 Sept 2009, 5:03 p.m.,
in response to message #1 by Angel Martin

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
Message #3 Posted by Marcus von Cube, Germany on 15 Sept 2009, 1:40 p.m.,
in response to message #2 by hugh steers

Go and ask Viktor T. Toth at rskey.org! He's dedicated part of his life to Gamma.

                  
Re: Complex Gamma Function... revisited
Message #4 Posted by Namir on 15 Sept 2009, 2:22 p.m.,
in response to message #3 by Marcus von Cube, Germany

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
Message #5 Posted by Marcus von Cube, Germany on 15 Sept 2009, 4:29 p.m.,
in response to message #4 by Namir

Namir, that depends on the calculator. On machines with decent complex support (like the TI-92) he calculates the complex Gamma function.

His general article about Gamma is worth reading.

                              
Re: Complex Gamma Function... revisited
Message #6 Posted by Angel Martin on 15 Sept 2009, 4:50 p.m.,
in response to message #5 by Marcus von Cube, Germany

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)
ZGPROD is thhe product of (z+k)
ZGLNC calculates the trascendent part of the formula, and
ZGNZG corrects the expression for Re(z)<0

Cheers,

                              
Re: Complex Gamma Function... revisited
Message #7 Posted by Namir on 16 Sept 2009, 10:31 a.m.,
in response to message #5 by Marcus von Cube, Germany

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
Message #8 Posted by Les Wright on 16 Sept 2009, 2:27 a.m.,
in response to message #2 by hugh steers

Hugh, I have done a lot of fiddling with Spouge, and the cancellation and digit loss issues persist there--only worse. You need a LOT of guard digits to preserve accuracy. I understand you double precision BCD-20 type has gone a long way to help here.

            
Re: Complex Gamma Function... revisited
Message #9 Posted by Steven Thomas Smith on 25 Sept 2009, 12:16 p.m.,
in response to message #2 by hugh steers

See GSL's complex Gamma implementation -- they use Lanczos for the right half-plane, 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.

      
Re: Complex Gamma Function... revisited
Message #10 Posted by Egan Ford on 15 Sept 2009, 4:06 p.m.,
in response to message #1 by Angel Martin

Nice. How about a complex LogGamma and a complex Lambert W?

            
Re: Complex Gamma Function... revisited
Message #11 Posted by hugh steers on 15 Sept 2009, 4:15 p.m.,
in response to message #10 by Egan Ford

You kind-of 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
Message #12 Posted by Angel Martin on 15 Sept 2009, 4:36 p.m.,
in response to message #10 by Egan Ford

Here's where I'm going to show my utter lack of math finesse but what the heck:

LBL "LNZG"
ZG
ZLN
END

Of course this is just a teaser, will look into a proper way during the week-end :)

Cheers,

'AM

            
Re: Complex Gamma Function... revisited
Message #13 Posted by Paul Dale on 16 Sept 2009, 12:31 a.m.,
in response to message #10 by Egan Ford

Quote:
Nice. How about a complex LogGamma and a complex Lambert W?

Wikipedia has an algorithm for Lambert's W function that seems okay and converges in the complex plane: here. This is the one I'm using in the 20b scientific firmware. Start with the approximation and then iterate to the solution - I've limited this to 20 times through.

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.

- Pauli

            
Re: Complex Gamma Function... revisited
Message #14 Posted by Ángel Martin on 22 Sept 2009, 8:51 a.m.,
in response to message #10 by Egan Ford

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 E-9), 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..(n-1)]

Method-2 is much faster, more accurate and takes less program memory... can you ask for more? Yes, an MCODE version will be implemented on the 41Z during the next few weeks :)

Enjoy, ÁM.

LBL "ZLNG" LBL "ZLNG2" 1 7 STO 02 + RDN ZST0 (00) ZSTO (00) 6 XEQ 05 CHS LBL 00 Z^X ZENTER^ 810 XEQ 05 ST/ Z Z+ / Z=WR? ZRCL (00) GTO 02 ZINV GTO 00 ZSINH LBL 02 ZRCL (00) ZRCL (00) Z* ZLN Z+ Z- ZLN ZRCL (00) ZRCL (00) 0,5772156649 ZLN ST* Z ZDBL * Z+ Z- 2 ZAVIEW - RTN ZRCL (00) LBL 05 Z* ZRCL (00) ZRCL (00) RCL 02 ZLN ST/ Z Z- / PI ZENTER^ ST+ X 1 LN + + ZLN ZHALF Z- ZRCL (00) 1 7 ST+ 02 - RDN ZGPROD END ZLN Z- ZAVIEW END

Edited: 22 Sept 2009, 9:16 a.m.


[ Return to Index | Top of Index ]

Go back to the main exhibit hall