def sum_simplify(s):
 from sympy.concrete.summations import Sum
 from sympy.core.function import expand
 terms = Add.make_args(expand(s))
 s_t = []
 o_t = []
 for term in terms:
  if isinstance(term, Mul):
   other = 1
   sum_terms = []
   if (not term.has(Sum)):
    o_t.append(term)
    continue
   mul_terms = Mul.make_args(term)
   for mul_term in mul_terms:
    if isinstance(mul_term, Sum):
     r = mul_term._eval_simplify()
     sum_terms.extend(Add.make_args(r))
    else:
     other = (other * mul_term)
   if len(sum_terms):
    s_t.append((Mul(*sum_terms) * other))
   else:
    o_t.append(other)
  elif isinstance(term, Sum):
   r = term._eval_simplify()
   s_t.extend(Add.make_args(r))
  else:
   o_t.append(term)
 result = Add(sum_combine(s_t), *o_t)
 return result

def sum_simplify ( s ) : from sympy . concrete . summations import Sum from sympy . core . function import expand terms = Add . make_args ( expand ( s ) ) s_t = [ ] o_t = [ ] for term in terms : if isinstance ( term , Mul ) : other = 1 sum_terms = [ ] if ( not term . has ( Sum ) ) : o_t . append ( term ) continue mul_terms = Mul . make_args ( term ) for mul_term in mul_terms : if isinstance ( mul_term , Sum ) : r = mul_term . _eval_simplify ( ) sum_terms . extend ( Add . make_args ( r ) ) else : other = ( other * mul_term ) if len ( sum_terms ) : s_t . append ( ( Mul ( * sum_terms ) * other ) ) else : o_t . append ( other ) elif isinstance ( term , Sum ) : r = term . _eval_simplify ( ) s_t . extend ( Add . make_args ( r ) ) else : o_t . append ( term ) result = Add ( sum_combine ( s_t ) , * o_t ) return result
main function for sum simplification .