code: def unitvec(vec):
      
      if scipy.sparse.issparse(vec): # convert scipy.sparse to standard numpy array
          vec = vec.tocsr()
          veclen = numpy.sqrt(numpy.sum(vec.data ** 2))
          if veclen > 0.0:
              return vec / veclen
          else:
              return vec
  
      if isinstance(vec, numpy.ndarray):
          vec = numpy.asarray(vec, dtype=float)
          veclen = numpy.linalg.norm(vec)
          if veclen > 0.0:
              return vec / veclen
          else:
              return vec
docs: Scale a vector to unit length. The only exception is the zero vector, which
      is returned back unchanged.