code: def associated_legendre_polynomials(k, zero_m_only=True):
      z = sym.symbols("z")
      P_l_m = [[0] * (j + 1) for j in range(k)]
  
      P_l_m[0][0] = 1
      if k > 0:
          P_l_m[1][0] = z
  
          for j in range(2, k):
              P_l_m[j][0] = sym.simplify(
                  (
                      (2 * j - 1) * z * P_l_m[j - 1][0]
                      - (j - 1) * P_l_m[j - 2][0]
                  )
                  / j
              )
          if not zero_m_only:
              for i in range(1, k):
                  P_l_m[i][i] = sym.simplify(
                      (1 - 2 * i) * P_l_m[i - 1][i - 1]
                  )
                  if i + 1 < k:
                      P_l_m[i + 1][i] = sym.simplify(
                          (2 * i + 1) * z * P_l_m[i][i]
                      )
                  for j in range(i + 2, k):
                      P_l_m[j][i] = sym.simplify(
                          (
                              (2 * j - 1) * z * P_l_m[j - 1][i]
                              - (i + j - 1) * P_l_m[j - 2][i]
                          )
                          / (j - i)
                      )
  
      return P_l_m
  
imports: import numpy
  from scipy.optimize import brentq
  from scipy import special
  import torch
  from math import pi
  import sympy
label_desc: Use image node_id 6 for calling a global function with example usage: associated_legendre_polynomials(k, zero_m_only) and returns: P_l_m