The following warnings occurred:
Warning [2] count(): Parameter must be an array or an object that implements Countable - Line: 795 - File: showthread.php PHP 7.4.33 (FreeBSD)
File Line Function
/showthread.php 795 errorHandler->error





Post Reply 
(DM42) Gamma function for complex values
04-03-2023, 08:57 AM (This post was last modified: 04-03-2023 04:12 PM by Gjermund Skailand.)
Post: #1
(DM42) Gamma function for complex values
Gamma function for complex values s = x +iy for DM42, Free 42
Motivation: The GAMMA function only accept real values.
Uses Bernoulli coefficients, requires the program B2n, see earlier post.
and reflection formula for x< 0.5
Accurate to 30 digits, (32 for "small" imaginary values).
For significant speed increase, pre-calculate and recall the Bernoulli coefficients (line 35).
If, say only 10-12 digits is needed, then reduce N from 42 to 15 (line 42),.
Compare accuracy with e.g. gamma function at http://www.wolframalpha.com
raw file included

Code:

00 { 198-Byte Prgm } @ skai 2023-04-02
01▸LBL "GamZ"         @ Calculates gamma function for complex input.
02 FUNC 11                @ Calculates about 30 correctly rounded digits,there is some cancellation
03 L4STK                   @ based on Bernoully coffecients, see program B2n
04 REAL?
05 GAMMA                 @ switches to GAMMA real values
06 REAL?     
07 RTN
08 4
09 1
10 NEWMAT
11 ENTER
12 COMPLEX
13 LSTO "REGS"
14 R↓
15 RAD
16 ENTER
17 COMPLEX
18 X<>Y
19 0.5            @ if RE(s) < 0.5 uses reflection formula
20 X≤Y?
21 GTO 00
22 STO+ ST X
23 -
24 ENTER
25 IP
26 X=Y?
27 GAMMA     @ force error exit. RTNERR 5  "Invalid Data" is bugged when called from NSTK mode
28 R↓
29 X<>Y
30 COMPLEX
31 +/-
32 PI
33 ENTER
34 ENTER
35 RCL× ST T
36 SIN
37 ÷
38▸LBL 00
39 LSTO "r"
40 R^
41 STO 00
42 42         @ 21 if only about 16 digits is needed, for real values s = x+i0 can compare with GAMMA
43 LSTO "N"
44 2
45 ÷
46 XEQ "B2n"  @ optionally pre-compute and recall for siginificant speed increase  
47 RCL 00
48 R^
49 +
50 STO 01
51 STO 03
52 X^2
53 STO 02
54 X<>Y
55 EDIT
56 CLST
57▸LBL 01
58 ISG ST Y
59 NOP
60 RCLEL
61 RCL ST Z
62 STO+ ST X
63 DSE ST X
64 RCL× ST T
65 STO+ ST X
66 RCL× 03
67 ÷
68 +
69 RCL 02
70 STO× 03
71 R↓
72 J+
73 FC? 77
74 GTO 01
75 RCLEL
76 EXITALL
77 R↓
78 E^X
79 RCL× 03
80 RCL÷ 01
81 RCL 00
82 RCL "N"
83 DSE ST X
84 RCL ST Z
85▸LBL 02
86 RCL ST Y
87 RCL+ ST T
88 ÷
89 DSE ST Y
90 GTO 02
91 RCL÷ ST Z
92 RCL 01
93 +/-
94 E^X
95 ×
96 RCL 01
97 RCL 00
98 0.5
99 -
100 Y^X
101 ×
102 PI
103 STO+ ST X
104 SQRT
105 ×
106 RCL "r"
107 STO ST Z
108 CPX?
109 CLX
110 R↓
111 0=? ST T
112 ÷
113 END

inspired by JM Baillard, https://hp41programs.yolasite.com
Thanks to Joe Horn for small correction.
best regards
Gjermund


Attached File(s)
.zip  GAMz.zip (Size: 1.37 KB / Downloads: 3)
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
(DM42) Gamma function for complex values - Gjermund Skailand - 04-03-2023 08:57 AM



User(s) browsing this thread: 1 Guest(s)