code: def _correlate_or_convolve(
      input, weights, output, mode, cval, origin, convolution
  ):
      input = numpy.asarray(input)
      weights = numpy.asarray(weights)
      complex_input = input.dtype.kind == "c"
      complex_weights = weights.dtype.kind == "c"
      if complex_input or complex_weights:
          if complex_weights and not convolution:
              # As for numpy.correlate, conjugate weights rather than input.
              weights = weights.conj()
          kwargs = dict(
              mode=mode, origin=origin, convolution=convolution
          )
          output = _ni_support._get_output(
              output, input, complex_output=True
          )
  
          return _complex_via_real_components(
              _correlate_or_convolve,
              input,
              weights,
              output,
              cval,
              **kwargs,
          )
  
      origins = _ni_support._normalize_sequence(origin, input.ndim)
      weights = numpy.asarray(weights, dtype=numpy.float64)
      wshape = [ii for ii in weights.shape if ii > 0]
      if len(wshape) != input.ndim:
          raise RuntimeError(
              "filter weights array has incorrect shape."
          )
      if convolution:
          weights = weights[
              tuple([slice(None, None, -1)] * weights.ndim)
          ]
          for ii in range(len(origins)):
              origins[ii] = -origins[ii]
              if not weights.shape[ii] & 1:
                  origins[ii] -= 1
      for origin, lenw in zip(origins, wshape):
          if _invalid_origin(origin, lenw):
              raise ValueError(
                  "Invalid origin; origin must satisfy "
                  "-(weights.shape[k] // 2) <= origin[k] <= "
                  "(weights.shape[k]-1) // 2"
              )
  
      if not weights.flags.contiguous:
          weights = weights.copy()
      output = _ni_support._get_output(output, input)
      temp_needed = numpy.may_share_memory(input, output)
      if temp_needed:
          # input and output arrays cannot share memory
          temp = output
          output = _ni_support._get_output(output.dtype, input)
      if not isinstance(mode, str) and isinstance(mode, Iterable):
          raise RuntimeError("A sequence of modes is not supported")
      mode = _ni_support._extend_mode_to_code(mode)
      _nd_image.correlate(input, weights, output, mode, cval, origins)
      if temp_needed:
          temp[...] = output
          output = temp
      return output
  
imports: from collections.abc import Iterable
  import numbers
  import warnings
  import numpy
  import operator
  from scipy._lib._util import normalize_axis_index
  from .None import _ni_support
  from .None import _nd_image
  from .None import _ni_docstrings
label_desc: Use image node_id 15 for calling a global function with example usage: _correlate_or_convolve(input, weights, output, mode, cval, origin, convolution) and returns: output, _complex_via_real_components