0
$\begingroup$

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$
3
  • 2
    $\begingroup$ "Does it mean, that s is any expression with head sum?" Yes. $\endgroup$
    – march
    Commented Oct 12, 2016 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
    Commented Oct 13, 2016 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
    Commented Oct 13, 2016 at 13:53

0

Browse other questions tagged or ask your own question.