def diag(v, k=0):
 if isinstance(v, cupy.ndarray):
  if (v.ndim == 1):
   size = (v.size + abs(k))
   ret = cupy.zeros((size, size), dtype=v.dtype)
   ret.diagonal(k)[:] = v
   return ret
  else:
   return v.diagonal(k)
 else:
  return cupy.array(numpy.diag(v, k))

def diag ( v , k = 0 ) : if isinstance ( v , cupy . ndarray ) : if ( v . ndim == 1 ) : size = ( v . size + abs ( k ) ) ret = cupy . zeros ( ( size , size ) , dtype = v . dtype ) ret . diagonal ( k ) [ : ] = v return ret else : return v . diagonal ( k ) else : return cupy . array ( numpy . diag ( v , k ) )
extracts the diagonal from a matrix or makes a vector a diagonal matrix .