code: def density(a_M, *args, **kwargs):
      
  
      rows, cols  = a_M.shape
      a_Mmask     = ones( (rows, cols) )
      if len(args):
          a_Mmask = args[0]
  
      a_M *= a_Mmask
      # The "binary" density determines the density of nonzero elements,
      # irrespective of their actual value
      f_binaryMass    = float(size(nonzero(a_M)[0]))
      f_actualMass    = a_M.sum()
  
      f_area          = float(size(nonzero(a_Mmask)[0]))
  
      f_binaryDensity = f_binaryMass / f_area;
      f_actualDensity = f_actualMass / f_area;
      return f_actualDensity, f_binaryDensity
docs: ARGS
          a_M                matrix to analyze
  
          *args[0]           optional mask matrix; if passed, calculate
                             density of a_M using non-zero elements of
                             args[0] as a mask.
  
      DESC
          Determine the "density" of a passed matrix. Two densities are returned:
  
              o f_actualDensity -- density of the matrix using matrix values
                                   as "mass"
              o f_binaryDensity -- density of the matrix irrespective of actual
                                   matrix values
  
          If the passed matrix contains only "ones", the f_binaryDensity will
          be equal to the f_actualDensity.