def density(a_M, *args, **kwargs):
rows, cols  = a_M.shape<EOL>a_Mmask     = ones( (rows, cols) )<EOL>if len(args):<EOL><INDENT>a_Mmask = args[<NUM_LIT:0>]<EOL><DEDENT>a_M *= a_Mmask<EOL>f_binaryMass    = float(size(nonzero(a_M)[<NUM_LIT:0>]))<EOL>f_actualMass    = a_M.sum()<EOL>f_area          = float(size(nonzero(a_Mmask)[<NUM_LIT:0>]))<EOL>f_binaryDensity = f_binaryMass / f_area;<EOL>f_actualDensity = f_actualMass / f_area;<EOL>return f_actualDensity, f_binaryDensity<EOL>
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.