def logsumexp(a, axis=None, b=None, keepdims=False, return_sign=False):
 a = _asarray_validated(a, check_finite=False)
 if (b is not None):
  (a, b) = np.broadcast_arrays(a, b)
  if np.any((b == 0)):
   a = (a + 0.0)
   a[(b == 0)] = (- np.inf)
 a_max = np.amax(a, axis=axis, keepdims=True)
 if (a_max.ndim > 0):
  a_max[(~ np.isfinite(a_max))] = 0
 elif (not np.isfinite(a_max)):
  a_max = 0
 if (b is not None):
  b = np.asarray(b)
  tmp = (b * np.exp((a - a_max)))
 else:
  tmp = np.exp((a - a_max))
 with np.errstate(divide='ignore'):
  s = np.sum(tmp, axis=axis, keepdims=keepdims)
  if return_sign:
   sgn = np.sign(s)
   s *= sgn
  out = np.log(s)
 if (not keepdims):
  a_max = np.squeeze(a_max, axis=axis)
 out += a_max
 if return_sign:
  return (out, sgn)
 else:
  return out

def logsumexp ( a , axis = None , b = None , keepdims = False , return_sign = False ) : a = _asarray_validated ( a , check_finite = False ) if ( b is not None ) : ( a , b ) = np . broadcast_arrays ( a , b ) if np . any ( ( b == 0 ) ) : a = ( a + 0.0 ) a [ ( b == 0 ) ] = ( - np . inf ) a_max = np . amax ( a , axis = axis , keepdims = True ) if ( a_max . ndim > 0 ) : a_max [ ( ~ np . isfinite ( a_max ) ) ] = 0 elif ( not np . isfinite ( a_max ) ) : a_max = 0 if ( b is not None ) : b = np . asarray ( b ) tmp = ( b * np . exp ( ( a - a_max ) ) ) else : tmp = np . exp ( ( a - a_max ) ) with np . errstate ( divide = 'ignore' ) : s = np . sum ( tmp , axis = axis , keepdims = keepdims ) if return_sign : sgn = np . sign ( s ) s *= sgn out = np . log ( s ) if ( not keepdims ) : a_max = np . squeeze ( a_max , axis = axis ) out += a_max if return_sign : return ( out , sgn ) else : return out
computes the sum of arr assuming arr is in the log domain .