0

Can I simplify the sum of a series like this using SymPy or other software?

I can simplify the sum of such series {i}:

summation(i, (i, 1, n))

I don't know how to deal with the series with subscripts {a_i}

a_1, a_2, a_3, a_4 = symbols("a_1 a_2 a_3 a_4")

Say, I have a equation a_1 + a_2 + ... + a_100 - x = 0. The answer will be : x = a_1 + a_2 + ... + a_100. It is too long. I want to make it shorter with some other symbols i and functions summation, like x = summation(a_i, (i, 1, 100 )).

The code Sum(Indexed("x", i), (i, 1, 4)).doit() will get the result x[1] + x[2] + x[3] + x[4]. However, I want to reverse the process. I want the code somefunction(x[1]+x[2]+x[3]+x[4]) to get the result Sum(Index("x", i), (i, 1, 4)).

Fangquan Shi
  • 192
  • 1
  • 1
  • 11
  • 1
    Is your question to detect that you input a series and output the closed form expression? My math terminology might be off. But I think the answers below don't answer your question in that case. – Eugene K Mar 27 '15 at 15:41
  • Yes, I just want to make the expression shorter. – Fangquan Shi Mar 27 '15 at 15:46
  • http://docs.sympy.org/0.7.1/modules/simplify/simplify.html Answer might be there. Never did this myself. – Eugene K Mar 27 '15 at 15:49
  • 1
    it is unclear what you are asking. If you show what you have tried it might help clarify the question. – agentp Mar 27 '15 at 15:58
  • 1
    possible duplicate: http://stackoverflow.com/questions/26402387/sympy-summation-with-indexed-variable. (I think you are clouding the issue by tagging a bunch of other languages, btw) – agentp Mar 27 '15 at 17:10
  • ok, now its clear..! I can tell you in mathematica there probably is some round about way to do that but it is *not trivial* – agentp Mar 27 '15 at 17:54

2 Answers2

0

You can do this with reflection:

local = locals()
sum(local['a_' + i] for i in range(1, n + 1))
Matt
  • 724
  • 4
  • 11
  • TypeError: cannot concatenate 'str' and 'Symbol' objects ? – Fangquan Shi Mar 27 '15 at 15:59
  • I try your answer. `sum(local['a_' + i] for i in range(1, n + 1))`. TypeError: is not a number. What module you answer use? I can't make it work. – Fangquan Shi Mar 27 '15 at 16:23
  • @FangquanShi, this is just vanilla python. As others have said it is not clear what you are asking. Can you edit your question and explain where you are getting the `a_1, a_2, ..., a_n` symbols from, what they represent, and what you want to do with them? – Matt Mar 27 '15 at 16:27
0

mathemetica solution (mostly to show how painful this is )

 pp[list_] /; Length@Union[Head /@ list] > 1 :=
      Total[ pp /@ GatherBy[list, Head] ]
 pp[list_] /; 
     Length@list > 1 && (Sort[Flatten[List @@ # & /@ list ]] == 
         (rg = Range[list[[1, 1]], list[[-1, 1]]])) :=
           sum[Head[list[[1]]][i], {i, rg[[1]], rg[[2]]}]
 pp[list_] := Plus @@ list


 (a[1] + a[2] + q[3] + 2)/(b[1] + b[2] + c[3] + b[3] + c[4]) /. 
       x_Plus :> (pp@(List @@ x))

enter image description here

note the lowercase s on sum (which is not a defined function) because mathematical will automatically expand out Sum as soon as you use it. We can recover the original with

 %/. sum->Sum

 (2 + a[1] + a[2] + q[3])/(b[1] + b[2] + c[3] + c[4])
agentp
  • 6,849
  • 2
  • 19
  • 37