The problem with telescoping sums is recognizing how many terms are needed before the cancellation is going to start happening. The built-in method only tries to see if cancellation occurs in adjacent terms. If the gap before cancellation is two terms or greater, then the built-in method fails and the sum is not evaluated. It's easy enough to extend the method, but apparently the user has to do this for themselves.
Here's a way to extend the built-in method to longer gaps (based on the code for sumFiniteTelescoping
):
clearSumCache[] := DownValues@Sum`SumParserDump`sumParserEvaluate =
DownValues[Sum`SumParserDump`sumParserEvaluate][[-2 ;;]];
clearSumCache[]; (* sum results are cached, so either Quit[] or clear the cache *)
mySum`sumFiniteTelescopingMaxGap = 3;
Internal`InheritedBlock[{Sum`FiniteSumDump`sumFiniteTelescoping},
Sum`FiniteSumDump`sumFiniteTelescoping[expr_, {k_, min_ : 1, max_}] /;
Head[expr] == Plus && Length[expr] == 2 :=
Module[{test, lim1, lim2, tgap, idx},
test = False; tgap = 0;
While[! test && tgap < mySum`sumFiniteTelescopingMaxGap,
tgap++;
test =
PossibleZeroQ[idx = 1; Together[(expr[[1]] /. {k -> k+tgap}) + expr[[2]]]] ||
PossibleZeroQ[idx = 2; Together[(expr[[2]] /. {k -> k+tgap}) + expr[[1]]]];
];
dbPrint[tgap];
(lim1 = Quiet[expr[[idx]] /. Table[{k -> k0}, {k0, min, min+tgap-1}] // Total];
(lim2 =
Quiet[expr[[3-idx]] /. Table[{k -> k0}, {k0, max-tgap+1, max}] // Total];
lim1 + lim2 /;
FreeQ[lim2, Indeterminate | DirectedInfinity]) /;
FreeQ[lim1, Indeterminate | DirectedInfinity]) /;
test === True];
Sum[2^(k + 1)^3 - 2^(k - 1)^3, {k, 0, n}]
]
(* -(3/2) + 2^n^3 + 2^(1 + n)^3 *)
[Edit: Refactored the following.]
Another approach is to use the above algorithm with plug-in Method
option:
ClearAll[myFiniteTelescoping, getFiniteTelescopingSum];
$myFiniteTelescopingMaxGap = 3;
getFiniteTelescopingSum[e1_, e2_, k_, min_, max_, offset_] /;
PossibleZeroQ[Together[(e1 /. {k -> k + offset}) + e2]] :=
Module[{lim1, lim2},
(lim1 = Quiet[e1 /. Table[{k -> k0}, {k0, min, min + offset - 1}] // Total];
(lim2 = Quiet[e2 /. Table[{k -> k0}, {k0, max - offset + 1, max}] // Total];
lim1 + lim2 /;
FreeQ[lim2, Indeterminate | DirectedInfinity]) /;
FreeQ[lim1, Indeterminate | DirectedInfinity])
];
getFiniteTelescopingSum[___] := $Failed;
myFiniteTelescoping[expr_, {k_, min_: 1, max_}] /;
Head[expr] == Plus && Length[expr] == 2 :=
Module[{tgap, res},
tgap = 1; res = $Failed;
While[res === $Failed && tgap <= $myFiniteTelescopingMaxGap,
res = getFiniteTelescopingSum[expr[[1]], expr[[2]], k, min, max, tgap];
If[res === $Failed,
res = getFiniteTelescopingSum[expr[[2]], expr[[1]], k, min, max, tgap]];
tgap++
];
res /; FreeQ[res, $Failed]];
myFiniteTelescoping[___] := $Failed;
Examples:
Sum[2^(k + 1)^3 - 2^(k - 1)^3, {k, 0, n}, Method -> myFiniteTelescoping]
(* -(3/2) + 2^n^3 + 2^(1 + n)^3 *)
(* Fails because gap is too great *)
Sum[2^(k + 3)^3 - 2^(k - 1)^3, {k, 0, n}, Method -> myFiniteTelescoping]
(* Sum[-2^(-1 + k)^3 + 2^(3 + k)^3, {k, 0, n}, Method -> myFiniteTelescoping] *)
(* Succeeds if we raise the max gap checked *)
Block[{myFiniteTelescopingMaxGap = 5},
Sum[2^(k + 3)^3 - 2^(k - 1)^3, {k, 0, n}, Method -> myFiniteTelescoping]]
(* -(519/2) + 2^n^3 + 2^(1 + n)^3 + 2^(2 + n)^3 + 2^(3 + n)^3 *)