0
$\begingroup$

This question already has an answer here:

I'm trying to understand an answer to the question: How to do algebra on summations of variable expressions? The following input:

reindex = s : sum[_, {i_, __}] :> (s /. i -> Unique[ToString[i]]);
unpower = s_sum^p_Integer :> Times @@ Table[s /. reindex, {p}];
test = (5*sum[x[k], {k, 1, kk}])^2;
test /. unpower

Out[4]= 25 sum[x[k3], {k3, 1, kk}] sum[x[k4], {k4, 1, kk}]

produces result, that differs from result produced by expressions ($25\neq625$):

unpowerstep1 = s_sum^p_Integer :> Table[s /. reindex, {p}];
intermediate = test /. unpowerstep1
Times @@ intermediate

Out[6]= {25 sum[x[k5], {k5, 1, kk}], 25 sum[x[k6], {k6, 1, kk}]}    
Out[7]= 625 sum[x[k5], {k5, 1, kk}] sum[x[k6], {k6, 1, kk}]

Also, I cannot undestand what s_sum mean. Does it mean, that s is any expression with head sum?

$\endgroup$

marked as duplicate by Kuba Nov 15 '18 at 8:22

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    $\begingroup$ "Does it mean, that s is any expression with head sum?" Yes. $\endgroup$ – march Oct 12 '16 at 19:55
  • 1
    $\begingroup$ Wolfram documentation states that pattern matching is based on the FullForm, but FullForm never returns the word Integer in its output. So, I didn't know that Integer is also the head, that caused confusion when thinking about meaning of s_sum and p_Integer. OK, if s is any expression with head sum then: why intermediate variable contains value 25 located out of the sum? $\endgroup$ – Shinrei Oct 13 '16 at 6:24
  • $\begingroup$ @Shinrei This should answer your doubts: Anything that AtomQ returns True for is atomic, and may behave in "strange" ways. $\endgroup$ – Kuba Oct 13 '16 at 13:53