Can Legendre polynomials be evaluated using a recurrence relation in Fotran 90?

  • Thread starter Thread starter quantumfireball
  • Start date Start date
  • Tags Tags
    Legendre
AI Thread Summary
The discussion focuses on evaluating Legendre polynomials using a recurrence relation in Fortran 90. It highlights that recursion is unnecessary when initial conditions, specifically P(0) and P(1), are known. An example in Fortran 77 illustrates the calculation of Legendre polynomials up to N=100, using an array to store values. The initial conditions for P(0), P(1), and P(2) are explicitly defined to avoid recursion issues. A more efficient version of the function is presented, which eliminates the need for arrays, allowing for larger N values limited only by round-off errors. This version uses three variables to compute the polynomial iteratively, maintaining clarity and efficiency in the calculations. The test program demonstrates the function's application by outputting results for N values from 1 to 10.
quantumfireball
Messages
90
Reaction score
0
Is it possible to evaluate a legendre polynomial p(n,x) using the recurrence relation

p(n,x)=p(n-1,x)*x-p(n-2,x) in fotran 90


{there are some other terms which i left out for brevity]
 
Technology news on Phys.org
Here is an example in F77 for N up to 100.
Recursion is not necessary when the initial conditions are known, i.e. P(0) and P(1).
P(2) was defined to avoid going back to P(0) which is not permitted in F77.

Code:
      FUNCTION FLEGENDRE(N,X)
      REAL*8 P(100)
      REAL*8 FI
      INTEGER I
C     CHECK FOR VALID VALUES OF N AND X HERE BEFORE PROCEEDING
C
C     PROCEEDING WITH CALCULATIONS
      IF (N.EQ.0) THEN
        FLEGENDRE=1
      ELSEIF (N.EQ.1) THEN
        FLEGENDRE=X
      ELSEIF (N.EQ.2) THEN
        FLEGENDRE=(3*X*X-1)/2.0
      ELSE
C       SYNTHETIC CALCULATIONS 
        P(1)=X
        P(2)=(3*X*X-1)/2.0
        DO 20 I=3,N
        FI=I
        P(I)=((I+I-1)*X*P(I-1)-(I-1)*P(I-2))/FI
   20   CONTINUE
        FLEGENDRE=P(N)
      ENDIF
      END
C   
C     TEST PROGRAM
C
      REAL*8 OUT
      DO 30 I=1,10
      OUT=FLEGENDRE(I,5.425)
      WRITE(6,999)I,OUT
   30 CONTINUE
  999 FORMAT(I3,F20.2)
      STOP
      END
 
Here a computationally more efficient version that obviates the use of arrays and hence no limit on the size of N except for round-off errors.

Code:
      FUNCTION FLEGENDRE(N,X)
      REAL*8 PI,PIM1,PIM2
      REAL*8 FI
      INTEGER I
C     CHECK FOR VALID VALUES OF N AND X HERE BEFORE PROCEEDING
C
C     PROCEEDING WITH CALCULATIONS
      IF (N.EQ.0) THEN
        FLEGENDRE=1
      ELSEIF (N.EQ.1) THEN
        FLEGENDRE=X
      ELSE
C       SYNTHETIC CALCULATIONS 
        PIM1=1
        PI=X
        DO 20 I=2,N
        FI=I
        PIM2=PIM1
        PIM1=PI
        PI=((I+I-1)*X*PIM1-(I-1)*PIM2)/FI
   20   CONTINUE
        FLEGENDRE=PI
      ENDIF
      END
C   
C     TEST PROGRAM
C
      REAL*8 OUT
      DO 30 I=1,10
      OUT=FLEGENDRE(I,5.425)
      WRITE(6,999)I,OUT
   30 CONTINUE
  999 FORMAT(I3,F20.2)
      STOP
      END
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top