def safe_sum_p_log_p(a, base=None):
 flat = ravel(a)
 nz = take(flat, nonzero(flat)[0])
 logs = log(nz)
 if base:
  logs /= log(base)
 return sum((nz * logs), 0)

def safe_sum_p_log_p ( a , base = None ) : flat = ravel ( a ) nz = take ( flat , nonzero ( flat ) [ 0 ] ) logs = log ( nz ) if base : logs /= log ( base ) return sum ( ( nz * logs ) , 0 )
calculates p * log(p) safely for an array that may contain zeros .