6
$\begingroup$

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

$\endgroup$
  • 1
    $\begingroup$ 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. $\endgroup$ – Mr.Wizard Oct 21 '14 at 18:04
  • $\begingroup$ Thanks, I think MovingMap pretty much solved it $\endgroup$ – user50473 Oct 22 '14 at 12:56
  • 1
    $\begingroup$ 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? $\endgroup$ – Mr.Wizard Oct 22 '14 at 20:45
  • $\begingroup$ @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. :) $\endgroup$ – Edmund Oct 22 '14 at 22:52
  • $\begingroup$ @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? $\endgroup$ – user50473 Oct 24 '14 at 15:28
4
$\begingroup$

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

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – user50473 Oct 28 '14 at 10:45
  • $\begingroup$ @user50473 I don't fully understand your question. Could you check? Is this right? $\endgroup$ – Junho Lee Oct 28 '14 at 11:43
  • $\begingroup$ The update is good! Thank you. $\endgroup$ – user50473 Oct 28 '14 at 12:36
5
$\begingroup$

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

$\endgroup$
2
$\begingroup$

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
$\endgroup$
  • $\begingroup$ You can also use Tr @ Developer`PartitionMap[Times @@ # &, data, 2, 1] $\endgroup$ – RunnyKine Oct 22 '14 at 15:32
  • $\begingroup$ @RunnyKine,In V8,DeveloperPartitionMap is not exist:-) $\endgroup$ – xyz Oct 22 '14 at 15:35
  • $\begingroup$ @RunnyKine, +1, I am always learnnig! BTW, I never knew Developer`PartitionMap $\endgroup$ – xyz Oct 22 '14 at 15:38
  • $\begingroup$ 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 $\endgroup$ – RunnyKine Oct 22 '14 at 15:38
  • $\begingroup$ @RunnyKine, In V8,Developer`PartitionMap indeed exist! $\endgroup$ – xyz Oct 22 '14 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.