code: def case_dispatcher(
      out: Tensor,
      cases: Iterable[CaseNd] = (),
      default: Callable[[BoolTensor], Tensor] = None,
  ) -> Tensor:
      r"""Basic implementation of a tensorized switching case statement.
  
      Args:
          out: Tensor to which case outcomes are written.
          cases: Iterable of function pairs (pred, func), where `mask=pred()` specifies
              whether `func` is applicable for each entry in `out`. Note that cases are
              resolved first-come, first-serve.
          default: Optional `func` to which all unclaimed entries of `out` are dispatched.
      """
      active = None
      for closure, func in cases:
          pred = closure()
          if not pred.any():
              continue
  
          mask = pred if (active is None) else pred & active
          if not mask.any():
              continue
  
          if (
              mask.all()
          ):  # where possible, use Ellipsis to avoid indexing
              out[...] = func(...)
              return out
  
          out[mask] = func(mask)
          if active is None:
              active = ~mask
          else:
              active[mask] = False
  
          if not active.any():
              break
  
      if default is not None:
          if active is None:
              out[...] = default(...)
          elif active.any():
              out[active] = default(active)
  
      return out
  
imports: from __future__ import annotations
  import math
  from functools import lru_cache
  from math import pi
  from numbers import Number
  from typing import Any, Callable, Iterable, Iterator, Optional, Tuple, Union
  import torch
  from botorch.utils.safe_math import logdiffexp
  from numpy.polynomial.legendre import leggauss
  from torch import BoolTensor, LongTensor, Tensor
label_desc: Use image node_id 1 for calling a global function with example usage: case_dispatcher(out, cases, default) and returns: out, out