original_docstring: 
      (Y, dY_dbeta, dY_dalpha) = SphericalHarmonic(n, m, alpha, sinbeta, cosbeta)
      	
      Computes a Spherical Harmonic function of order (m,n) and it's angular derivatives.
      	
      Arguments - these should all be scalars:
      r is radius
      alpha is azimuth angle (angle in radians from the positive x axis, with
      rotation around the positive z axis according to the right-hand screw rule)
      beta is polar angle, but it is specified as two arrays of its cos and sin values. 
      m and n should be integer scalars; n should be non-negative and m should be in the range -n<=m<=n
      	
      Returned data will be vectors of length (Order+1)^2.
      
      Associated Legendre function its derivatives for |m|:
      
      
docstring: (Y, dY_dbeta, dY_dalpha) = SphericalHarmonic(n, m, alpha, sinbeta, cosbeta)
  Computes a Spherical Harmonic function of order (m,n) and it's angular derivatives.
  
  these should all be scalars:
  r is radius
  alpha is azimuth angle (angle in radians from the positive x axis, with
  rotation around the positive z axis according to the right-hand screw rule)
  beta is polar angle, but it is specified as two arrays of its cos and sin values.
  m and n should be integer scalars; n should be non-negative and m should be in the range -n<=m<=n
  
  Returned data will be vectors of length (Order+1)^2.
  
  Associated Legendre function its derivatives for |m|.
docstring_tokens: ( Y dY_dbeta dY_dalpha ) = SphericalHarmonic ( n m alpha sinbeta cosbeta ) Computes a Spherical Harmonic function of order ( m n ) and it ' s angular derivatives . these should all be scalars : r is radius alpha is azimuth angle ( angle in radians from the positive x axis with rotation around the positive z axis according to the right - hand screw rule ) beta is polar angle but it is specified as two arrays of its cos and sin values . m and n should be integer scalars ; n should be non - negative and m should be in the range - n< = m< = n Returned data will be vectors of length ( Order + 1 ) ^2 . Associated Legendre function its derivatives for |m| .
code: def spherical_harmonic(n, m, alpha, sinbeta, cosbeta):
  	
      """
      (Y, dY_dbeta, dY_dalpha) = SphericalHarmonic(n, m, alpha, sinbeta, cosbeta)
      	
      Computes a Spherical Harmonic function of order (m,n) and it's angular derivatives.
      	
      Arguments - these should all be scalars:
      r is radius
      alpha is azimuth angle (angle in radians from the positive x axis, with
      rotation around the positive z axis according to the right-hand screw rule)
      beta is polar angle, but it is specified as two arrays of its cos and sin values. 
      m and n should be integer scalars; n should be non-negative and m should be in the range -n<=m<=n
      	
      Returned data will be vectors of length (Order+1)^2.
      
      Associated Legendre function its derivatives for |m|:
      
      """
      
      p_nm = lpmv(abs(m), n, cosbeta)
      if n == 0:
          dPmn_dbeta = 0
      elif m == 0:
          dPmn_dbeta = lpmv(1, n, cosbeta)
      elif abs(m) < n:
          dPmn_dbeta = 0.5 * lpmv(abs(m) + 1, n, cosbeta) - 0.5 * (n + abs(m)) * (n - abs(m) + 1) * lpmv(abs(m) - 1, n, cosbeta);
      elif (abs(m) == 1) and (n == 1):
          dPmn_dbeta = -cosbeta
      elif sinbeta<=np.finfo(float).eps:
          dPmn_dbeta = 0
      else:
          dPmn_dbeta = -abs(m) * cosbeta * p_nm / sinbeta - (n + abs(m)) * (n - abs(m) + 1) * lpmv(abs(m) - 1, n, cosbeta)
  
      # Compute scaling term, including sign factor:
      scaling_term = ((-1) ** m) * np.sqrt((2 * n + 1) / (4 * np.pi * np.prod(np.float64(range(n - abs(m) + 1, n + abs(m) + 1)))))
  
      # Compute exponential term:
      exp_term = np.exp(1j * m * alpha)
  
      # Put it all together:
      y = scaling_term * exp_term * p_nm
      dy_dbeta = scaling_term * exp_term * dPmn_dbeta
      dy_dalpha = y * 1j * m
  
      return (y, dy_dbeta, dy_dalpha)
code_tokens: def spherical_harmonic ( n , m , alpha , sinbeta , cosbeta ) : p_nm = lpmv ( abs ( m ) , n , cosbeta ) if n == 0 : dPmn_dbeta = 0 elif m == 0 : dPmn_dbeta = lpmv ( 1 , n , cosbeta ) elif abs ( m ) < n : dPmn_dbeta = 0.5 * lpmv ( abs ( m ) + 1 , n , cosbeta ) - 0.5 * ( n + abs ( m ) ) * ( n - abs ( m ) + 1 ) * lpmv ( abs ( m ) - 1 , n , cosbeta ) ; elif ( abs ( m ) == 1 ) and ( n == 1 ) : dPmn_dbeta = - cosbeta elif sinbeta <= np . finfo ( float ) . eps : dPmn_dbeta = 0 else : dPmn_dbeta = - abs ( m ) * cosbeta * p_nm / sinbeta - ( n + abs ( m ) ) * ( n - abs ( m ) + 1 ) * lpmv ( abs ( m ) - 1 , n , cosbeta ) scaling_term = ( ( - 1 ) ** m ) * np . sqrt ( ( 2 * n + 1 ) / ( 4 * np . pi * np . prod ( np . float64 ( range ( n - abs ( m ) + 1 , n + abs ( m ) + 1 ) ) ) ) ) exp_term = np . exp ( 1j * m * alpha ) y = scaling_term * exp_term * p_nm dy_dbeta = scaling_term * exp_term * dPmn_dbeta dy_dalpha = y * 1j * m return ( y , dy_dbeta , dy_dalpha )
short_docstring: (Y, dY_dbeta, dY_dalpha) = SphericalHarmonic(n, m, alpha, sinbeta, cosbeta)
  Computes a Spherical Harmonic function of order (m,n) and it's angular derivatives.
short_docstring_tokens: ( Y dY_dbeta dY_dalpha ) = SphericalHarmonic ( n m alpha sinbeta cosbeta ) Computes a Spherical Harmonic function of order ( m n ) and it ' s angular derivatives .
comment: """
      (Y, dY_dbeta, dY_dalpha) = SphericalHarmonic(n, m, alpha, sinbeta, cosbeta)
      	
      Computes a Spherical Harmonic function of order (m,n) and it's angular derivatives.
      	
      Arguments - these should all be scalars:
      r is radius
      alpha is azimuth angle (angle in radians from the positive x axis, with
      rotation around the positive z axis according to the right-hand screw rule)
      beta is polar angle, but it is specified as two arrays of its cos and sin values. 
      m and n should be integer scalars; n should be non-negative and m should be in the range -n<=m<=n
      	
      Returned data will be vectors of length (Order+1)^2.
      
      Associated Legendre function its derivatives for |m|:
      
      """$SEP$# Compute scaling term, including sign factor:$SEP$# Compute exponential term:$SEP$# Put it all together: