(33S) Legendre Polynomials
|
05-18-2022, 02:59 AM
Post: #1
|
|||
|
|||
(33S) Legendre Polynomials
Legendre Polynomials
The value of Legendre Polynomials can be calculated using a closed formula from Rodrigues' formula: P_n(x) = Σ( comb(n, k) * comb(n+k, k) * ((x - 1)/2)^k, k= 0, n) Program: HP 33S: LBL L: Size: LN = 30, CK = 14EC LBL M: Size: LN = 105, CK = AA05 Run XEQ L. Code:
Examples: N = 2, X = 0.25; Result: -0.406250 N = 3, X = -0.46; Result: 0.446660 N = 4, X = 0.73; Result: -0.380952 Source: "Legendre polynomials" Wikipedia. https://en.wikipedia.org/wiki/Legendre_polynomials Updated April 6, 2022. Last Accessed April 29, 2022. |
|||
05-18-2022, 04:02 PM
Post: #2
|
|||
|
|||
RE: (33S) Legendre Polynomials
Or then we use Horner's method to avoid exponentiation, e.g. for \(n = 5\):
\( \begin{align} u &= \frac{x - 1}{2} \\ \\ P_5(x) &= 1 + u \cdot \left(30 + u \cdot \left(210 + u \cdot \left(560 + u \cdot \left(630 + u \cdot 252 \right) \right) \right) \right) \\ \end{align} \) It's easy to see that: \( \begin{align} \binom{n}{k-1} &= \binom{n}{k} \frac{k}{n-k+1} \\ \\ \binom {n+k-1}{k-1} &= \binom {n+k}{k} \frac{k}{n + k} \\ \end{align} \) This leads to a recursive formula to calculate the coefficient from \(k \to k-1\): \( \begin{align} \binom{n}{k-1} \binom {n+k-1}{k-1} = \binom {n}{k}\binom {n+k}{k} \frac{k}{n-k+1} \frac{k}{n + k} \end{align} \) It is used to calculate the next coefficient from the previous one. Note: All of these coefficients are integers. Therefore, the order of operation is important. We also try to keep the intermediate results small. Python Code: from math import comb Example \( P_5(0.3) \) 5 ENTER 0.3 XEQ "LP" 0.34538625 Programs In case of the HP-42S and HP-15C (and other models as well) RCL-arithmetic could be used. Also I tried to avoid stack acrobatics to keep the code similar. Feel free to optimize the program for your favourite model. HP-42S Code: 00 { 52-Byte Prgm } HP-15C Code: 000 { } Some models use a specific register I for loops. HP-11C Code: 01-42,21,11 # f LBL A For models that don't have a function to compute binomial coefficients, we can use factorial instead: \( \begin{align} \binom{2n}{n} = \frac{(2n)!}{(n!)^2} \end{align} \) HP-67 Code: 001: 31 25 11 # f LBL A |
|||
05-18-2022, 05:11 PM
Post: #3
|
|||
|
|||
RE: (33S) Legendre Polynomials
Not sure this is applicable here, but this section of the Wikipedia article shows a way to compute the coefficients without using COMB. This should be faster since it uses only simple arithmetic.
|
|||
05-18-2022, 07:34 PM
Post: #4
|
|||
|
|||
RE: (33S) Legendre Polynomials
From the aforementioned link:
\( (n+1)P_{n+1}(x)=(2n+1)xP_{n}(x)-nP_{n-1}(x) \) Initial values are: \( \begin{align} P_0(x) &= 1 \\ P_1(x) &= x \\ \end{align} \) I knew that we have an expert here: (49g 50g) Gegenbauer and Jacobi Polynomials From the Wikipedia article Gegenbauer polynomials: Quote:When α = 1/2, the equation reduces to the Legendre equation, and the Gegenbauer polynomials reduce to the Legendre polynomials. |
|||
05-18-2022, 08:34 PM
(This post was last modified: 05-18-2022 09:08 PM by Albert Chan.)
Post: #5
|
|||
|
|||
RE: (33S) Legendre Polynomials
(05-18-2022 02:59 AM)Eddie W. Shore Wrote: Legendre Polynomials I think Pn as polynomial of (x-1)/2, is meant for symbolic calculations. BTW, mpmath use this form: hyp2f1(-n, n+1, 1, (1-x)/2). For integer n, both forms are equivalent (hyp2f1 version can work with arbitrary n) Code: function hyp2f1(a,b,c,z) Numerical calculations with big n, coefs blow-up, with alternate signs, we have massive cancellations. Below example, terms grow as big as ±1E48, before tappering down, making sum garbage. P100(0.25) = 1 - 3787.5 + 3585578.90625 - 1508034728.3203125 + ... lua> P_bad(100, 0.25) 9.184297382421194e+031 Code: function P(n,x) -- legendre recurrence relation formula Reccurence relation formula is faster, and *much* more accurate. It may seems we have the same cancellation issues, but half the subtractions are really sums. The other half, most of subtractions does not suffer catastrophic cancellations. lua> P(100, 0.25) -- error = -2 ULP 0.07812465474298284 |
|||
05-18-2022, 11:13 PM
Post: #6
|
|||
|
|||
RE: (33S) Legendre Polynomials
Here we go with the recursive formula:
\( \begin{align} P_0(x) &= 1 \\ \\ P_1(x) &= x \\ \\ P_{n+1}(x) &= \frac{(2n+1)xP_{n}(x)-nP_{n-1}(x)}{n+1} \end{align} \) HP-25 Code: 01: 23 01 : STO 1 Registers 0: \(n\) … the order of the Legendre polynomial 1: \(x\) … where to evaluate the polynomial at 2: \(P_{k-1}\) … the previous value 3: \(k\) … the counter Examples I've changed the usage slightly since entering the order \( n \) again and again annoyed me. Initialisation 5 STO 0 CLEAR PRGM Calculation 0.3 R/S 0.345386250 0.6 R/S -0.152640000 This leads to a blinkenlicht festival. 100 STO 0 0.25 R/S 0.07812465477 |
|||
05-20-2022, 04:10 PM
Post: #7
|
|||
|
|||
RE: (33S) Legendre Polynomials
Or then we use program L4 (Legendre Polynomials) of the HP-25 Library of the PPC Journal:
Code: 01: 23 01 : STO 1 Examples CLEAR RPGM 4 STO 0 R/S 1060.375 9 STO 0 0.5 R/S -0.267898559 The program also implements the Legendre functions of the second kind (Qn) Use a negative value for \( x \) in this case: 0 STO 0 -0.5 R/S 0.549306145 10 STO 0 -0.4 R/S 0.373991229 Resources (05-17-2018 02:31 PM)rprosperi Wrote: Here's a link: HP-25 Library Programs-PPC Jrnl (05-18-2018 04:07 AM)teenix Wrote: I have extracted about 63 of the HP-25 programs and have them in a zip file at |
|||
05-20-2022, 05:51 PM
(This post was last modified: 05-20-2022 06:39 PM by Albert Chan.)
Post: #8
|
|||
|
|||
RE: (33S) Legendre Polynomials
From https://solitaryroad.com/c679.html, P(n,x) and Q(n,x) are based on same recurrence relation formula.
Note: this version extended range to all non-negative integers n. (i.e. n=0 included) Code: function legendre_hlp(n,x,p0,k0) lua> P(4,4) 1060.375 lua> P(9,0.5) -0.2678985595703125 lua> Q(0, 0.5) 0.5493061443340549 lua> Q(10, 0.4) 0.37399122844670774 Numbers matched L4 (Legendre Polynomials) numbers (previous post) (05-20-2022 04:10 PM)Thomas Klemm Wrote: The program also implements the Legendre functions of the second kind (Qn) What happens if x = 0 ? Which legendre kind get returned ? I would assume first kind (x = +0), since signed zero may not be supported. This may be confusing, since P(n,0) ≠ Q(n,0) |
|||
05-20-2022, 08:08 PM
Post: #9
|
|||
|
|||
RE: (33S) Legendre Polynomials
(05-20-2022 05:51 PM)Albert Chan Wrote: What happens if x = 0 ? Which legendre kind get returned ? If only there was an online emulator for the HP-25, with instructions on how to load a program: Quote:As with my other HTML5 microcode emulators, tap the display (LED digits) to bring up more options. (05-20-2022 05:51 PM)Albert Chan Wrote: I would assume first kind (x = +0), since signed zero may not be supported. If only polynomials were continuous and we could enter tiny numbers like \( -10^{-99} \) or \( 10^{-99} \), that would be great. And if we only could understand the meaning of these instructions: Code: 06: 24 01 : RCL 1 I highly recommend to consult the original contribution by James J. Davidson (547) in his neat handwriting. It contains stack diagrams and comments that are helpful to understand the program. |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 5 Guest(s)