code: def affinity_distance(A, alpha=0.5, R=5, k=20, epsilon=4.0):
      
      if not sparse.isspmatrix_csr(A):
          A = sparse.csr_matrix(A)
  
      if alpha < 0:
          raise ValueError('expected alpha>0')
  
      if R <= 0 or not isinstance(R, int):
          raise ValueError('expected integer R>0')
  
      if k <= 0 or not isinstance(k, int):
          raise ValueError('expected integer k>0')
  
      if epsilon < 1:
          raise ValueError('expected epsilon>1.0')
  
      def distance(x):
          (rows, cols) = A.nonzero()
          return 1 - np.sum(x[rows] * x[cols], axis=1)**2 / \
              (np.sum(x[rows]**2, axis=1) * np.sum(x[cols]**2, axis=1))
  
      return distance_measure_common(A, distance, alpha, R, k, epsilon)
docs: Affinity Distance Strength Measure.
  
      Parameters
      ----------
      A : csr_matrix
          Sparse NxN matrix
      alpha : scalar
          Weight for Jacobi
      R : integer
          Number of random vectors
      k : integer
          Number of relaxation passes
      epsilon : scalar
          Drop tolerance
  
      Returns
      -------
      C : csr_matrix
          Sparse matrix of strength values
  
      References
      ----------
      .. [LiBr] Oren E. Livne and Achi Brandt, "Lean Algebraic Multigrid
          (LAMG): Fast Graph Laplacian Linear Solver"
  
      Notes
      -----
      No unit testing yet.
  
      Does not handle BSR matrices yet.
  
      See [LiBr]_ for more details.