code: def _causal_init_code(mode):
      """Code for causal initialization step of IIR filtering.
  
      c is a 1d array of length n and z is a filter pole
      """
      code = f"""
          // causal init for mode={mode}"""
      if mode == "mirror":
          code += """
          z_i = z;
          z_n_1 = pow(z, (P)(n - 1));
  
          c[0] = c[0] + z_n_1 * c[(n - 1) * element_stride];
          for (i = 1; i < min(n - 1, static_cast<idx_t>({n_boundary})); ++i) {{
              c[0] += z_i * (c[i * element_stride] +
                             z_n_1 * c[(n - 1 - i) * element_stride]);
              z_i *= z;
          }}
          c[0] /= 1 - z_n_1 * z_n_1;"""
      elif mode == "grid-wrap":
          code += """
          z_i = z;
  
          for (i = 1; i < min(n, static_cast<idx_t>({n_boundary})); ++i) {{
              c[0] += z_i * c[(n - i) * element_stride];
              z_i *= z;
          }}
          c[0] /= 1 - z_i; /* z_i = pow(z, n) */"""
      elif mode == "reflect":
          code += """
          z_i = z;
          z_n = pow(z, (P)n);
          c0 = c[0];
  
          c[0] = c[0] + z_n * c[(n - 1) * element_stride];
          for (i = 1; i < min(n, static_cast<idx_t>({n_boundary})); ++i) {{
              c[0] += z_i * (c[i * element_stride] +
                             z_n * c[(n - 1 - i) * element_stride]);
              z_i *= z;
          }}
          c[0] *= z / (1 - z_n * z_n);
          c[0] += c0;"""
      else:
          raise ValueError("invalid mode: {}".format(mode))
      return code
  
file_path: cupy/cupyx/scipy/ndimage/_spline_prefilter_core.py
imports: import functools
  import math
  import operator
  import textwrap
  import cupy
label_desc: Use image node_id 3 for calling a global function with example usage: _causal_init_code(mode) and returns: code