code: def find_n_pc(u, factor=0.5):
      
  
      if np.sqrt(u.shape[0]) % 1:
          raise ValueError('Invalid left singular value. The size of the first '
                           'dimenion of u must be perfect square.')
  
      # Get the shape of the array
      array_shape = np.repeat(np.int(np.sqrt(u.shape[0])), 2)
  
      # Find the auto correlation of the left singular vector.
      u_auto = [convolve(a.reshape(array_shape),
                np.rot90(a.reshape(array_shape), 2)) for a in u.T]
  
      # Return the required number of principal components.
      return np.sum([(a[tuple(zip(array_shape // 2))] ** 2 <= factor *
                     np.sum(a ** 2)) for a in u_auto])
docs: Find number of principal components
  
      This method finds the minimum number of principal components required
  
      Parameters
      ----------
      u : np.ndarray
          Left singular vector of the original data
      factor : float, optional
          Factor for testing the auto correlation (default is '0.5')
  
      Returns
      -------
      int number of principal components
  
      Examples
      --------
      >>> from scipy.linalg import svd
      >>> from modopt.signal.svd import find_n_pc
      >>> x = np.arange(18).reshape(9, 2).astype(float)
      >>> find_n_pc(svd(x)[0])
      array([3])