code: def project_simplex(v, dim, z=1):
      v_sorted, _ = torch.sort(v, dim=dim, descending=True)
      cssv = torch.cumsum(v_sorted, dim=dim) - z
      ind = (
          torch.arange(1, 1 + v.shape[dim])
          .to(dtype=v.dtype)
          .to(v.device)
      )
      cond = v_sorted - cssv / ind >= 0
      k = cond.sum(dim=dim, keepdim=True)
      tau = cssv.gather(dim, k - 1) / k.to(dtype=v.dtype)
      w = torch.clamp(v - tau, min=0)
      return w
  
file_path: pytorch-struct/torch_struct/semirings/sparse_max.py
imports: import torch
  from .semirings import _BaseLog
label_desc: Use image node_id 1 for calling a global function with example usage: project_simplex(v, dim, z) and returns: w