This is an archived, read-only copy of the United-TI subforum , including posts and topic from May 2003 to April 2012. If you would like to discuss any of the topics in this forum, you can visit Cemetech's TI-BASIC subforum. Some of these topics may also be directly-linked to active Cemetech topics. If you are a Cemetech member with a linked United-TI account, you can link United-TI topics here with your current Cemetech topics.

This forum is locked: you cannot post, reply to, or edit topics. TI-Basic => TI-BASIC
United-TI Archives -> TI-Basic
 
    » Goto page 1, 2  Next
» View previous topic :: View next topic  
Author Message
thornahawk
μολών λαβέ


Active Member


Joined: 27 Mar 2005
Posts: 569

Posted: 07 Dec 2005 11:50:38 pm    Post subject:

I still need to write the documentation for this, so I thought some of you might want to test this for the time being:


Code:
PROGRAM:GAMMA
:dim(∟GAMMA)–2→H
:If 2X=int(2X) and real(X)≥-.5 and imag(X)=0
:Then
:(X–1)!→Y
:Else
:X→Z
:If real(X)<0
:1–Z→Z
:ln(√(2π)sum(∟GAMMA/augment({1},Z+seq(I,I,0,H))))+(Z–.5)ln(Z+H–.5)–Z–H+.5→Y
:e^(Y)→Y
:If real(X)<0
:2πi/Y/(e^(πiX)–e^(-πiX))→Y
:End
:If imag(Y)=0
:real(Y)→Y
:Y


where


Code:
{1, 5716.4001882743, -14815.304267684, 14291.492776575, -6348.1602176415, 1301.6082860583, -108.17670535144, 2.6056965056118, -7.4234525102010 × 10^-3, 5.3841364325096 × 10^-8, -4.0235331412682 × 10^-9}→∟GAMMA


This computes the gamma function (generalized factorial) for complex arguments, using the Lanczos approximation (see here for the mathematics behind.). Usage would be something like


Code:
.5+i→X:prgmGAMMA


with the result stored in Y. It will return real output for real input (unless you give it a nonnegative integer, which is not a valid argument for the function). A few cursory tests against the results generated by Mathematica shows that the program achieves full precision in the result.

To spare the interested from having to type all of these out, I'm attaching the program and the associated list. As soon as I finish the documentation, I'm uploading this to the program archive. :)

A note on the implementation: I chose to store all the coefficients in an explicit list such that they can be replaced with a different set of coefficients if less (or more) precision is desired. The table given in the above link gives other coefficient sets and a method to compute those coefficients (which, BTW, cannot be done on the calculator since this needs really high precision).

thornahawk


Last edited by Guest on 08 Dec 2005 12:00:22 am; edited 1 time in total
Back to top
Weregoose
Authentic INTJ


Super Elite (Last Title)


Joined: 25 Nov 2004
Posts: 3976

Posted: 08 Dec 2005 12:04:53 am    Post subject:

I'm getting increment errors for any input for X not having a fraction part divisible by .5 or having an imaginary
part, and I'm also getting data type errors for lists. Some elaborate documentation would indeed be welcome.

Also, if I may: [font="courier new"]fnInt(T^(X-1)e^(‾T),T,0,E2


My guess is that you are familiar with this method, but I
wonder if it can be extrapolated to benefit your results?
Back to top
thornahawk
μολών λαβέ


Active Member


Joined: 27 Mar 2005
Posts: 569

Posted: 08 Dec 2005 12:12:28 am    Post subject:

Goose, you may have mistyped. Download the attachment and see if that works. I've done (slightly) rigorous tests for random values in the complex plane, and I've no errors so far...

I'm familiar with your one-liner because 1) that's the defining integral due to Euler, and 2) it's in your site. Smile No, the Lanczos approximation and the integral approximation are really different beasts, so I can't say anything about generalizing the integral, only that negative reals and complexes will not succumb to the treatment since fnInt( accepts only real inputs, and the integral is only valid for complex numbers with positive real part.

thornahawk
Back to top
Weregoose
Authentic INTJ


Super Elite (Last Title)


Joined: 25 Nov 2004
Posts: 3976

Posted: 08 Dec 2005 12:35:13 am    Post subject:

I downloaded the program, but ran into some errors again. The first one was because [font="courier new"]LGAMMA was not defined. I thought to correct this by storing [font="courier new"]{1} to that list, but then the aforementioned errors regarding [font="courier new"]X showed back up, and imaginary numbers failed. Storing a longer list, [font="courier new"]{2,5,‾5}, allowed the other decimal inputs for [font="courier new"]X to go through, but the results were inaccurate. I'm almost sure this is only because I don't understand the connection between the two types of variables and their usage in this program.

If it works for you, superb! My hope is just that the readme will reveal the understanding of how to make it work. :|

On the other hand, inserting imaginary numbers into the Gamma function is still completely news to me. Very Happy
Back to top
thornahawk
μολών λαβέ


Active Member


Joined: 27 Mar 2005
Posts: 569

Posted: 08 Dec 2005 01:12:49 am    Post subject:

Uh-oh... you need the list to be in the calculator as well. So that's why you were getting erroneous results. Surprised The GAMMA list and the program are both in the attached archive, and should not be separated Wink. The GAMMA list included is the coefficient set I found to be most appropriate for TI calculators.

thornahawk
Back to top
Weregoose
Authentic INTJ


Super Elite (Last Title)


Joined: 25 Nov 2004
Posts: 3976

Posted: 08 Dec 2005 02:13:56 am    Post subject:

I see! I'm glad I read that article explaining the algorithm; it was a real eye-opener. Translating from the design of the Lanczos approximation, your program implements it quite nicely, and so there is no doubt that it works how it's supposed to. I'm starting to like this "hunt-and-kill" technique for developing math programs... I may have to join in the fun. Smile
Back to top
thornahawk
μολών λαβέ


Active Member


Joined: 27 Mar 2005
Posts: 569

Posted: 08 Dec 2005 03:17:31 am    Post subject:

Let's just say I spend at most three weeks in research compared to at most four days in writing a math program. All that effort to make sure it works for all reasonable inputs. :D

thornahawk

P.S. For the interested, it's pronounced LAN-tsosh. Maybe the Hungarians here can confirm? Wink


Last edited by Guest on 08 Dec 2005 10:21:17 am; edited 1 time in total
Back to top
Ray Kremer


Member


Joined: 16 Feb 2004
Posts: 237

Posted: 12 Dec 2005 12:40:38 pm    Post subject:

If you just want a gamma function (as opposed to wanting the experience of writing the program yourself):
http://www.technicalc.org/tiplist/en/files...tips/tip6_4.pdf
Back to top
thornahawk
μολών λαβέ


Active Member


Joined: 27 Mar 2005
Posts: 569

Posted: 16 Dec 2005 03:29:49 am    Post subject:

I must comment that the program Ray was pointing to uses the shifted Stirling approximation. Pretty accurate for large arguments, but not the best for small arguments. For about the same cost of storage, my professional opinion is that Lanczos has slightly higher accuracy than Stirling.

thornahawk
Back to top
Weregoose
Authentic INTJ


Super Elite (Last Title)


Joined: 25 Nov 2004
Posts: 3976

Posted: 16 Jun 2006 06:48:34 am    Post subject:

[font="courier new;font-size:9pt;line-height:100%;color:darkblue"](Ans√(Ans(e^(Ans‾¹)-e^(‾Ans‾¹))/2+Ans^‾6/810)/e)^Ans√(2[font="times new roman"]π/Ans

This is based on an approximation found by Robert H. Windschitl, and noted on Wikipedia's Stirling's approximation page. It accepts all numbers (complex and negative included) and works at a very decent pace. Inputs closer to [font="courier new;font-size:9pt;line-height:100%;color:darkblue"]0 produce poor results, but as the absolute values of both the real and imaginary parts go up past [font="courier new;font-size:9pt;line-height:100%;color:darkblue"]2.5, the numbers start to become more reliable. After that, it continues to behave better than Stirling's approach, or at least within the range of this calculator.

Feel free to compare answers with Google's calculator.

Program:

[font="courier new;font-size:9pt;line-height:100%;color:darkblue"]4.738+2[font="times new roman"]πi:prgmGAMMA

‾.1639600232-.381098101[font="times new roman"]i

Google [Search]:

(4.738+2 pi i-1)!

-0.163960023 - 0.381098101 i

Just remember to subtract one like I did before factorializing in order to mimic the Gamma function.

By the way, was it DarkerLine or Brazucs? Whoever it was, nice job on the tag update. :biggrin:
Back to top
thornahawk
μολών λαβέ


Active Member


Joined: 27 Mar 2005
Posts: 569

Posted: 29 Oct 2007 11:55:27 am    Post subject:

(repost of a reply made on June 2006, plus some additionals)

Windschitl's approximation was first mentioned in this page along with his inspired derivation for the approximation, FYI. Wink However, there's a sinh() function available in the TI 83+ that could be used if one is to restrict him/herself to real inputs. Wink Otherwise, it's fine the way Goose wrote it. :D

At any rate, the fact that this approximation is inaccurate for small arguments means that one might want to use the recursion Γ(x+1) == x*Γ(x) for a certain number of times before applying Windschitl's approximation. Wink I have yet to experiment with the optimum number of recursions needed.

The Wolfram Functions Site provides for an online comparison as well:

http://functions.wolfram.com/webMathematic...i%20I&digits=10

which returns -0.1639600231 - 0.3810981009*i, close to the mark. ;)

(added 10/2007:

I came across this page by Peter Luschny showcasing a wide variety of simple approximations to the gamma function whenever only a few significant digits are required. One particularly striking approximation (at least to me) was the set of formulae found by Gergö Nemes; in particular, the formula referred to as "nemesgam(n)" is particularly compact and slightly more accurate than Windschitl's formula for real arguments.

I have yet to study the behavior of these approximations in the complex plane, and due to the fact that I am currently far away from computers that have Mathematica installed, I am not sure if the ~7 significant figure accuracy of "nemesgam(n)" (and more for large arguments; a bit like Stirling's approximation) carry over to the complex plane. Maybe somebody else interested in this stuff can look into it. ;)

)

thornahawk
Back to top
Weregoose
Authentic INTJ


Super Elite (Last Title)


Joined: 25 Nov 2004
Posts: 3976

Posted: 29 Oct 2007 09:46:19 pm    Post subject:

Some test cases using Γ(Z) = √(2[font="times new roman"]π/Z)Z‾¹[font="times new roman"]×√([font="times new roman"]e‾¹(Z+1/(12Z-.1/Z...

[font="courier new"]Input: 8 - 5*i
Expected: -588.88799388302 + 881.10762099358*i
Returned: -588.8879959986 + 881.10762996475*i
Error: -2.11558×10^-6 - 8.97117×10^-6*i

Input: 20 + 50*i
Expected: -0.40679637241150 + 0.11115264756461*i
Returned: -0.40679637241044 + 0.11115264756141*i
Error: -1.06004×10^-12 + 3.20001×10^-12*i

Input: 20*i - 50
Expected: -1.9742544936476×10^-90 + 4.6716195712878×10^-90*i
Returned: -1.9742544936159×10^-90 + 4.6716195712363×10^-90*i
Error: -3.17×10^-101 + 5.15004×10^-101*i

Input: 0.63 + 0.07*i
Expected: 1.40552627214996 - 0.14104383585673*i
Returned: 1.4027091907892 - 0.13946560251071*i
Error: 2.81708×10^-3 - 1.57823×10^-3*i

Input: 0.07*i - 0.63
Expected: -3.7034012672553 + 0.3071363819433*i
Returned: -5.205100133524 + 2.1813798323048*i
Error: 1.5017×10^0 - 1.87424×10^0*i

That was nemesgam(n). Post with any other fringe parameters you'd like to see tested.

Last edited by Guest on 30 Jul 2010 05:30:31 am; edited 1 time in total
Back to top
thornahawk
μολών λαβέ


Active Member


Joined: 27 Mar 2005
Posts: 569

Posted: 30 Oct 2007 11:40:11 am    Post subject:

Hmm, looks good. Very Happy As it stands, it, like most other simple approximations, does not do well for small magnitudes, but performs better for large magnitudes. What I think would be most illuminating would be a three-dimensional plot of |(Γ(x+iy)-nemesgam(x+iy-1)/Γ(x+iy)| for a variety of appropriate ranges. ;)

Anyway, for those who felt too lazy to stare at the web page I gave earlier, here is the formula Goose and I were looking at:



thornahawk
Back to top
DarkerLine
ceci n'est pas une |


Super Elite (Last Title)


Joined: 04 Nov 2003
Posts: 8328

Posted: 30 Oct 2007 01:30:19 pm    Post subject:

My skills at illuminating plots in Mathematica clearly leave much to be desired. Nevertheless, here are two plots: one with the default range and box parameters, the other my best attempt at a closeup of the weird stuff near the origin.
Back to top
Weregoose
Authentic INTJ


Super Elite (Last Title)


Joined: 25 Nov 2004
Posts: 3976

Posted: 29 Jul 2008 11:48:46 am    Post subject:

I went at it again, and I must say that I am very happy this time with the results. The new program I made handles real-numbered inputs, both positive and negative, large and small, and returns answers quickly and with brilliant accuracy – the worst case has 8 correct digits; an input of n or greater, where Γ(n) = 100, suffices to get 12. In terms of its alacrity and precision, this is now virtually as good as just another function on the calculator! :lol:

See the Gamma Function here.


Last edited by Guest on 29 Jul 2008 11:58:06 am; edited 1 time in total
Back to top
spandiv
-- Retired --


Active Member


Joined: 25 May 2003
Posts: 650

Posted: 29 Jul 2008 12:09:31 pm    Post subject:

That looks pretty nice and complicated Very Happy In any case, I noticed you can simplify

:If 2≤abs(A
:Then
:sum(12‾¹/{Ans,‾30Ans³,105Ans^5,‾140Ans^7,99Ans^9
:Else
:2fnInt(tan‾¹(T/Ans)/(e^(2πT)-1),T,0,4
:End


to

:sum(12‾¹/{Ans,‾30Ans³,105Ans^5,‾140Ans^7,99Ans^9
:If 2>abs(A
:2fnInt(tan‾¹(T/Cool/(e^(2πT)-1),T,0,4

unless there is a reason for keeping the If-Then conditional.


Last edited by Guest on 30 Jul 2010 05:30:53 am; edited 1 time in total
Back to top
DarkerLine
ceci n'est pas une |


Super Elite (Last Title)


Joined: 04 Nov 2003
Posts: 8328

Posted: 29 Jul 2008 12:16:29 pm    Post subject:

Well, there's the obvious speed reason - you don't want to do both calculations when you can only do one.
Back to top
Weregoose
Authentic INTJ


Super Elite (Last Title)


Joined: 25 Nov 2004
Posts: 3976

Posted: 29 Jul 2008 01:38:30 pm    Post subject:

In fact, the entire program could be reduced to just the integral and the operations following it – speed was definitely the target here.
Back to top
Weregoose
Authentic INTJ


Super Elite (Last Title)


Joined: 25 Nov 2004
Posts: 3976

Posted: 30 Jul 2008 12:01:33 am    Post subject:

To compute Γ‾¹(x) for positive reals greater than one:

‾1+solve(Ans-([font="arial"]√(2[font="times new roman"]π
)X^(X-.5)/(X-1)[font="times new roman"]e^(12‾¹/X-360‾¹/X[font="verdana"]³+
1260‾¹/X^5-1680‾¹/X^7+1188‾¹/X^9-X)),X,[font="verdana"]E9,{2,[font="verdana"]E99

This takes less than four seconds on the TI-84+SE.

As an experiment:

[font="times new roman"]π:prgmINVGAMMA
_____3.448618111
prgmGAMMA
_____3.141592654
prgmINVGAMMA
_____3.448618111
prgmGAMMA
_____3.141592654
Ans-[font="times new roman"]π
_______________0

Note that it works equally well across the board.

This will be added to my site, but I wonder whether "inverse gamma" is an appropriate name for it, as there is the distinct but similarly-titled Inverse-gamma distribution.

Last edited by Guest on 30 Jul 2010 05:27:16 am; edited 1 time in total
Back to top
DarkerLine
ceci n'est pas une |


Super Elite (Last Title)


Joined: 04 Nov 2003
Posts: 8328

Posted: 30 Jul 2008 09:32:10 am    Post subject:

If you wanted to, you could call it arcΓ.
Back to top
Display posts from previous:   
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
    » Goto page 1, 2  Next
» View previous topic :: View next topic  
Page 1 of 2 » All times are UTC - 5 Hours

 

Advertisement