code: def rank(idx, dim):
      
      idxm = multi_index(idx, dim)
      out = 0
      while idxm[-1:] == (0,):
          out += 1
          idxm = idxm[:-1]
      return out
docs: Calculate the index rank according to Bertran's notation.