6

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\}]$

user50473
  • 579
  • 2
  • 12
  • 1
    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. – Mr.Wizard Oct 21 '14 at 18:04
  • Thanks, I think MovingMap pretty much solved it – user50473 Oct 22 '14 at 12:56
  • 1
    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? – Mr.Wizard Oct 22 '14 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. :) – Edmund Oct 22 '14 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? – user50473 Oct 24 '14 at 15:28

3 Answers3

5

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

Edmund
  • 42,267
  • 3
  • 51
  • 143
4

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

Junho Lee
  • 5,155
  • 1
  • 15
  • 33
2

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
Öskå
  • 8,587
  • 4
  • 30
  • 49
xyz
  • 605
  • 4
  • 38
  • 117