Riemann's Zeta Function - another approach (RPL)
|
06-28-2017, 07:45 PM
(This post was last modified: 06-28-2017 07:47 PM by Gerson W. Barbosa.)
Post: #21
|
|||
|
|||
RE: Riemann's Zeta Function - another approach (RPL)
Three lines shorter:
Code:
\[\zeta(x)=\frac{2\cdot (-x)!\cdot \sin \left ( x\cdot \sin^{-1} 1 \right )\zeta (1-x)}{(2 \pi )^{1-x}}\] |
|||
06-29-2017, 07:47 PM
(This post was last modified: 06-30-2017 06:25 AM by Dieter.)
Post: #22
|
|||
|
|||
RE: Riemann's Zeta Function - another approach (RPL)
(06-28-2017 07:45 PM)Gerson W. Barbosa Wrote: Three lines shorter: Here is my first attempt at a HP41 version for x > 1. Maybe this or that can be reused in your 15C program, e.g. the use of less registers (the 15C would require R0...R2) or the DSE within the loop. Code: 01 LBL"ZETA" Edit: corrected an error in line 47 Your method for calculating the number of required terms of the sum is fine, actually I got the same result. However the program uses a shorter formula that yields very similar results. The final calculation with this power-of-2-fraction can cause significant errors for x close to 1, so I chose a different approach with the 41's e^x–1 function which should reduce the error here. The simplified Zeta evaluation is used for 1 < x ≤ 1,01. At x=1,01 both methods agree and return the correct result 100,5779433. The examples in your first post (except the two cases that cannot be handled by the program above) are all evaluated to ten correct digits. But of course the program is not always that accurate. ;-) Dieter |
|||
06-30-2017, 02:20 AM
(This post was last modified: 06-30-2017 02:27 AM by Gerson W. Barbosa.)
Post: #23
|
|||
|
|||
RE: Riemann's Zeta Function - another approach (RPL)
(06-29-2017 07:47 PM)Dieter Wrote: Here is my first attempt at a HP41 version for x > 1. Maybe this or that can be reused in your 15C program, e.g. the use of less registers (the 15C would require R0...R2) or the DSE within the loop. Both your method and mine causes the series to be calculated to more terms than are really needed. For instance, Zeta(2) requires only 22 terms, but we evaluate 28 and 30 terms, respectively. A closer fit would require a longer formula, so yours presents a good compromise between size and exactness. 1.2 40 2 22 3 18 4 16 5 12 6 10 7 8 8 8 9 6 10 6 ... 12 6 13 4 ... 20 4 21 2 ... 40 2 For the 15C I gave two programs, A and B. If there's no need for the analytical continuation then B should be ignored. But A should be fast enough for x >= 1/2 if it is supposed to be called by B. Your HP-41 programs computes Zeta(1/2) in about one minute, which is pretty good. There is a typo in your line 47: RCL 00, not 01. Thank you for providing a 41 version. I will try it on the 42S. Gerson. |
|||
06-30-2017, 06:42 AM
Post: #24
|
|||
|
|||
RE: Riemann's Zeta Function - another approach (RPL)
(06-30-2017 02:20 AM)Gerson W. Barbosa Wrote: Both your method and mine causes the series to be calculated to more terms than are really needed. For instance, Zeta(2) requires only 22 terms, but we evaluate 28 and 30 terms, respectively. A closer fit would require a longer formula, so yours presents a good compromise between size and exactness. Yes, especially between 1 and 2,5 the calculated number of terms is higher than required. BTW, how did you determine the exact numbers here? (06-30-2017 02:20 AM)Gerson W. Barbosa Wrote: There is a typo in your line 47: RCL 00, not 01. Yes, thank you. This has been corrected now. Dieter |
|||
06-30-2017, 11:06 AM
(This post was last modified: 06-30-2017 11:18 AM by Gerson W. Barbosa.)
Post: #25
|
|||
|
|||
RE: Riemann's Zeta Function - another approach (RPL)
(06-30-2017 06:42 AM)Dieter Wrote:(06-30-2017 02:20 AM)Gerson W. Barbosa Wrote: Both your method and mine causes the series to be calculated to more terms than are really needed. For instance, Zeta(2) requires only 22 terms, but we evaluate 28 and 30 terms, respectively. A closer fit would require a longer formula, so yours presents a good compromise between size and exactness. I tested one by one with ever higher n until I got all 10 significant digits right. I did this on the 15C LE which is 150x faster than the original one. I could have used the HP-15C iOS app as it is even faster: 0.0001 GSB A -> -0.5000919(117) (8 s, 95272 terms) Do you intend to write a full range HP-41 version? I'll be traveling the next few days and I won't take any 41 along. Thanks again for your great contributions, especially for your striving to always get as many significant digits as possible. Gerson. |
|||
06-30-2017, 12:08 PM
Post: #26
|
|||
|
|||
RE: Riemann's Zeta Function - another approach (RPL)
How does this compare to Jean-Marc Baillard's implementation of Borwein's second algorithm?
The 34S uses this algorithm. Originally in C but later in XROM. Borwein's paper includes an error term which means that for real arguments, the number of terms for a specified precision is constant & can be determined in advance. This isn't true for complex numbers, where the number of terms depends on the magnitude of the complex part. Pauli |
|||
06-30-2017, 02:09 PM
(This post was last modified: 06-30-2017 02:38 PM by Gerson W. Barbosa.)
Post: #27
|
|||
|
|||
RE: Riemann's Zeta Function - another approach (RPL)
(06-30-2017 12:08 PM)Paul Dale Wrote: How does this compare to Jean-Marc Baillard's implementation of Borwein's second algorithm? Quoting from your first link: Quote: 3 XEQ "ZETA" >>>> Zeta(3) = 1.202056903 ---Execution time = 21s--- 3 XEQ "ZETA" -> 1.202056903 (15 s) [HP-41CV] -7.49 GSB B -> 0.003312040168 (26 s) [HP-15C] (probably 11 seconds on the HP-41CV) 1.1 XEQ "ZETA" -> 10.58444846 (34 s) [HP-41CV] This relies on an empirical correction expression I've found, though: 1/(2*((n + 1/2 + (s + 1)/(8*n) - (2*s + 1)/(24(?)*n^2) + ... )^x)) But I am still not sure wheter the last term is correct or if this is correct at all... Gerson. PS: Perhaps Borwein's algorithm is overkill for the HP-41. If more digits are to be calculated, as on the wp34s, then is should be faster, even if more terms of the correction expression were available. |
|||
06-30-2017, 04:50 PM
(This post was last modified: 07-01-2017 09:30 AM by Dieter.)
Post: #28
|
|||
|
|||
RE: Riemann's Zeta Function - another approach (RPL)
(06-30-2017 11:06 AM)Gerson W. Barbosa Wrote: I tested one by one with ever higher n until I got all 10 significant digits right. LOL – I did the same with Excel (VBA) and 15-digit precision, with a threshold of 0,5 ULP compared to the (more or less) exact result with 200 terms. I now have slightly modified the estimate for the required number of terms, see below. (06-30-2017 11:06 AM)Gerson W. Barbosa Wrote: Do you intend to write a full range HP-41 version? I'll be traveling the next few days and I won't take any 41 along. It seems that this would require some kind of Gamma routine, yes? (06-30-2017 11:06 AM)Gerson W. Barbosa Wrote: Thanks again for your great contributions, especially for your striving to always get as many significant digits as possible. While we're at it: I just looked at JMB's routines and found two interesting things: first, in the Borwein program he uses the same e^x–1 method as my own program, and the other version has is a nice trick for evaluating series' with alternating signs. Finally I now got a good approximation for x between 1 and 1,05 (!) which (sufficient precision provided) is within ±0,2 ULP of 10 digits: Zeta(x) ~ 1/u + u/(0,9246 · u + 13,733) + 0,577215654 where u = x–1 All this leads to the following new and improved version: Code: 01 LBL"ZETA" Again, this works for x > 1. Addendum for the 15C and others: The benefit of the e^x–1 method for the resulting accuracy can also be achieved in another way. 2x/(2x–2) – 1 = 1/(2x–1–1) Edit: not sure if this is a good idea for x close to 1 – see further below for an ex–1 implementation on the 15C. So the final multiplication with this power-of-2-fraction can also be coded like this (assuming –x is stored in R1, as in your 15C program): Code: ... Edit: Finally, here is a 15C version. Please also note the alternative solution with an ex–1 implementation a few posts below. Code: # -------------------------------------------- Hint: replaced the strange "+" and "x" characters generated by the emulator with more common ones. Hint2: the emulator's extended precision is great, but it seems to use binary arithmetics: enter x=1,05 and the x>y? test that compares x–1 with 0,05 tests true (!) and jumps to LBL 0. Dieter |
|||
06-30-2017, 11:23 PM
Post: #29
|
|||
|
|||
RE: Riemann's Zeta Function - another approach (RPL) | |||
07-01-2017, 08:30 AM
(This post was last modified: 07-01-2017 08:34 AM by Dieter.)
Post: #30
|
|||
|
|||
RE: Riemann's Zeta Function - another approach (RPL)
(06-30-2017 11:23 PM)Paul Dale Wrote: How about \( e^x - 1 = tanh\left(\frac{x}{2}\right) \left( e^x + 1 \right) \)? Ah, this reminds me of an earlier thread on ex–1. In this special case the function may be coded this way, cf. Gerson's post #19 in the mentioned thread: Code: .. ... ... Do not use ln √2 instead of ½ ln 2 here as on the 10 digit calculators this causes an error of several ULPs. Dieter |
|||
07-01-2017, 02:57 PM
(This post was last modified: 07-01-2017 02:59 PM by Gerson W. Barbosa.)
Post: #31
|
|||
|
|||
RE: Riemann's Zeta Function - another approach (RPL)
(06-30-2017 04:50 PM)Dieter Wrote:(06-30-2017 11:06 AM)Gerson W. Barbosa Wrote: Do you intend to write a full range HP-41 version? I'll be traveling the next few days and I won't take any 41 along. Oops! I forgot the HP-41 lacked Gamma. (06-30-2017 04:50 PM)Dieter Wrote: While we're at it: I just looked at JMB's routines and found two interesting things: first, in the Borwein program he uses the same e^x–1 method as my own program, and the other version has is a nice trick for evaluating series' with alternating signs. I chose to evaluate two terms per loop because it's faster. For example, your older program takes 65 seconds to compute Zeta(1/2), if your restriction is by-passed, while your new one takes 73 seconds. Instead of the first DSE, I tried SIGN ST+Y X<>L, but this would take 70 seconds, so I kept DSE. It's not a true DEC (decrement) instruction as it includes an implicit time consuming IF, but it's faster than what I'd been using in the 15C program. (06-30-2017 04:50 PM)Dieter Wrote: Finally I now got a good approximation for x between 1 and 1,05 (!) which (sufficient precision provided) is within ±0,2 ULP of 10 digits: I have used it in my new version. Not all your suggestions have been implemented yet. Code:
-1.5 GSB A -> -0.02548520190 [ 89] ( 47 s) -1 GSB A -> -0.08333333332 [ 3] ( 52 s) 0 GSB A -> -0.500000000 ( 1 s) 0.5 GSB A -> -1.460354507 [ 9] ( 144 s) 0.02 GSB A -> -0.5187882122 [ 14] ( 11 s) 0.94 GSB A -> -16.09383730 [ 2] ( 86 s) 0.959 GSB A -> -23.81602202 [181] ( 5 s) * 0.961 GSB A -> -25.06665734 [ 14] ( 5 s) 1.02 GSB A -> 50.57867004 ( 5 s) 1.04 GSB A -> 25.58012052 ( 5 s) 1.041 GSB A -> 24.97043684 [ 5] ( 5 s) 1.06 GSB A -> 17.24823369 [ 7] ( 83 s) 2 GSB A -> 1.644934067 ( 48 s) 3 GSB A -> 1.202056903 ( 36 s) 4 GSB A -> 1.082323234 ( 31 s) 10 GSB A -> 1.000994575 ( 17 s) The angle mode is irrelevant. * Somewhat off! Will see that later. Vacation time mode on :-) |
|||
07-01-2017, 05:09 PM
(This post was last modified: 07-01-2017 05:18 PM by Dieter.)
Post: #32
|
|||
|
|||
RE: Riemann's Zeta Function - another approach (RPL)
(07-01-2017 02:57 PM)Gerson W. Barbosa Wrote: Oops! I forgot the HP-41 lacked Gamma. That's an essential point here. (07-01-2017 02:57 PM)Gerson W. Barbosa Wrote: I chose to evaluate two terms per loop because it's faster. For example, your older program takes 65 seconds to compute Zeta(1/2), if your restriction is by-passed, while your new one takes 73 seconds. Yes, evaluating multiple terms per loop is faster, especially since the GTO and the required label search take some time. On the 41 this is less of an issue because of the "compiled GTO" feature. But anyway it still makes a small difference. (07-01-2017 02:57 PM)Gerson W. Barbosa Wrote: Instead of the first DSE, I tried SIGN ST+Y X<>L, but this would take 70 seconds, so I kept DSE. It's not a true DEC (decrement) instruction as it includes an implicit time consuming IF, but it's faster than what I'd been using in the 15C program. DSE usually is very fast, not only on the 41. Sure, the implicit test is not required, but all is done in a single command and without stack interference, so this is the preferred choice. For (integer) 2x–1 I often use ST+X DSE X. Code: 010 { 42 21 2 } f LBL 2 This can be recplaced by LBL 2 X>Y? GTO 4 Since LBL 3 is not used elsewhere it can be omitted. Code: 042 { 42 21 4 } f LBL 4 This tests whether x is in 0,95...1,05. My approximation is valid for 1...1,05. It hasn't been designed for x<1. Here the error is substantially larger, that's why your results for x=0,959 and 0,961 are a bit off in the last digits. At this point the error is about 20 ULP, and that's exactly what your results show. Code: 053 { 45 3 } RCL 3 First, the calculated number of terms has no added constant, so for x>50,7 it will evaluate to zero. Then, why do you first pust R3 onto the stack and then pop it back later? 2 5 RCL 3 , 8 2 y^x / INT 1 + ENTER + STO I STO 2 RCL 3 CHS STO 1 Also another register for the summed terms is not required, you can simply do it on the stack. ;-) (07-01-2017 02:57 PM)Gerson W. Barbosa Wrote: * Somewhat off! Will see that later. cf. my comment above. (07-01-2017 02:57 PM)Gerson W. Barbosa Wrote: Vacation time mode on :-) In this case: have a good time. :-) Dieter |
|||
07-02-2017, 12:52 PM
(This post was last modified: 07-02-2017 12:53 PM by Gerson W. Barbosa.)
Post: #33
|
|||
|
|||
RE: Riemann's Zeta Function - another approach (RPL)
Thank you very much, Dieter, for kind of doing my homework:-)
l'm looking foward for other implementations which might demonstrate whether this approach is worthwhile or not. I wasn't aware of J. M. Baillard's new programs, else probably I would't even have tried all this. Gerson |
|||
07-02-2017, 02:54 PM
(This post was last modified: 07-02-2017 05:02 PM by Dieter.)
Post: #34
|
|||
|
|||
RE: Riemann's Zeta Function - another approach (RPL)
(07-02-2017 12:52 PM)Gerson W. Barbosa Wrote: Thank you very much, Dieter, for kind of doing my homework:-) I also did mine, and so here is a new HP41 program. First, there is a new approximation for 0,97≤x≤1,03 with an error of approx. ±0,5 units in the 10th significant digit. Then I realized that for 0,3454<x<0,97 the program may use a constant number of iterations and the results show a relatively constant error (only a few ULP) that can be compensated by a heuristc formula. Here I chose 54 terms, and the result is corrected by an amount of approx. 5 E–9/x¼. This keeps the error within about ±0,6 ULP. The lower limit for x is the point where Zeta(x) is exactly –1. Beyond this the accuracy will substantially degrade, so x=0,3453726573 is the limit here. Below this the program throws a DATA ERROR. So here is the latest version: Code: 01 LBL "ZETA" Examples: Code: 0,35 XEQ"ZETA" => -1,010511224 exact 37 s Finally Zeta(0,3453726573) returns exactly –1. The given execution times are due to V41 and a speed setting that quite exactly matches the real thing. Dieter |
|||
07-02-2017, 11:53 PM
Post: #35
|
|||
|
|||
RE: Riemann's Zeta Function - another approach (RPL)
(07-02-2017 02:54 PM)Dieter Wrote: First, there is a new approximation for 0,97≤x≤1,03 with an error of approx. ±0,5 units in the 10th significant digit. Great! It needs to be accurate only for x > 0.5. Then the functional equation can handle the rest if Gamma is available. Your code can be nicely pasted into Free42, except line 91 (because the space between the last digit and E). LBL 2 is not necessary, unless there's a reason to keep it: Code:
Gerson. |
|||
07-03-2017, 07:07 AM
(This post was last modified: 07-03-2017 11:25 AM by Dieter.)
Post: #36
|
|||
|
|||
RE: Riemann's Zeta Function - another approach (RPL)
(07-02-2017 11:53 PM)Gerson W. Barbosa Wrote: Great! It needs to be accurate only for x > 0.5. Then the functional equation can handle the rest if Gamma is available. I have tried also this on the '41, using the FACT function so that at least integer arguments can be handled, and it worked fine. Now, if there only was a Gamma function... BTW, does anyone know if there was a ROM that included Gamma in Mcode (!) ? Except the ones by Ángel, that is. ;-) The Advantage ROM was said to update the '41 to features that else were only available on the 34C and 15C (Solve, Integrate, Matrix operations), but AFAIK this did not include Gamma. (07-02-2017 11:53 PM)Gerson W. Barbosa Wrote: Your code can be nicely pasted into Free42, except line 91 (because the space between the last digit and E). The listing was generated by hp41uc. But remember that all the math is designed for 10-digit precision. On the 42s you may be better off with a constant 5 E–9. Try it and see what you get. Maybe you want to round the results to 10 digits by SCI 9 RND ALL. ;-) On the 42s you can also apply RCL-arithmetics which will save a few steps. (07-02-2017 11:53 PM)Gerson W. Barbosa Wrote: LBL 2 is not necessary, unless there's a reason to keep it: The reason is a common exit point at the end of the program. This way it can be restarted with a simple R/S. ;-) Dieter |
|||
07-03-2017, 12:33 PM
Post: #37
|
|||
|
|||
RE: Riemann's Zeta Function - another approach (RPL)
(07-03-2017 07:07 AM)Dieter Wrote: I have tried also this on the '41, using the FACT function so that at least integer arguments can be handled, and it worked fine. Now, if there only was a Gamma function... 1) Unfortunately Gamma didn't make into the Advantage ROM. That's the only ROM module I have. Really disappointing. I guess you haven't considered RPN implementations because they are rather slow. 2) Yes, I am aware of that. I used Free42 only to check your new version on what I had at hand. 3) That makes quite sense! Anyway, on the 42 I usually assign the program I am most interested at the moment to the Sigma+ key to avoid the lengthy XEQ ALPHA XXXX ALPHA sequence. Gerson. |
|||
07-03-2017, 07:30 PM
(This post was last modified: 07-04-2017 05:25 PM by Dieter.)
Post: #38
|
|||
|
|||
RE: Riemann's Zeta Function - another approach (RPL)
(07-02-2017 02:54 PM)Dieter Wrote: First, there is a new approximation for 0,97≤x≤1,03 with an error of approx. ±0,5 units in the 10th significant digit. Update: I remembered the Zeta evaluation using a polynomial with the Stieltjes' constants, and so the idea was to develop a dedicated polynomial approximation. Here I chose a simple quadratic equation so that only three constants are required. This is what I came up with: Zeta(x) ~ 1/u + 0,577215665 + 0,07281553 · u – 0,0048452 · u² where u = x–1 and 0,965 ≤ x ≤ 1,035 The error within the given domain is less than 4 E–9, i.e. here less than 0,4 ULP (if evaluated with sufficient precision, that is). One more term (i.e. a third order polynomial) improves the results substantially, you can easily handle 0,9 ≤ x ≤ 1,1 with a possible accuracy better than 0,2 units in the 10th significant digit: Zeta(x) ~ 1/u + 0,5772156632 + 0,072815845 · u – 0,00484404 · u² – 0,0003424 · u³ where u = x–1 and 0,9 ≤ x ≤ 1,1 One more update: Think big. ;-) I tried a fourth-order polynomial and the results are quite promising. You can go all the way from 1,1 down to 0,5 (!) with an error less than half a unit in the 10th significant digit. Maybe you want to try this: Zeta(x) ~ 1/u + 0,5772156625 + 0,072815839 · u – 0,004844823 · u² – 0,000339474 · u³ + 0,000104308 · u4 where u = x–1 and 0,5 ≤ x ≤ 1,1 Dieter |
|||
07-07-2017, 07:40 PM
(This post was last modified: 07-09-2017 01:40 PM by Dieter.)
Post: #39
|
|||
|
|||
RE: Riemann's Zeta Function - another approach (RPL)
(07-03-2017 07:30 PM)Dieter Wrote: I tried a fourth-order polynomial and the results are quite promising. You can go all the way from 1,1 down to 0,5 (!) with an error less than half a unit in the 10th significant digit. If you got a calculator with, say, 12 digit precision. ;-) Anyway, here is a final addition. It is a fifth order approximation for 0≤x≤1,05 which has been tested in Excel with every intermediate result rounded to 10 significant digits. And indeed the results seem to match those on a real 15C or 41C. If the implementation is carefully coded, cf. step 23 ff. in the listing below, the results should be within 2 units in the 10th place. Which I think is as good as it gets with ten-digit working precision. The formula: Zeta(x) = 1/u + 0,577215668 + 0,0728159383 · u – 0,0048444781 · u² – 0,0003401389 · u³ + 0,0001000277 · u4 – 4,58184E-6 · u5 where u = x – 1 and 0 ≤ x ≤ 1,05. Edit: please note the slightly improved coefficients in post #42. After storing the constants in R0 (=0,5772...) to R5 (=–4,58184E-6) – be sure to observe the correct signs – the following program should yield results within 2 ULP. If you find cases with larger errors, please report here. Code: 01 LBL A Examples: 1,05 => 20,58084431 (+1 ULP) 0,5 => -1,460354508 (–1 ULP) 0 => -0,5000000000 (exact) Dieter |
|||
07-08-2017, 11:30 PM
Post: #40
|
|||
|
|||
RE: Riemann's Zeta Function - another approach (RPL)
(07-07-2017 07:40 PM)Dieter Wrote: Anyway, here is a final addition. It is a fifth order approximation for 0≤x≤1,05 which has been tested in Excel with every intermediate result rounded to 10 significant digits. And indeed the results seem to match those on a real 15C or 41C. If the implementation is carefully coded, cf. step 23 ff. in the listing below, the results should be within 2 units in the 10th place. Which I think is as good as it gets with ten-digit working precision. Very nice! http://m.wolframalpha.com/input/?i=plot+...05&x=2&y=6 Do you have something as good as that for 12-digits calculators? Thanks! Gerson. |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 2 Guest(s)