Digamma Function for the free42
|
04-22-2022, 02:20 PM
Post: #1
|
|||
|
|||
Digamma Function for the free42
The next program uses the formula provided by Albert Chan here. The program shifts the argument value 35 units to improve precision and then by the recurrence formula ψ(x+1)=ψ(x)+1/x calculates the desired value. The values I obtained seem to be accurate in the first 15 digits. It would be interesting if some one can try it in the original hp 42s.
PHP Code: 00 { 73-Byte Prgm } |
|||
04-23-2022, 02:21 AM
Post: #2
|
|||
|
|||
RE: Digamma Function for the free42
(08-28-2020 09:26 PM)Albert Chan Wrote: \(\qquad\qquad\exp( \psi(x+1/2)) = x "Last" constant may be adjusted to fit better. 74381/689976 * 3.7 ≈ .3989 >>> from mpmath import * >>> mp.dps = 34 >>> def psi0(x): x-=mpf(.5); return log(x + 1/(24*x + 37/(10*x+3986/(1000*x)))) ... >>> for x in range(31, 40): print '%d\t%+g' % (x, psi(0,x) - psi0(x)) ... 31 -5.06859e-16 32 -2.81682e-16 33 -1.30994e-16 34 -3.12469e-17 35 +3.35827e-17 36 +7.44676e-17 37 +9.89633e-17 38 +1.12294e-16 39 +1.18083e-16 Sweet spot is to get ψ(x+33), then adjust back down, ψ(x) = ψ(x+1) - 1/x Code: 00 { 58-Byte Prgm } For euler_gamma constant, it matched 16 digits 1 XEQ "Psi" +/- → 0.5772156649015328293596189489309783 |
|||
04-23-2022, 05:06 PM
Post: #3
|
|||
|
|||
RE: Digamma Function for the free42
Thanks Albert for taking some time looking at my post, your program is nice and short.
|
|||
04-23-2022, 05:56 PM
Post: #4
|
|||
|
|||
RE: Digamma Function for the free42
Previous Psi code seems unable to cope with complex argument.
Storing complex numbers in memory 11 cause a "Invalid Type" error. (why ?) This version do it all in the stack. Code: 00 { 60-Byte Prgm } Example, Ψ(1+i) 1+i XEQ "Psi" → 0.094650320622476991469435654672848+1.076674047468581096181134287806413i Argument with negative real part, we can use reflection formula \( ψ(z) = ψ(1-z) - \pi \cot(\pi\,z) \) |
|||
04-23-2022, 06:07 PM
Post: #5
|
|||
|
|||
RE: Digamma Function for the free42
(04-23-2022 05:56 PM)Albert Chan Wrote: Storing complex numbers in memory 11 cause a "Invalid Type" error. (why ?) The numbered registers are cells in a matrix named REGS. If REGS is a real matrix, the numbered registers can hold real numbers or strings, and if REGS is a complex matrix, the numbered registers can contain complex numbers (and only complex numbers!). This is all HP-42S behavior. I have an option on my to-do list to allow using a list for REGS, which would mean you could store values of any type. No ETA on that feature yet, though. |
|||
04-24-2022, 04:46 PM
Post: #6
|
|||
|
|||
RE: Digamma Function for the free42
Thanks Albert, the new improvement loos neat, I didn’t even consider complex numbers, I used the register 11 just in case someone wanted to try it in the hp 42s, I was using a local variable (LSTO “x”) in my free42 and also FUNC 11 at the start of the program so the program behaves like a build in function..
I always thought the hp 50g should have a list that acts like the registers in the RPN calculators with the arithmetic functions, that implementation in the free42 would be awesome. |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)