9
$\begingroup$

I'm newbie in Mathematica. I'd like to obtain nice and verbose output for any series calculation.

For example, given a simple sequence n*(-1)^(n-1) and hypothetical function NoEval, I see it as follows:

sequence = n*(-1)^(n-1);
range = {n, 1, 10};
seriesFormula = NoEval[Sum[sequence, range]];
seriesExpanded = Sum[NoEval[sequence], range];
seriesSum = Sum[sequence, range];
Row[{seriesFormula, seriesExpanded, seriesSum}, "="]

expected output is:

enter image description here

Is there a simple way to do this?

I know there are Hold*, Unevaluated, Inactivate, etc, but I just couldn't get a concise solution without multiple definitions of the same series. I'd like to define target series just once, and then get different symbolic and/or numerical representations of it.

Thank you


Bonus question (answered) Is it possible to make Mathematica sum divergent series (Cesàro, Borel, etc) like:

1 - 1 + 1 - 1 ... = 1/2 or 1 - 2 + 4 - 8 ... = 1/3

Update: Regularization parameter of Sum function does the job for divergent series.

$\endgroup$
  • 2
    $\begingroup$ With respect to your bonus: did you already look up Regularization in the docs? $\endgroup$ – J. M. is away Mar 20 '16 at 2:00
  • $\begingroup$ Indeed, thank you $\endgroup$ – nazikus Mar 20 '16 at 2:03
9
$\begingroup$

With UpValues and Inactive:

noEval /:
 Sum[noEval[expr_],
  (indx : ({_Symbol, _Integer} | {_Symbol, _Integer, _Integer} | {_Symbol, _Integer, _Integer, _Integer}) ..)] :=
   Inactive[Sum][expr, indx] == 
    Inactive[Plus] @@ Flatten@Table[expr, indx] == 
    Sum[expr, indx]

Then

Sum[noEval[n*(-1)^(n - 1)], {n, 1, 10}]

Sum[noEval[i + j], {i, 1, 5}, {j, 1, 2}]

Mathematica graphics

I've just discovered UpValues and am finding questions that suit it quite regularly.

Hope this helps.

$\endgroup$
8
$\begingroup$

Not as sophisticated as the other solutions, but it's good for at least single sums:

Options[displaySum] = {"Terms" -> 10};

displaySum[a_, {n_, n1_, n2_}, opts : OptionsPattern[]] /; n1 <= n2 :=
  Module[{nf = Min[n1 + OptionValue["Terms"] - 1, n2]}, 
         Row[{Defer[Sum[a, {n, n1, n2}]], 
              Composition[Defer, Plus] @@
              Append[Table[a, {n, n1, nf}], If[n2 === ∞, "⋯", Nothing]], 
              Sum[a, {n, n1, n2}]}, "="]]

Some examples:

finite sum infinite sum

$\endgroup$
  • $\begingroup$ "Terms" is very nice. I find it a bit confusing that Defer@*Plus @@ _ displays as expected but Defer[Plus @@ _ displays the deferred Apply instead of the terms. Aren't both pieces of code doing the same thing? $\endgroup$ – Edmund Mar 20 '16 at 9:56
  • $\begingroup$ @Edmund, I'd chalk it up to nonstandard evaluation within Defer[]. TracePrint[] seems to show how things proceed. $\endgroup$ – J. M. is away Mar 20 '16 at 14:04
5
$\begingroup$

How about this?:

Clear@seriesExpanded; 
seriesExpanded[expr_, iter : {_, _, _, ___} ..] := 
 Row[If[First@# === "+", Delete[#, 1], #] &@
  Flatten@Replace[
    Flatten@Table[expr, iter], {a : (_?Negative | _?Negative _) :> {"-", -a}, 
     a_ :> {"+", a}}, {1}], " "]
display[a__] := HoldForm@Sum[a] == seriesExpanded[a]

display[(-1)^(i + j) Subscript[a, i, j] j/i, {i, 1, 2}, {j, 2, 5}]

enter image description here

$\endgroup$

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.