code: def setdim(P, dim=None):
      
      P = P.copy()
  
      ldim = P.dim
      if not dim:
          dim = ldim+1
  
      if dim==ldim:
          return P
  
      P.dim = dim
      if dim>ldim:
  
          key = numpy.zeros(dim, dtype=int)
          for lkey in P.keys:
              key[:ldim] = lkey
              P.A[tuple(key)] = P.A.pop(lkey)
  
      else:
  
          key = numpy.zeros(dim, dtype=int)
          for lkey in P.keys:
              if not sum(lkey[ldim-1:]) or not sum(lkey):
                  P.A[lkey[:dim]] = P.A.pop(lkey)
              else:
                  del P.A[lkey]
  
      P.keys = sorted(P.A.keys(), key=sort_key)
      return P
docs: Adjust the dimensions of a polynomial.
  
      Output the results into Poly object
  
      Args:
          P (Poly) : Input polynomial
          dim (int) : The dimensions of the output polynomial. If omitted,
                  increase polynomial with one dimension. If the new dim is
                  smaller then P's dimensions, variables with cut components are
                  all cut.
  
      Examples:
          >>> x,y = chaospy.variable(2)
          >>> P = x*x-x*y
          >>> print(chaospy.setdim(P, 1))
          q0^2