2
$\begingroup$

I'm looking to code the following function:

$$|g(\theta)=\frac{1}{2k}\sum_{\ell=0}^{\infty} (2\ell +1)\sin(2\delta_{\ell})P_{\ell}\cos(\theta)|^2$$

 import numpy as np

 import scipy.special as sp


 def Legendre(n,x):
x=np.array(x)
if (n==0):`
    return x*0+1.0
elif (n==1):
    return x
else:
    return ((2.0*n-1.0)*x*Legendre(n-1,x)-(n-1)*Legendre(n-2,x))/n
def RealFn(x):
  """
Fuction to evaluate the Real element of Phase Shift
"""
# Test for valid input
if (x<0):
    print("Error: x must be non negative");
    return
 RealFn = (1/(2*k)*(?)(2*l + 1)*np.sin(2*delta)*Legendre(??)*np.cos(x))**2
 return RealFn

If anyone can help me with the coding of the Legendre Polynomial and the infinite sum I'd really appreciate it!

The value of $\delta$ is known also so that's just a simple input.

I believe I'm missing bounds for $\theta$ also.

$\endgroup$

1 Answer 1

7
$\begingroup$

You try to be too literal: These functions are defined using an infinite sum, but of course computers can not execute infinite sums. Rather, they need to truncate any sums after a finite number of terms so that they can return a result after a finite amount of time.

But even then, functions like yours might be defined this way, but that's not how they are actually implemented. There are very clever ways to evaluate these kinds of functions in different ways for given arguments that yield high accuracy without having to sum to large numbers of terms. In other words, we only define but not implement these functions in the way you show.

$\endgroup$
2
  • $\begingroup$ ...Thanks for your help. What kind of 'clever ways' would you advise to solve this equation? $\endgroup$
    – Student146
    May 3, 2020 at 12:41
  • 1
    $\begingroup$ You don't want to do it yourself -- it's a science in itself to figure this out, and people have devoted their whole lives to it. The general area you should look into is called "special functions". The classic almanach of how to implement special functions is Abramowich & Stegun, of which a current and updated version is available in the form of the NIST Digital Library of Mathematical Functions at dlmf.nist.gov $\endgroup$ May 3, 2020 at 22:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.