# Can I regroup terms into a sum?

Is there a way to regroup terms into a sum? I mean, for example, if I have the sequence

$\quad \quad S_1 S_2 + S_2 S_3 + S_3 S_4 + S_4 S_5$

I would like to get the form

$\quad \quad {\rm Sum}[S_i\,S_{i+1},\ \{i,\,4\}]$.

I actually need this for a way longer and more complicated expression.

EDIT

Following the discussion in the comments, I think I should clarify the intent of the question: How do I construct a function that, given the input:

$\quad \quad S_1 S_2 + S_2 S_3 + S_3 S_4 + S_4 S_5$

gives the output

$\quad \quad {\rm Sum}[S_i\,S_{i+1},\ \{i,\,4\}]$

• I recall that a similar question has been asked before, and I do not believe that any general answer was (or can be) given. However if you give a sample of your expression I may be able to recommend a method. Commented Oct 21, 2014 at 18:04
• Thanks, I think MovingMap pretty much solved it Commented Oct 22, 2014 at 12:56
• Based on the answers received I don't think I understood the question. However none of the answers actually return an expression involving Sum which is what I thought wanted. Could you clarify your intent? Would you give a short example of the specific input and the output that would like the hypothetical function to produce? Commented Oct 22, 2014 at 20:45
• @user50473 Glad to help. If you are accepting my answer may you click the check mark next to it. It lets everyone know and answer has been accepted for the question. Also, it would be my first accepted answer. :) Commented Oct 22, 2014 at 22:52
• @Mr.Wizard Indeed now I run into a problem, Edmund's answer doesn't work for symbols (but works great for numbers). So let's assume that the specific input and the desired output of my hypothetical function are exactly the ones stated in the question: the input is S_1S_2+S_2S_3+S_3S_4+S_4S_5 and I want as output an expression involving Sum How would you construct such a function? Commented Oct 24, 2014 at 15:28

Update

exp = Subscript[S, 3] Subscript[S, 4] +
Subscript[S, 2] Subscript[S, 3] + Subscript[S, 4] Subscript[S, 5]


$S_2 S_3+S_4 S_3+S_4 S_5$

This code toSum check for able form of Sum and if possible then it is applied Defer.

toSum[exp_] := Module[{l = List @@ exp, l2, s},
l = l /. Subscript[S, a_] Subscript[S, b_] -> {a, b};
s = l[[1, 1]];
l2 = Table[{i, i + 1}, {i, s, s + Length[l] - 1}];
If[l === l2,
Defer@Sum[
Subscript[S,i] Subscript[S, i+1], {i, #1, #2}] & @@ {s, s+Length[l]-1},
exp]
]


Have try this.

toSum[exp]


$\sum _{i=2}^4 S_i S_{i+1}$

If it is not proper then return input expression.

exp2 = Subscript[S, 3] Subscript[S, 4] +
Subscript[S, 2] Subscript[S, 3] + Subscript[S, 4] Subscript[S, 6]


$S_2 S_3+S_4 S_3+S_4 S_6$

toSum[exp2]


$S_2 S_3+S_4 S_3+S_4 S_6$

Original

 data = {16, 13, 42, 26, 35, 39, 47, 49, 1, 9};


This is my try.

Most@data Rest@data


{208, 546, 1092, 910, 1365, 1833, 2303, 49, 9}

Dot[Most@data, Rest@data]


8315

• Ok, looks like I didn't explain myself correctly: I would like to have a SYMBOLIC expression involving the function Sum as output. No numbers. Commented Oct 28, 2014 at 10:45
• @user50473 I don't fully understand your question. Could you check? Is this right? Commented Oct 28, 2014 at 11:43
• The update is good! Thank you. Commented Oct 28, 2014 at 12:36

You can use MovingMap in version 10.

data = RandomInteger[{1, 50}, 10]
(* {16, 13, 42, 26, 35, 39, 47, 49, 1, 9} *)
MovingMap[Times @@ # &, data, {2, Left}]
(* {208, 546, 1092, 910, 1365, 1833, 2303, 49, 9} *)


There you can see the items muliplied by the pairs. Now just Apply Plus to get the total.

Plus @@ MovingMap[Times @@ # &, data, {2, Left}]
(* 8315 *)


Edmund

You can use the Partition:

data= {16, 13, 42, 26, 35, 39, 47, 49, 1, 9};
Times @@@ Partition[data, 2, 1]

{208, 546, 1092, 910, 1365, 1833, 2303, 49, 9}


The summation of the list:

Plus@@(Times @@@ Partition[data, 2, 1])

 8315

• You can also use Tr @ DeveloperPartitionMap[Times @@ # &, data, 2, 1] Commented Oct 22, 2014 at 15:32
• @RunnyKine,In V8,DeveloperPartitionMap is not exist:-)
– xyz
Commented Oct 22, 2014 at 15:35
• @RunnyKine, +1, I am always learnnig! BTW, I never knew DeveloperPartitionMap
– xyz
Commented Oct 22, 2014 at 15:38
• The way you typed it is not correct. You could be right since I don't have v8, but it's in v9 and 10 Commented Oct 22, 2014 at 15:38
• @RunnyKine, In V8,Developer`PartitionMap indeed exist!
– xyz
Commented Oct 22, 2014 at 15:40